l2_distance.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include "l2_distance.h"
  2. #include <library/cpp/sse/sse.h>
  3. #include <contrib/libs/cblas/include/cblas.h>
  4. #include <util/system/platform.h>
  5. template <typename Result, typename Number>
  6. inline Result SqrDelta(Number a, Number b) {
  7. Result diff = a < b ? b - a : a - b;
  8. return diff * diff;
  9. }
  10. template <typename Result, typename Number>
  11. inline Result L2SqrDistanceImpl(const Number* a, const Number* b, int length) {
  12. Result res = 0;
  13. for (int i = 0; i < length; i++) {
  14. res += SqrDelta<Result, Number>(a[i], b[i]);
  15. }
  16. return res;
  17. }
  18. template <typename Result, typename Number>
  19. inline Result L2SqrDistanceImpl2(const Number* a, const Number* b, int length) {
  20. Result s0 = 0;
  21. Result s1 = 0;
  22. while (length >= 2) {
  23. s0 += SqrDelta<Result, Number>(a[0], b[0]);
  24. s1 += SqrDelta<Result, Number>(a[1], b[1]);
  25. a += 2;
  26. b += 2;
  27. length -= 2;
  28. }
  29. while (length--)
  30. s0 += SqrDelta<Result, Number>(*a++, *b++);
  31. return s0 + s1;
  32. }
  33. template <typename Result, typename Number>
  34. inline Result L2SqrDistanceImpl4(const Number* a, const Number* b, int length) {
  35. Result s0 = 0;
  36. Result s1 = 0;
  37. Result s2 = 0;
  38. Result s3 = 0;
  39. while (length >= 4) {
  40. s0 += SqrDelta<Result, Number>(a[0], b[0]);
  41. s1 += SqrDelta<Result, Number>(a[1], b[1]);
  42. s2 += SqrDelta<Result, Number>(a[2], b[2]);
  43. s3 += SqrDelta<Result, Number>(a[3], b[3]);
  44. a += 4;
  45. b += 4;
  46. length -= 4;
  47. }
  48. while (length--)
  49. s0 += SqrDelta<Result, Number>(*a++, *b++);
  50. return s0 + s1 + s2 + s3;
  51. }
  52. inline ui32 L2SqrDistanceImplUI4(const ui8* a, const ui8* b, int length) {
  53. ui32 res = 0;
  54. for (int i = 0; i < length; i++) {
  55. res += SqrDelta<ui32, ui8>(a[i] & 0x0f, b[i] & 0x0f);
  56. res += SqrDelta<ui32, ui8>(a[i] & 0xf0, b[i] & 0xf0) >> 8;
  57. }
  58. return res;
  59. }
  60. #ifdef ARCADIA_SSE
  61. namespace NL2Distance {
  62. static const __m128i MASK_UI4_1 = _mm_set_epi8(0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
  63. 0x0f, 0x0f, 0x0f, 0x0f, 0x0f);
  64. static const __m128i MASK_UI4_2 = _mm_set_epi8(0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  65. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0);
  66. }
  67. ui32 L2SqrDistance(const i8* lhs, const i8* rhs, int length) {
  68. const __m128i zero = _mm_setzero_si128();
  69. __m128i resVec = zero;
  70. while (length >= 16) {
  71. __m128i vec = _mm_subs_epi8(_mm_loadu_si128((const __m128i*)lhs), _mm_loadu_si128((const __m128i*)rhs));
  72. #ifdef _sse4_1_
  73. __m128i lo = _mm_cvtepi8_epi16(vec);
  74. __m128i hi = _mm_cvtepi8_epi16(_mm_alignr_epi8(vec, vec, 8));
  75. #else
  76. __m128i lo = _mm_srai_epi16(_mm_unpacklo_epi8(zero, vec), 8);
  77. __m128i hi = _mm_srai_epi16(_mm_unpackhi_epi8(zero, vec), 8);
  78. #endif
  79. resVec = _mm_add_epi32(resVec,
  80. _mm_add_epi32(_mm_madd_epi16(lo, lo), _mm_madd_epi16(hi, hi)));
  81. lhs += 16;
  82. rhs += 16;
  83. length -= 16;
  84. }
  85. alignas(16) ui32 res[4];
  86. _mm_store_si128((__m128i*)res, resVec);
  87. ui32 sum = res[0] + res[1] + res[2] + res[3];
  88. for (int i = 0; i < length; ++i) {
  89. sum += Sqr(static_cast<i32>(lhs[i]) - static_cast<i32>(rhs[i]));
  90. }
  91. return sum;
  92. }
  93. ui32 L2SqrDistance(const ui8* lhs, const ui8* rhs, int length) {
  94. const __m128i zero = _mm_setzero_si128();
  95. __m128i resVec = zero;
  96. while (length >= 16) {
  97. __m128i lVec = _mm_loadu_si128((const __m128i*)lhs);
  98. __m128i rVec = _mm_loadu_si128((const __m128i*)rhs);
  99. // We will think about this vectors as about i16.
  100. __m128i lo = _mm_sub_epi16(_mm_unpacklo_epi8(lVec, zero), _mm_unpacklo_epi8(rVec, zero));
  101. __m128i hi = _mm_sub_epi16(_mm_unpackhi_epi8(lVec, zero), _mm_unpackhi_epi8(rVec, zero));
  102. resVec = _mm_add_epi32(resVec,
  103. _mm_add_epi32(_mm_madd_epi16(lo, lo), _mm_madd_epi16(hi, hi)));
  104. lhs += 16;
  105. rhs += 16;
  106. length -= 16;
  107. }
  108. alignas(16) ui32 res[4];
  109. _mm_store_si128((__m128i*)res, resVec);
  110. ui32 sum = res[0] + res[1] + res[2] + res[3];
  111. for (int i = 0; i < length; ++i) {
  112. sum += Sqr(static_cast<i32>(lhs[i]) - static_cast<i32>(rhs[i]));
  113. }
  114. return sum;
  115. }
  116. float L2SqrDistance(const float* lhs, const float* rhs, int length) {
  117. __m128 sum = _mm_setzero_ps();
  118. while (length >= 4) {
  119. __m128 a = _mm_loadu_ps(lhs);
  120. __m128 b = _mm_loadu_ps(rhs);
  121. __m128 delta = _mm_sub_ps(a, b);
  122. sum = _mm_add_ps(sum, _mm_mul_ps(delta, delta));
  123. length -= 4;
  124. rhs += 4;
  125. lhs += 4;
  126. }
  127. alignas(16) float res[4];
  128. _mm_store_ps(res, sum);
  129. while (length--)
  130. res[0] += Sqr(*rhs++ - *lhs++);
  131. return res[0] + res[1] + res[2] + res[3];
  132. }
  133. double L2SqrDistance(const double* lhs, const double* rhs, int length) {
  134. __m128d sum = _mm_setzero_pd();
  135. while (length >= 2) {
  136. __m128d a = _mm_loadu_pd(lhs);
  137. __m128d b = _mm_loadu_pd(rhs);
  138. __m128d delta = _mm_sub_pd(a, b);
  139. sum = _mm_add_pd(sum, _mm_mul_pd(delta, delta));
  140. length -= 2;
  141. rhs += 2;
  142. lhs += 2;
  143. }
  144. alignas(16) double res[2];
  145. _mm_store_pd(res, sum);
  146. while (length--)
  147. res[0] += Sqr(*rhs++ - *lhs++);
  148. return res[0] + res[1];
  149. }
  150. ui64 L2SqrDistance(const i32* lhs, const i32* rhs, int length) {
  151. __m128i zero = _mm_setzero_si128();
  152. __m128i res = zero;
  153. while (length >= 4) {
  154. __m128i a = _mm_loadu_si128((const __m128i*)lhs);
  155. __m128i b = _mm_loadu_si128((const __m128i*)rhs);
  156. #ifdef _sse4_1_
  157. // In SSE4.1 si32*si32->si64 is available, so we may do just (a-b)*(a-b) not caring about (a-b) sign
  158. a = _mm_sub_epi32(a, b);
  159. res = _mm_add_epi64(_mm_mul_epi32(a, a), res);
  160. a = _mm_alignr_epi8(a, a, 4);
  161. res = _mm_add_epi64(_mm_mul_epi32(a, a), res);
  162. #else
  163. __m128i mask = _mm_cmpgt_epi32(a, b); // mask = a > b? 0xffffffff: 0;
  164. __m128i a2 = _mm_sub_epi32(_mm_and_si128(mask, a), _mm_and_si128(mask, b)); // a2 = (a & mask) - (b & mask) (for a > b)
  165. b = _mm_sub_epi32(_mm_andnot_si128(mask, b), _mm_andnot_si128(mask, a)); // b = (b & ~mask) - (a & ~mask) (for b > a)
  166. a = _mm_or_si128(a2, b); // a = abs(a - b)
  167. a2 = _mm_unpackhi_epi32(a, zero);
  168. res = _mm_add_epi64(_mm_mul_epu32(a2, a2), res);
  169. a2 = _mm_unpacklo_epi32(a, zero);
  170. res = _mm_add_epi64(_mm_mul_epu32(a2, a2), res);
  171. #endif
  172. rhs += 4;
  173. lhs += 4;
  174. length -= 4;
  175. }
  176. alignas(16) ui64 r[2];
  177. _mm_store_si128((__m128i*)r, res);
  178. ui64 sum = r[0] + r[1];
  179. while (length) {
  180. sum += SqrDelta<ui64, i32>(lhs[0], rhs[0]);
  181. ++lhs;
  182. ++rhs;
  183. --length;
  184. }
  185. return sum;
  186. }
  187. ui64 L2SqrDistance(const ui32* lhs, const ui32* rhs, int length) {
  188. __m128i zero = _mm_setzero_si128();
  189. __m128i shift = _mm_set1_epi32(0x80000000);
  190. __m128i res = zero;
  191. while (length >= 4) {
  192. __m128i a = _mm_add_epi32(_mm_loadu_si128((const __m128i*)lhs), shift);
  193. __m128i b = _mm_add_epi32(_mm_loadu_si128((const __m128i*)rhs), shift);
  194. __m128i mask = _mm_cmpgt_epi32(a, b); // mask = a > b? 0xffffffff: 0;
  195. __m128i a2 = _mm_sub_epi32(_mm_and_si128(mask, a), _mm_and_si128(mask, b)); // a2 = (a & mask) - (b & mask) (for a > b)
  196. b = _mm_sub_epi32(_mm_andnot_si128(mask, b), _mm_andnot_si128(mask, a)); // b = (b & ~mask) - (a & ~mask) (for b > a)
  197. a = _mm_or_si128(a2, b); // a = abs(a - b)
  198. #ifdef _sse4_1_
  199. res = _mm_add_epi64(_mm_mul_epu32(a, a), res);
  200. a = _mm_alignr_epi8(a, a, 4);
  201. res = _mm_add_epi64(_mm_mul_epu32(a, a), res);
  202. #else
  203. a2 = _mm_unpackhi_epi32(a, zero);
  204. res = _mm_add_epi64(_mm_mul_epu32(a2, a2), res);
  205. a2 = _mm_unpacklo_epi32(a, zero);
  206. res = _mm_add_epi64(_mm_mul_epu32(a2, a2), res);
  207. #endif
  208. rhs += 4;
  209. lhs += 4;
  210. length -= 4;
  211. }
  212. alignas(16) ui64 r[2];
  213. _mm_store_si128((__m128i*)r, res);
  214. ui64 sum = r[0] + r[1];
  215. while (length) {
  216. sum += SqrDelta<ui64, ui32>(lhs[0], rhs[0]);
  217. ++lhs;
  218. ++rhs;
  219. --length;
  220. }
  221. return sum;
  222. }
  223. ui32 L2SqrDistanceUI4(const ui8* lhs, const ui8* rhs, int length) {
  224. const __m128i zero = _mm_setzero_si128();
  225. __m128i resVec1 = zero;
  226. __m128i resVec2 = zero;
  227. while (length >= 16) {
  228. __m128i lVec = _mm_loadu_si128((const __m128i*)lhs);
  229. __m128i rVec = _mm_loadu_si128((const __m128i*)rhs);
  230. __m128i lVec1 = _mm_and_si128(lVec, NL2Distance::MASK_UI4_1);
  231. __m128i lVec2 = _mm_and_si128(lVec, NL2Distance::MASK_UI4_2);
  232. __m128i rVec1 = _mm_and_si128(rVec, NL2Distance::MASK_UI4_1);
  233. __m128i rVec2 = _mm_and_si128(rVec, NL2Distance::MASK_UI4_2);
  234. // We will think about this vectors as about i16.
  235. __m128i lo1 = _mm_sub_epi16(_mm_unpacklo_epi8(lVec1, zero), _mm_unpacklo_epi8(rVec1, zero));
  236. __m128i hi1 = _mm_sub_epi16(_mm_unpackhi_epi8(lVec1, zero), _mm_unpackhi_epi8(rVec1, zero));
  237. __m128i lo2 = _mm_sub_epi16(_mm_unpacklo_epi8(lVec2, zero), _mm_unpacklo_epi8(rVec2, zero));
  238. __m128i hi2 = _mm_sub_epi16(_mm_unpackhi_epi8(lVec2, zero), _mm_unpackhi_epi8(rVec2, zero));
  239. resVec1 = _mm_add_epi32(resVec1, _mm_add_epi32(_mm_madd_epi16(lo1, lo1), _mm_madd_epi16(hi1, hi1)));
  240. resVec2 = _mm_add_epi32(resVec2, _mm_add_epi32(_mm_madd_epi16(lo2, lo2), _mm_madd_epi16(hi2, hi2)));
  241. lhs += 16;
  242. rhs += 16;
  243. length -= 16;
  244. }
  245. alignas(16) ui32 res[4];
  246. _mm_store_si128((__m128i*)res, resVec1);
  247. ui32 sum = res[0] + res[1] + res[2] + res[3];
  248. _mm_store_si128((__m128i*)res, resVec2);
  249. sum += (res[0] + res[1] + res[2] + res[3]) >> 8;
  250. for (int i = 0; i < length; ++i) {
  251. sum += Sqr(static_cast<i32>(lhs[i] & 0x0f) - static_cast<i32>(rhs[i] & 0x0f));
  252. sum += Sqr(static_cast<i32>(lhs[i] & 0xf0) - static_cast<i32>(rhs[i] & 0xf0)) >> 8;
  253. }
  254. return sum;
  255. }
  256. #else /* !ARCADIA_SSE */
  257. ui32 L2SqrDistance(const i8* lhs, const i8* rhs, int length) {
  258. return L2SqrDistanceImpl<ui32, i8>(lhs, rhs, length);
  259. }
  260. ui32 L2SqrDistance(const ui8* lhs, const ui8* rhs, int length) {
  261. return L2SqrDistanceImpl<ui32, ui8>(lhs, rhs, length);
  262. }
  263. ui64 L2SqrDistance(const i32* a, const i32* b, int length) {
  264. return L2SqrDistanceImpl2<ui64, i32>(a, b, length);
  265. }
  266. ui64 L2SqrDistance(const ui32* a, const ui32* b, int length) {
  267. return L2SqrDistanceImpl2<ui64, ui32>(a, b, length);
  268. }
  269. float L2SqrDistance(const float* a, const float* b, int length) {
  270. return L2SqrDistanceImpl4<float, float>(a, b, length);
  271. }
  272. double L2SqrDistance(const double* a, const double* b, int length) {
  273. return L2SqrDistanceImpl2<double, double>(a, b, length);
  274. }
  275. ui32 L2SqrDistanceUI4(const ui8* lhs, const ui8* rhs, int length) {
  276. return L2SqrDistanceImplUI4(lhs, rhs, length);
  277. }
  278. #endif /* ARCADIA_SSE */
  279. ui32 L2SqrDistanceSlow(const i8* lhs, const i8* rhs, int length) {
  280. return L2SqrDistanceImpl<ui32, i8>(lhs, rhs, length);
  281. }
  282. ui32 L2SqrDistanceSlow(const ui8* lhs, const ui8* rhs, int length) {
  283. return L2SqrDistanceImpl<ui32, ui8>(lhs, rhs, length);
  284. }
  285. ui64 L2SqrDistanceSlow(const i32* a, const i32* b, int length) {
  286. return L2SqrDistanceImpl2<ui64, i32>(a, b, length);
  287. }
  288. ui64 L2SqrDistanceSlow(const ui32* a, const ui32* b, int length) {
  289. return L2SqrDistanceImpl2<ui64, ui32>(a, b, length);
  290. }
  291. float L2SqrDistanceSlow(const float* a, const float* b, int length) {
  292. return L2SqrDistanceImpl4<float, float>(a, b, length);
  293. }
  294. double L2SqrDistanceSlow(const double* a, const double* b, int length) {
  295. return L2SqrDistanceImpl2<double, double>(a, b, length);
  296. }
  297. ui32 L2SqrDistanceUI4Slow(const ui8* lhs, const ui8* rhs, int length) {
  298. return L2SqrDistanceImplUI4(lhs, rhs, length);
  299. }