1 /* Copyright (C) 2007-2008 Jean-Marc Valin
2 * Copyright (C) 2008 Thorvald Natvig
6 @brief Resampler functions (SSE version)
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
13 - Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
16 - Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
20 - Neither the name of the Xiph.org Foundation nor the names of its
21 contributors may be used to endorse or promote products derived from
22 this software without specific prior written permission.
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #ifdef HAVE_XMMINTRIN_H
38 #include <xmmintrin.h>
41 #define OVERRIDE_INNER_PRODUCT_SINGLE
42 static inline float inner_product_single(const float *a, const float *b, unsigned int len)
46 __m128 sum = _mm_setzero_ps();
49 sum = _mm_add_ps(sum, _mm_mul_ps(_mm_loadu_ps(a+i), _mm_loadu_ps(b+i)));
50 sum = _mm_add_ps(sum, _mm_mul_ps(_mm_loadu_ps(a+i+4), _mm_loadu_ps(b+i+4)));
52 sum = _mm_add_ps(sum, _mm_movehl_ps(sum, sum));
53 sum = _mm_add_ss(sum, _mm_shuffle_ps(sum, sum, 0x55));
54 _mm_store_ss(&ret, sum);
58 #define OVERRIDE_INTERPOLATE_PRODUCT_SINGLE
59 static inline float interpolate_product_single(const float *a, const float *b, unsigned int len, const spx_uint32_t oversample, float *frac) {
62 __m128 sum = _mm_setzero_ps();
63 __m128 f = _mm_loadu_ps(frac);
66 sum = _mm_add_ps(sum, _mm_mul_ps(_mm_load1_ps(a+i), _mm_loadu_ps(b+i*oversample)));
67 sum = _mm_add_ps(sum, _mm_mul_ps(_mm_load1_ps(a+i+1), _mm_loadu_ps(b+(i+1)*oversample)));
69 sum = _mm_mul_ps(f, sum);
70 sum = _mm_add_ps(sum, _mm_movehl_ps(sum, sum));
71 sum = _mm_add_ss(sum, _mm_shuffle_ps(sum, sum, 0x55));
72 _mm_store_ss(&ret, sum);
77 #ifdef HAVE_EMMINTRIN_H
78 #include <emmintrin.h>
80 #define OVERRIDE_INNER_PRODUCT_DOUBLE
82 #ifdef DOUBLE_PRECISION
83 static inline double inner_product_double(const double *a, const double *b, unsigned int len)
87 __m128d sum = _mm_setzero_pd();
90 sum = _mm_add_pd(sum, _mm_mul_pd(_mm_loadu_pd(a+i), _mm_loadu_pd(b+i)));
91 sum = _mm_add_pd(sum, _mm_mul_pd(_mm_loadu_pd(a+i+2), _mm_loadu_pd(b+i+2)));
93 sum = _mm_add_sd(sum, _mm_unpackhi_pd(sum, sum));
94 _mm_store_sd(&ret, sum);
98 static inline double inner_product_double(const float *a, const float *b, unsigned int len)
102 __m128d sum = _mm_setzero_pd();
106 t = _mm_mul_ps(_mm_loadu_ps(a+i), _mm_loadu_ps(b+i));
107 sum = _mm_add_pd(sum, _mm_cvtps_pd(t));
108 sum = _mm_add_pd(sum, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
110 t = _mm_mul_ps(_mm_loadu_ps(a+i+4), _mm_loadu_ps(b+i+4));
111 sum = _mm_add_pd(sum, _mm_cvtps_pd(t));
112 sum = _mm_add_pd(sum, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
114 sum = _mm_add_sd(sum, _mm_unpackhi_pd(sum, sum));
115 _mm_store_sd(&ret, sum);
121 #define OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE
123 #ifdef DOUBLE_PRECISION
124 static inline double interpolate_product_double(const double *a, const double *b, unsigned int len, const spx_uint32_t oversample, double *frac) {
128 __m128d sum1 = _mm_setzero_pd();
129 __m128d sum2 = _mm_setzero_pd();
130 __m128d f1 = _mm_loadu_pd(frac);
131 __m128d f2 = _mm_loadu_pd(frac+2);
135 t = _mm_mul_pd(_mm_load1_pd(a+i), _mm_loadu_pd(b+i*oversample));
136 sum1 = _mm_add_pd(sum1, t);
137 sum2 = _mm_add_pd(sum2, _mm_unpackhi_pd(t, t));
139 t = _mm_mul_pd(_mm_load1_pd(a+i+1), _mm_loadu_pd(b+(i+1)*oversample));
140 sum1 = _mm_add_pd(sum1, t);
141 sum2 = _mm_add_pd(sum2, _mm_unpackhi_pd(t, t));
143 sum1 = _mm_mul_pd(f1, sum1);
144 sum2 = _mm_mul_pd(f2, sum2);
145 sum = _mm_add_pd(sum1, sum2);
146 sum = _mm_add_sd(sum, _mm_unpackhi_pd(sum, sum));
147 _mm_store_sd(&ret, sum);
151 static inline double interpolate_product_double(const float *a, const float *b, unsigned int len, const spx_uint32_t oversample, float *frac) {
155 __m128d sum1 = _mm_setzero_pd();
156 __m128d sum2 = _mm_setzero_pd();
157 __m128 f = _mm_loadu_ps(frac);
158 __m128d f1 = _mm_cvtps_pd(f);
159 __m128d f2 = _mm_cvtps_pd(_mm_movehl_ps(f,f));
163 t = _mm_mul_ps(_mm_load1_ps(a+i), _mm_loadu_ps(b+i*oversample));
164 sum1 = _mm_add_pd(sum1, _mm_cvtps_pd(t));
165 sum2 = _mm_add_pd(sum2, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
167 t = _mm_mul_ps(_mm_load1_ps(a+i+1), _mm_loadu_ps(b+(i+1)*oversample));
168 sum1 = _mm_add_pd(sum1, _mm_cvtps_pd(t));
169 sum2 = _mm_add_pd(sum2, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
171 sum1 = _mm_mul_pd(f1, sum1);
172 sum2 = _mm_mul_pd(f2, sum2);
173 sum = _mm_add_pd(sum1, sum2);
174 sum = _mm_add_sd(sum, _mm_unpackhi_pd(sum, sum));
175 _mm_store_sd(&ret, sum);