Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

signed 16-bit SSE average

_mm_avg_epu16 provides an average of two unsigned 16-bit integers via PAVGW. Is converting to float and dividing by 2. the only appropriate (optimal) way to use SSE to get a average of two signed 16-bit integers a "signed average that rounds up followed by inverting the top bit" (@Mysticial), or is there another way?


EDIT: Here's the code I'd like to optimize, so far all my attempts using SSE have been close but not exactly matching, usually surrounding problems w/ saturation/overflow-wrapping:

int16_t *a;
int16_t *b;
uint16_t *out;

out[i] = int((a[i] + b[i]) / 2.0f + 32768.5f)

Attempt #1:

const __m128i outputVal = _mm_add_epi16(_mm_avg_epu16(a, b),  _mm_set1_epi16(32768));

Attempt #2:

const __m128i sum = _mm_add_epi16(a, b);
const __m128i outputVal = _mm_add_epi16(_mm_srai_epi16(sum, 1), _mm_set1_epi16(32768));

Attempt #3:

const __m128 elt_offset = _mm_set1_ps(32768.5f);

const __m128 avg_divisor = _mm_set1_ps(2.f);

const __m128i eltSum = _mm_add_epi16(edgeRowElts, edgeInnerRowElts); /* eltSum = int((inputData[i] + inputData[i + (direction*x)]) */
const __m64 eltSumLow  = _mm_movepi64_pi64(eltSum); /* eltSumLow = (__m64) (0x0ffffffff & eltSum) */
const __m64 eltSumHigh = _mm_movepi64_pi64(_mm_srli_si128(eltSum, 8)); /* eltSumHigh = (__m64) (0x0ffffffff & (eltSum >> 64)) */

/* Lower */
__m128 eltSumF = _mm_cvtpi16_ps(eltSumLow); /* eltSumF = (float) eltSum; */

__m128 eltAvg  = _mm_div_ps(eltSumF, avg_divisor); /* eltAvg = eltSum / 2.0f */
__m128 eltAvgOffset = _mm_add_ps(eltAvg,  elt_offset); /* eltAvgOffset = eltAvg + 32768.5f */
const __m64 outputValLow  = _mm_cvtps_pi16(eltAvgOffset); /* outputVal = (short) eltAvgOffset */

/* Upper */
eltSumF = _mm_cvtpi16_ps(eltSumHigh); /* eltSumF = (float) eltSum; */

eltAvg  = _mm_div_ps(eltSumF, avg_divisor); /* eltAvg = eltSum / 2.0f */
eltAvgOffset = _mm_add_ps(eltAvg,  elt_offset); /* eltAvgOffset = eltAvg + 32768.5f */
const __m64 outputValHigh = _mm_cvtps_pi16(eltAvgOffset); /* outputVal = (short) eltAvgOffset */

__m128i outputVal = _mm_slli_si128(_mm_movpi64_epi64(outputValHigh), 8); /* outputVal = (outputValHigh << 64); */
outputVal = _mm_or_si128(outputVal, _mm_movpi64_epi64(outputValLow)); /* outputVal = outputVal | (outputValLow); */
like image 348
Brian Cain Avatar asked May 28 '26 13:05

Brian Cain


1 Answers

I'm not sure I fully understand all the requirements here, but it seems that:

a = _mm_add_epi16(a, _mm_set1_epi16(32768));
b = _mm_add_epi16(b, _mm_set1_epi16(32768));
outputVal = _mm_avg_epu16(a, b);

should give you everything apart from the rounding up requirement.

If so then it shouldn't be to hard to fix up the rounding after the fact:

round = _mm_xor_si128(a, b);
round = _mm_and_si128(round, _mm_set1_epi16(1));
outputVal = _mm_add_epi16(outputVal, round);
like image 110
Paul R Avatar answered May 31 '26 05:05

Paul R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!