dot_product_avx2.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "dot_product_avx2.h"
  2. #include "dot_product_simple.h"
  3. #include "dot_product_sse.h"
  4. #if defined(_avx2_) && defined(_fma_)
  5. #include <util/system/platform.h>
  6. #include <util/system/compiler.h>
  7. #include <util/generic/utility.h>
  8. #include <immintrin.h>
  9. namespace {
  10. constexpr i64 Bits(int n) {
  11. return i64(-1) ^ ((i64(1) << (64 - n)) - 1);
  12. }
  13. constexpr __m256 BlendMask64[8] = {
  14. __m256i{Bits(64), Bits(64), Bits(64), Bits(64)},
  15. __m256i{0, Bits(64), Bits(64), Bits(64)},
  16. __m256i{0, 0, Bits(64), Bits(64)},
  17. __m256i{0, 0, 0, Bits(64)},
  18. };
  19. constexpr __m256 BlendMask32[8] = {
  20. __m256i{Bits(64), Bits(64), Bits(64), Bits(64)},
  21. __m256i{Bits(32), Bits(64), Bits(64), Bits(64)},
  22. __m256i{0, Bits(64), Bits(64), Bits(64)},
  23. __m256i{0, Bits(32), Bits(64), Bits(64)},
  24. __m256i{0, 0, Bits(64), Bits(64)},
  25. __m256i{0, 0, Bits(32), Bits(64)},
  26. __m256i{0, 0, 0, Bits(64)},
  27. __m256i{0, 0, 0, Bits(32)},
  28. };
  29. constexpr __m128 BlendMask8[16] = {
  30. __m128i{Bits(64), Bits(64)},
  31. __m128i{Bits(56), Bits(64)},
  32. __m128i{Bits(48), Bits(64)},
  33. __m128i{Bits(40), Bits(64)},
  34. __m128i{Bits(32), Bits(64)},
  35. __m128i{Bits(24), Bits(64)},
  36. __m128i{Bits(16), Bits(64)},
  37. __m128i{Bits(8), Bits(64)},
  38. __m128i{0, Bits(64)},
  39. __m128i{0, Bits(56)},
  40. __m128i{0, Bits(48)},
  41. __m128i{0, Bits(40)},
  42. __m128i{0, Bits(32)},
  43. __m128i{0, Bits(24)},
  44. __m128i{0, Bits(16)},
  45. __m128i{0, Bits(8)},
  46. };
  47. // See https://stackoverflow.com/a/60109639
  48. // Horizontal sum of eight i32 values in an avx register
  49. i32 HsumI32(__m256i v) {
  50. __m128i x = _mm_add_epi32(_mm256_castsi256_si128(v), _mm256_extracti128_si256(v, 1));
  51. __m128i hi64 = _mm_unpackhi_epi64(x, x);
  52. __m128i sum64 = _mm_add_epi32(hi64, x);
  53. __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  54. __m128i sum32 = _mm_add_epi32(sum64, hi32);
  55. return _mm_cvtsi128_si32(sum32);
  56. }
  57. // Horizontal sum of four i64 values in an avx register
  58. i64 HsumI64(__m256i v) {
  59. __m128i x = _mm_add_epi64(_mm256_castsi256_si128(v), _mm256_extracti128_si256(v, 1));
  60. return _mm_cvtsi128_si64(x) + _mm_extract_epi64(x, 1);
  61. }
  62. // Horizontal sum of eight float values in an avx register
  63. float HsumFloat(__m256 v) {
  64. __m256 y = _mm256_permute2f128_ps(v, v, 1);
  65. v = _mm256_add_ps(v, y);
  66. v = _mm256_hadd_ps(v, v);
  67. return _mm256_cvtss_f32(_mm256_hadd_ps(v, v));
  68. }
  69. // Horizontal sum of four double values in an avx register
  70. double HsumDouble(__m256 v) {
  71. __m128d x = _mm_add_pd(_mm256_castpd256_pd128(v), _mm256_extractf128_pd(v, 1));
  72. x = _mm_add_pd(x, _mm_shuffle_pd(x, x, 1));
  73. return _mm_cvtsd_f64(x);
  74. }
  75. __m128i Load128i(const void* ptr) {
  76. return _mm_loadu_si128((const __m128i*)ptr);
  77. }
  78. __m256i Load256i(const void* ptr) {
  79. return _mm256_loadu_si256((const __m256i*)ptr);
  80. }
  81. // Unrolled dot product for relatively small sizes
  82. // The loop with known upper bound is unrolled by the compiler, no need to do anything special about it
  83. template <size_t size, class TInput, class TExtend>
  84. i32 DotProductInt8Avx2_Unroll(const TInput* lhs, const TInput* rhs, TExtend extend) noexcept {
  85. static_assert(size % 16 == 0);
  86. auto sum = _mm256_setzero_ps();
  87. for (size_t i = 0; i != size; i += 16) {
  88. sum = _mm256_add_epi32(sum, _mm256_madd_epi16(extend(Load128i(lhs + i)), extend(Load128i(rhs + i))));
  89. }
  90. return HsumI32(sum);
  91. }
  92. template <class TInput, class TExtend>
  93. i32 DotProductInt8Avx2(const TInput* lhs, const TInput* rhs, size_t length, TExtend extend) noexcept {
  94. // Fully unrolled versions for small multiples for 16
  95. switch (length) {
  96. case 16: return DotProductInt8Avx2_Unroll<16>(lhs, rhs, extend);
  97. case 32: return DotProductInt8Avx2_Unroll<32>(lhs, rhs, extend);
  98. case 48: return DotProductInt8Avx2_Unroll<48>(lhs, rhs, extend);
  99. case 64: return DotProductInt8Avx2_Unroll<64>(lhs, rhs, extend);
  100. }
  101. __m256i sum = _mm256_setzero_ps();
  102. if (const auto leftover = length % 16; leftover != 0) {
  103. auto a = _mm_blendv_epi8(
  104. Load128i(lhs), _mm_setzero_ps(), BlendMask8[leftover]);
  105. auto b = _mm_blendv_epi8(
  106. Load128i(rhs), _mm_setzero_ps(), BlendMask8[leftover]);
  107. sum = _mm256_madd_epi16(extend(a), extend(b));
  108. lhs += leftover;
  109. rhs += leftover;
  110. length -= leftover;
  111. }
  112. while (length >= 32) {
  113. const auto l0 = extend(Load128i(lhs));
  114. const auto r0 = extend(Load128i(rhs));
  115. const auto l1 = extend(Load128i(lhs + 16));
  116. const auto r1 = extend(Load128i(rhs + 16));
  117. const auto s0 = _mm256_madd_epi16(l0, r0);
  118. const auto s1 = _mm256_madd_epi16(l1, r1);
  119. sum = _mm256_add_epi32(sum, _mm256_add_epi32(s0, s1));
  120. lhs += 32;
  121. rhs += 32;
  122. length -= 32;
  123. }
  124. if (length > 0) {
  125. auto l = extend(Load128i(lhs));
  126. auto r = extend(Load128i(rhs));
  127. sum = _mm256_add_epi32(sum, _mm256_madd_epi16(l, r));
  128. }
  129. return HsumI32(sum);
  130. }
  131. }
  132. i32 DotProductAvx2(const i8* lhs, const i8* rhs, size_t length) noexcept {
  133. if (length < 16) {
  134. return DotProductSse(lhs, rhs, length);
  135. }
  136. return DotProductInt8Avx2(lhs, rhs, length, [](const __m128i x) {
  137. return _mm256_cvtepi8_epi16(x);
  138. });
  139. }
  140. ui32 DotProductAvx2(const ui8* lhs, const ui8* rhs, size_t length) noexcept {
  141. if (length < 16) {
  142. return DotProductSse(lhs, rhs, length);
  143. }
  144. return DotProductInt8Avx2(lhs, rhs, length, [](const __m128i x) {
  145. return _mm256_cvtepu8_epi16(x);
  146. });
  147. }
  148. i64 DotProductAvx2(const i32* lhs, const i32* rhs, size_t length) noexcept {
  149. if (length < 16) {
  150. return DotProductSse(lhs, rhs, length);
  151. }
  152. __m256i res = _mm256_setzero_ps();
  153. if (const auto leftover = length % 8; leftover != 0) {
  154. // Use floating-point blendv. Who cares as long as the size is right.
  155. __m256i a = _mm256_blendv_ps(
  156. Load256i(lhs), _mm256_setzero_ps(), BlendMask32[leftover]);
  157. __m256i b = _mm256_blendv_ps(
  158. Load256i(rhs), _mm256_setzero_ps(), BlendMask32[leftover]);
  159. res = _mm256_mul_epi32(a, b);
  160. a = _mm256_alignr_epi8(a, a, 4);
  161. b = _mm256_alignr_epi8(b, b, 4);
  162. res = _mm256_add_epi64(_mm256_mul_epi32(a, b), res);
  163. lhs += leftover;
  164. rhs += leftover;
  165. length -= leftover;
  166. }
  167. while (length >= 8) {
  168. __m256i a = Load256i(lhs);
  169. __m256i b = Load256i(rhs);
  170. res = _mm256_add_epi64(_mm256_mul_epi32(a, b), res); // This is lower parts multiplication
  171. a = _mm256_alignr_epi8(a, a, 4);
  172. b = _mm256_alignr_epi8(b, b, 4);
  173. res = _mm256_add_epi64(_mm256_mul_epi32(a, b), res);
  174. rhs += 8;
  175. lhs += 8;
  176. length -= 8;
  177. }
  178. return HsumI64(res);
  179. }
  180. float DotProductAvx2(const float* lhs, const float* rhs, size_t length) noexcept {
  181. if (length < 16) {
  182. return DotProductSse(lhs, rhs, length);
  183. }
  184. __m256 sum1 = _mm256_setzero_ps();
  185. __m256 sum2 = _mm256_setzero_ps();
  186. __m256 a1, b1, a2, b2;
  187. if (const auto leftover = length % 8; leftover != 0) {
  188. a1 = _mm256_blendv_ps(
  189. _mm256_loadu_ps(lhs), _mm256_setzero_ps(), BlendMask32[leftover]);
  190. b1 = _mm256_blendv_ps(
  191. _mm256_loadu_ps(rhs), _mm256_setzero_ps(), BlendMask32[leftover]);
  192. sum1 = _mm256_mul_ps(a1, b1);
  193. lhs += leftover;
  194. rhs += leftover;
  195. length -= leftover;
  196. }
  197. while (length >= 16) {
  198. a1 = _mm256_loadu_ps(lhs);
  199. b1 = _mm256_loadu_ps(rhs);
  200. a2 = _mm256_loadu_ps(lhs + 8);
  201. b2 = _mm256_loadu_ps(rhs + 8);
  202. sum1 = _mm256_fmadd_ps(a1, b1, sum1);
  203. sum2 = _mm256_fmadd_ps(a2, b2, sum2);
  204. length -= 16;
  205. lhs += 16;
  206. rhs += 16;
  207. }
  208. if (length > 0) {
  209. a1 = _mm256_loadu_ps(lhs);
  210. b1 = _mm256_loadu_ps(rhs);
  211. sum1 = _mm256_fmadd_ps(a1, b1, sum1);
  212. }
  213. return HsumFloat(_mm256_add_ps(sum1, sum2));
  214. }
  215. double DotProductAvx2(const double* lhs, const double* rhs, size_t length) noexcept {
  216. if (length < 16) {
  217. return DotProductSse(lhs, rhs, length);
  218. }
  219. __m256d sum1 = _mm256_setzero_pd();
  220. __m256d sum2 = _mm256_setzero_pd();
  221. __m256d a1, b1, a2, b2;
  222. if (const auto leftover = length % 4; leftover != 0) {
  223. a1 = _mm256_blendv_pd(
  224. _mm256_loadu_pd(lhs), _mm256_setzero_ps(), BlendMask64[leftover]);
  225. b1 = _mm256_blendv_pd(
  226. _mm256_loadu_pd(rhs), _mm256_setzero_ps(), BlendMask64[leftover]);
  227. sum1 = _mm256_mul_pd(a1, b1);
  228. lhs += leftover;
  229. rhs += leftover;
  230. length -= leftover;
  231. }
  232. while (length >= 8) {
  233. a1 = _mm256_loadu_pd(lhs);
  234. b1 = _mm256_loadu_pd(rhs);
  235. a2 = _mm256_loadu_pd(lhs + 4);
  236. b2 = _mm256_loadu_pd(rhs + 4);
  237. sum1 = _mm256_fmadd_pd(a1, b1, sum1);
  238. sum2 = _mm256_fmadd_pd(a2, b2, sum2);
  239. length -= 8;
  240. lhs += 8;
  241. rhs += 8;
  242. }
  243. if (length > 0) {
  244. a1 = _mm256_loadu_pd(lhs);
  245. b1 = _mm256_loadu_pd(rhs);
  246. sum1 = _mm256_fmadd_pd(a1, b1, sum1);
  247. }
  248. return HsumDouble(_mm256_add_pd(sum1, sum2));
  249. }
  250. #elif defined(ARCADIA_SSE)
  251. i32 DotProductAvx2(const i8* lhs, const i8* rhs, size_t length) noexcept {
  252. return DotProductSse(lhs, rhs, length);
  253. }
  254. ui32 DotProductAvx2(const ui8* lhs, const ui8* rhs, size_t length) noexcept {
  255. return DotProductSse(lhs, rhs, length);
  256. }
  257. i64 DotProductAvx2(const i32* lhs, const i32* rhs, size_t length) noexcept {
  258. return DotProductSse(lhs, rhs, length);
  259. }
  260. float DotProductAvx2(const float* lhs, const float* rhs, size_t length) noexcept {
  261. return DotProductSse(lhs, rhs, length);
  262. }
  263. double DotProductAvx2(const double* lhs, const double* rhs, size_t length) noexcept {
  264. return DotProductSse(lhs, rhs, length);
  265. }
  266. #else
  267. i32 DotProductAvx2(const i8* lhs, const i8* rhs, size_t length) noexcept {
  268. return DotProductSimple(lhs, rhs, length);
  269. }
  270. ui32 DotProductAvx2(const ui8* lhs, const ui8* rhs, size_t length) noexcept {
  271. return DotProductSimple(lhs, rhs, length);
  272. }
  273. i64 DotProductAvx2(const i32* lhs, const i32* rhs, size_t length) noexcept {
  274. return DotProductSimple(lhs, rhs, length);
  275. }
  276. float DotProductAvx2(const float* lhs, const float* rhs, size_t length) noexcept {
  277. return DotProductSimple(lhs, rhs, length);
  278. }
  279. double DotProductAvx2(const double* lhs, const double* rhs, size_t length) noexcept {
  280. return DotProductSimple(lhs, rhs, length);
  281. }
  282. #endif