My input data is 16-bit data, and I need to find a median of 3 values using SSE2 instruction set.
If I have 3 16-bits input values A, B and C, I thought to do it like this:
D = max( max( A, B ), C )
E = min( min( A, B ), C )
median = A + B + C - D - E
C functions I am planing to use are :
- max – _mm_max_epi16
- min – _mm_min_epi16
- addition – _mm_add_epi16
- subtract – _mm_sub_epi16
Can anyone suggest a better way?
Your idea is quite clever but you can do it with fewer operations just using max and min.
This will be just 4 SSE instructions compared with 8 in your original implementation.
Note that this is actually just a pruned sorting network for N = 3.