sum_squares_sse2.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2016 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 <assert.h>
  11. #include <emmintrin.h>
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "vpx_dsp/x86/mem_sse2.h"
  14. uint64_t vpx_sum_squares_2d_i16_sse2(const int16_t *src, int stride, int size) {
  15. // Over 75% of all calls are with size == 4.
  16. if (size == 4) {
  17. __m128i s[2], sq[2], ss;
  18. s[0] = _mm_loadl_epi64((const __m128i *)(src + 0 * stride));
  19. s[0] = loadh_epi64(s[0], src + 1 * stride);
  20. s[1] = _mm_loadl_epi64((const __m128i *)(src + 2 * stride));
  21. s[1] = loadh_epi64(s[1], src + 3 * stride);
  22. sq[0] = _mm_madd_epi16(s[0], s[0]);
  23. sq[1] = _mm_madd_epi16(s[1], s[1]);
  24. sq[0] = _mm_add_epi32(sq[0], sq[1]);
  25. ss = _mm_add_epi32(sq[0], _mm_srli_si128(sq[0], 8));
  26. ss = _mm_add_epi32(ss, _mm_srli_epi64(ss, 32));
  27. return (uint64_t)_mm_cvtsi128_si32(ss);
  28. } else {
  29. // Generic case
  30. int r = size;
  31. const __m128i v_zext_mask_q = _mm_set_epi32(0, 0xffffffff, 0, 0xffffffff);
  32. __m128i v_acc_q = _mm_setzero_si128();
  33. assert(size % 8 == 0);
  34. do {
  35. int c = 0;
  36. __m128i v_acc_d = _mm_setzero_si128();
  37. do {
  38. const int16_t *const b = src + c;
  39. const __m128i v_val_0_w =
  40. _mm_load_si128((const __m128i *)(b + 0 * stride));
  41. const __m128i v_val_1_w =
  42. _mm_load_si128((const __m128i *)(b + 1 * stride));
  43. const __m128i v_val_2_w =
  44. _mm_load_si128((const __m128i *)(b + 2 * stride));
  45. const __m128i v_val_3_w =
  46. _mm_load_si128((const __m128i *)(b + 3 * stride));
  47. const __m128i v_val_4_w =
  48. _mm_load_si128((const __m128i *)(b + 4 * stride));
  49. const __m128i v_val_5_w =
  50. _mm_load_si128((const __m128i *)(b + 5 * stride));
  51. const __m128i v_val_6_w =
  52. _mm_load_si128((const __m128i *)(b + 6 * stride));
  53. const __m128i v_val_7_w =
  54. _mm_load_si128((const __m128i *)(b + 7 * stride));
  55. const __m128i v_sq_0_d = _mm_madd_epi16(v_val_0_w, v_val_0_w);
  56. const __m128i v_sq_1_d = _mm_madd_epi16(v_val_1_w, v_val_1_w);
  57. const __m128i v_sq_2_d = _mm_madd_epi16(v_val_2_w, v_val_2_w);
  58. const __m128i v_sq_3_d = _mm_madd_epi16(v_val_3_w, v_val_3_w);
  59. const __m128i v_sq_4_d = _mm_madd_epi16(v_val_4_w, v_val_4_w);
  60. const __m128i v_sq_5_d = _mm_madd_epi16(v_val_5_w, v_val_5_w);
  61. const __m128i v_sq_6_d = _mm_madd_epi16(v_val_6_w, v_val_6_w);
  62. const __m128i v_sq_7_d = _mm_madd_epi16(v_val_7_w, v_val_7_w);
  63. const __m128i v_sum_01_d = _mm_add_epi32(v_sq_0_d, v_sq_1_d);
  64. const __m128i v_sum_23_d = _mm_add_epi32(v_sq_2_d, v_sq_3_d);
  65. const __m128i v_sum_45_d = _mm_add_epi32(v_sq_4_d, v_sq_5_d);
  66. const __m128i v_sum_67_d = _mm_add_epi32(v_sq_6_d, v_sq_7_d);
  67. const __m128i v_sum_0123_d = _mm_add_epi32(v_sum_01_d, v_sum_23_d);
  68. const __m128i v_sum_4567_d = _mm_add_epi32(v_sum_45_d, v_sum_67_d);
  69. v_acc_d = _mm_add_epi32(v_acc_d, v_sum_0123_d);
  70. v_acc_d = _mm_add_epi32(v_acc_d, v_sum_4567_d);
  71. c += 8;
  72. } while (c < size);
  73. v_acc_q = _mm_add_epi64(v_acc_q, _mm_and_si128(v_acc_d, v_zext_mask_q));
  74. v_acc_q = _mm_add_epi64(v_acc_q, _mm_srli_epi64(v_acc_d, 32));
  75. src += 8 * stride;
  76. r -= 8;
  77. } while (r);
  78. v_acc_q = _mm_add_epi64(v_acc_q, _mm_srli_si128(v_acc_q, 8));
  79. #if ARCH_X86_64
  80. return (uint64_t)_mm_cvtsi128_si64(v_acc_q);
  81. #else
  82. {
  83. uint64_t tmp;
  84. _mm_storel_epi64((__m128i *)&tmp, v_acc_q);
  85. return tmp;
  86. }
  87. #endif
  88. }
  89. }