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"
40 pa_volume_u8_mmx (uint8_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
44 for (channel
= 0; length
; length
--) {
47 hi
= volumes
[channel
] >> 16;
48 lo
= volumes
[channel
] & 0xFFFF;
50 t
= (int32_t) *samples
- 0x80;
51 t
= ((t
* lo
) >> 16) + (t
* hi
);
52 t
= PA_CLAMP_UNLIKELY(t
, -0x80, 0x7F);
53 *samples
++ = (uint8_t) (t
+ 0x80);
55 if (PA_UNLIKELY(++channel
>= channels
))
61 pa_volume_alaw_mmx (uint8_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
65 for (channel
= 0; length
; length
--) {
68 hi
= volumes
[channel
] >> 16;
69 lo
= volumes
[channel
] & 0xFFFF;
71 t
= (int32_t) st_alaw2linear16(*samples
);
72 t
= ((t
* lo
) >> 16) + (t
* hi
);
73 t
= PA_CLAMP_UNLIKELY(t
, -0x8000, 0x7FFF);
74 *samples
++ = (uint8_t) st_13linear2alaw((int16_t) t
>> 3);
76 if (PA_UNLIKELY(++channel
>= channels
))
82 pa_volume_ulaw_mmx (uint8_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
86 for (channel
= 0; length
; length
--) {
89 hi
= volumes
[channel
] >> 16;
90 lo
= volumes
[channel
] & 0xFFFF;
92 t
= (int32_t) st_ulaw2linear16(*samples
);
93 t
= ((t
* lo
) >> 16) + (t
* hi
);
94 t
= PA_CLAMP_UNLIKELY(t
, -0x8000, 0x7FFF);
95 *samples
++ = (uint8_t) st_14linear2ulaw((int16_t) t
>> 2);
97 if (PA_UNLIKELY(++channel
>= channels
))
103 /* in s: 2 int16_t samples
104 * in v: 2 int32_t volumes, fixed point 16:16
105 * out s: contains scaled and clamped int16_t samples.
107 * We calculate the high 32 bits of a 32x16 multiply which we then
108 * clamp to 16 bits. The calulcation is:
112 * s = ((s * vl) >> 16) + (s * vh);
114 * For the first multiply we have to do a sign correction as we need to
115 * multiply a signed int with an unsigned int. Hacker's delight 8-3 gives a
116 * simple formula to correct the sign of the high word after the signed
119 #define VOLUME_32x16(s,v) /* .. | vh | vl | */ \
120 " pxor %%mm4, %%mm4 \n\t" /* .. | 0 | 0 | */ \
121 " punpcklwd %%mm4, "#s" \n\t" /* .. | 0 | p0 | */ \
122 " pcmpgtw "#v", %%mm4 \n\t" /* .. | 0 | s(vl) | */ \
123 " pand "#s", %%mm4 \n\t" /* .. | 0 | (p0) | (vl >> 15) & p */ \
124 " movq %%mm6, %%mm5 \n\t" /* .. | ffff | 0 | */ \
125 " pand "#v", %%mm5 \n\t" /* .. | vh | 0 | */ \
126 " por %%mm5, %%mm4 \n\t" /* .. | vh | (p0) | */ \
127 " pmulhw "#s", "#v" \n\t" /* .. | 0 | vl*p0 | */ \
128 " paddw %%mm4, "#v" \n\t" /* .. | vh | vl*p0 | vh + sign correct */ \
129 " pslld $16, "#s" \n\t" /* .. | p0 | 0 | */ \
130 " por %%mm7, "#s" \n\t" /* .. | p0 | 1 | */ \
131 " pmaddwd "#s", "#v" \n\t" /* .. | p0 * v0 | */ \
132 " packssdw "#v", "#v" \n\t" /* .. | p1*v1 | p0*v0 | */
134 /* approximately advances %3 = (%3 + a) % b. This function requires that
136 #define MOD_ADD(a,b) \
137 " add "#a", %3 \n\t" \
139 " sub "#b", %4 \n\t" \
140 " cmp "#b", %3 \n\t" \
141 " cmovae %4, %3 \n\t"
145 " movq "#s", %%mm4 \n\t" /* .. | h l | */ \
146 " psrlw $8, %%mm4 \n\t" /* .. | 0 h | */ \
147 " psllw $8, "#s" \n\t" /* .. | l 0 | */ \
148 " por %%mm4, "#s" \n\t" /* .. | l h | */
150 /* swap 2 registers 16 bits for better pairing */
151 #define SWAP_16_2(s1,s2) \
152 " movq "#s1", %%mm4 \n\t" /* .. | h l | */ \
153 " movq "#s2", %%mm5 \n\t" \
154 " psrlw $8, %%mm4 \n\t" /* .. | 0 h | */ \
155 " psrlw $8, %%mm5 \n\t" \
156 " psllw $8, "#s1" \n\t" /* .. | l 0 | */ \
157 " psllw $8, "#s2" \n\t" \
158 " por %%mm4, "#s1" \n\t" /* .. | l h | */ \
159 " por %%mm5, "#s2" \n\t"
162 pa_volume_s16ne_mmx (int16_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
164 pa_reg_x86 channel
, temp
;
166 /* the max number of samples we process at a time, this is also the max amount
167 * we overread the volume array, which should have enough padding. */
168 channels
= MAX (4, channels
);
170 __asm__
__volatile__ (
172 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
173 " pcmpeqw %%mm6, %%mm6 \n\t" /* .. | ffff | ffff | */
174 " pcmpeqw %%mm7, %%mm7 \n\t" /* .. | ffff | ffff | */
175 " pslld $16, %%mm6 \n\t" /* .. | ffff | 0 | */
176 " psrld $31, %%mm7 \n\t" /* .. | 0 | 1 | */
178 " test $1, %2 \n\t" /* check for odd samples */
181 " movd (%1, %3, 4), %%mm0 \n\t" /* | v0h | v0l | */
182 " movw (%0), %4 \n\t" /* .. | p0 | */
183 " movd %4, %%mm1 \n\t"
184 VOLUME_32x16 (%%mm1
, %%mm0
)
185 " movd %%mm0, %4 \n\t" /* .. | p0*v0 | */
186 " movw %4, (%0) \n\t"
191 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
192 " test $1, %2 \n\t" /* check for odd samples */
195 "3: \n\t" /* do samples in groups of 2 */
196 " movq (%1, %3, 4), %%mm0 \n\t" /* | v1h | v1l | v0h | v0l | */
197 " movd (%0), %%mm1 \n\t" /* .. | p1 | p0 | */
198 VOLUME_32x16 (%%mm1
, %%mm0
)
199 " movd %%mm0, (%0) \n\t" /* | p1*v1 | p0*v0 | */
204 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
208 "5: \n\t" /* do samples in groups of 4 */
209 " movq (%1, %3, 4), %%mm0 \n\t" /* | v1h | v1l | v0h | v0l | */
210 " movq 8(%1, %3, 4), %%mm2 \n\t" /* | v3h | v3l | v2h | v2l | */
211 " movd (%0), %%mm1 \n\t" /* .. | p1 | p0 | */
212 " movd 4(%0), %%mm3 \n\t" /* .. | p3 | p2 | */
213 VOLUME_32x16 (%%mm1
, %%mm0
)
214 VOLUME_32x16 (%%mm3
, %%mm2
)
215 " movd %%mm0, (%0) \n\t" /* | p1*v1 | p0*v0 | */
216 " movd %%mm2, 4(%0) \n\t" /* | p3*v3 | p2*v2 | */
225 : "+r" (samples
), "+r" (volumes
), "+r" (length
), "=D" ((pa_reg_x86
)channel
), "=&r" (temp
)
226 : "r" ((pa_reg_x86
)channels
)
232 pa_volume_s16re_mmx (int16_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
234 pa_reg_x86 channel
, temp
;
236 /* the max number of samples we process at a time, this is also the max amount
237 * we overread the volume array, which should have enough padding. */
238 channels
= MAX (4, channels
);
240 __asm__
__volatile__ (
242 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
243 " pcmpeqw %%mm6, %%mm6 \n\t" /* .. | ffff | ffff | */
244 " pcmpeqw %%mm7, %%mm7 \n\t" /* .. | ffff | ffff | */
245 " pslld $16, %%mm6 \n\t" /* .. | ffff | 0 | */
246 " psrld $31, %%mm7 \n\t" /* .. | 0 | 1 | */
248 " test $1, %2 \n\t" /* check for odd samples */
251 " movd (%1, %3, 4), %%mm0 \n\t" /* | v0h | v0l | */
252 " movw (%0), %4 \n\t" /* .. | p0 | */
254 " movd %4, %%mm1 \n\t"
255 VOLUME_32x16 (%%mm1
, %%mm0
)
256 " movd %%mm0, %4 \n\t" /* .. | p0*v0 | */
258 " movw %4, (%0) \n\t"
263 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
264 " test $1, %2 \n\t" /* check for odd samples */
267 "3: \n\t" /* do samples in groups of 2 */
268 " movq (%1, %3, 4), %%mm0 \n\t" /* | v1h | v1l | v0h | v0l | */
269 " movd (%0), %%mm1 \n\t" /* .. | p1 | p0 | */
271 VOLUME_32x16 (%%mm1
, %%mm0
)
273 " movd %%mm0, (%0) \n\t" /* | p1*v1 | p0*v0 | */
278 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
282 "5: \n\t" /* do samples in groups of 4 */
283 " movq (%1, %3, 4), %%mm0 \n\t" /* | v1h | v1l | v0h | v0l | */
284 " movq 8(%1, %3, 4), %%mm2 \n\t" /* | v3h | v3l | v2h | v2l | */
285 " movd (%0), %%mm1 \n\t" /* .. | p1 | p0 | */
286 " movd 4(%0), %%mm3 \n\t" /* .. | p3 | p2 | */
287 SWAP_16_2 (%%mm1
, %%mm3
)
288 VOLUME_32x16 (%%mm1
, %%mm0
)
289 VOLUME_32x16 (%%mm3
, %%mm2
)
290 SWAP_16_2 (%%mm0
, %%mm2
)
291 " movd %%mm0, (%0) \n\t" /* | p1*v1 | p0*v0 | */
292 " movd %%mm2, 4(%0) \n\t" /* | p3*v3 | p2*v2 | */
301 : "+r" (samples
), "+r" (volumes
), "+r" (length
), "=D" ((pa_reg_x86
)channel
), "=&r" (temp
)
302 : "r" ((pa_reg_x86
)channels
)
309 pa_volume_float32ne_mmx (float *samples
, float *volumes
, unsigned channels
, unsigned length
)
313 length
/= sizeof (float);
315 for (channel
= 0; length
; length
--) {
316 *samples
++ *= volumes
[channel
];
318 if (PA_UNLIKELY(++channel
>= channels
))
324 pa_volume_float32re_mmx (float *samples
, float *volumes
, unsigned channels
, unsigned length
)
328 length
/= sizeof (float);
330 for (channel
= 0; length
; length
--) {
333 t
= PA_FLOAT32_SWAP(*samples
);
334 t
*= volumes
[channel
];
335 *samples
++ = PA_FLOAT32_SWAP(t
);
337 if (PA_UNLIKELY(++channel
>= channels
))
343 pa_volume_s32ne_mmx (int32_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
347 length
/= sizeof (int32_t);
349 for (channel
= 0; length
; length
--) {
352 t
= (int64_t)(*samples
);
353 t
= (t
* volumes
[channel
]) >> 16;
354 t
= PA_CLAMP_UNLIKELY(t
, -0x80000000LL
, 0x7FFFFFFFLL
);
355 *samples
++ = (int32_t) t
;
357 if (PA_UNLIKELY(++channel
>= channels
))
363 pa_volume_s32re_mmx (int32_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
367 length
/= sizeof (int32_t);
369 for (channel
= 0; length
; length
--) {
372 t
= (int64_t) PA_INT32_SWAP(*samples
);
373 t
= (t
* volumes
[channel
]) >> 16;
374 t
= PA_CLAMP_UNLIKELY(t
, -0x80000000LL
, 0x7FFFFFFFLL
);
375 *samples
++ = PA_INT32_SWAP((int32_t) t
);
377 if (PA_UNLIKELY(++channel
>= channels
))
383 pa_volume_s24ne_mmx (uint8_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
388 e
= samples
+ length
;
390 for (channel
= 0; samples
< e
; samples
+= 3) {
393 t
= (int64_t)((int32_t) (PA_READ24NE(samples
) << 8));
394 t
= (t
* volumes
[channel
]) >> 16;
395 t
= PA_CLAMP_UNLIKELY(t
, -0x80000000LL
, 0x7FFFFFFFLL
);
396 PA_WRITE24NE(samples
, ((uint32_t) (int32_t) t
) >> 8);
398 if (PA_UNLIKELY(++channel
>= channels
))
404 pa_volume_s24re_mmx (uint8_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
409 e
= samples
+ length
;
411 for (channel
= 0; samples
< e
; samples
+= 3) {
414 t
= (int64_t)((int32_t) (PA_READ24RE(samples
) << 8));
415 t
= (t
* volumes
[channel
]) >> 16;
416 t
= PA_CLAMP_UNLIKELY(t
, -0x80000000LL
, 0x7FFFFFFFLL
);
417 PA_WRITE24RE(samples
, ((uint32_t) (int32_t) t
) >> 8);
419 if (PA_UNLIKELY(++channel
>= channels
))
425 pa_volume_s24_32ne_mmx (uint32_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
429 length
/= sizeof (uint32_t);
431 for (channel
= 0; length
; length
--) {
434 t
= (int64_t) ((int32_t) (*samples
<< 8));
435 t
= (t
* volumes
[channel
]) >> 16;
436 t
= PA_CLAMP_UNLIKELY(t
, -0x80000000LL
, 0x7FFFFFFFLL
);
437 *samples
++ = ((uint32_t) ((int32_t) t
)) >> 8;
439 if (PA_UNLIKELY(++channel
>= channels
))
445 pa_volume_s24_32re_mmx (uint32_t *samples
, int32_t *volumes
, unsigned channels
, unsigned length
)
449 length
/= sizeof (uint32_t);
451 for (channel
= 0; length
; length
--) {
454 t
= (int64_t) ((int32_t) (PA_UINT32_SWAP(*samples
) << 8));
455 t
= (t
* volumes
[channel
]) >> 16;
456 t
= PA_CLAMP_UNLIKELY(t
, -0x80000000LL
, 0x7FFFFFFFLL
);
457 *samples
++ = PA_UINT32_SWAP(((uint32_t) ((int32_t) t
)) >> 8);
459 if (PA_UNLIKELY(++channel
>= channels
))
473 static void run_test (void) {
474 int16_t samples
[SAMPLES
];
475 int16_t samples_ref
[SAMPLES
];
476 int16_t samples_orig
[SAMPLES
];
477 int32_t volumes
[CHANNELS
+ PADDING
];
479 pa_do_volume_func_t func
;
480 struct timeval start
, stop
;
482 func
= pa_get_volume_func (PA_SAMPLE_S16NE
);
484 printf ("checking MMX %zd\n", sizeof (samples
));
486 pa_random (samples
, sizeof (samples
));
487 memcpy (samples_ref
, samples
, sizeof (samples
));
488 memcpy (samples_orig
, samples
, sizeof (samples
));
490 for (i
= 0; i
< CHANNELS
; i
++)
491 volumes
[i
] = rand() >> 1;
492 for (padding
= 0; padding
< PADDING
; padding
++, i
++)
493 volumes
[i
] = volumes
[padding
];
495 func (samples_ref
, volumes
, CHANNELS
, sizeof (samples
));
496 pa_volume_s16ne_mmx (samples
, volumes
, CHANNELS
, sizeof (samples
));
497 for (i
= 0; i
< SAMPLES
; i
++) {
498 if (samples
[i
] != samples_ref
[i
]) {
499 printf ("%d: %04x != %04x (%04x * %04x)\n", i
, samples
[i
], samples_ref
[i
],
500 samples_orig
[i
], volumes
[i
% CHANNELS
]);
504 pa_gettimeofday(&start
);
505 for (j
= 0; j
< TIMES
; j
++) {
506 memcpy (samples
, samples_orig
, sizeof (samples
));
507 pa_volume_s16ne_mmx (samples
, volumes
, CHANNELS
, sizeof (samples
));
509 pa_gettimeofday(&stop
);
510 pa_log_info("MMX: %llu usec.", (long long unsigned int)pa_timeval_diff (&stop
, &start
));
512 pa_gettimeofday(&start
);
513 for (j
= 0; j
< TIMES
; j
++) {
514 memcpy (samples_ref
, samples_orig
, sizeof (samples
));
515 func (samples_ref
, volumes
, CHANNELS
, sizeof (samples
));
517 pa_gettimeofday(&stop
);
518 pa_log_info("ref: %llu usec.", (long long unsigned int)pa_timeval_diff (&stop
, &start
));
522 void pa_volume_func_init_mmx (pa_cpu_x86_flag_t flags
) {
523 pa_log_info("Initialising MMX optimized functions.");
529 pa_set_volume_func (PA_SAMPLE_S16NE
, (pa_do_volume_func_t
) pa_volume_s16ne_mmx
);
530 pa_set_volume_func (PA_SAMPLE_S16RE
, (pa_do_volume_func_t
) pa_volume_s16re_mmx
);