]>
code.delx.au - pulseaudio/blob - src/pulsecore/svolume_sse.c
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2009 Wim Taymans <wim.taymans@collabora.co.uk>
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.
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.
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
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>
35 #include "sample-util.h"
36 #include "endianmacros.h"
38 #define VOLUME_32x16(s,v) /* .. | vh | vl | */ \
39 " pxor %%xmm4, %%xmm4 \n\t" /* .. | 0 | 0 | */ \
40 " punpcklwd %%xmm4, "#s" \n\t" /* .. | 0 | p0 | */ \
41 " pcmpgtw "#s", %%xmm4 \n\t" /* .. | 0 | s(p0) | */ \
42 " pand "#v", %%xmm4 \n\t" /* .. | 0 | (vl) | */ \
43 " movdqa "#s", %%xmm5 \n\t" \
44 " pmulhuw "#v", "#s" \n\t" /* .. | 0 | vl*p0 | */ \
45 " psubd %%xmm4, "#s" \n\t" /* .. | 0 | vl*p0 | + sign correct */ \
46 " psrld $16, "#v" \n\t" /* .. | p0 | 0 | */ \
47 " pmaddwd %%xmm5, "#v" \n\t" /* .. | p0 * vh | */ \
48 " paddd "#s", "#v" \n\t" /* .. | p0 * v0 | */ \
49 " packssdw "#v", "#v" \n\t" /* .. | p1*v1 | p0*v0 | */
51 #define MOD_ADD(a,b) \
52 " add "#a", %3 \n\t" /* channel += inc */ \
54 " sub "#b", %4 \n\t" /* tmp = channel - channels */ \
55 " cmovae %4, %3 \n\t" /* if (tmp >= 0) channel = tmp */
59 " movdqa "#s", %%xmm4 \n\t" /* .. | h l | */ \
60 " psrlw $8, %%xmm4 \n\t" /* .. | 0 h | */ \
61 " psllw $8, "#s" \n\t" /* .. | l 0 | */ \
62 " por %%xmm4, "#s" \n\t" /* .. | l h | */
64 /* swap 2 registers 16 bits for better pairing */
65 #define SWAP_16_2(s1,s2) \
66 " movdqa "#s1", %%xmm4 \n\t" /* .. | h l | */ \
67 " movdqa "#s2", %%xmm5 \n\t" \
68 " psrlw $8, %%xmm4 \n\t" /* .. | 0 h | */ \
69 " psrlw $8, %%xmm5 \n\t" \
70 " psllw $8, "#s1" \n\t" /* .. | l 0 | */ \
71 " psllw $8, "#s2" \n\t" \
72 " por %%xmm4, "#s1" \n\t" /* .. | l h | */ \
73 " por %%xmm5, "#s2" \n\t"
76 pa_volume_s16ne_sse (int16_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
78 pa_reg_x86 channel
, temp
;
80 /* the max number of samples we process at a time, this is also the max amount
81 * we overread the volume array, which should have enough padding. */
82 channels
= MAX (8, channels
);
84 __asm__
__volatile__ (
86 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
88 " test $1, %2 \n\t" /* check for odd samples */
91 " movd (%1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
92 " movw (%0), %4 \n\t" /* .. | p0 | */
93 " movd %4, %%xmm1 \n\t"
94 VOLUME_32x16 (%%xmm1
, %%xmm0
)
95 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
101 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
105 "3: \n\t" /* do samples in groups of 2 */
106 " movq (%1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
107 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
108 VOLUME_32x16 (%%xmm1
, %%xmm0
)
109 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
114 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
118 "5: \n\t" /* do samples in groups of 4 */
119 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
120 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
121 VOLUME_32x16 (%%xmm1
, %%xmm0
)
122 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
127 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
131 "7: \n\t" /* do samples in groups of 8 */
132 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
133 " movdqu 16(%1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
134 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
135 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
136 VOLUME_32x16 (%%xmm1
, %%xmm0
)
137 VOLUME_32x16 (%%xmm3
, %%xmm2
)
138 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
139 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
146 : "+r" (samples
), "+r" (volumes
), "+r" (length
), "=D" (channel
), "=&r" (temp
)
147 : "r" ((pa_reg_x86
)channels
)
153 pa_volume_s16re_sse (int16_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
155 pa_reg_x86 channel
, temp
;
157 /* the max number of samples we process at a time, this is also the max amount
158 * we overread the volume array, which should have enough padding. */
159 channels
= MAX (8, channels
);
161 __asm__
__volatile__ (
163 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
165 " test $1, %2 \n\t" /* check for odd samples */
168 " movd (%1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
169 " movw (%0), %4 \n\t" /* .. | p0 | */
171 " movd %4, %%xmm1 \n\t"
172 VOLUME_32x16 (%%xmm1
, %%xmm0
)
173 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
175 " movw %4, (%0) \n\t"
180 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
184 "3: \n\t" /* do samples in groups of 2 */
185 " movq (%1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
186 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
188 VOLUME_32x16 (%%xmm1
, %%xmm0
)
190 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
195 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
199 /* FIXME, we can do aligned access of the volume values if we can guarantee
200 * that the array is 16 bytes aligned, we probably have to do the odd values
201 * after this then. */
202 "5: \n\t" /* do samples in groups of 4 */
203 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
204 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
206 VOLUME_32x16 (%%xmm1
, %%xmm0
)
208 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
213 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
217 "7: \n\t" /* do samples in groups of 8 */
218 " movdqu (%1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
219 " movdqu 16(%1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
220 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
221 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
222 SWAP_16_2 (%%xmm1
, %%xmm3
)
223 VOLUME_32x16 (%%xmm1
, %%xmm0
)
224 VOLUME_32x16 (%%xmm3
, %%xmm2
)
225 SWAP_16_2 (%%xmm0
, %%xmm2
)
226 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
227 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
234 : "+r" (samples
), "+r" (volumes
), "+r" (length
), "=D" (channel
), "=&r" (temp
)
235 : "r" ((pa_reg_x86
)channels
)
248 static void run_test (void) {
249 int16_t samples
[SAMPLES
];
250 int16_t samples_ref
[SAMPLES
];
251 int16_t samples_orig
[SAMPLES
];
252 int32_t volumes
[CHANNELS
+ PADDING
];
254 pa_do_volume_func_t func
;
255 struct timeval start
, stop
;
257 func
= pa_get_volume_func (PA_SAMPLE_S16NE
);
259 printf ("checking SSE %zd\n", sizeof (samples
));
261 pa_random (samples
, sizeof (samples
));
262 memcpy (samples_ref
, samples
, sizeof (samples
));
263 memcpy (samples_orig
, samples
, sizeof (samples
));
265 for (i
= 0; i
< CHANNELS
; i
++)
266 volumes
[i
] = rand() >> 1;
267 for (padding
= 0; padding
< PADDING
; padding
++, i
++)
268 volumes
[i
] = volumes
[padding
];
270 func (samples_ref
, volumes
, CHANNELS
, sizeof (samples
));
271 pa_volume_s16ne_sse (samples
, volumes
, CHANNELS
, sizeof (samples
));
272 for (i
= 0; i
< SAMPLES
; i
++) {
273 if (samples
[i
] != samples_ref
[i
]) {
274 printf ("%d: %04x != %04x (%04x * %04x)\n", i
, samples
[i
], samples_ref
[i
],
275 samples_orig
[i
], volumes
[i
% CHANNELS
]);
279 pa_gettimeofday(&start
);
280 for (j
= 0; j
< TIMES
; j
++) {
281 memcpy (samples
, samples_orig
, sizeof (samples
));
282 pa_volume_s16ne_sse (samples
, volumes
, CHANNELS
, sizeof (samples
));
284 pa_gettimeofday(&stop
);
285 pa_log_info("SSE: %llu usec.", (long long unsigned int)pa_timeval_diff (&stop
, &start
));
287 pa_gettimeofday(&start
);
288 for (j
= 0; j
< TIMES
; j
++) {
289 memcpy (samples_ref
, samples_orig
, sizeof (samples
));
290 func (samples_ref
, volumes
, CHANNELS
, sizeof (samples
));
292 pa_gettimeofday(&stop
);
293 pa_log_info("ref: %llu usec.", (long long unsigned int)pa_timeval_diff (&stop
, &start
));
297 void pa_volume_func_init_sse (pa_cpu_x86_flag_t flags
) {
298 pa_log_info("Initialising SSE optimized functions.");
304 pa_set_volume_func (PA_SAMPLE_S16NE
, (pa_do_volume_func_t
) pa_volume_s16ne_sse
);
305 pa_set_volume_func (PA_SAMPLE_S16RE
, (pa_do_volume_func_t
) pa_volume_s16re_sse
);