简体   繁体   中英

SSE unsigned/signed subtraction of 16 bit register

I have a __m128i register (Vector A) with 16 bit values with the content:

{100,26,26,26,26,26,26,100} // A Vector

Now I subtract the vector

{82,82,82,82,82,82,82,82}

With the instruction

_mm_sub_epi16(a_vec,_mm_set1_epi16(82)) 

The expected result should be the following vector

{18,-56,-56,-56,-56,-56,-56,18}

But I get

{18,65480,65480,65480,65480,65480,65480,18}

How can I solve that the vector is treated as signed?

The A Vector was created by this instruction:

__m128i a_vec = _mm_srli_epi16(_mm_unpacklo_epi8(score_vec_8bit, score_vec_8bit), 8)

65480 is the same value as -56 (they are both 0xffc8 at the register level) - you're just displaying it as if it were an unsigned short.

Note that for non-saturating addition and subtraction of binary values without carry/borrow flags it really is irrelevant whether the values are signed or unsigned - hence the same instruction can be used for adding both signed and unsigned shorts - the only difference is how you subsequently interpret (or display) the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM