resample_sse.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright (C) 2007-2008 Jean-Marc Valin
  2. * Copyright (C) 2008 Thorvald Natvig
  3. */
  4. /**
  5. @file resample_sse.h
  6. @brief Resampler functions (SSE version)
  7. */
  8. /*
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. - Neither the name of the Xiph.org Foundation nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  24. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <xmmintrin.h>
  33. #define OVERRIDE_INNER_PRODUCT_SINGLE
  34. static inline float inner_product_single(const float *a, const float *b, unsigned int len)
  35. {
  36. int i;
  37. float ret;
  38. __m128 sum = _mm_setzero_ps();
  39. for (i=0;i<len;i+=8)
  40. {
  41. sum = _mm_add_ps(sum, _mm_mul_ps(_mm_loadu_ps(a+i), _mm_loadu_ps(b+i)));
  42. sum = _mm_add_ps(sum, _mm_mul_ps(_mm_loadu_ps(a+i+4), _mm_loadu_ps(b+i+4)));
  43. }
  44. sum = _mm_add_ps(sum, _mm_movehl_ps(sum, sum));
  45. sum = _mm_add_ss(sum, _mm_shuffle_ps(sum, sum, 0x55));
  46. _mm_store_ss(&ret, sum);
  47. return ret;
  48. }
  49. #define OVERRIDE_INTERPOLATE_PRODUCT_SINGLE
  50. static inline float interpolate_product_single(const float *a, const float *b, unsigned int len, const spx_uint32_t oversample, float *frac) {
  51. int i;
  52. float ret;
  53. __m128 sum = _mm_setzero_ps();
  54. __m128 f = _mm_loadu_ps(frac);
  55. for(i=0;i<len;i+=2)
  56. {
  57. sum = _mm_add_ps(sum, _mm_mul_ps(_mm_load1_ps(a+i), _mm_loadu_ps(b+i*oversample)));
  58. sum = _mm_add_ps(sum, _mm_mul_ps(_mm_load1_ps(a+i+1), _mm_loadu_ps(b+(i+1)*oversample)));
  59. }
  60. sum = _mm_mul_ps(f, sum);
  61. sum = _mm_add_ps(sum, _mm_movehl_ps(sum, sum));
  62. sum = _mm_add_ss(sum, _mm_shuffle_ps(sum, sum, 0x55));
  63. _mm_store_ss(&ret, sum);
  64. return ret;
  65. }
  66. #ifdef USE_SSE2
  67. #include <emmintrin.h>
  68. #define OVERRIDE_INNER_PRODUCT_DOUBLE
  69. static inline double inner_product_double(const float *a, const float *b, unsigned int len)
  70. {
  71. int i;
  72. double ret;
  73. __m128d sum = _mm_setzero_pd();
  74. __m128 t;
  75. for (i=0;i<len;i+=8)
  76. {
  77. t = _mm_mul_ps(_mm_loadu_ps(a+i), _mm_loadu_ps(b+i));
  78. sum = _mm_add_pd(sum, _mm_cvtps_pd(t));
  79. sum = _mm_add_pd(sum, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
  80. t = _mm_mul_ps(_mm_loadu_ps(a+i+4), _mm_loadu_ps(b+i+4));
  81. sum = _mm_add_pd(sum, _mm_cvtps_pd(t));
  82. sum = _mm_add_pd(sum, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
  83. }
  84. sum = _mm_add_sd(sum, _mm_unpackhi_pd(sum, sum));
  85. _mm_store_sd(&ret, sum);
  86. return ret;
  87. }
  88. #define OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE
  89. static inline double interpolate_product_double(const float *a, const float *b, unsigned int len, const spx_uint32_t oversample, float *frac) {
  90. int i;
  91. double ret;
  92. __m128d sum;
  93. __m128d sum1 = _mm_setzero_pd();
  94. __m128d sum2 = _mm_setzero_pd();
  95. __m128 f = _mm_loadu_ps(frac);
  96. __m128d f1 = _mm_cvtps_pd(f);
  97. __m128d f2 = _mm_cvtps_pd(_mm_movehl_ps(f,f));
  98. __m128 t;
  99. for(i=0;i<len;i+=2)
  100. {
  101. t = _mm_mul_ps(_mm_load1_ps(a+i), _mm_loadu_ps(b+i*oversample));
  102. sum1 = _mm_add_pd(sum1, _mm_cvtps_pd(t));
  103. sum2 = _mm_add_pd(sum2, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
  104. t = _mm_mul_ps(_mm_load1_ps(a+i+1), _mm_loadu_ps(b+(i+1)*oversample));
  105. sum1 = _mm_add_pd(sum1, _mm_cvtps_pd(t));
  106. sum2 = _mm_add_pd(sum2, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
  107. }
  108. sum1 = _mm_mul_pd(f1, sum1);
  109. sum2 = _mm_mul_pd(f2, sum2);
  110. sum = _mm_add_pd(sum1, sum2);
  111. sum = _mm_add_sd(sum, _mm_unpackhi_pd(sum, sum));
  112. _mm_store_sd(&ret, sum);
  113. return ret;
  114. }
  115. #endif