]> code.delx.au - pulseaudio/blob - src/pulsecore/svolume_sse.c
svolume: remove unneeded compare
[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 #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 | */
50
51 #define MOD_ADD(a,b) \
52 " add "#a", %3 \n\t" /* channel += inc */ \
53 " mov %3, %4 \n\t" \
54 " sub "#b", %4 \n\t" /* tmp = channel - channels */ \
55 " cmovae %4, %3 \n\t" /* if (tmp >= 0) channel = tmp */
56
57 /* swap 16 bits */
58 #define SWAP_16(s) \
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 | */
63
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"
74
75 static void
76 pa_volume_s16ne_sse (int16_t *samples, int32_t *volumes, unsigned channels, unsigned length)
77 {
78 pa_reg_x86 channel, temp;
79
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);
83
84 __asm__ __volatile__ (
85 " xor %3, %3 \n\t"
86 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
87
88 " test $1, %2 \n\t" /* check for odd samples */
89 " je 2f \n\t"
90
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 | */
96 " movw %4, (%0) \n\t"
97 " add $2, %0 \n\t"
98 MOD_ADD ($1, %5)
99
100 "2: \n\t"
101 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
102 " test $1, %2 \n\t"
103 " je 4f \n\t"
104
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 | */
110 " add $4, %0 \n\t"
111 MOD_ADD ($2, %5)
112
113 "4: \n\t"
114 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
115 " test $1, %2 \n\t"
116 " je 6f \n\t"
117
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 | */
123 " add $8, %0 \n\t"
124 MOD_ADD ($4, %5)
125
126 "6: \n\t"
127 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
128 " cmp $0, %2 \n\t"
129 " je 8f \n\t"
130
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 | */
140 " add $16, %0 \n\t"
141 MOD_ADD ($8, %5)
142 " dec %2 \n\t"
143 " jne 7b \n\t"
144 "8: \n\t"
145
146 : "+r" (samples), "+r" (volumes), "+r" (length), "=D" (channel), "=&r" (temp)
147 : "r" ((pa_reg_x86)channels)
148 : "cc"
149 );
150 }
151
152 static void
153 pa_volume_s16re_sse (int16_t *samples, int32_t *volumes, unsigned channels, unsigned length)
154 {
155 pa_reg_x86 channel, temp;
156
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);
160
161 __asm__ __volatile__ (
162 " xor %3, %3 \n\t"
163 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
164
165 " test $1, %2 \n\t" /* check for odd samples */
166 " je 2f \n\t"
167
168 " movd (%1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
169 " movw (%0), %4 \n\t" /* .. | p0 | */
170 " rorw $8, %4 \n\t"
171 " movd %4, %%xmm1 \n\t"
172 VOLUME_32x16 (%%xmm1, %%xmm0)
173 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
174 " rorw $8, %4 \n\t"
175 " movw %4, (%0) \n\t"
176 " add $2, %0 \n\t"
177 MOD_ADD ($1, %5)
178
179 "2: \n\t"
180 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
181 " test $1, %2 \n\t"
182 " je 4f \n\t"
183
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 | */
187 SWAP_16 (%%xmm1)
188 VOLUME_32x16 (%%xmm1, %%xmm0)
189 SWAP_16 (%%xmm0)
190 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
191 " add $4, %0 \n\t"
192 MOD_ADD ($2, %5)
193
194 "4: \n\t"
195 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
196 " test $1, %2 \n\t"
197 " je 6f \n\t"
198
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 | */
205 SWAP_16 (%%xmm1)
206 VOLUME_32x16 (%%xmm1, %%xmm0)
207 SWAP_16 (%%xmm0)
208 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
209 " add $8, %0 \n\t"
210 MOD_ADD ($4, %5)
211
212 "6: \n\t"
213 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
214 " cmp $0, %2 \n\t"
215 " je 8f \n\t"
216
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 | */
228 " add $16, %0 \n\t"
229 MOD_ADD ($8, %5)
230 " dec %2 \n\t"
231 " jne 7b \n\t"
232 "8: \n\t"
233
234 : "+r" (samples), "+r" (volumes), "+r" (length), "=D" (channel), "=&r" (temp)
235 : "r" ((pa_reg_x86)channels)
236 : "cc"
237 );
238 }
239
240 #undef RUN_TEST
241
242 #ifdef RUN_TEST
243 #define CHANNELS 2
244 #define SAMPLES 1021
245 #define TIMES 1000
246 #define PADDING 16
247
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];
253 int i, j, padding;
254 pa_do_volume_func_t func;
255 struct timeval start, stop;
256
257 func = pa_get_volume_func (PA_SAMPLE_S16NE);
258
259 printf ("checking SSE %zd\n", sizeof (samples));
260
261 pa_random (samples, sizeof (samples));
262 memcpy (samples_ref, samples, sizeof (samples));
263 memcpy (samples_orig, samples, sizeof (samples));
264
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];
269
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]);
276 }
277 }
278
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));
283 }
284 pa_gettimeofday(&stop);
285 pa_log_info("SSE: %llu usec.", (long long unsigned int)pa_timeval_diff (&stop, &start));
286
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));
291 }
292 pa_gettimeofday(&stop);
293 pa_log_info("ref: %llu usec.", (long long unsigned int)pa_timeval_diff (&stop, &start));
294 }
295 #endif
296
297 void pa_volume_func_init_sse (pa_cpu_x86_flag_t flags) {
298 pa_log_info("Initialising SSE optimized functions.");
299
300 #ifdef RUN_TEST
301 run_test ();
302 #endif
303
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);
306 }