Debian Codegraph @

libmp3lame/vector/xmm_quantize_sub.c

raw
1 /*
2 * MP3 quantization, intrinsics functions
3 *
4 * Copyright (c) 2005-2006 Gabriel Bouvigne
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include "lame.h"
28 #include "machine.h"
29 #include "encoder.h"
30 #include "util.h"
31 #include "lame_intrin.h"
32
33
34
35 #ifdef HAVE_XMMINTRIN_H
36
37 #include <xmmintrin.h>
38
39 typedef union {
40 int32_t _i_32[4]; /* unions are initialized by its first member */
41 float _float[4];
42 __m128 _m128;
43 } vecfloat_union;
44
45 #define TRI_SIZE (5-1) /* 1024 = 4**5 */
46 static const FLOAT costab[TRI_SIZE * 2] = {
47 9.238795325112867e-01, 3.826834323650898e-01,
48 9.951847266721969e-01, 9.801714032956060e-02,
49 9.996988186962042e-01, 2.454122852291229e-02,
50 9.999811752826011e-01, 6.135884649154475e-03
51 };
52
53
54 /* make sure functions with SSE instructions maintain their own properly aligned stack */
55 #if defined (__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)))
56 #define REALIGN __attribute__((force_align_arg_pointer))
57 #define TARGET(x) __attribute__((target(x)))
58 #else
59 #define REALIGN
60 #define TARGET(x)
61 #endif
62
63 #define SSE_FUNCTION REALIGN TARGET("sse2")
64
65
66 SSE_FUNCTION void
67 init_xrpow_core_sse(gr_info * const cod_info, FLOAT xrpow[576], int max_nz, FLOAT * sum)
68 {
69 int i;
70 float tmp_max = 0;
71 float tmp_sum = 0;
72 int upper = max_nz + 1;
73 int upper4 = (upper / 4) * 4;
74 int rest = upper-upper4;
75
76 const vecfloat_union fabs_mask = {{ 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF }};
77 const __m128 vec_fabs_mask = _mm_loadu_ps(&fabs_mask._float[0]);
78 vecfloat_union vec_xrpow_max;
79 vecfloat_union vec_sum;
80 vecfloat_union vec_tmp;
81
82 _mm_prefetch((char const *) cod_info->xr, _MM_HINT_T0);
83 _mm_prefetch((char const *) xrpow, _MM_HINT_T0);
84
85 vec_xrpow_max._m128 = _mm_set_ps1(0);
86 vec_sum._m128 = _mm_set_ps1(0);
87
88 for (i = 0; i < upper4; i += 4) {
89 vec_tmp._m128 = _mm_loadu_ps(&(cod_info->xr[i])); /* load */
90 vec_tmp._m128 = _mm_and_ps(vec_tmp._m128, vec_fabs_mask); /* fabs */
91 vec_sum._m128 = _mm_add_ps(vec_sum._m128, vec_tmp._m128);
92 vec_tmp._m128 = _mm_sqrt_ps(_mm_mul_ps(vec_tmp._m128, _mm_sqrt_ps(vec_tmp._m128)));
93 vec_xrpow_max._m128 = _mm_max_ps(vec_xrpow_max._m128, vec_tmp._m128); /* retrieve max */
94 _mm_storeu_ps(&(xrpow[i]), vec_tmp._m128); /* store into xrpow[] */
95 }
96 vec_tmp._m128 = _mm_set_ps1(0);
97 switch (rest) {
98 case 3: vec_tmp._float[2] = cod_info->xr[upper4+2];
99 case 2: vec_tmp._float[1] = cod_info->xr[upper4+1];
100 case 1: vec_tmp._float[0] = cod_info->xr[upper4+0];
101 vec_tmp._m128 = _mm_and_ps(vec_tmp._m128, vec_fabs_mask); /* fabs */
102 vec_sum._m128 = _mm_add_ps(vec_sum._m128, vec_tmp._m128);
103 vec_tmp._m128 = _mm_sqrt_ps(_mm_mul_ps(vec_tmp._m128, _mm_sqrt_ps(vec_tmp._m128)));
104 vec_xrpow_max._m128 = _mm_max_ps(vec_xrpow_max._m128, vec_tmp._m128); /* retrieve max */
105 switch (rest) {
106 case 3: xrpow[upper4+2] = vec_tmp._float[2];
107 case 2: xrpow[upper4+1] = vec_tmp._float[1];
108 case 1: xrpow[upper4+0] = vec_tmp._float[0];
109 default:
110 break;
111 }
112 default:
113 break;
114 }
115 tmp_sum = vec_sum._float[0] + vec_sum._float[1] + vec_sum._float[2] + vec_sum._float[3];
116 {
117 float ma = vec_xrpow_max._float[0] > vec_xrpow_max._float[1]
118 ? vec_xrpow_max._float[0] : vec_xrpow_max._float[1];
119 float mb = vec_xrpow_max._float[2] > vec_xrpow_max._float[3]
120 ? vec_xrpow_max._float[2] : vec_xrpow_max._float[3];
121 tmp_max = ma > mb ? ma : mb;
122 }
123 cod_info->xrpow_max = tmp_max;
124 *sum = tmp_sum;
125 }
126
127
128 SSE_FUNCTION static void
129 store4(__m128 v, float* f0, float* f1, float* f2, float* f3)
130 {
131 vecfloat_union r;
132 r._m128 = v;
133 *f0 = r._float[0];
134 *f1 = r._float[1];
135 *f2 = r._float[2];
136 *f3 = r._float[3];
137 }
138
139
140 SSE_FUNCTION void
141 fht_SSE2(FLOAT * fz, int n)
142 {
143 const FLOAT *tri = costab;
144 int k4;
145 FLOAT *fi, *gi;
146 FLOAT const *fn;
147
148 n <<= 1; /* to get BLKSIZE, because of 3DNow! ASM routine */
149 fn = fz + n;
150 k4 = 4;
151 do {
152 FLOAT s1, c1;
153 int i, k1, k2, k3, kx;
154 kx = k4 >> 1;
155 k1 = k4;
156 k2 = k4 << 1;
157 k3 = k2 + k1;
158 k4 = k2 << 1;
159 fi = fz;
160 gi = fi + kx;
161 do {
162 FLOAT f0, f1, f2, f3;
163 f1 = fi[0] - fi[k1];
164 f0 = fi[0] + fi[k1];
165 f3 = fi[k2] - fi[k3];
166 f2 = fi[k2] + fi[k3];
167 fi[k2] = f0 - f2;
168 fi[0] = f0 + f2;
169 fi[k3] = f1 - f3;
170 fi[k1] = f1 + f3;
171 f1 = gi[0] - gi[k1];
172 f0 = gi[0] + gi[k1];
173 f3 = SQRT2 * gi[k3];
174 f2 = SQRT2 * gi[k2];
175 gi[k2] = f0 - f2;
176 gi[0] = f0 + f2;
177 gi[k3] = f1 - f3;
178 gi[k1] = f1 + f3;
179 gi += k4;
180 fi += k4;
181 } while (fi < fn);
182 c1 = tri[0];
183 s1 = tri[1];
184 for (i = 1; i < kx; i++) {
185 __m128 v_s2;
186 __m128 v_c2;
187 __m128 v_c1;
188 __m128 v_s1;
189 FLOAT c2, s2, s1_2 = s1+s1;
190 c2 = 1 - s1_2 * s1;
191 s2 = s1_2 * c1;
192 fi = fz + i;
193 gi = fz + k1 - i;
194 v_c1 = _mm_set_ps1(c1);
195 v_s1 = _mm_set_ps1(s1);
196 v_c2 = _mm_set_ps1(c2);
197 v_s2 = _mm_set_ps1(s2);
198 {
199 static const vecfloat_union sign_mask = {{0x80000000,0,0,0}};
200 v_c1 = _mm_xor_ps(sign_mask._m128, v_c1); /* v_c1 := {-c1, +c1, +c1, +c1} */
201 }
202 {
203 static const vecfloat_union sign_mask = {{0,0x80000000,0,0}};
204 v_s1 = _mm_xor_ps(sign_mask._m128, v_s1); /* v_s1 := {+s1, -s1, +s1, +s1} */
205 }
206 {
207 static const vecfloat_union sign_mask = {{0,0,0x80000000,0x80000000}};
208 v_c2 = _mm_xor_ps(sign_mask._m128, v_c2); /* v_c2 := {+c2, +c2, -c2, -c2} */
209 }
210 do {
211 __m128 p, q, r;
212
213 q = _mm_setr_ps(fi[k1], fi[k3], gi[k1], gi[k3]); /* Q := {fi_k1,fi_k3,gi_k1,gi_k3}*/
214 p = _mm_mul_ps(v_s2, q); /* P := s2 * Q */
215 q = _mm_mul_ps(v_c2, q); /* Q := c2 * Q */
216 q = _mm_shuffle_ps(q, q, _MM_SHUFFLE(1,0,3,2)); /* Q := {-c2*gi_k1,-c2*gi_k3,c2*fi_k1,c2*fi_k3} */
217 p = _mm_add_ps(p, q);
218
219 r = _mm_setr_ps(gi[0], gi[k2], fi[0], fi[k2]); /* R := {gi_0,gi_k2,fi_0,fi_k2} */
220 q = _mm_sub_ps(r, p); /* Q := {gi_0-p0,gi_k2-p1,fi_0-p2,fi_k2-p3} */
221 r = _mm_add_ps(r, p); /* R := {gi_0+p0,gi_k2+p1,fi_0+p2,fi_k2+p3} */
222 p = _mm_shuffle_ps(q, r, _MM_SHUFFLE(2,0,2,0)); /* P := {q0,q2,r0,r2} */
223 p = _mm_shuffle_ps(p, p, _MM_SHUFFLE(3,1,2,0)); /* P := {q0,r0,q2,r2} */
224 q = _mm_shuffle_ps(q, r, _MM_SHUFFLE(3,1,3,1)); /* Q := {q1,q3,r1,r3} */
225 r = _mm_mul_ps(v_c1, q);
226 q = _mm_mul_ps(v_s1, q);
227 q = _mm_shuffle_ps(q, q, _MM_SHUFFLE(0,1,2,3)); /* Q := {q3,q2,q1,q0} */
228 q = _mm_add_ps(q, r);
229
230 store4(_mm_sub_ps(p, q), &gi[k3], &gi[k2], &fi[k3], &fi[k2]);
231 store4(_mm_add_ps(p, q), &gi[k1], &gi[ 0], &fi[k1], &fi[ 0]);
232
233 gi += k4;
234 fi += k4;
235 } while (fi < fn);
236 c2 = c1;
237 c1 = c2 * tri[0] - s1 * tri[1];
238 s1 = c2 * tri[1] + s1 * tri[0];
239 }
240 tri += 2;
241 } while (k4 < n);
242 }
243
244 #endif /* HAVE_XMMINTRIN_H */
245