我正在尝试在VS 2010中的Windows窗体应用程序中使用SSE指令。我在我的应用程序中使用 sum_array 函数,在以下链接中给出 SSE instructions to add all elements of an array
但是当我编译应用程序时,它会给出以下错误
error C3645: 'plot_rectangle::Form1::sum_array' : __clrcall cannot be used on functions compiled to native code
由于我在我的应用程序中也使用 OpenCV 函数,所以我必须选择/ clr编译器选项。
当我们在OpenCV中使用SSE时,该错误的解决方案是什么。
我还在pragma之间尝试了SSE指令,如
#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)
但得到同样的错误。
任何人都可以帮我解决这个问题。
答案 0 :(得分:3)
您不能在编译为IL的代码中使用内联汇编或SSE内在函数。解决方法很简单,将它写在一个单独的帮助函数中,并用#pragma managed括起来,如下所示:
#pragma managed(push, off)
void func foo(args...)
{
// It's fine here
//...
}
#pragma managed(pop)
从Form1 :: sum_array()方法中调用该函数。