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