daemon: add nice value in service file to improve performance
[platform/upstream/pulseaudio.git] / 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, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <pulse/rtclock.h>
26
27 #include <pulsecore/random.h>
28 #include <pulsecore/macro.h>
29 #include <pulsecore/endianmacros.h>
30
31 #include "cpu-x86.h"
32
33 #include "sample-util.h"
34
35 #if (!defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && defined (__i386__)) || defined (__amd64__)
36
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 | */
49
50 #define MOD_ADD(a,b) \
51       " add "#a", %3                 \n\t" /* channel += inc           */ \
52       " mov %3, %4                   \n\t"                                \
53       " sub "#b", %4                 \n\t" /* tmp = channel - channels */ \
54       " cmovae %4, %3                \n\t" /* if (tmp >= 0) channel = tmp  */
55
56 /* swap 16 bits */
57 #define SWAP_16(s) \
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 |  */
62
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"
73
74 static int channel_overread_table[8] = {8,8,8,12,8,10,12,14};
75
76 static void pa_volume_s16ne_sse2(int16_t *samples, const int32_t *volumes, unsigned channels, unsigned length) {
77     pa_reg_x86 channel, temp;
78
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. */
82     if (channels < 8)
83         channels = channel_overread_table[channels];
84
85     __asm__ __volatile__ (
86         " xor %3, %3                    \n\t"
87         " sar $1, %2                    \n\t" /* length /= sizeof (int16_t) */
88
89         " test $1, %2                   \n\t" /* check for odd samples */
90         " je 2f                         \n\t"
91
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"
98         " add $2, %0                    \n\t"
99         MOD_ADD ($1, %5)
100
101         "2:                             \n\t"
102         " sar $1, %2                    \n\t" /* prepare for processing 2 samples at a time */
103         " test $1, %2                   \n\t"
104         " je 4f                         \n\t"
105
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 | */
111         " add $4, %0                    \n\t"
112         MOD_ADD ($2, %5)
113
114         "4:                             \n\t"
115         " sar $1, %2                    \n\t" /* prepare for processing 4 samples at a time */
116         " test $1, %2                   \n\t"
117         " je 6f                         \n\t"
118
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 | */
127         " add $8, %0                    \n\t"
128         MOD_ADD ($4, %5)
129
130         "6:                             \n\t"
131         " sar $1, %2                    \n\t" /* prepare for processing 8 samples at a time */
132         " cmp $0, %2                    \n\t"
133         " je 8f                         \n\t"
134
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 | */
144         " add $16, %0                   \n\t"
145         MOD_ADD ($8, %5)
146         " dec %2                        \n\t"
147         " jne 7b                        \n\t"
148         "8:                             \n\t"
149
150         : "+r" (samples), "+r" (volumes), "+r" (length), "=&D" (channel), "=&r" (temp)
151 #if defined (__i386__)
152         : "m" (channels)
153 #else
154         : "r" ((pa_reg_x86)channels)
155 #endif
156         : "cc"
157     );
158 }
159
160 static void pa_volume_s16re_sse2(int16_t *samples, const int32_t *volumes, unsigned channels, unsigned length) {
161     pa_reg_x86 channel, temp;
162
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. */
166     if (channels < 8)
167         channels = channel_overread_table[channels];
168
169     __asm__ __volatile__ (
170         " xor %3, %3                    \n\t"
171         " sar $1, %2                    \n\t" /* length /= sizeof (int16_t) */
172
173         " test $1, %2                   \n\t" /* check for odd samples */
174         " je 2f                         \n\t"
175
176         " movd (%q1, %3, 4), %%xmm0     \n\t" /* |  v0h  |  v0l  | */
177         " movw (%0), %w4                \n\t" /*     ..  |   p0  | */
178         " rorw $8, %w4                  \n\t"
179         " movd %4, %%xmm1               \n\t"
180         VOLUME_32x16 (%%xmm1, %%xmm0)
181         " movd %%xmm0, %4               \n\t" /*     ..  | p0*v0 | */
182         " rorw $8, %w4                  \n\t"
183         " movw %w4, (%0)                \n\t"
184         " add $2, %0                    \n\t"
185         MOD_ADD ($1, %5)
186
187         "2:                             \n\t"
188         " sar $1, %2                    \n\t" /* prepare for processing 2 samples at a time */
189         " test $1, %2                   \n\t"
190         " je 4f                         \n\t"
191
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   | */
195         SWAP_16 (%%xmm1)
196         VOLUME_32x16 (%%xmm1, %%xmm0)
197         SWAP_16 (%%xmm0)
198         " movd %%xmm0, (%0)             \n\t" /*              .. | p1*v1 | p0*v0 | */
199         " add $4, %0                    \n\t"
200         MOD_ADD ($2, %5)
201
202         "4:                             \n\t"
203         " sar $1, %2                    \n\t" /* prepare for processing 4 samples at a time */
204         " test $1, %2                   \n\t"
205         " je 6f                         \n\t"
206
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   | */
213         SWAP_16 (%%xmm1)
214         VOLUME_32x16 (%%xmm1, %%xmm0)
215         SWAP_16 (%%xmm0)
216         " movq %%xmm0, (%0)             \n\t" /*              .. | p3*v3 .. p0*v0 | */
217         " add $8, %0                    \n\t"
218         MOD_ADD ($4, %5)
219
220         "6:                             \n\t"
221         " sar $1, %2                    \n\t" /* prepare for processing 8 samples at a time */
222         " cmp $0, %2                    \n\t"
223         " je 8f                         \n\t"
224
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 | */
236         " add $16, %0                   \n\t"
237         MOD_ADD ($8, %5)
238         " dec %2                        \n\t"
239         " jne 7b                        \n\t"
240         "8:                             \n\t"
241
242         : "+r" (samples), "+r" (volumes), "+r" (length), "=&D" (channel), "=&r" (temp)
243 #if defined (__i386__)
244         : "m" (channels)
245 #else
246         : "r" ((pa_reg_x86)channels)
247 #endif
248         : "cc"
249     );
250 }
251
252 #endif /* (!defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && defined (__i386__)) || defined (__amd64__) */
253
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.");
258
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);
261     }
262 #endif /* (!defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && defined (__i386__)) || defined (__amd64__) */
263 }