annotate src/opus-1.3/celt/x86/vq_sse2.c @ 77:4edcd14160a5 pa_catalina

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