Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
All you need is the single instruction
vqmovun.s16
,
vqmovun_s16
in intrinsics.
Vector Saturating(q) Move Unsigned Narrow
int16x8_t input;
uint8x8_t result;
result = vqmovun_s16(input);
Any negative element will be replaced with 0 while all the numbers bigger than 255 will be set as 255 then narrowed to unsigned 8bit elements, and all these in a single cycle, EXACTLY what you need.
There is also vqmovn_s16
which keeps the values signed (-128~127)
PS: Are you working on YUV to RGB conversion? That's the one time I needed this instruction.
int16x8_t q_result;
int16x8_t max_value = vdupq_n_s16(255);
int16x8_t min_value = vdupq_n_s16(0);
uint16x8_t max_mask, min_mask;
max_mask = vcgtq_s16(q_result, max_value);
min_mask = vcltq_s16(q_result, min_value);
q_result = vbslq_s16(max_mask, max_value, q_result);
q_result = vbslq_s16(min_mask, min_value, q_result);
const int16x8_t max_value = vdupq_n_s16(255);
const int16x8_t min_value = vdupq_n_s16(0);
int16x8_t q_result = ...;
q_result = vmaxq_s16(min_value, q_result);
q_result = vminq_s16(max_value, q_result);
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.