2

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?

Antonio
  • 19,451
  • 13
  • 99
  • 197
BЈовић
  • 62,405
  • 41
  • 173
  • 273

1 Answers1

7

Your idea is quite clever but you can do it with fewer operations just using max and min.

t1 = min(A, B)
t2 = max(A, B)
t3 = min(t2, C)
median = max(t1, t3)

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560