| 1 | #include "cvd/utility.h" |
| 2 | #include "cvd_src/utility_helpers.h" |
| 3 | #include <mmintrin.h> |
| 4 | |
| 5 | namespace CVD |
| 6 | { |
| 7 | |
| 8 | void byte_to_short_differences(const __m64* a, const __m64* b, __m64* diff, size_t count) |
| 9 | { |
| 10 | __m64 z = _mm_setzero_si64(); |
| 11 | for(; count; --count, ++a, ++b, diff += 2) |
| 12 | { |
| 13 | __m64 aq = *a; |
| 14 | __m64 bq = *b; |
| 15 | __m64 alo = _mm_unpacklo_pi8(aq, z); |
| 16 | __m64 ahi = _mm_unpackhi_pi8(aq, z); |
| 17 | __m64 blo = _mm_unpacklo_pi8(bq, z); |
| 18 | __m64 bhi = _mm_unpackhi_pi8(bq, z); |
| 19 | diff[0] = _mm_sub_pi16(alo, blo); |
| 20 | diff[1] = _mm_sub_pi16(ahi, bhi); |
| 21 | } |
| 22 | _mm_empty(); |
| 23 | } |
| 24 | |
| 25 | void short_differences(const __m64* a, const __m64* b, __m64* diff, size_t count) |
| 26 | { |
| 27 | while(count--) |
| 28 | { |
| 29 | *(diff++) = _mm_sub_pi16(*(a++), *(b++)); |
| 30 | } |
| 31 | _mm_empty(); |
| 32 | } |
| 33 | |
| 34 | struct MMX_funcs |
| 35 | { |
| 36 | template <class T1, class T2> |
| 37 | static inline void unaligned_differences(const T1* a, const T1* b, T2* diff, size_t count) |
| 38 | { |
| 39 | differences<T1, T2>(a, b, diff, count); |
| 40 | } |
| 41 | static inline void aligned_differences(const byte* a, const byte* b, short* diff, size_t count) |
| 42 | { |
| 43 | if(is_aligned<8>(b)) |
| 44 | byte_to_short_differences((const __m64*)a, (const __m64*)b, (__m64*)diff, count >> 3); |
| 45 | else |
| 46 | unaligned_differences(a, b, diff, count); |
| 47 | } |
| 48 | |
| 49 | static inline void aligned_differences(const short* a, const short* b, short* diff, size_t count) |
| 50 | { |
| 51 | if(is_aligned<8>(b)) |
| 52 | short_differences((const __m64*)a, (const __m64*)b, (__m64*)diff, count >> 2); |
| 53 | else |
| 54 | unaligned_differences(a, b, diff, count); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | void differences(const byte* a, const byte* b, short* diff, size_t count) |
| 59 | { |
| 60 | maybe_aligned_differences<MMX_funcs, byte, short, 8, 8>(a, b, diff, count); |
| 61 | } |
| 62 | |
| 63 | void differences(const short* a, const short* b, short* diff, size_t count) |
| 64 | { |
| 65 | maybe_aligned_differences<MMX_funcs, short, short, 8, 4>(a, b, diff, count); |
| 66 | } |
| 67 | } |