| 1 | #include "cvd/utility.h" |
| 2 | #include "cvd_src/utility_helpers.h" |
| 3 | |
| 4 | #include <xmmintrin.h> |
| 5 | |
| 6 | using namespace std; |
| 7 | |
| 8 | namespace CVD |
| 9 | { |
| 10 | |
| 11 | //Some versions of gcc do not define _mm_load?_ps as taking a const float* |
| 12 | //Later versions always use the const. |
| 13 | template <bool Aligned> |
| 14 | inline __m128 load_ps(const void* addr) { return _mm_loadu_ps(static_cast<float*>(const_cast<void*>(addr))); } |
| 15 | template <> |
| 16 | inline __m128 load_ps<true>(const void* addr) { return _mm_load_ps(static_cast<float*>(const_cast<void*>(addr))); } |
| 17 | |
| 18 | template <bool Aligned> |
| 19 | inline void store_ps(__m128 m, void* addr) { return _mm_storeu_ps((float*)addr, m); } |
| 20 | template <> |
| 21 | inline void store_ps<true>(__m128 m, void* addr) { return _mm_store_ps((float*)addr, m); } |
| 22 | |
| 23 | template <bool Aligned_b> |
| 24 | void float_differences(const __m128* a, const __m128* b, __m128* diff, size_t count) |
| 25 | { |
| 26 | while(count--) |
| 27 | { |
| 28 | _mm_stream_ps((float*)diff, _mm_sub_ps(load_ps<true>(a), load_ps<Aligned_b>(b))); |
| 29 | ++diff; |
| 30 | ++a; |
| 31 | ++b; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | template <bool Aligned_b> |
| 36 | void float_add_multiple_of_sum(const __m128* a, const __m128* b, const float& c, __m128* out, size_t count) |
| 37 | { |
| 38 | __m128 cccc = _mm_set1_ps(c); |
| 39 | while(count--) |
| 40 | { |
| 41 | *out = _mm_add_ps(_mm_mul_ps(_mm_add_ps(load_ps<true>(a), load_ps<Aligned_b>(b)), cccc), *out); |
| 42 | ++out; |
| 43 | ++a; |
| 44 | ++b; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | template <bool Aligned_out> |
| 49 | inline void float_assign_multiple(const __m128* a, const float& c, __m128* out, size_t count) |
| 50 | { |
| 51 | const __m128 cccc = _mm_set1_ps(c); |
| 52 | while(count--) |
| 53 | store_ps<Aligned_out>(_mm_mul_ps(*(a++), cccc), out++); |
| 54 | } |
| 55 | |
| 56 | template <bool Aligned_b> |
| 57 | double float_inner_product(const __m128* a, const __m128* b, size_t count) |
| 58 | { |
| 59 | float sums_store[4]; |
| 60 | const size_t BLOCK = 1 << 10; |
| 61 | double dot = 0; |
| 62 | while(count) |
| 63 | { |
| 64 | size_t pass = std::min(count, BLOCK); |
| 65 | count -= pass; |
| 66 | __m128 sums = _mm_setzero_ps(); |
| 67 | while(pass--) |
| 68 | { |
| 69 | __m128 prod = _mm_mul_ps(*(a++), load_ps<Aligned_b>(b++)); |
| 70 | sums = _mm_add_ps(prod, sums); |
| 71 | } |
| 72 | _mm_storeu_ps(sums_store, sums); |
| 73 | dot += sums_store[0] + sums_store[1] + sums_store[2] + sums_store[3]; |
| 74 | } |
| 75 | return dot; |
| 76 | } |
| 77 | |
| 78 | template <bool Aligned_b> |
| 79 | inline double float_sum_squared_differences(const __m128* a, const __m128* b, size_t count) |
| 80 | { |
| 81 | float sums_store[4]; |
| 82 | const size_t BLOCK = 1 << 10; |
| 83 | double ssd = 0; |
| 84 | while(count) |
| 85 | { |
| 86 | size_t pass = std::min(count, BLOCK); |
| 87 | count -= pass; |
| 88 | __m128 sums = _mm_setzero_ps(); |
| 89 | while(pass--) |
| 90 | { |
| 91 | __m128 diff = _mm_sub_ps(*(a++), load_ps<Aligned_b>(b++)); |
| 92 | sums = _mm_add_ps(_mm_mul_ps(diff, diff), sums); |
| 93 | } |
| 94 | _mm_storeu_ps(sums_store, sums); |
| 95 | ssd += sums_store[0] + sums_store[1] + sums_store[2] + sums_store[3]; |
| 96 | } |
| 97 | return ssd; |
| 98 | } |
| 99 | |
| 100 | template <bool Aligned_out> |
| 101 | void float_square(const __m128* in, __m128* out, size_t count) |
| 102 | { |
| 103 | while(count--) |
| 104 | { |
| 105 | __m128 x = load_ps<true>(in); |
| 106 | store_ps<Aligned_out>(_mm_mul_ps(x, x), out); |
| 107 | ++in; |
| 108 | ++out; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | template <bool Aligned_out> |
| 113 | void float_subtract_square(const __m128* in, __m128* out, size_t count) |
| 114 | { |
| 115 | while(count--) |
| 116 | { |
| 117 | __m128 x = load_ps<true>(in); |
| 118 | __m128 y = load_ps<Aligned_out>(out); |
| 119 | store_ps<Aligned_out>(_mm_sub_ps(y, _mm_mul_ps(x, x)), out); |
| 120 | ++in; |
| 121 | ++out; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | struct SSE_funcs |
| 126 | { |
| 127 | template <class T1, class T2> |
| 128 | static inline void unaligned_differences(const T1* a, const T1* b, T2* diff, size_t count) |
| 129 | { |
| 130 | differences<T1, T2>(a, b, diff, count); |
| 131 | } |
| 132 | |
| 133 | static inline void aligned_differences(const float* a, const float* b, float* diff, size_t count) |
| 134 | { |
| 135 | if(is_aligned<16>(b)) |
| 136 | float_differences<true>((const __m128*)a, (const __m128*)b, (__m128*)diff, count >> 2); |
| 137 | else |
| 138 | float_differences<false>((const __m128*)a, (const __m128*)b, (__m128*)diff, count >> 2); |
| 139 | } |
| 140 | |
| 141 | template <class T1, class T2> |
| 142 | static inline void unaligned_add_mul_add(const T1* a, const T1* b, const T1& c, T2* out, size_t count) |
| 143 | { |
| 144 | add_multiple_of_sum<T1, T2>(a, b, c, out, count); |
| 145 | } |
| 146 | static inline void aligned_add_mul_add(const float* a, const float* b, const float& c, float* out, size_t count) |
| 147 | { |
| 148 | if(is_aligned<16>(b)) |
| 149 | float_add_multiple_of_sum<true>((const __m128*)a, (const __m128*)b, c, (__m128*)out, count >> 2); |
| 150 | else |
| 151 | float_add_multiple_of_sum<false>((const __m128*)a, (const __m128*)b, c, (__m128*)out, count >> 2); |
| 152 | } |
| 153 | |
| 154 | template <class T1, class T2> |
| 155 | static inline void unaligned_assign_mul(const T1* a, const T1& c, T2* out, size_t count) |
| 156 | { |
| 157 | assign_multiple<T1, T2>(a, c, out, count); |
| 158 | } |
| 159 | static inline void aligned_assign_mul(const float* a, const float& c, float* out, size_t count) |
| 160 | { |
| 161 | if(is_aligned<16>(out)) |
| 162 | float_assign_multiple<false>((const __m128*)a, c, (__m128*)out, count >> 2); |
| 163 | else |
| 164 | float_assign_multiple<false>((const __m128*)a, c, (__m128*)out, count >> 2); |
| 165 | } |
| 166 | |
| 167 | template <class T1> |
| 168 | static inline double unaligned_inner_product(const T1* a, const T1* b, size_t count) |
| 169 | { |
| 170 | return inner_product<T1>(a, b, count); |
| 171 | } |
| 172 | |
| 173 | static inline double aligned_inner_product(const float* a, const float* b, size_t count) |
| 174 | { |
| 175 | if(is_aligned<16>(b)) |
| 176 | return float_inner_product<true>((const __m128*)a, (const __m128*)b, count >> 2); |
| 177 | else |
| 178 | return float_inner_product<false>((const __m128*)a, (const __m128*)b, count >> 2); |
| 179 | } |
| 180 | |
| 181 | template <class T1> |
| 182 | static inline double unaligned_ssd(const T1* a, const T1* b, size_t count) |
| 183 | { |
| 184 | return sum_squared_differences<T1>(a, b, count); |
| 185 | } |
| 186 | |
| 187 | static inline double aligned_ssd(const float* a, const float* b, size_t count) |
| 188 | { |
| 189 | if(is_aligned<16>(b)) |
| 190 | return float_sum_squared_differences<true>((const __m128*)a, (const __m128*)b, count >> 2); |
| 191 | else |
| 192 | return float_sum_squared_differences<false>((const __m128*)a, (const __m128*)b, count >> 2); |
| 193 | } |
| 194 | |
| 195 | template <class T1, class T2> |
| 196 | static inline void unaligned_square(const T1* in, T2* out, size_t count) |
| 197 | { |
| 198 | square<T1, T2>(in, out, count); |
| 199 | } |
| 200 | |
| 201 | static inline void aligned_square(const float* in, float* out, size_t count) |
| 202 | { |
| 203 | if(is_aligned<16>(out)) |
| 204 | float_square<true>((const __m128*)in, (__m128*)out, count >> 2); |
| 205 | else |
| 206 | float_square<false>((const __m128*)in, (__m128*)out, count >> 2); |
| 207 | } |
| 208 | template <class T1, class T2> |
| 209 | static inline void unaligned_subtract_square(const T1* in, T2* out, size_t count) |
| 210 | { |
| 211 | subtract_square<T1, T2>(in, out, count); |
| 212 | } |
| 213 | |
| 214 | static inline void aligned_subtract_square(const float* in, float* out, size_t count) |
| 215 | { |
| 216 | if(is_aligned<16>(out)) |
| 217 | float_subtract_square<true>((const __m128*)in, (__m128*)out, count >> 2); |
| 218 | else |
| 219 | float_subtract_square<false>((const __m128*)in, (__m128*)out, count >> 2); |
| 220 | } |
| 221 | }; |
| 222 | |
| 223 | void differences(const float* a, const float* b, float* diff, size_t size) |
| 224 | { |
| 225 | maybe_aligned_differences<SSE_funcs, float, float, 16, 4>(a, b, diff, size); |
| 226 | } |
| 227 | |
| 228 | void add_multiple_of_sum(const float* a, const float* b, const float& c, float* out, size_t count) |
| 229 | { |
| 230 | maybe_aligned_add_mul_add<SSE_funcs, float, float, 16, 4>(a, b, c, out, count); |
| 231 | } |
| 232 | |
| 233 | void assign_multiple(const float* a, const float& c, float* out, size_t count) |
| 234 | { |
| 235 | maybe_aligned_assign_mul<SSE_funcs, float, float, 16, 4>(a, c, out, count); |
| 236 | } |
| 237 | |
| 238 | double inner_product(const float* a, const float* b, size_t count) |
| 239 | { |
| 240 | return maybe_aligned_inner_product<SSE_funcs, double, float, 16, 4>(a, b, count); |
| 241 | } |
| 242 | |
| 243 | double sum_squared_differences(const float* a, const float* b, size_t count) |
| 244 | { |
| 245 | return maybe_aligned_ssd<SSE_funcs, double, float, 16, 4>(a, b, count); |
| 246 | } |
| 247 | |
| 248 | void square(const float* in, float* out, size_t count) |
| 249 | { |
| 250 | maybe_aligned_square<SSE_funcs, float, float, 16, 4>(in, out, count); |
| 251 | } |
| 252 | |
| 253 | void subtract_square(const float* in, float* out, size_t count) |
| 254 | { |
| 255 | maybe_aligned_subtract_square<SSE_funcs, float, float, 16, 4>(in, out, count); |
| 256 | } |
| 257 | |
| 258 | } |