Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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:
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.
35 */
37 #include <xmmintrin.h>
39 #define OVERRIDE_INNER_PRODUCT_SINGLE
40 static inline float inner_product_single(const float *a, const float *b, unsigned int len)
41 {
42 int i;
43 float ret;
44 if (1)
45 {
46 __m128 sum = _mm_setzero_ps();
47 for (i=0;i<len;i+=8)
48 {
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)));
51 }
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);
55 }
56 else
57 {
58 ret = 0;
59 for (i=0;i<len;i++) ret += a[i] * b[i];
60 }
61 return ret;
62 }
64 #define OVERRIDE_INTERPOLATE_PRODUCT_SINGLE
65 static inline float interpolate_product_single(const float *a, const float *b, unsigned int len, const spx_uint32_t oversample, float *frac) {
66 int i;
67 float ret;
68 if (1)
69 {
70 __m128 sum = _mm_setzero_ps();
71 __m128 f = _mm_loadu_ps(frac);
72 for(i=0;i<len;i+=2)
73 {
74 sum = _mm_add_ps(sum, _mm_mul_ps(_mm_load1_ps(a+i), _mm_loadu_ps(b+i*oversample)));
75 sum = _mm_add_ps(sum, _mm_mul_ps(_mm_load1_ps(a+i+1), _mm_loadu_ps(b+(i+1)*oversample)));
76 }
77 sum = _mm_mul_ps(f, sum);
78 sum = _mm_add_ps(sum, _mm_movehl_ps(sum, sum));
79 sum = _mm_add_ss(sum, _mm_shuffle_ps(sum, sum, 0x55));
80 _mm_store_ss(&ret, sum);
81 }
82 else
83 {
84 float accum[4] = {0,0,0,0};
85 for(i=0;i<len;i++)
86 {
87 const float curr_in=a[i];
88 accum[0] += curr_in * b[i * oversample + 0];
89 accum[1] += curr_in * b[i * oversample + 1];
90 accum[2] += curr_in * b[i * oversample + 2];
91 accum[3] += curr_in * b[i * oversample + 3];
92 }
93 ret = accum[0] * frac[0] + accum[1] * frac[1] + accum[2] * frac[2] + accum[3] * frac[3];
94 }
95 return ret;
96 }
98 #ifdef __SSE2__
99 #include <emmintrin.h>
100 #define OVERRIDE_INNER_PRODUCT_DOUBLE
102 static inline double inner_product_double(const float *a, const float *b, unsigned int len)
103 {
104 int i;
105 double ret;
106 __m128d sum = _mm_setzero_pd();
107 __m128 t;
108 for (i=0;i<len;i+=8)
109 {
110 t = _mm_mul_ps(_mm_loadu_ps(a+i), _mm_loadu_ps(b+i));
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 t = _mm_mul_ps(_mm_loadu_ps(a+i+4), _mm_loadu_ps(b+i+4));
115 sum = _mm_add_pd(sum, _mm_cvtps_pd(t));
116 sum = _mm_add_pd(sum, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
117 }
118 sum = _mm_add_sd(sum, (__m128d) _mm_movehl_ps((__m128) sum, (__m128) sum));
119 _mm_store_sd(&ret, sum);
120 return ret;
121 }
123 #define OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE
124 static inline double interpolate_product_double(const float *a, const float *b, unsigned int len, const spx_uint32_t oversample, float *frac) {
125 int i;
126 double ret;
127 __m128d sum;
128 __m128d sum1 = _mm_setzero_pd();
129 __m128d sum2 = _mm_setzero_pd();
130 __m128 f = _mm_loadu_ps(frac);
131 __m128d f1 = _mm_cvtps_pd(f);
132 __m128d f2 = _mm_cvtps_pd(_mm_movehl_ps(f,f));
133 __m128 t;
134 for(i=0;i<len;i+=2)
135 {
136 t = _mm_mul_ps(_mm_load1_ps(a+i), _mm_loadu_ps(b+i*oversample));
137 sum1 = _mm_add_pd(sum1, _mm_cvtps_pd(t));
138 sum2 = _mm_add_pd(sum2, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
140 t = _mm_mul_ps(_mm_load1_ps(a+i+1), _mm_loadu_ps(b+(i+1)*oversample));
141 sum1 = _mm_add_pd(sum1, _mm_cvtps_pd(t));
142 sum2 = _mm_add_pd(sum2, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
143 }
144 sum1 = _mm_mul_pd(f1, sum1);
145 sum2 = _mm_mul_pd(f2, sum2);
146 sum = _mm_add_pd(sum1, sum2);
147 sum = _mm_add_sd(sum, (__m128d) _mm_movehl_ps((__m128) sum, (__m128) sum));
148 _mm_store_sd(&ret, sum);
149 return ret;
150 }
152 #endif