]>
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/rtclock.h>
29 #include <pulsecore/random.h>
30 #include <pulsecore/macro.h>
31 #include <pulsecore/endianmacros.h>
35 #include "sample-util.h"
37 #if (!defined(__FreeBSD__) && defined (__i386__)) || defined (__amd64__)
39 #define VOLUME_32x16(s,v) /* .. | vh | vl | */ \
40 " pxor %%xmm4, %%xmm4 \n\t" /* .. | 0 | 0 | */ \
41 " punpcklwd %%xmm4, "#s" \n\t" /* .. | 0 | p0 | */ \
42 " pcmpgtw "#s", %%xmm4 \n\t" /* .. | 0 | s(p0) | */ \
43 " pand "#v", %%xmm4 \n\t" /* .. | 0 | (vl) | */ \
44 " movdqa "#s", %%xmm5 \n\t" \
45 " pmulhuw "#v", "#s" \n\t" /* .. | 0 | vl*p0 | */ \
46 " psubd %%xmm4, "#s" \n\t" /* .. | 0 | vl*p0 | + sign correct */ \
47 " psrld $16, "#v" \n\t" /* .. | 0 | vh | */ \
48 " pmaddwd %%xmm5, "#v" \n\t" /* .. | p0 * vh | */ \
49 " paddd "#s", "#v" \n\t" /* .. | p0 * v0 | */ \
50 " packssdw "#v", "#v" \n\t" /* .. | p1*v1 | p0*v0 | */
52 #define MOD_ADD(a,b) \
53 " add "#a", %3 \n\t" /* channel += inc */ \
55 " sub "#b", %4 \n\t" /* tmp = channel - channels */ \
56 " cmovae %4, %3 \n\t" /* if (tmp >= 0) channel = tmp */
60 " movdqa "#s", %%xmm4 \n\t" /* .. | h l | */ \
61 " psrlw $8, %%xmm4 \n\t" /* .. | 0 h | */ \
62 " psllw $8, "#s" \n\t" /* .. | l 0 | */ \
63 " por %%xmm4, "#s" \n\t" /* .. | l h | */
65 /* swap 2 registers 16 bits for better pairing */
66 #define SWAP_16_2(s1,s2) \
67 " movdqa "#s1", %%xmm4 \n\t" /* .. | h l | */ \
68 " movdqa "#s2", %%xmm5 \n\t" \
69 " psrlw $8, %%xmm4 \n\t" /* .. | 0 h | */ \
70 " psrlw $8, %%xmm5 \n\t" \
71 " psllw $8, "#s1" \n\t" /* .. | l 0 | */ \
72 " psllw $8, "#s2" \n\t" \
73 " por %%xmm4, "#s1" \n\t" /* .. | l h | */ \
74 " por %%xmm5, "#s2" \n\t"
76 static int channel_overread_table
[8] = {8,8,8,12,8,10,12,14};
78 static void pa_volume_s16ne_sse2(int16_t *samples
, const int32_t *volumes
, unsigned channels
, unsigned length
) {
79 pa_reg_x86 channel
, temp
;
81 /* Channels must be at least 8 and always a multiple of the original number.
82 * This is also the max amount we overread the volume array, which should
83 * have enough padding. */
85 channels
= channel_overread_table
[channels
];
87 __asm__
__volatile__ (
89 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
91 " test $1, %2 \n\t" /* check for odd samples */
94 " movd (%q1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
95 " movw (%0), %w4 \n\t" /* .. | p0 | */
96 " movd %4, %%xmm1 \n\t"
97 VOLUME_32x16 (%%xmm1
, %%xmm0
)
98 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
99 " movw %w4, (%0) \n\t"
104 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
108 "3: \n\t" /* do samples in groups of 2 */
109 " movq (%q1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
110 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
111 VOLUME_32x16 (%%xmm1
, %%xmm0
)
112 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
117 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
121 /* FIXME, we can do aligned access of the volume values if we can guarantee
122 * that the array is 16 bytes aligned, we probably have to do the odd values
123 * after this then. */
124 "5: \n\t" /* do samples in groups of 4 */
125 " movdqu (%q1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
126 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
127 VOLUME_32x16 (%%xmm1
, %%xmm0
)
128 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
133 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
137 "7: \n\t" /* do samples in groups of 8 */
138 " movdqu (%q1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
139 " movdqu 16(%q1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
140 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
141 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
142 VOLUME_32x16 (%%xmm1
, %%xmm0
)
143 VOLUME_32x16 (%%xmm3
, %%xmm2
)
144 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
145 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
152 : "+r" (samples
), "+r" (volumes
), "+r" (length
), "=D" (channel
), "=&r" (temp
)
153 #if defined (__i386__)
156 : "r" ((pa_reg_x86
)channels
)
162 static void pa_volume_s16re_sse2(int16_t *samples
, const int32_t *volumes
, unsigned channels
, unsigned length
) {
163 pa_reg_x86 channel
, temp
;
165 /* Channels must be at least 8 and always a multiple of the original number.
166 * This is also the max amount we overread the volume array, which should
167 * have enough padding. */
169 channels
= channel_overread_table
[channels
];
171 __asm__
__volatile__ (
173 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
175 " test $1, %2 \n\t" /* check for odd samples */
178 " movd (%q1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
179 " movw (%0), %w4 \n\t" /* .. | p0 | */
181 " movd %4, %%xmm1 \n\t"
182 VOLUME_32x16 (%%xmm1
, %%xmm0
)
183 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
185 " movw %w4, (%0) \n\t"
190 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
194 "3: \n\t" /* do samples in groups of 2 */
195 " movq (%q1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
196 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
198 VOLUME_32x16 (%%xmm1
, %%xmm0
)
200 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
205 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
209 /* FIXME, we can do aligned access of the volume values if we can guarantee
210 * that the array is 16 bytes aligned, we probably have to do the odd values
211 * after this then. */
212 "5: \n\t" /* do samples in groups of 4 */
213 " movdqu (%q1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
214 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
216 VOLUME_32x16 (%%xmm1
, %%xmm0
)
218 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
223 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
227 "7: \n\t" /* do samples in groups of 8 */
228 " movdqu (%q1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
229 " movdqu 16(%q1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
230 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
231 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
232 SWAP_16_2 (%%xmm1
, %%xmm3
)
233 VOLUME_32x16 (%%xmm1
, %%xmm0
)
234 VOLUME_32x16 (%%xmm3
, %%xmm2
)
235 SWAP_16_2 (%%xmm0
, %%xmm2
)
236 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
237 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
244 : "+r" (samples
), "+r" (volumes
), "+r" (length
), "=D" (channel
), "=&r" (temp
)
245 #if defined (__i386__)
248 : "r" ((pa_reg_x86
)channels
)
254 #endif /* (!defined(__FreeBSD__) && defined (__i386__)) || defined (__amd64__) */
256 void pa_volume_func_init_sse(pa_cpu_x86_flag_t flags
) {
257 #if (!defined(__FreeBSD__) && defined (__i386__)) || defined (__amd64__)
258 if (flags
& PA_CPU_X86_SSE2
) {
259 pa_log_info("Initialising SSE2 optimized volume functions.");
261 pa_set_volume_func(PA_SAMPLE_S16NE
, (pa_do_volume_func_t
) pa_volume_s16ne_sse2
);
262 pa_set_volume_func(PA_SAMPLE_S16RE
, (pa_do_volume_func_t
) pa_volume_s16re_sse2
);
264 #endif /* (!defined(__FreeBSD__) && defined (__i386__)) || defined (__amd64__) */