如何使用SSE将16位整数除以255? [英] How to divide 16-bit integer by 255 with using SSE?
本文介绍了如何使用SSE将16位整数除以255?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我处理图像处理. 我需要将16位整数SSE向量除以255.
I deal with image processing. I need to divide 16-bit integer SSE vector by 255.
我不能使用_mm_srli_epi16()之类的移位运算符,因为255不是2的幂的倍数.
I can't use shift operator like _mm_srli_epi16(), because 255 is not a multiple of power of 2.
我当然知道可以将整数转换为浮点数,执行除法,然后再转换为整数.
I know of course that it is possible convert integer to float, perform division and then back conversion to integer.
但是也许有人知道另一种解决方案...
But might somebody knows another solution...
推荐答案
除以255的整数近似值:
There is an integer approximation of division by 255:
inline int DivideBy255(int value)
{
return (value + 1 + (value >> 8)) >> 8;
}
因此使用SSE2时,它看起来像:
So with using of SSE2 it will look like:
inline __m128i DivideI16By255(__m128i value)
{
return _mm_srli_epi16(_mm_add_epi16(
_mm_add_epi16(value, _mm_set1_epi16(1)), _mm_srli_epi16(value, 8)), 8);
}
对于AVX2:
inline __m256i DivideI16By255(__m256i value)
{
return _mm256_srli_epi16(_mm256_add_epi16(
_mm256_add_epi16(value, _mm256_set1_epi16(1)), _mm256_srli_epi16(value, 8)), 8);
}
对于Altivec(电源):
For Altivec (Power):
typedef __vector int16_t v128_s16;
const v128_s16 K16_0001 = {1, 1, 1, 1, 1, 1, 1, 1};
const v128_s16 K16_0008 = {8, 8, 8, 8, 8, 8, 8, 8};
inline v128_s16 DivideBy255(v128_s16 value)
{
return vec_sr(vec_add(vec_add(value, K16_0001), vec_sr(value, K16_0008)), K16_0008);
}
对于NEON(ARM):
For NEON (ARM):
inline int16x8_t DivideI16By255(int16x8_t value)
{
return vshrq_n_s16(vaddq_s16(
vaddq_s16(value, vdupq_n_s16(1)), vshrq_n_s16(value, 8)), 8);
}
这篇关于如何使用SSE将16位整数除以255?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文