]> code.delx.au - pulseaudio/blob - src/pulsecore/svolume_sse.c
Fix up according to Coding Style
[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 <pulse/rtclock.h>
29
30 #include <pulsecore/random.h>
31 #include <pulsecore/macro.h>
32 #include <pulsecore/g711.h>
33 #include <pulsecore/core-util.h>
34
35 #include "cpu-x86.h"
36
37 #include "sample-util.h"
38 #include "endianmacros.h"
39
40 #if defined (__i386__) || defined (__amd64__)
41
42 #define VOLUME_32x16(s,v) /* .. | vh | vl | */ \
43 " pxor %%xmm4, %%xmm4 \n\t" /* .. | 0 | 0 | */ \
44 " punpcklwd %%xmm4, "#s" \n\t" /* .. | 0 | p0 | */ \
45 " pcmpgtw "#s", %%xmm4 \n\t" /* .. | 0 | s(p0) | */ \
46 " pand "#v", %%xmm4 \n\t" /* .. | 0 | (vl) | */ \
47 " movdqa "#s", %%xmm5 \n\t" \
48 " pmulhuw "#v", "#s" \n\t" /* .. | 0 | vl*p0 | */ \
49 " psubd %%xmm4, "#s" \n\t" /* .. | 0 | vl*p0 | + sign correct */ \
50 " psrld $16, "#v" \n\t" /* .. | 0 | vh | */ \
51 " pmaddwd %%xmm5, "#v" \n\t" /* .. | p0 * vh | */ \
52 " paddd "#s", "#v" \n\t" /* .. | p0 * v0 | */ \
53 " packssdw "#v", "#v" \n\t" /* .. | p1*v1 | p0*v0 | */
54
55 #define MOD_ADD(a,b) \
56 " add "#a", %3 \n\t" /* channel += inc */ \
57 " mov %3, %4 \n\t" \
58 " sub "#b", %4 \n\t" /* tmp = channel - channels */ \
59 " cmovae %4, %3 \n\t" /* if (tmp >= 0) channel = tmp */
60
61 /* swap 16 bits */
62 #define SWAP_16(s) \
63 " movdqa "#s", %%xmm4 \n\t" /* .. | h l | */ \
64 " psrlw $8, %%xmm4 \n\t" /* .. | 0 h | */ \
65 " psllw $8, "#s" \n\t" /* .. | l 0 | */ \
66 " por %%xmm4, "#s" \n\t" /* .. | l h | */
67
68 /* swap 2 registers 16 bits for better pairing */
69 #define SWAP_16_2(s1,s2) \
70 " movdqa "#s1", %%xmm4 \n\t" /* .. | h l | */ \
71 " movdqa "#s2", %%xmm5 \n\t" \
72 " psrlw $8, %%xmm4 \n\t" /* .. | 0 h | */ \
73 " psrlw $8, %%xmm5 \n\t" \
74 " psllw $8, "#s1" \n\t" /* .. | l 0 | */ \
75 " psllw $8, "#s2" \n\t" \
76 " por %%xmm4, "#s1" \n\t" /* .. | l h | */ \
77 " por %%xmm5, "#s2" \n\t"
78
79
80 static int channel_overread_table[8] = {8,8,8,12,8,10,12,14};
81
82 static void pa_volume_s16ne_sse2(int16_t *samples, int32_t *volumes, unsigned channels, unsigned length) {
83 pa_reg_x86 channel, temp;
84
85 /* Channels must be at least 8 and always a multiple of the original number.
86 * This is also the max amount we overread the volume array, which should
87 * have enough padding. */
88 if (channels < 8)
89 channels = channel_overread_table[channels];
90
91 __asm__ __volatile__ (
92 " xor %3, %3 \n\t"
93 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
94
95 " test $1, %2 \n\t" /* check for odd samples */
96 " je 2f \n\t"
97
98 " movd (%1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
99 " movw (%0), %w4 \n\t" /* .. | p0 | */
100 " movd %4, %%xmm1 \n\t"
101 VOLUME_32x16 (%%xmm1, %%xmm0)
102 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
103 " movw %w4, (%0) \n\t"
104 " add $2, %0 \n\t"
105 MOD_ADD ($1, %5)
106
107 "2: \n\t"
108 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
109 " test $1, %2 \n\t"
110 " je 4f \n\t"
111
112 "3: \n\t" /* do samples in groups of 2 */
113 " movq (%1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
114 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
115 VOLUME_32x16 (%%xmm1, %%xmm0)
116 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
117 " add $4, %0 \n\t"
118 MOD_ADD ($2, %5)
119
120 "4: \n\t"
121 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
122 " test $1, %2 \n\t"
123 " je 6f \n\t"
124
125 /* FIXME, we can do aligned access of the volume values if we can guarantee
126 * that the array is 16 bytes aligned, we probably have to do the odd values
127 * after this then. */
128 "5: \n\t" /* do samples in groups of 4 */
129 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
130 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
131 VOLUME_32x16 (%%xmm1, %%xmm0)
132 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
133 " add $8, %0 \n\t"
134 MOD_ADD ($4, %5)
135
136 "6: \n\t"
137 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
138 " cmp $0, %2 \n\t"
139 " je 8f \n\t"
140
141 "7: \n\t" /* do samples in groups of 8 */
142 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
143 " movdqu 16(%1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
144 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
145 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
146 VOLUME_32x16 (%%xmm1, %%xmm0)
147 VOLUME_32x16 (%%xmm3, %%xmm2)
148 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
149 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
150 " add $16, %0 \n\t"
151 MOD_ADD ($8, %5)
152 " dec %2 \n\t"
153 " jne 7b \n\t"
154 "8: \n\t"
155
156 : "+r" (samples), "+r" (volumes), "+r" (length), "=D" (channel), "=&r" (temp)
157 : "rm" ((pa_reg_x86)channels)
158 : "cc"
159 );
160 }
161
162 static void pa_volume_s16re_sse2(int16_t *samples, int32_t *volumes, unsigned channels, unsigned length) {
163 pa_reg_x86 channel, temp;
164
165 /* Channels must be at least 8 and always a multiple of the original number.
166 * This is also the max amount we overread the volume array, which should
167 * have enough padding. */
168 if (channels < 8)
169 channels = channel_overread_table[channels];
170
171 __asm__ __volatile__ (
172 " xor %3, %3 \n\t"
173 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
174
175 " test $1, %2 \n\t" /* check for odd samples */
176 " je 2f \n\t"
177
178 " movd (%1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
179 " movw (%0), %w4 \n\t" /* .. | p0 | */
180 " rorw $8, %w4 \n\t"
181 " movd %4, %%xmm1 \n\t"
182 VOLUME_32x16 (%%xmm1, %%xmm0)
183 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
184 " rorw $8, %w4 \n\t"
185 " movw %w4, (%0) \n\t"
186 " add $2, %0 \n\t"
187 MOD_ADD ($1, %5)
188
189 "2: \n\t"
190 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
191 " test $1, %2 \n\t"
192 " je 4f \n\t"
193
194 "3: \n\t" /* do samples in groups of 2 */
195 " movq (%1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
196 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
197 SWAP_16 (%%xmm1)
198 VOLUME_32x16 (%%xmm1, %%xmm0)
199 SWAP_16 (%%xmm0)
200 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
201 " add $4, %0 \n\t"
202 MOD_ADD ($2, %5)
203
204 "4: \n\t"
205 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
206 " test $1, %2 \n\t"
207 " je 6f \n\t"
208
209 /* FIXME, we can do aligned access of the volume values if we can guarantee
210 * that the array is 16 bytes aligned, we probably have to do the odd values
211 * after this then. */
212 "5: \n\t" /* do samples in groups of 4 */
213 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
214 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
215 SWAP_16 (%%xmm1)
216 VOLUME_32x16 (%%xmm1, %%xmm0)
217 SWAP_16 (%%xmm0)
218 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
219 " add $8, %0 \n\t"
220 MOD_ADD ($4, %5)
221
222 "6: \n\t"
223 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
224 " cmp $0, %2 \n\t"
225 " je 8f \n\t"
226
227 "7: \n\t" /* do samples in groups of 8 */
228 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
229 " movdqu 16(%1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
230 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
231 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
232 SWAP_16_2 (%%xmm1, %%xmm3)
233 VOLUME_32x16 (%%xmm1, %%xmm0)
234 VOLUME_32x16 (%%xmm3, %%xmm2)
235 SWAP_16_2 (%%xmm0, %%xmm2)
236 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
237 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
238 " add $16, %0 \n\t"
239 MOD_ADD ($8, %5)
240 " dec %2 \n\t"
241 " jne 7b \n\t"
242 "8: \n\t"
243
244 : "+r" (samples), "+r" (volumes), "+r" (length), "=D" (channel), "=&r" (temp)
245 : "rm" ((pa_reg_x86)channels)
246 : "cc"
247 );
248 }
249
250 #undef RUN_TEST
251
252 #ifdef RUN_TEST
253 #define CHANNELS 2
254 #define SAMPLES 1022
255 #define TIMES 1000
256 #define PADDING 16
257
258 static void run_test(void) {
259 int16_t samples[SAMPLES];
260 int16_t samples_ref[SAMPLES];
261 int16_t samples_orig[SAMPLES];
262 int32_t volumes[CHANNELS + PADDING];
263 int i, j, padding;
264 pa_do_volume_func_t func;
265 pa_usec_t start, stop;
266
267 func = pa_get_volume_func(PA_SAMPLE_S16NE);
268
269 printf("checking SSE2 %zd\n", sizeof(samples));
270
271 pa_random(samples, sizeof(samples));
272 memcpy(samples_ref, samples, sizeof(samples));
273 memcpy(samples_orig, samples, sizeof(samples));
274
275 for (i = 0; i < CHANNELS; i++)
276 volumes[i] = PA_CLAMP_VOLUME(rand() >> 1);
277 for (padding = 0; padding < PADDING; padding++, i++)
278 volumes[i] = volumes[padding];
279
280 func(samples_ref, volumes, CHANNELS, sizeof(samples));
281 pa_volume_s16ne_sse2(samples, volumes, CHANNELS, sizeof(samples));
282 for (i = 0; i < SAMPLES; i++) {
283 if (samples[i] != samples_ref[i]) {
284 printf ("%d: %04x != %04x (%04x * %04x)\n", i, samples[i], samples_ref[i],
285 samples_orig[i], volumes[i % CHANNELS]);
286 }
287 }
288
289 start = pa_rtclock_now();
290 for (j = 0; j < TIMES; j++) {
291 memcpy(samples, samples_orig, sizeof(samples));
292 pa_volume_s16ne_sse2(samples, volumes, CHANNELS, sizeof(samples));
293 }
294 stop = pa_rtclock_now();
295 pa_log_info("SSE: %llu usec.", (long long unsigned int)(stop - start));
296
297 start = pa_rtclock_now();
298 for (j = 0; j < TIMES; j++) {
299 memcpy(samples_ref, samples_orig, sizeof(samples));
300 func(samples_ref, volumes, CHANNELS, sizeof (samples));
301 }
302 stop = pa_rtclock_now();
303 pa_log_info("ref: %llu usec.", (long long unsigned int)(stop - start));
304
305 pa_assert_se(memcmp(samples_ref, samples, sizeof(samples)) == 0);
306 }
307 #endif
308 #endif /* defined (__i386__) || defined (__amd64__) */
309
310 void pa_volume_func_init_sse(pa_cpu_x86_flag_t flags) {
311 #if defined (__i386__) || defined (__amd64__)
312
313 #ifdef RUN_TEST
314 run_test();
315 #endif
316
317 if (flags & PA_CPU_X86_SSE2) {
318 pa_log_info("Initialising SSE2 optimized functions.");
319
320 pa_set_volume_func(PA_SAMPLE_S16NE, (pa_do_volume_func_t) pa_volume_s16ne_sse2);
321 pa_set_volume_func(PA_SAMPLE_S16RE, (pa_do_volume_func_t) pa_volume_s16re_sse2);
322 }
323 #endif /* defined (__i386__) || defined (__amd64__) */
324 }