]> code.delx.au - pulseaudio/blob - src/pulsecore/svolume_sse.c
svolume: fix compilation in 32bits
[pulseaudio] / src / pulsecore / svolume_sse.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2009 Wim Taymans <wim.taymans@collabora.co.uk>
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <pulse/timeval.h>
28 #include <pulsecore/random.h>
29 #include <pulsecore/macro.h>
30 #include <pulsecore/g711.h>
31 #include <pulsecore/core-util.h>
32
33 #include "cpu-x86.h"
34
35 #include "sample-util.h"
36 #include "endianmacros.h"
37
38 #if defined (__i386__) || defined (__amd64__)
39
40 #define VOLUME_32x16(s,v) /* .. | vh | vl | */ \
41 " pxor %%xmm4, %%xmm4 \n\t" /* .. | 0 | 0 | */ \
42 " punpcklwd %%xmm4, "#s" \n\t" /* .. | 0 | p0 | */ \
43 " pcmpgtw "#s", %%xmm4 \n\t" /* .. | 0 | s(p0) | */ \
44 " pand "#v", %%xmm4 \n\t" /* .. | 0 | (vl) | */ \
45 " movdqa "#s", %%xmm5 \n\t" \
46 " pmulhuw "#v", "#s" \n\t" /* .. | 0 | vl*p0 | */ \
47 " psubd %%xmm4, "#s" \n\t" /* .. | 0 | vl*p0 | + sign correct */ \
48 " psrld $16, "#v" \n\t" /* .. | p0 | 0 | */ \
49 " pmaddwd %%xmm5, "#v" \n\t" /* .. | p0 * vh | */ \
50 " paddd "#s", "#v" \n\t" /* .. | p0 * v0 | */ \
51 " packssdw "#v", "#v" \n\t" /* .. | p1*v1 | p0*v0 | */
52
53 #define MOD_ADD(a,b) \
54 " add "#a", %3 \n\t" /* channel += inc */ \
55 " mov %3, %4 \n\t" \
56 " sub "#b", %4 \n\t" /* tmp = channel - channels */ \
57 " cmovae %4, %3 \n\t" /* if (tmp >= 0) channel = tmp */
58
59 /* swap 16 bits */
60 #define SWAP_16(s) \
61 " movdqa "#s", %%xmm4 \n\t" /* .. | h l | */ \
62 " psrlw $8, %%xmm4 \n\t" /* .. | 0 h | */ \
63 " psllw $8, "#s" \n\t" /* .. | l 0 | */ \
64 " por %%xmm4, "#s" \n\t" /* .. | l h | */
65
66 /* swap 2 registers 16 bits for better pairing */
67 #define SWAP_16_2(s1,s2) \
68 " movdqa "#s1", %%xmm4 \n\t" /* .. | h l | */ \
69 " movdqa "#s2", %%xmm5 \n\t" \
70 " psrlw $8, %%xmm4 \n\t" /* .. | 0 h | */ \
71 " psrlw $8, %%xmm5 \n\t" \
72 " psllw $8, "#s1" \n\t" /* .. | l 0 | */ \
73 " psllw $8, "#s2" \n\t" \
74 " por %%xmm4, "#s1" \n\t" /* .. | l h | */ \
75 " por %%xmm5, "#s2" \n\t"
76
77 static void
78 pa_volume_s16ne_sse (int16_t *samples, int32_t *volumes, unsigned channels, unsigned length)
79 {
80 pa_reg_x86 channel, temp;
81
82 /* the max number of samples we process at a time, this is also the max amount
83 * we overread the volume array, which should have enough padding. */
84 channels = MAX (8, channels);
85
86 __asm__ __volatile__ (
87 " xor %3, %3 \n\t"
88 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
89
90 " test $1, %2 \n\t" /* check for odd samples */
91 " je 2f \n\t"
92
93 " movd (%1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
94 " movw (%0), %w4 \n\t" /* .. | p0 | */
95 " movd %4, %%xmm1 \n\t"
96 VOLUME_32x16 (%%xmm1, %%xmm0)
97 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
98 " movw %w4, (%0) \n\t"
99 " add $2, %0 \n\t"
100 MOD_ADD ($1, %5)
101
102 "2: \n\t"
103 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
104 " test $1, %2 \n\t"
105 " je 4f \n\t"
106
107 "3: \n\t" /* do samples in groups of 2 */
108 " movq (%1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
109 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
110 VOLUME_32x16 (%%xmm1, %%xmm0)
111 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
112 " add $4, %0 \n\t"
113 MOD_ADD ($2, %5)
114
115 "4: \n\t"
116 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
117 " test $1, %2 \n\t"
118 " je 6f \n\t"
119
120 "5: \n\t" /* do samples in groups of 4 */
121 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
122 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
123 VOLUME_32x16 (%%xmm1, %%xmm0)
124 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
125 " add $8, %0 \n\t"
126 MOD_ADD ($4, %5)
127
128 "6: \n\t"
129 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
130 " cmp $0, %2 \n\t"
131 " je 8f \n\t"
132
133 "7: \n\t" /* do samples in groups of 8 */
134 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
135 " movdqu 16(%1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
136 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
137 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
138 VOLUME_32x16 (%%xmm1, %%xmm0)
139 VOLUME_32x16 (%%xmm3, %%xmm2)
140 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
141 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
142 " add $16, %0 \n\t"
143 MOD_ADD ($8, %5)
144 " dec %2 \n\t"
145 " jne 7b \n\t"
146 "8: \n\t"
147
148 : "+r" (samples), "+r" (volumes), "+r" (length), "=D" (channel), "=&r" (temp)
149 : "r" ((pa_reg_x86)channels)
150 : "cc"
151 );
152 }
153
154 static void
155 pa_volume_s16re_sse (int16_t *samples, int32_t *volumes, unsigned channels, unsigned length)
156 {
157 pa_reg_x86 channel, temp;
158
159 /* the max number of samples we process at a time, this is also the max amount
160 * we overread the volume array, which should have enough padding. */
161 channels = MAX (8, channels);
162
163 __asm__ __volatile__ (
164 " xor %3, %3 \n\t"
165 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
166
167 " test $1, %2 \n\t" /* check for odd samples */
168 " je 2f \n\t"
169
170 " movd (%1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
171 " movw (%0), %w4 \n\t" /* .. | p0 | */
172 " rorw $8, %w4 \n\t"
173 " movd %4, %%xmm1 \n\t"
174 VOLUME_32x16 (%%xmm1, %%xmm0)
175 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
176 " rorw $8, %w4 \n\t"
177 " movw %w4, (%0) \n\t"
178 " add $2, %0 \n\t"
179 MOD_ADD ($1, %5)
180
181 "2: \n\t"
182 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
183 " test $1, %2 \n\t"
184 " je 4f \n\t"
185
186 "3: \n\t" /* do samples in groups of 2 */
187 " movq (%1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
188 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
189 SWAP_16 (%%xmm1)
190 VOLUME_32x16 (%%xmm1, %%xmm0)
191 SWAP_16 (%%xmm0)
192 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
193 " add $4, %0 \n\t"
194 MOD_ADD ($2, %5)
195
196 "4: \n\t"
197 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
198 " test $1, %2 \n\t"
199 " je 6f \n\t"
200
201 /* FIXME, we can do aligned access of the volume values if we can guarantee
202 * that the array is 16 bytes aligned, we probably have to do the odd values
203 * after this then. */
204 "5: \n\t" /* do samples in groups of 4 */
205 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
206 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
207 SWAP_16 (%%xmm1)
208 VOLUME_32x16 (%%xmm1, %%xmm0)
209 SWAP_16 (%%xmm0)
210 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
211 " add $8, %0 \n\t"
212 MOD_ADD ($4, %5)
213
214 "6: \n\t"
215 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
216 " cmp $0, %2 \n\t"
217 " je 8f \n\t"
218
219 "7: \n\t" /* do samples in groups of 8 */
220 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
221 " movdqu 16(%1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
222 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
223 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
224 SWAP_16_2 (%%xmm1, %%xmm3)
225 VOLUME_32x16 (%%xmm1, %%xmm0)
226 VOLUME_32x16 (%%xmm3, %%xmm2)
227 SWAP_16_2 (%%xmm0, %%xmm2)
228 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
229 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
230 " add $16, %0 \n\t"
231 MOD_ADD ($8, %5)
232 " dec %2 \n\t"
233 " jne 7b \n\t"
234 "8: \n\t"
235
236 : "+r" (samples), "+r" (volumes), "+r" (length), "=D" (channel), "=&r" (temp)
237 : "r" ((pa_reg_x86)channels)
238 : "cc"
239 );
240 }
241
242 #undef RUN_TEST
243
244 #ifdef RUN_TEST
245 #define CHANNELS 2
246 #define SAMPLES 1021
247 #define TIMES 1000
248 #define PADDING 16
249
250 static void run_test (void) {
251 int16_t samples[SAMPLES];
252 int16_t samples_ref[SAMPLES];
253 int16_t samples_orig[SAMPLES];
254 int32_t volumes[CHANNELS + PADDING];
255 int i, j, padding;
256 pa_do_volume_func_t func;
257 struct timeval start, stop;
258
259 func = pa_get_volume_func (PA_SAMPLE_S16NE);
260
261 printf ("checking SSE %zd\n", sizeof (samples));
262
263 pa_random (samples, sizeof (samples));
264 memcpy (samples_ref, samples, sizeof (samples));
265 memcpy (samples_orig, samples, sizeof (samples));
266
267 for (i = 0; i < CHANNELS; i++)
268 volumes[i] = rand() >> 1;
269 for (padding = 0; padding < PADDING; padding++, i++)
270 volumes[i] = volumes[padding];
271
272 func (samples_ref, volumes, CHANNELS, sizeof (samples));
273 pa_volume_s16ne_sse (samples, volumes, CHANNELS, sizeof (samples));
274 for (i = 0; i < SAMPLES; i++) {
275 if (samples[i] != samples_ref[i]) {
276 printf ("%d: %04x != %04x (%04x * %04x)\n", i, samples[i], samples_ref[i],
277 samples_orig[i], volumes[i % CHANNELS]);
278 }
279 }
280
281 pa_gettimeofday(&start);
282 for (j = 0; j < TIMES; j++) {
283 memcpy (samples, samples_orig, sizeof (samples));
284 pa_volume_s16ne_sse (samples, volumes, CHANNELS, sizeof (samples));
285 }
286 pa_gettimeofday(&stop);
287 pa_log_info("SSE: %llu usec.", (long long unsigned int)pa_timeval_diff (&stop, &start));
288
289 pa_gettimeofday(&start);
290 for (j = 0; j < TIMES; j++) {
291 memcpy (samples_ref, samples_orig, sizeof (samples));
292 func (samples_ref, volumes, CHANNELS, sizeof (samples));
293 }
294 pa_gettimeofday(&stop);
295 pa_log_info("ref: %llu usec.", (long long unsigned int)pa_timeval_diff (&stop, &start));
296 }
297 #endif
298 #endif /* defined (__i386__) || defined (__amd64__) */
299
300 void pa_volume_func_init_sse (pa_cpu_x86_flag_t flags) {
301 #if defined (__i386__) || defined (__amd64__)
302 pa_log_info("Initialising SSE optimized functions.");
303
304 #ifdef RUN_TEST
305 run_test ();
306 #endif
307
308 pa_set_volume_func (PA_SAMPLE_S16NE, (pa_do_volume_func_t) pa_volume_s16ne_sse);
309 pa_set_volume_func (PA_SAMPLE_S16RE, (pa_do_volume_func_t) pa_volume_s16re_sse);
310 #endif /* defined (__i386__) || defined (__amd64__) */
311 }