0

I load 8 16-bit integers into a 128 bit variable. I compare each 16 bit quantity against a scalar. The result is FFFF or 0000 for each of the 16 bit quantities. How do I extract the low 8 bits of each quantity and write them to memory?

Here is the C scalar code. You are welcome to just use the intrinsic functions available in the VS C++ compiler. I am writing the results to an array of uchars.

short v = wfin[i];
predout[i] = (v < tupper && v > tlower) ? 1 : 0;
eng
  • 443
  • 1
  • 4
  • 10
user2607207
  • 141
  • 2
  • Your scalar code is storing a `1` or `0`, not `-1` or `0`. On SSE compare results (or `pand` of compare results), you can use `packsswb` to narrow them, preserving the all-ones / all-zero state. If you want to turn that into 1 / 0, you could `pabsb`. To make a bitmask, you could use `pmovmskb` of course, before or after narrowing. – Peter Cordes Aug 05 '23 at 06:09
  • Actually you may be able to use `psubw` + `pcmpgtw` instead of 2x `pcmpgtw` + `pand`. Usually this trick is used with unsigned compares for range-checks, but by flipping the high bit in `tlower` you can range-shift to use signed compares as unsigned. I think there's a way to make that work for runtime-variable ranges; fixed ranges definitely works: *[Convert a String In C++ To Upper Case](https://stackoverflow.com/a/37151084)*. And/or something involving `psubsw` saturating subtract or `pminsw` signed-min. – Peter Cordes Aug 05 '23 at 06:16
  • [Intel intrinsics: vector comparison result to array of bool conversion](https://stackoverflow.com/q/72735770) shows 2 packing steps since it's going from int32 to bool for the compare results. – Peter Cordes Aug 05 '23 at 06:27
  • 1
    @eng: your edit (https://stackoverflow.com/review/suggested-edits/34797759) only replaced indentation with backticks. That's not useful on its own, please don't bump questions with edits that don't change anything about how they're rendered, not even syntax highlighting (which you could have done with "```c" as the opening of the code block). And until you have more than 2k rep so your edits don't need to go through the review queue, even that's too minor IMHO. The review queues are already crowded, at least be fixing a typo or doing something useful with a suggested edit, please and thanks. – Peter Cordes Aug 11 '23 at 23:28

0 Answers0