]> code.delx.au - pulseaudio/blob - src/pulsecore/svolume_sse.c
simd: be more precise which SIMD optimizations we activate
[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_sse2 (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 = PA_MAX (8U, 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 /* FIXME, we can do aligned access of the volume values if we can guarantee
121 * that the array is 16 bytes aligned, we probably have to do the odd values
122 * after this then. */
123 "5: \n\t" /* do samples in groups of 4 */
124 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
125 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
126 VOLUME_32x16 (%%xmm1, %%xmm0)
127 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
128 " add $8, %0 \n\t"
129 MOD_ADD ($4, %5)
130
131 "6: \n\t"
132 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
133 " cmp $0, %2 \n\t"
134 " je 8f \n\t"
135
136 "7: \n\t" /* do samples in groups of 8 */
137 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
138 " movdqu 16(%1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
139 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
140 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
141 VOLUME_32x16 (%%xmm1, %%xmm0)
142 VOLUME_32x16 (%%xmm3, %%xmm2)
143 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
144 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
145 " add $16, %0 \n\t"
146 MOD_ADD ($8, %5)
147 " dec %2 \n\t"
148 " jne 7b \n\t"
149 "8: \n\t"
150
151 : "+r" (samples), "+r" (volumes), "+r" (length), "=D" (channel), "=&r" (temp)
152 : "r" ((pa_reg_x86)channels)
153 : "cc"
154 );
155 }
156
157 static void
158 pa_volume_s16re_sse2 (int16_t *samples, int32_t *volumes, unsigned channels, unsigned length)
159 {
160 pa_reg_x86 channel, temp;
161
162 /* the max number of samples we process at a time, this is also the max amount
163 * we overread the volume array, which should have enough padding. */
164 channels = PA_MAX (8U, channels);
165
166 __asm__ __volatile__ (
167 " xor %3, %3 \n\t"
168 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
169
170 " test $1, %2 \n\t" /* check for odd samples */
171 " je 2f \n\t"
172
173 " movd (%1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
174 " movw (%0), %w4 \n\t" /* .. | p0 | */
175 " rorw $8, %w4 \n\t"
176 " movd %4, %%xmm1 \n\t"
177 VOLUME_32x16 (%%xmm1, %%xmm0)
178 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
179 " rorw $8, %w4 \n\t"
180 " movw %w4, (%0) \n\t"
181 " add $2, %0 \n\t"
182 MOD_ADD ($1, %5)
183
184 "2: \n\t"
185 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
186 " test $1, %2 \n\t"
187 " je 4f \n\t"
188
189 "3: \n\t" /* do samples in groups of 2 */
190 " movq (%1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
191 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
192 SWAP_16 (%%xmm1)
193 VOLUME_32x16 (%%xmm1, %%xmm0)
194 SWAP_16 (%%xmm0)
195 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
196 " add $4, %0 \n\t"
197 MOD_ADD ($2, %5)
198
199 "4: \n\t"
200 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
201 " test $1, %2 \n\t"
202 " je 6f \n\t"
203
204 /* FIXME, we can do aligned access of the volume values if we can guarantee
205 * that the array is 16 bytes aligned, we probably have to do the odd values
206 * after this then. */
207 "5: \n\t" /* do samples in groups of 4 */
208 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
209 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
210 SWAP_16 (%%xmm1)
211 VOLUME_32x16 (%%xmm1, %%xmm0)
212 SWAP_16 (%%xmm0)
213 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
214 " add $8, %0 \n\t"
215 MOD_ADD ($4, %5)
216
217 "6: \n\t"
218 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
219 " cmp $0, %2 \n\t"
220 " je 8f \n\t"
221
222 "7: \n\t" /* do samples in groups of 8 */
223 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
224 " movdqu 16(%1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
225 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
226 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
227 SWAP_16_2 (%%xmm1, %%xmm3)
228 VOLUME_32x16 (%%xmm1, %%xmm0)
229 VOLUME_32x16 (%%xmm3, %%xmm2)
230 SWAP_16_2 (%%xmm0, %%xmm2)
231 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
232 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
233 " add $16, %0 \n\t"
234 MOD_ADD ($8, %5)
235 " dec %2 \n\t"
236 " jne 7b \n\t"
237 "8: \n\t"
238
239 : "+r" (samples), "+r" (volumes), "+r" (length), "=D" (channel), "=&r" (temp)
240 : "r" ((pa_reg_x86)channels)
241 : "cc"
242 );
243 }
244
245 #undef RUN_TEST
246
247 #ifdef RUN_TEST
248 #define CHANNELS 2
249 #define SAMPLES 1021
250 #define TIMES 1000
251 #define PADDING 16
252
253 static void run_test (void) {
254 int16_t samples[SAMPLES];
255 int16_t samples_ref[SAMPLES];
256 int16_t samples_orig[SAMPLES];
257 int32_t volumes[CHANNELS + PADDING];
258 int i, j, padding;
259 pa_do_volume_func_t func;
260 pa_usec_t start, stop;
261
262 func = pa_get_volume_func (PA_SAMPLE_S16NE);
263
264 printf ("checking SSE %zd\n", sizeof (samples));
265
266 pa_random (samples, sizeof (samples));
267 memcpy (samples_ref, samples, sizeof (samples));
268 memcpy (samples_orig, samples, sizeof (samples));
269
270 for (i = 0; i < CHANNELS; i++)
271 volumes[i] = rand() >> 1;
272 for (padding = 0; padding < PADDING; padding++, i++)
273 volumes[i] = volumes[padding];
274
275 func (samples_ref, volumes, CHANNELS, sizeof (samples));
276 pa_volume_s16ne_sse (samples, volumes, CHANNELS, sizeof (samples));
277 for (i = 0; i < SAMPLES; i++) {
278 if (samples[i] != samples_ref[i]) {
279 printf ("%d: %04x != %04x (%04x * %04x)\n", i, samples[i], samples_ref[i],
280 samples_orig[i], volumes[i % CHANNELS]);
281 }
282 }
283
284 start = pa_rtclock_now();
285 for (j = 0; j < TIMES; j++) {
286 memcpy (samples, samples_orig, sizeof (samples));
287 pa_volume_s16ne_sse (samples, volumes, CHANNELS, sizeof (samples));
288 }
289 stop = pa_rtclock_now();
290 pa_log_info("SSE: %llu usec.", (long long unsigned int)(stop - start));
291
292 start = pa_rtclock_now();
293 for (j = 0; j < TIMES; j++) {
294 memcpy (samples_ref, samples_orig, sizeof (samples));
295 func (samples_ref, volumes, CHANNELS, sizeof (samples));
296 }
297 stop = pa_rtclock_now();
298 pa_log_info("ref: %llu usec.", (long long unsigned int)(stop - start));
299 }
300 #endif
301 #endif /* defined (__i386__) || defined (__amd64__) */
302
303 void pa_volume_func_init_sse (pa_cpu_x86_flag_t flags) {
304 #if defined (__i386__) || defined (__amd64__)
305
306 #ifdef RUN_TEST
307 run_test ();
308 #endif
309
310 if (flags & PA_CPU_X86_SSE2) {
311 pa_log_info("Initialising SSE2 optimized functions.");
312
313 pa_set_volume_func (PA_SAMPLE_S16NE, (pa_do_volume_func_t) pa_volume_s16ne_sse2);
314 pa_set_volume_func (PA_SAMPLE_S16RE, (pa_do_volume_func_t) pa_volume_s16re_sse2);
315 }
316 #endif /* defined (__i386__) || defined (__amd64__) */
317 }