cannam@154
|
1 /* Copyright (c) 2007-2008 CSIRO
|
cannam@154
|
2 Copyright (c) 2007-2009 Xiph.Org Foundation
|
cannam@154
|
3 Copyright (c) 2007-2016 Jean-Marc Valin */
|
cannam@154
|
4 /*
|
cannam@154
|
5 Redistribution and use in source and binary forms, with or without
|
cannam@154
|
6 modification, are permitted provided that the following conditions
|
cannam@154
|
7 are met:
|
cannam@154
|
8
|
cannam@154
|
9 - Redistributions of source code must retain the above copyright
|
cannam@154
|
10 notice, this list of conditions and the following disclaimer.
|
cannam@154
|
11
|
cannam@154
|
12 - Redistributions in binary form must reproduce the above copyright
|
cannam@154
|
13 notice, this list of conditions and the following disclaimer in the
|
cannam@154
|
14 documentation and/or other materials provided with the distribution.
|
cannam@154
|
15
|
cannam@154
|
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
cannam@154
|
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
cannam@154
|
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
cannam@154
|
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
cannam@154
|
20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
cannam@154
|
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
cannam@154
|
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
cannam@154
|
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
cannam@154
|
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
cannam@154
|
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
cannam@154
|
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
cannam@154
|
27 */
|
cannam@154
|
28
|
cannam@154
|
29 #ifdef HAVE_CONFIG_H
|
cannam@154
|
30 #include "config.h"
|
cannam@154
|
31 #endif
|
cannam@154
|
32
|
cannam@154
|
33 #include <xmmintrin.h>
|
cannam@154
|
34 #include <emmintrin.h>
|
cannam@154
|
35 #include "celt_lpc.h"
|
cannam@154
|
36 #include "stack_alloc.h"
|
cannam@154
|
37 #include "mathops.h"
|
cannam@154
|
38 #include "vq.h"
|
cannam@154
|
39 #include "x86cpu.h"
|
cannam@154
|
40
|
cannam@154
|
41
|
cannam@154
|
42 #ifndef FIXED_POINT
|
cannam@154
|
43
|
cannam@154
|
44 opus_val16 op_pvq_search_sse2(celt_norm *_X, int *iy, int K, int N, int arch)
|
cannam@154
|
45 {
|
cannam@154
|
46 int i, j;
|
cannam@154
|
47 int pulsesLeft;
|
cannam@154
|
48 float xy, yy;
|
cannam@154
|
49 VARDECL(celt_norm, y);
|
cannam@154
|
50 VARDECL(celt_norm, X);
|
cannam@154
|
51 VARDECL(float, signy);
|
cannam@154
|
52 __m128 signmask;
|
cannam@154
|
53 __m128 sums;
|
cannam@154
|
54 __m128i fours;
|
cannam@154
|
55 SAVE_STACK;
|
cannam@154
|
56
|
cannam@154
|
57 (void)arch;
|
cannam@154
|
58 /* All bits set to zero, except for the sign bit. */
|
cannam@154
|
59 signmask = _mm_set_ps1(-0.f);
|
cannam@154
|
60 fours = _mm_set_epi32(4, 4, 4, 4);
|
cannam@154
|
61 ALLOC(y, N+3, celt_norm);
|
cannam@154
|
62 ALLOC(X, N+3, celt_norm);
|
cannam@154
|
63 ALLOC(signy, N+3, float);
|
cannam@154
|
64
|
cannam@154
|
65 OPUS_COPY(X, _X, N);
|
cannam@154
|
66 X[N] = X[N+1] = X[N+2] = 0;
|
cannam@154
|
67 sums = _mm_setzero_ps();
|
cannam@154
|
68 for (j=0;j<N;j+=4)
|
cannam@154
|
69 {
|
cannam@154
|
70 __m128 x4, s4;
|
cannam@154
|
71 x4 = _mm_loadu_ps(&X[j]);
|
cannam@154
|
72 s4 = _mm_cmplt_ps(x4, _mm_setzero_ps());
|
cannam@154
|
73 /* Get rid of the sign */
|
cannam@154
|
74 x4 = _mm_andnot_ps(signmask, x4);
|
cannam@154
|
75 sums = _mm_add_ps(sums, x4);
|
cannam@154
|
76 /* Clear y and iy in case we don't do the projection. */
|
cannam@154
|
77 _mm_storeu_ps(&y[j], _mm_setzero_ps());
|
cannam@154
|
78 _mm_storeu_si128((__m128i*)&iy[j], _mm_setzero_si128());
|
cannam@154
|
79 _mm_storeu_ps(&X[j], x4);
|
cannam@154
|
80 _mm_storeu_ps(&signy[j], s4);
|
cannam@154
|
81 }
|
cannam@154
|
82 sums = _mm_add_ps(sums, _mm_shuffle_ps(sums, sums, _MM_SHUFFLE(1, 0, 3, 2)));
|
cannam@154
|
83 sums = _mm_add_ps(sums, _mm_shuffle_ps(sums, sums, _MM_SHUFFLE(2, 3, 0, 1)));
|
cannam@154
|
84
|
cannam@154
|
85 xy = yy = 0;
|
cannam@154
|
86
|
cannam@154
|
87 pulsesLeft = K;
|
cannam@154
|
88
|
cannam@154
|
89 /* Do a pre-search by projecting on the pyramid */
|
cannam@154
|
90 if (K > (N>>1))
|
cannam@154
|
91 {
|
cannam@154
|
92 __m128i pulses_sum;
|
cannam@154
|
93 __m128 yy4, xy4;
|
cannam@154
|
94 __m128 rcp4;
|
cannam@154
|
95 opus_val32 sum = _mm_cvtss_f32(sums);
|
cannam@154
|
96 /* If X is too small, just replace it with a pulse at 0 */
|
cannam@154
|
97 /* Prevents infinities and NaNs from causing too many pulses
|
cannam@154
|
98 to be allocated. 64 is an approximation of infinity here. */
|
cannam@154
|
99 if (!(sum > EPSILON && sum < 64))
|
cannam@154
|
100 {
|
cannam@154
|
101 X[0] = QCONST16(1.f,14);
|
cannam@154
|
102 j=1; do
|
cannam@154
|
103 X[j]=0;
|
cannam@154
|
104 while (++j<N);
|
cannam@154
|
105 sums = _mm_set_ps1(1.f);
|
cannam@154
|
106 }
|
cannam@154
|
107 /* Using K+e with e < 1 guarantees we cannot get more than K pulses. */
|
cannam@154
|
108 rcp4 = _mm_mul_ps(_mm_set_ps1((float)(K+.8)), _mm_rcp_ps(sums));
|
cannam@154
|
109 xy4 = yy4 = _mm_setzero_ps();
|
cannam@154
|
110 pulses_sum = _mm_setzero_si128();
|
cannam@154
|
111 for (j=0;j<N;j+=4)
|
cannam@154
|
112 {
|
cannam@154
|
113 __m128 rx4, x4, y4;
|
cannam@154
|
114 __m128i iy4;
|
cannam@154
|
115 x4 = _mm_loadu_ps(&X[j]);
|
cannam@154
|
116 rx4 = _mm_mul_ps(x4, rcp4);
|
cannam@154
|
117 iy4 = _mm_cvttps_epi32(rx4);
|
cannam@154
|
118 pulses_sum = _mm_add_epi32(pulses_sum, iy4);
|
cannam@154
|
119 _mm_storeu_si128((__m128i*)&iy[j], iy4);
|
cannam@154
|
120 y4 = _mm_cvtepi32_ps(iy4);
|
cannam@154
|
121 xy4 = _mm_add_ps(xy4, _mm_mul_ps(x4, y4));
|
cannam@154
|
122 yy4 = _mm_add_ps(yy4, _mm_mul_ps(y4, y4));
|
cannam@154
|
123 /* double the y[] vector so we don't have to do it in the search loop. */
|
cannam@154
|
124 _mm_storeu_ps(&y[j], _mm_add_ps(y4, y4));
|
cannam@154
|
125 }
|
cannam@154
|
126 pulses_sum = _mm_add_epi32(pulses_sum, _mm_shuffle_epi32(pulses_sum, _MM_SHUFFLE(1, 0, 3, 2)));
|
cannam@154
|
127 pulses_sum = _mm_add_epi32(pulses_sum, _mm_shuffle_epi32(pulses_sum, _MM_SHUFFLE(2, 3, 0, 1)));
|
cannam@154
|
128 pulsesLeft -= _mm_cvtsi128_si32(pulses_sum);
|
cannam@154
|
129 xy4 = _mm_add_ps(xy4, _mm_shuffle_ps(xy4, xy4, _MM_SHUFFLE(1, 0, 3, 2)));
|
cannam@154
|
130 xy4 = _mm_add_ps(xy4, _mm_shuffle_ps(xy4, xy4, _MM_SHUFFLE(2, 3, 0, 1)));
|
cannam@154
|
131 xy = _mm_cvtss_f32(xy4);
|
cannam@154
|
132 yy4 = _mm_add_ps(yy4, _mm_shuffle_ps(yy4, yy4, _MM_SHUFFLE(1, 0, 3, 2)));
|
cannam@154
|
133 yy4 = _mm_add_ps(yy4, _mm_shuffle_ps(yy4, yy4, _MM_SHUFFLE(2, 3, 0, 1)));
|
cannam@154
|
134 yy = _mm_cvtss_f32(yy4);
|
cannam@154
|
135 }
|
cannam@154
|
136 X[N] = X[N+1] = X[N+2] = -100;
|
cannam@154
|
137 y[N] = y[N+1] = y[N+2] = 100;
|
cannam@154
|
138 celt_sig_assert(pulsesLeft>=0);
|
cannam@154
|
139
|
cannam@154
|
140 /* This should never happen, but just in case it does (e.g. on silence)
|
cannam@154
|
141 we fill the first bin with pulses. */
|
cannam@154
|
142 if (pulsesLeft > N+3)
|
cannam@154
|
143 {
|
cannam@154
|
144 opus_val16 tmp = (opus_val16)pulsesLeft;
|
cannam@154
|
145 yy = MAC16_16(yy, tmp, tmp);
|
cannam@154
|
146 yy = MAC16_16(yy, tmp, y[0]);
|
cannam@154
|
147 iy[0] += pulsesLeft;
|
cannam@154
|
148 pulsesLeft=0;
|
cannam@154
|
149 }
|
cannam@154
|
150
|
cannam@154
|
151 for (i=0;i<pulsesLeft;i++)
|
cannam@154
|
152 {
|
cannam@154
|
153 int best_id;
|
cannam@154
|
154 __m128 xy4, yy4;
|
cannam@154
|
155 __m128 max, max2;
|
cannam@154
|
156 __m128i count;
|
cannam@154
|
157 __m128i pos;
|
cannam@154
|
158 /* The squared magnitude term gets added anyway, so we might as well
|
cannam@154
|
159 add it outside the loop */
|
cannam@154
|
160 yy = ADD16(yy, 1);
|
cannam@154
|
161 xy4 = _mm_load1_ps(&xy);
|
cannam@154
|
162 yy4 = _mm_load1_ps(&yy);
|
cannam@154
|
163 max = _mm_setzero_ps();
|
cannam@154
|
164 pos = _mm_setzero_si128();
|
cannam@154
|
165 count = _mm_set_epi32(3, 2, 1, 0);
|
cannam@154
|
166 for (j=0;j<N;j+=4)
|
cannam@154
|
167 {
|
cannam@154
|
168 __m128 x4, y4, r4;
|
cannam@154
|
169 x4 = _mm_loadu_ps(&X[j]);
|
cannam@154
|
170 y4 = _mm_loadu_ps(&y[j]);
|
cannam@154
|
171 x4 = _mm_add_ps(x4, xy4);
|
cannam@154
|
172 y4 = _mm_add_ps(y4, yy4);
|
cannam@154
|
173 y4 = _mm_rsqrt_ps(y4);
|
cannam@154
|
174 r4 = _mm_mul_ps(x4, y4);
|
cannam@154
|
175 /* Update the index of the max. */
|
cannam@154
|
176 pos = _mm_max_epi16(pos, _mm_and_si128(count, _mm_castps_si128(_mm_cmpgt_ps(r4, max))));
|
cannam@154
|
177 /* Update the max. */
|
cannam@154
|
178 max = _mm_max_ps(max, r4);
|
cannam@154
|
179 /* Update the indices (+4) */
|
cannam@154
|
180 count = _mm_add_epi32(count, fours);
|
cannam@154
|
181 }
|
cannam@154
|
182 /* Horizontal max */
|
cannam@154
|
183 max2 = _mm_max_ps(max, _mm_shuffle_ps(max, max, _MM_SHUFFLE(1, 0, 3, 2)));
|
cannam@154
|
184 max2 = _mm_max_ps(max2, _mm_shuffle_ps(max2, max2, _MM_SHUFFLE(2, 3, 0, 1)));
|
cannam@154
|
185 /* Now that max2 contains the max at all positions, look at which value(s) of the
|
cannam@154
|
186 partial max is equal to the global max. */
|
cannam@154
|
187 pos = _mm_and_si128(pos, _mm_castps_si128(_mm_cmpeq_ps(max, max2)));
|
cannam@154
|
188 pos = _mm_max_epi16(pos, _mm_unpackhi_epi64(pos, pos));
|
cannam@154
|
189 pos = _mm_max_epi16(pos, _mm_shufflelo_epi16(pos, _MM_SHUFFLE(1, 0, 3, 2)));
|
cannam@154
|
190 best_id = _mm_cvtsi128_si32(pos);
|
cannam@154
|
191
|
cannam@154
|
192 /* Updating the sums of the new pulse(s) */
|
cannam@154
|
193 xy = ADD32(xy, EXTEND32(X[best_id]));
|
cannam@154
|
194 /* We're multiplying y[j] by two so we don't have to do it here */
|
cannam@154
|
195 yy = ADD16(yy, y[best_id]);
|
cannam@154
|
196
|
cannam@154
|
197 /* Only now that we've made the final choice, update y/iy */
|
cannam@154
|
198 /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
|
cannam@154
|
199 y[best_id] += 2;
|
cannam@154
|
200 iy[best_id]++;
|
cannam@154
|
201 }
|
cannam@154
|
202
|
cannam@154
|
203 /* Put the original sign back */
|
cannam@154
|
204 for (j=0;j<N;j+=4)
|
cannam@154
|
205 {
|
cannam@154
|
206 __m128i y4;
|
cannam@154
|
207 __m128i s4;
|
cannam@154
|
208 y4 = _mm_loadu_si128((__m128i*)&iy[j]);
|
cannam@154
|
209 s4 = _mm_castps_si128(_mm_loadu_ps(&signy[j]));
|
cannam@154
|
210 y4 = _mm_xor_si128(_mm_add_epi32(y4, s4), s4);
|
cannam@154
|
211 _mm_storeu_si128((__m128i*)&iy[j], y4);
|
cannam@154
|
212 }
|
cannam@154
|
213 RESTORE_STACK;
|
cannam@154
|
214 return yy;
|
cannam@154
|
215 }
|
cannam@154
|
216
|
cannam@154
|
217 #endif
|