1

I am trying to use SSE instruction in Windows form application in VS 2010. And I am using sum_array function in my application given at the following link SSE instructions to add all elements of an array

But when I am compiling the application it is giving the following error

 error C3645: 'plot_rectangle::Form1::sum_array' : __clrcall cannot be used on functions compiled to native code

As I am also using OpenCV functions in my application so I have to choose /clr compiler option for that.

So what is the solution for that error when we are using SSE with OpenCV.

I have also tried that SSE instruction in between pragma like

#pragma managed(push, off)
uint32_t sum_array(const uint8_t a[], int n)
{
    const __m128i vk0 = _mm_set1_epi8(0);       // constant vector of all 0s for use with _mm_unpacklo_epi8/_mm_unpackhi_epi8
    const __m128i vk1 = _mm_set1_epi16(1);      // constant vector of all 1s for use with _mm_madd_epi16
    __m128i vsum = _mm_set1_epi32(0);           // initialise vector of four partial 32 bit sums
    uint32_t sum;
    int i;

    for (i = 0; i < n; i += 16)
    {
        __m128i v = _mm_load_si128((const __m128i *)&a[i]);      // load vector of 8 bit values
        __m128i vl = _mm_unpacklo_epi8(v, vk0); // unpack to two vectors of 16 bit values
        __m128i vh = _mm_unpackhi_epi8(v, vk0);
        vsum = _mm_add_epi32(vsum, _mm_madd_epi16(vl, vk1));
        vsum = _mm_add_epi32(vsum, _mm_madd_epi16(vh, vk1));
                                                // unpack and accumulate 16 bit values to
                                                // 32 bit partial sum vector

    }
    // horizontal add of four 32 bit partial sums and return result
    vsum = _mm_add_epi32(vsum, _mm_srli_si128(vsum, 8));
    vsum = _mm_add_epi32(vsum, _mm_srli_si128(vsum, 4));
    sum = _mm_cvtsi128_si32(vsum);
    return sum;
}
#pragma managed(pop)

But getting same error.

Can any body please help me sort out this problem.

Community
  • 1
  • 1
geeta
  • 689
  • 3
  • 17
  • 33

1 Answers1

3

You cannot use inline assembly or SSE intrinsics in code that gets compiled to IL. The workaround is simple, write it in a separate helper function that you bracket with #pragma managed, like this:

#pragma managed(push, off)
void func foo(args...)
{
   // It's fine here
   //...
}
#pragma managed(pop)

And call that function from your Form1::sum_array() method.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks for reply. I have also tried by SSE code in between pragma. I have also pasted the code as i am doing. But getting same error. – geeta Jun 13 '12 at 07:22
  • That compile just fine when I tried it after #including the correct headers. As it should. You cannot get the same error, you might certainly get a different one. – Hans Passant Jun 13 '12 at 07:45
  • Thanks for reply. You were right it was also showing error related to pragma to declare pragma in namespace. So now it is working. – geeta Jun 13 '12 at 08:54