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, see <http://www.gnu.org/licenses/>.
25 #include <pulse/rtclock.h>
27 #include <pulsecore/random.h>
28 #include <pulsecore/macro.h>
29 #include <pulsecore/endianmacros.h>
33 #include "sample-util.h"
35 #if (!defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && defined (__i386__)) || defined (__amd64__)
37 #define VOLUME_32x16(s,v) /* .. | vh | vl | */ \
38 " pxor %%xmm4, %%xmm4 \n\t" /* .. | 0 | 0 | */ \
39 " punpcklwd %%xmm4, "#s" \n\t" /* .. | 0 | p0 | */ \
40 " pcmpgtw "#s", %%xmm4 \n\t" /* .. | 0 | s(p0) | */ \
41 " pand "#v", %%xmm4 \n\t" /* .. | 0 | (vl) | */ \
42 " movdqa "#s", %%xmm5 \n\t" \
43 " pmulhuw "#v", "#s" \n\t" /* .. | 0 | vl*p0 | */ \
44 " psubd %%xmm4, "#s" \n\t" /* .. | 0 | vl*p0 | + sign correct */ \
45 " psrld $16, "#v" \n\t" /* .. | 0 | vh | */ \
46 " pmaddwd %%xmm5, "#v" \n\t" /* .. | p0 * vh | */ \
47 " paddd "#s", "#v" \n\t" /* .. | p0 * v0 | */ \
48 " packssdw "#v", "#v" \n\t" /* .. | p1*v1 | p0*v0 | */
50 #define MOD_ADD(a,b) \
51 " add "#a", %3 \n\t" /* channel += inc */ \
53 " sub "#b", %4 \n\t" /* tmp = channel - channels */ \
54 " cmovae %4, %3 \n\t" /* if (tmp >= 0) channel = tmp */
58 " movdqa "#s", %%xmm4 \n\t" /* .. | h l | */ \
59 " psrlw $8, %%xmm4 \n\t" /* .. | 0 h | */ \
60 " psllw $8, "#s" \n\t" /* .. | l 0 | */ \
61 " por %%xmm4, "#s" \n\t" /* .. | l h | */
63 /* swap 2 registers 16 bits for better pairing */
64 #define SWAP_16_2(s1,s2) \
65 " movdqa "#s1", %%xmm4 \n\t" /* .. | h l | */ \
66 " movdqa "#s2", %%xmm5 \n\t" \
67 " psrlw $8, %%xmm4 \n\t" /* .. | 0 h | */ \
68 " psrlw $8, %%xmm5 \n\t" \
69 " psllw $8, "#s1" \n\t" /* .. | l 0 | */ \
70 " psllw $8, "#s2" \n\t" \
71 " por %%xmm4, "#s1" \n\t" /* .. | l h | */ \
72 " por %%xmm5, "#s2" \n\t"
74 static int channel_overread_table[8] = {8,8,8,12,8,10,12,14};
76 static void pa_volume_s16ne_sse2(int16_t *samples, const int32_t *volumes, unsigned channels, unsigned length) {
77 pa_reg_x86 channel, temp;
79 /* Channels must be at least 8 and always a multiple of the original number.
80 * This is also the max amount we overread the volume array, which should
81 * have enough padding. */
83 channels = channel_overread_table[channels];
85 __asm__ __volatile__ (
87 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
89 " test $1, %2 \n\t" /* check for odd samples */
92 " movd (%q1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
93 " movw (%0), %w4 \n\t" /* .. | p0 | */
94 " movd %4, %%xmm1 \n\t"
95 VOLUME_32x16 (%%xmm1, %%xmm0)
96 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
97 " movw %w4, (%0) \n\t"
102 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
106 "3: \n\t" /* do samples in groups of 2 */
107 " movq (%q1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
108 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
109 VOLUME_32x16 (%%xmm1, %%xmm0)
110 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
115 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
119 /* FIXME, we can do aligned access of the volume values if we can guarantee
120 * that the array is 16 bytes aligned, we probably have to do the odd values
121 * after this then. */
122 "5: \n\t" /* do samples in groups of 4 */
123 " movdqu (%q1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
124 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
125 VOLUME_32x16 (%%xmm1, %%xmm0)
126 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
131 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
135 "7: \n\t" /* do samples in groups of 8 */
136 " movdqu (%q1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
137 " movdqu 16(%q1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
138 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
139 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
140 VOLUME_32x16 (%%xmm1, %%xmm0)
141 VOLUME_32x16 (%%xmm3, %%xmm2)
142 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
143 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
150 : "+r" (samples), "+r" (volumes), "+r" (length), "=&D" (channel), "=&r" (temp)
151 #if defined (__i386__)
154 : "r" ((pa_reg_x86)channels)
160 static void pa_volume_s16re_sse2(int16_t *samples, const int32_t *volumes, unsigned channels, unsigned length) {
161 pa_reg_x86 channel, temp;
163 /* Channels must be at least 8 and always a multiple of the original number.
164 * This is also the max amount we overread the volume array, which should
165 * have enough padding. */
167 channels = channel_overread_table[channels];
169 __asm__ __volatile__ (
171 " sar $1, %2 \n\t" /* length /= sizeof (int16_t) */
173 " test $1, %2 \n\t" /* check for odd samples */
176 " movd (%q1, %3, 4), %%xmm0 \n\t" /* | v0h | v0l | */
177 " movw (%0), %w4 \n\t" /* .. | p0 | */
179 " movd %4, %%xmm1 \n\t"
180 VOLUME_32x16 (%%xmm1, %%xmm0)
181 " movd %%xmm0, %4 \n\t" /* .. | p0*v0 | */
183 " movw %w4, (%0) \n\t"
188 " sar $1, %2 \n\t" /* prepare for processing 2 samples at a time */
192 "3: \n\t" /* do samples in groups of 2 */
193 " movq (%q1, %3, 4), %%xmm0 \n\t" /* | v1h | v1l | v0h | v0l | */
194 " movd (%0), %%xmm1 \n\t" /* .. | p1 | p0 | */
196 VOLUME_32x16 (%%xmm1, %%xmm0)
198 " movd %%xmm0, (%0) \n\t" /* .. | p1*v1 | p0*v0 | */
203 " sar $1, %2 \n\t" /* prepare for processing 4 samples at a time */
207 /* FIXME, we can do aligned access of the volume values if we can guarantee
208 * that the array is 16 bytes aligned, we probably have to do the odd values
209 * after this then. */
210 "5: \n\t" /* do samples in groups of 4 */
211 " movdqu (%q1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
212 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
214 VOLUME_32x16 (%%xmm1, %%xmm0)
216 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
221 " sar $1, %2 \n\t" /* prepare for processing 8 samples at a time */
225 "7: \n\t" /* do samples in groups of 8 */
226 " movdqu (%q1, %3, 4), %%xmm0 \n\t" /* | v3h | v3l .. v0h | v0l | */
227 " movdqu 16(%q1, %3, 4), %%xmm2 \n\t" /* | v7h | v7l .. v4h | v4l | */
228 " movq (%0), %%xmm1 \n\t" /* .. | p3 .. p0 | */
229 " movq 8(%0), %%xmm3 \n\t" /* .. | p7 .. p4 | */
230 SWAP_16_2 (%%xmm1, %%xmm3)
231 VOLUME_32x16 (%%xmm1, %%xmm0)
232 VOLUME_32x16 (%%xmm3, %%xmm2)
233 SWAP_16_2 (%%xmm0, %%xmm2)
234 " movq %%xmm0, (%0) \n\t" /* .. | p3*v3 .. p0*v0 | */
235 " movq %%xmm2, 8(%0) \n\t" /* .. | p7*v7 .. p4*v4 | */
242 : "+r" (samples), "+r" (volumes), "+r" (length), "=&D" (channel), "=&r" (temp)
243 #if defined (__i386__)
246 : "r" ((pa_reg_x86)channels)
252 #endif /* (!defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && defined (__i386__)) || defined (__amd64__) */
254 void pa_volume_func_init_sse(pa_cpu_x86_flag_t flags) {
255 #if (!defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && defined (__i386__)) || defined (__amd64__)
256 if (flags & PA_CPU_X86_SSE2) {
257 pa_log_info("Initialising SSE2 optimized volume functions.");
259 pa_set_volume_func(PA_SAMPLE_S16NE, (pa_do_volume_func_t) pa_volume_s16ne_sse2);
260 pa_set_volume_func(PA_SAMPLE_S16RE, (pa_do_volume_func_t) pa_volume_s16re_sse2);
262 #endif /* (!defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && defined (__i386__)) || defined (__amd64__) */