]>
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 #if defined (__i386__) || defined (__amd64__)
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 | */
53 #define MOD_ADD(a,b) \
54 " add "#a", %3 \n\t" /* channel += inc */ \
56 " sub "#b", %4 \n\t" /* tmp = channel - channels */ \
57 " cmovae %4, %3 \n\t" /* if (tmp >= 0) channel = tmp */
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 | */
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"
78 pa_volume_s16ne_sse (int16_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
80 pa_reg_x86 channel
, temp
;
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
);
86 __asm__
__volatile__ (
88 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
90 " test $1, %2 \n\t" /* check for odd samples */
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"
103 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
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 | */
116 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
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 | */
129 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
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 | */
148 : "+r" (samples
), "+r" (volumes
), "+r" (length
), "=D" (channel
), "=&r" (temp
)
149 : "r" ((pa_reg_x86
)channels
)
155 pa_volume_s16re_sse (int16_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
157 pa_reg_x86 channel
, temp
;
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
);
163 __asm__
__volatile__ (
165 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
167 " test $1, %2 \n\t" /* check for odd samples */
170 " movd (%1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
171 " movw (%0), %w4 \n\t" /* .. | p0 | */
173 " movd %4, %%xmm1 \n\t"
174 VOLUME_32x16 (%%xmm1
, %%xmm0
)
175 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
177 " movw %w4, (%0) \n\t"
182 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
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 | */
190 VOLUME_32x16 (%%xmm1
, %%xmm0
)
192 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
197 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
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 | */
208 VOLUME_32x16 (%%xmm1
, %%xmm0
)
210 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
215 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
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 | */
236 : "+r" (samples
), "+r" (volumes
), "+r" (length
), "=D" (channel
), "=&r" (temp
)
237 : "r" ((pa_reg_x86
)channels
)
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
];
256 pa_do_volume_func_t func
;
257 struct timeval start
, stop
;
259 func
= pa_get_volume_func (PA_SAMPLE_S16NE
);
261 printf ("checking SSE %zd\n", sizeof (samples
));
263 pa_random (samples
, sizeof (samples
));
264 memcpy (samples_ref
, samples
, sizeof (samples
));
265 memcpy (samples_orig
, samples
, sizeof (samples
));
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
];
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
]);
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
));
286 pa_gettimeofday(&stop
);
287 pa_log_info("SSE: %llu usec.", (long long unsigned int)pa_timeval_diff (&stop
, &start
));
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
));
294 pa_gettimeofday(&stop
);
295 pa_log_info("ref: %llu usec.", (long long unsigned int)pa_timeval_diff (&stop
, &start
));
298 #endif /* defined (__i386__) || defined (__amd64__) */
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.");
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__) */