variance_impl_avx2.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <immintrin.h> // AVX2
  11. #include "./vpx_dsp_rtcd.h"
  12. void vpx_get16x16var_avx2(const unsigned char *src_ptr,
  13. int source_stride,
  14. const unsigned char *ref_ptr,
  15. int recon_stride,
  16. unsigned int *SSE,
  17. int *Sum) {
  18. __m256i src, src_expand_low, src_expand_high, ref, ref_expand_low;
  19. __m256i ref_expand_high, madd_low, madd_high;
  20. unsigned int i, src_2strides, ref_2strides;
  21. __m256i zero_reg = _mm256_set1_epi16(0);
  22. __m256i sum_ref_src = _mm256_set1_epi16(0);
  23. __m256i madd_ref_src = _mm256_set1_epi16(0);
  24. // processing two strides in a 256 bit register reducing the number
  25. // of loop stride by half (comparing to the sse2 code)
  26. src_2strides = source_stride << 1;
  27. ref_2strides = recon_stride << 1;
  28. for (i = 0; i < 8; i++) {
  29. src = _mm256_castsi128_si256(
  30. _mm_loadu_si128((__m128i const *) (src_ptr)));
  31. src = _mm256_inserti128_si256(src,
  32. _mm_loadu_si128((__m128i const *)(src_ptr+source_stride)), 1);
  33. ref =_mm256_castsi128_si256(
  34. _mm_loadu_si128((__m128i const *) (ref_ptr)));
  35. ref = _mm256_inserti128_si256(ref,
  36. _mm_loadu_si128((__m128i const *)(ref_ptr+recon_stride)), 1);
  37. // expanding to 16 bit each lane
  38. src_expand_low = _mm256_unpacklo_epi8(src, zero_reg);
  39. src_expand_high = _mm256_unpackhi_epi8(src, zero_reg);
  40. ref_expand_low = _mm256_unpacklo_epi8(ref, zero_reg);
  41. ref_expand_high = _mm256_unpackhi_epi8(ref, zero_reg);
  42. // src-ref
  43. src_expand_low = _mm256_sub_epi16(src_expand_low, ref_expand_low);
  44. src_expand_high = _mm256_sub_epi16(src_expand_high, ref_expand_high);
  45. // madd low (src - ref)
  46. madd_low = _mm256_madd_epi16(src_expand_low, src_expand_low);
  47. // add high to low
  48. src_expand_low = _mm256_add_epi16(src_expand_low, src_expand_high);
  49. // madd high (src - ref)
  50. madd_high = _mm256_madd_epi16(src_expand_high, src_expand_high);
  51. sum_ref_src = _mm256_add_epi16(sum_ref_src, src_expand_low);
  52. // add high to low
  53. madd_ref_src = _mm256_add_epi32(madd_ref_src,
  54. _mm256_add_epi32(madd_low, madd_high));
  55. src_ptr+= src_2strides;
  56. ref_ptr+= ref_2strides;
  57. }
  58. {
  59. __m128i sum_res, madd_res;
  60. __m128i expand_sum_low, expand_sum_high, expand_sum;
  61. __m128i expand_madd_low, expand_madd_high, expand_madd;
  62. __m128i ex_expand_sum_low, ex_expand_sum_high, ex_expand_sum;
  63. // extract the low lane and add it to the high lane
  64. sum_res = _mm_add_epi16(_mm256_castsi256_si128(sum_ref_src),
  65. _mm256_extractf128_si256(sum_ref_src, 1));
  66. madd_res = _mm_add_epi32(_mm256_castsi256_si128(madd_ref_src),
  67. _mm256_extractf128_si256(madd_ref_src, 1));
  68. // padding each 2 bytes with another 2 zeroed bytes
  69. expand_sum_low = _mm_unpacklo_epi16(_mm256_castsi256_si128(zero_reg),
  70. sum_res);
  71. expand_sum_high = _mm_unpackhi_epi16(_mm256_castsi256_si128(zero_reg),
  72. sum_res);
  73. // shifting the sign 16 bits right
  74. expand_sum_low = _mm_srai_epi32(expand_sum_low, 16);
  75. expand_sum_high = _mm_srai_epi32(expand_sum_high, 16);
  76. expand_sum = _mm_add_epi32(expand_sum_low, expand_sum_high);
  77. // expand each 32 bits of the madd result to 64 bits
  78. expand_madd_low = _mm_unpacklo_epi32(madd_res,
  79. _mm256_castsi256_si128(zero_reg));
  80. expand_madd_high = _mm_unpackhi_epi32(madd_res,
  81. _mm256_castsi256_si128(zero_reg));
  82. expand_madd = _mm_add_epi32(expand_madd_low, expand_madd_high);
  83. ex_expand_sum_low = _mm_unpacklo_epi32(expand_sum,
  84. _mm256_castsi256_si128(zero_reg));
  85. ex_expand_sum_high = _mm_unpackhi_epi32(expand_sum,
  86. _mm256_castsi256_si128(zero_reg));
  87. ex_expand_sum = _mm_add_epi32(ex_expand_sum_low, ex_expand_sum_high);
  88. // shift 8 bytes eight
  89. madd_res = _mm_srli_si128(expand_madd, 8);
  90. sum_res = _mm_srli_si128(ex_expand_sum, 8);
  91. madd_res = _mm_add_epi32(madd_res, expand_madd);
  92. sum_res = _mm_add_epi32(sum_res, ex_expand_sum);
  93. *((int*)SSE)= _mm_cvtsi128_si32(madd_res);
  94. *((int*)Sum)= _mm_cvtsi128_si32(sum_res);
  95. }
  96. }
  97. void vpx_get32x32var_avx2(const unsigned char *src_ptr,
  98. int source_stride,
  99. const unsigned char *ref_ptr,
  100. int recon_stride,
  101. unsigned int *SSE,
  102. int *Sum) {
  103. __m256i src, src_expand_low, src_expand_high, ref, ref_expand_low;
  104. __m256i ref_expand_high, madd_low, madd_high;
  105. unsigned int i;
  106. __m256i zero_reg = _mm256_set1_epi16(0);
  107. __m256i sum_ref_src = _mm256_set1_epi16(0);
  108. __m256i madd_ref_src = _mm256_set1_epi16(0);
  109. // processing 32 elements in parallel
  110. for (i = 0; i < 16; i++) {
  111. src = _mm256_loadu_si256((__m256i const *) (src_ptr));
  112. ref = _mm256_loadu_si256((__m256i const *) (ref_ptr));
  113. // expanding to 16 bit each lane
  114. src_expand_low = _mm256_unpacklo_epi8(src, zero_reg);
  115. src_expand_high = _mm256_unpackhi_epi8(src, zero_reg);
  116. ref_expand_low = _mm256_unpacklo_epi8(ref, zero_reg);
  117. ref_expand_high = _mm256_unpackhi_epi8(ref, zero_reg);
  118. // src-ref
  119. src_expand_low = _mm256_sub_epi16(src_expand_low, ref_expand_low);
  120. src_expand_high = _mm256_sub_epi16(src_expand_high, ref_expand_high);
  121. // madd low (src - ref)
  122. madd_low = _mm256_madd_epi16(src_expand_low, src_expand_low);
  123. // add high to low
  124. src_expand_low = _mm256_add_epi16(src_expand_low, src_expand_high);
  125. // madd high (src - ref)
  126. madd_high = _mm256_madd_epi16(src_expand_high, src_expand_high);
  127. sum_ref_src = _mm256_add_epi16(sum_ref_src, src_expand_low);
  128. // add high to low
  129. madd_ref_src = _mm256_add_epi32(madd_ref_src,
  130. _mm256_add_epi32(madd_low, madd_high));
  131. src_ptr+= source_stride;
  132. ref_ptr+= recon_stride;
  133. }
  134. {
  135. __m256i expand_sum_low, expand_sum_high, expand_sum;
  136. __m256i expand_madd_low, expand_madd_high, expand_madd;
  137. __m256i ex_expand_sum_low, ex_expand_sum_high, ex_expand_sum;
  138. // padding each 2 bytes with another 2 zeroed bytes
  139. expand_sum_low = _mm256_unpacklo_epi16(zero_reg, sum_ref_src);
  140. expand_sum_high = _mm256_unpackhi_epi16(zero_reg, sum_ref_src);
  141. // shifting the sign 16 bits right
  142. expand_sum_low = _mm256_srai_epi32(expand_sum_low, 16);
  143. expand_sum_high = _mm256_srai_epi32(expand_sum_high, 16);
  144. expand_sum = _mm256_add_epi32(expand_sum_low, expand_sum_high);
  145. // expand each 32 bits of the madd result to 64 bits
  146. expand_madd_low = _mm256_unpacklo_epi32(madd_ref_src, zero_reg);
  147. expand_madd_high = _mm256_unpackhi_epi32(madd_ref_src, zero_reg);
  148. expand_madd = _mm256_add_epi32(expand_madd_low, expand_madd_high);
  149. ex_expand_sum_low = _mm256_unpacklo_epi32(expand_sum, zero_reg);
  150. ex_expand_sum_high = _mm256_unpackhi_epi32(expand_sum, zero_reg);
  151. ex_expand_sum = _mm256_add_epi32(ex_expand_sum_low, ex_expand_sum_high);
  152. // shift 8 bytes eight
  153. madd_ref_src = _mm256_srli_si256(expand_madd, 8);
  154. sum_ref_src = _mm256_srli_si256(ex_expand_sum, 8);
  155. madd_ref_src = _mm256_add_epi32(madd_ref_src, expand_madd);
  156. sum_ref_src = _mm256_add_epi32(sum_ref_src, ex_expand_sum);
  157. // extract the low lane and the high lane and add the results
  158. *((int*)SSE)= _mm_cvtsi128_si32(_mm256_castsi256_si128(madd_ref_src)) +
  159. _mm_cvtsi128_si32(_mm256_extractf128_si256(madd_ref_src, 1));
  160. *((int*)Sum)= _mm_cvtsi128_si32(_mm256_castsi256_si128(sum_ref_src)) +
  161. _mm_cvtsi128_si32(_mm256_extractf128_si256(sum_ref_src, 1));
  162. }
  163. }