]> code.delx.au - pulseaudio/blob - src/pulsecore/svolume_sse.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[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/rtclock.h>
28
29 #include <pulsecore/random.h>
30 #include <pulsecore/macro.h>
31 #include <pulsecore/endianmacros.h>
32
33 #include "cpu-x86.h"
34
35 #include "sample-util.h"
36
37 #if (!defined(__FreeBSD__) && defined (__i386__)) || defined (__amd64__)
38
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 | */
51
52 #define MOD_ADD(a,b) \
53 " add "#a", %3 \n\t" /* channel += inc */ \
54 " mov %3, %4 \n\t" \
55 " sub "#b", %4 \n\t" /* tmp = channel - channels */ \
56 " cmovae %4, %3 \n\t" /* if (tmp >= 0) channel = tmp */
57
58 /* swap 16 bits */
59 #define SWAP_16(s) \
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 | */
64
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"
75
76 static int channel_overread_table[8] = {8,8,8,12,8,10,12,14};
77
78 static void pa_volume_s16ne_sse2(int16_t *samples, const int32_t *volumes, unsigned channels, unsigned length) {
79 pa_reg_x86 channel, temp;
80
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. */
84 if (channels < 8)
85 channels = channel_overread_table[channels];
86
87 __asm__ __volatile__ (
88 " xor %3, %3 \n\t"
89 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
90
91 " test $1, %2 \n\t" /* check for odd samples */
92 " je 2f \n\t"
93
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"
100 " add $2, %0 \n\t"
101 MOD_ADD ($1, %5)
102
103 "2: \n\t"
104 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
105 " test $1, %2 \n\t"
106 " je 4f \n\t"
107
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 | */
113 " add $4, %0 \n\t"
114 MOD_ADD ($2, %5)
115
116 "4: \n\t"
117 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
118 " test $1, %2 \n\t"
119 " je 6f \n\t"
120
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 | */
129 " add $8, %0 \n\t"
130 MOD_ADD ($4, %5)
131
132 "6: \n\t"
133 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
134 " cmp $0, %2 \n\t"
135 " je 8f \n\t"
136
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 | */
146 " add $16, %0 \n\t"
147 MOD_ADD ($8, %5)
148 " dec %2 \n\t"
149 " jne 7b \n\t"
150 "8: \n\t"
151
152 : "+r" (samples), "+r" (volumes), "+r" (length), "=D" (channel), "=&r" (temp)
153 #if defined (__i386__)
154 : "m" (channels)
155 #else
156 : "r" ((pa_reg_x86)channels)
157 #endif
158 : "cc"
159 );
160 }
161
162 static void pa_volume_s16re_sse2(int16_t *samples, const int32_t *volumes, unsigned channels, unsigned length) {
163 pa_reg_x86 channel, temp;
164
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. */
168 if (channels < 8)
169 channels = channel_overread_table[channels];
170
171 __asm__ __volatile__ (
172 " xor %3, %3 \n\t"
173 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
174
175 " test $1, %2 \n\t" /* check for odd samples */
176 " je 2f \n\t"
177
178 " movd (%q1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
179 " movw (%0), %w4 \n\t" /* .. | p0 | */
180 " rorw $8, %w4 \n\t"
181 " movd %4, %%xmm1 \n\t"
182 VOLUME_32x16 (%%xmm1, %%xmm0)
183 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
184 " rorw $8, %w4 \n\t"
185 " movw %w4, (%0) \n\t"
186 " add $2, %0 \n\t"
187 MOD_ADD ($1, %5)
188
189 "2: \n\t"
190 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
191 " test $1, %2 \n\t"
192 " je 4f \n\t"
193
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 | */
197 SWAP_16 (%%xmm1)
198 VOLUME_32x16 (%%xmm1, %%xmm0)
199 SWAP_16 (%%xmm0)
200 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
201 " add $4, %0 \n\t"
202 MOD_ADD ($2, %5)
203
204 "4: \n\t"
205 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
206 " test $1, %2 \n\t"
207 " je 6f \n\t"
208
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 | */
215 SWAP_16 (%%xmm1)
216 VOLUME_32x16 (%%xmm1, %%xmm0)
217 SWAP_16 (%%xmm0)
218 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
219 " add $8, %0 \n\t"
220 MOD_ADD ($4, %5)
221
222 "6: \n\t"
223 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
224 " cmp $0, %2 \n\t"
225 " je 8f \n\t"
226
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 | */
238 " add $16, %0 \n\t"
239 MOD_ADD ($8, %5)
240 " dec %2 \n\t"
241 " jne 7b \n\t"
242 "8: \n\t"
243
244 : "+r" (samples), "+r" (volumes), "+r" (length), "=D" (channel), "=&r" (temp)
245 #if defined (__i386__)
246 : "m" (channels)
247 #else
248 : "r" ((pa_reg_x86)channels)
249 #endif
250 : "cc"
251 );
252 }
253
254 #endif /* (!defined(__FreeBSD__) && defined (__i386__)) || defined (__amd64__) */
255
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.");
260
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);
263 }
264 #endif /* (!defined(__FreeBSD__) && defined (__i386__)) || defined (__amd64__) */
265 }