vp9_error_intrin_avx2.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
  3. *
  4. * Usee 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 "./vp9_rtcd.h"
  12. #include "vpx/vpx_integer.h"
  13. int64_t vp9_block_error_avx2(const int16_t *coeff, const int16_t *dqcoeff,
  14. intptr_t block_size, int64_t *ssz) {
  15. __m256i sse_reg, ssz_reg, coeff_reg, dqcoeff_reg;
  16. __m256i exp_dqcoeff_lo, exp_dqcoeff_hi, exp_coeff_lo, exp_coeff_hi;
  17. __m256i sse_reg_64hi, ssz_reg_64hi;
  18. __m128i sse_reg128, ssz_reg128;
  19. int64_t sse;
  20. int i;
  21. const __m256i zero_reg = _mm256_set1_epi16(0);
  22. // init sse and ssz registerd to zero
  23. sse_reg = _mm256_set1_epi16(0);
  24. ssz_reg = _mm256_set1_epi16(0);
  25. for (i = 0; i < block_size; i += 16) {
  26. // load 32 bytes from coeff and dqcoeff
  27. coeff_reg = _mm256_loadu_si256((const __m256i *)(coeff + i));
  28. dqcoeff_reg = _mm256_loadu_si256((const __m256i *)(dqcoeff + i));
  29. // dqcoeff - coeff
  30. dqcoeff_reg = _mm256_sub_epi16(dqcoeff_reg, coeff_reg);
  31. // madd (dqcoeff - coeff)
  32. dqcoeff_reg = _mm256_madd_epi16(dqcoeff_reg, dqcoeff_reg);
  33. // madd coeff
  34. coeff_reg = _mm256_madd_epi16(coeff_reg, coeff_reg);
  35. // expand each double word of madd (dqcoeff - coeff) to quad word
  36. exp_dqcoeff_lo = _mm256_unpacklo_epi32(dqcoeff_reg, zero_reg);
  37. exp_dqcoeff_hi = _mm256_unpackhi_epi32(dqcoeff_reg, zero_reg);
  38. // expand each double word of madd (coeff) to quad word
  39. exp_coeff_lo = _mm256_unpacklo_epi32(coeff_reg, zero_reg);
  40. exp_coeff_hi = _mm256_unpackhi_epi32(coeff_reg, zero_reg);
  41. // add each quad word of madd (dqcoeff - coeff) and madd (coeff)
  42. sse_reg = _mm256_add_epi64(sse_reg, exp_dqcoeff_lo);
  43. ssz_reg = _mm256_add_epi64(ssz_reg, exp_coeff_lo);
  44. sse_reg = _mm256_add_epi64(sse_reg, exp_dqcoeff_hi);
  45. ssz_reg = _mm256_add_epi64(ssz_reg, exp_coeff_hi);
  46. }
  47. // save the higher 64 bit of each 128 bit lane
  48. sse_reg_64hi = _mm256_srli_si256(sse_reg, 8);
  49. ssz_reg_64hi = _mm256_srli_si256(ssz_reg, 8);
  50. // add the higher 64 bit to the low 64 bit
  51. sse_reg = _mm256_add_epi64(sse_reg, sse_reg_64hi);
  52. ssz_reg = _mm256_add_epi64(ssz_reg, ssz_reg_64hi);
  53. // add each 64 bit from each of the 128 bit lane of the 256 bit
  54. sse_reg128 = _mm_add_epi64(_mm256_castsi256_si128(sse_reg),
  55. _mm256_extractf128_si256(sse_reg, 1));
  56. ssz_reg128 = _mm_add_epi64(_mm256_castsi256_si128(ssz_reg),
  57. _mm256_extractf128_si256(ssz_reg, 1));
  58. // store the results
  59. _mm_storel_epi64((__m128i *)(&sse), sse_reg128);
  60. _mm_storel_epi64((__m128i *)(ssz), ssz_reg128);
  61. return sse;
  62. }