vp9_error_avx2.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <assert.h>
  11. #include <immintrin.h>
  12. #include "./vp9_rtcd.h"
  13. #include "vpx/vpx_integer.h"
  14. #include "vpx_dsp/vpx_dsp_common.h"
  15. #include "vpx_dsp/x86/bitdepth_conversion_avx2.h"
  16. int64_t vp9_block_error_avx2(const tran_low_t *coeff, const tran_low_t *dqcoeff,
  17. intptr_t block_size, int64_t *ssz) {
  18. __m256i sse_256, ssz_256;
  19. __m256i exp_dqcoeff_lo, exp_dqcoeff_hi, exp_coeff_lo, exp_coeff_hi;
  20. __m256i sse_hi, ssz_hi;
  21. __m128i sse_128, ssz_128;
  22. int64_t sse;
  23. const __m256i zero = _mm256_setzero_si256();
  24. // If the block size is 16 then the results will fit in 32 bits.
  25. if (block_size == 16) {
  26. __m256i coeff_256, dqcoeff_256, coeff_hi, dqcoeff_hi;
  27. // Load 16 elements for coeff and dqcoeff.
  28. coeff_256 = load_tran_low(coeff);
  29. dqcoeff_256 = load_tran_low(dqcoeff);
  30. // dqcoeff - coeff
  31. dqcoeff_256 = _mm256_sub_epi16(dqcoeff_256, coeff_256);
  32. // madd (dqcoeff - coeff)
  33. dqcoeff_256 = _mm256_madd_epi16(dqcoeff_256, dqcoeff_256);
  34. // madd coeff
  35. coeff_256 = _mm256_madd_epi16(coeff_256, coeff_256);
  36. // Save the higher 64 bit of each 128 bit lane.
  37. dqcoeff_hi = _mm256_srli_si256(dqcoeff_256, 8);
  38. coeff_hi = _mm256_srli_si256(coeff_256, 8);
  39. // Add the higher 64 bit to the low 64 bit.
  40. dqcoeff_256 = _mm256_add_epi32(dqcoeff_256, dqcoeff_hi);
  41. coeff_256 = _mm256_add_epi32(coeff_256, coeff_hi);
  42. // Expand each double word in the lower 64 bits to quad word.
  43. sse_256 = _mm256_unpacklo_epi32(dqcoeff_256, zero);
  44. ssz_256 = _mm256_unpacklo_epi32(coeff_256, zero);
  45. } else {
  46. int i;
  47. assert(block_size % 32 == 0);
  48. sse_256 = zero;
  49. ssz_256 = zero;
  50. for (i = 0; i < block_size; i += 32) {
  51. __m256i coeff_0, coeff_1, dqcoeff_0, dqcoeff_1;
  52. // Load 32 elements for coeff and dqcoeff.
  53. coeff_0 = load_tran_low(coeff + i);
  54. dqcoeff_0 = load_tran_low(dqcoeff + i);
  55. coeff_1 = load_tran_low(coeff + i + 16);
  56. dqcoeff_1 = load_tran_low(dqcoeff + i + 16);
  57. // dqcoeff - coeff
  58. dqcoeff_0 = _mm256_sub_epi16(dqcoeff_0, coeff_0);
  59. dqcoeff_1 = _mm256_sub_epi16(dqcoeff_1, coeff_1);
  60. // madd (dqcoeff - coeff)
  61. dqcoeff_0 = _mm256_madd_epi16(dqcoeff_0, dqcoeff_0);
  62. dqcoeff_1 = _mm256_madd_epi16(dqcoeff_1, dqcoeff_1);
  63. // madd coeff
  64. coeff_0 = _mm256_madd_epi16(coeff_0, coeff_0);
  65. coeff_1 = _mm256_madd_epi16(coeff_1, coeff_1);
  66. // Add the first madd (dqcoeff - coeff) with the second.
  67. dqcoeff_0 = _mm256_add_epi32(dqcoeff_0, dqcoeff_1);
  68. // Add the first madd (coeff) with the second.
  69. coeff_0 = _mm256_add_epi32(coeff_0, coeff_1);
  70. // Expand each double word of madd (dqcoeff - coeff) to quad word.
  71. exp_dqcoeff_lo = _mm256_unpacklo_epi32(dqcoeff_0, zero);
  72. exp_dqcoeff_hi = _mm256_unpackhi_epi32(dqcoeff_0, zero);
  73. // expand each double word of madd (coeff) to quad word
  74. exp_coeff_lo = _mm256_unpacklo_epi32(coeff_0, zero);
  75. exp_coeff_hi = _mm256_unpackhi_epi32(coeff_0, zero);
  76. // Add each quad word of madd (dqcoeff - coeff) and madd (coeff).
  77. sse_256 = _mm256_add_epi64(sse_256, exp_dqcoeff_lo);
  78. ssz_256 = _mm256_add_epi64(ssz_256, exp_coeff_lo);
  79. sse_256 = _mm256_add_epi64(sse_256, exp_dqcoeff_hi);
  80. ssz_256 = _mm256_add_epi64(ssz_256, exp_coeff_hi);
  81. }
  82. }
  83. // Save the higher 64 bit of each 128 bit lane.
  84. sse_hi = _mm256_srli_si256(sse_256, 8);
  85. ssz_hi = _mm256_srli_si256(ssz_256, 8);
  86. // Add the higher 64 bit to the low 64 bit.
  87. sse_256 = _mm256_add_epi64(sse_256, sse_hi);
  88. ssz_256 = _mm256_add_epi64(ssz_256, ssz_hi);
  89. // Add each 64 bit from each of the 128 bit lane of the 256 bit.
  90. sse_128 = _mm_add_epi64(_mm256_castsi256_si128(sse_256),
  91. _mm256_extractf128_si256(sse_256, 1));
  92. ssz_128 = _mm_add_epi64(_mm256_castsi256_si128(ssz_256),
  93. _mm256_extractf128_si256(ssz_256, 1));
  94. // Store the results.
  95. _mm_storel_epi64((__m128i *)(&sse), sse_128);
  96. _mm_storel_epi64((__m128i *)(ssz), ssz_128);
  97. return sse;
  98. }