daemon: add nice value in service file to improve performance
[platform/upstream/pulseaudio.git] / src / pulsecore / svolume_mmx.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 /* in s: 2 int16_t samples
37  * in v: 2 int32_t volumes, fixed point 16:16
38  * out s: contains scaled and clamped int16_t samples.
39  *
40  * We calculate the high 32 bits of a 32x16 multiply which we then
41  * clamp to 16 bits. The calculation is:
42  *
43  *  vl = (v & 0xffff)
44  *  vh = (v >> 16)
45  *  s = ((s * vl) >> 16) + (s * vh);
46  *
47  * For the first multiply we have to do a sign correction as we need to
48  * multiply a signed int with an unsigned int. Hacker's delight 8-3 gives a
49  * simple formula to correct the sign of the high word after the signed
50  * multiply.
51  */
52 #define VOLUME_32x16(s,v)                  /* .. |   vh  |   vl  | */                   \
53       " pxor  %%mm4, %%mm4           \n\t" /* .. |    0  |    0  | */                   \
54       " punpcklwd %%mm4, "#s"        \n\t" /* .. |    0  |   p0  | */                   \
55       " pcmpgtw "#v", %%mm4          \n\t" /* .. |    0  | s(vl) | */                   \
56       " pand "#s", %%mm4             \n\t" /* .. |    0  |  (p0) |  (vl >> 15) & p */   \
57       " movq "#s", %%mm5             \n\t"                                              \
58       " pmulhw "#v", "#s"            \n\t" /* .. |    0  | vl*p0 | */                   \
59       " paddw %%mm4, "#s"            \n\t" /* .. |    0  | vl*p0 | + sign correct */    \
60       " pslld $16, "#s"              \n\t" /* .. | vl*p0 |   0   | */                   \
61       " psrld $16, "#v"              \n\t" /* .. |    0  |   vh  | */                   \
62       " psrad $16, "#s"              \n\t" /* .. |     vl*p0     | sign extend */       \
63       " pmaddwd %%mm5, "#v"          \n\t" /* .. |    p0 * vh    | */                   \
64       " paddd "#s", "#v"             \n\t" /* .. |    p0 * v0    | */                   \
65       " packssdw "#v", "#v"          \n\t" /* .. | p1*v1 | p0*v0 | */
66
67 /* approximately advances %3 = (%3 + a) % b. This function requires that
68  * a <= b. */
69 #define MOD_ADD(a,b) \
70       " add "#a", %3                 \n\t" \
71       " mov %3, %4                   \n\t" \
72       " sub "#b", %4                 \n\t" \
73       " cmovae %4, %3                \n\t"
74
75 /* swap 16 bits */
76 #define SWAP_16(s) \
77       " movq "#s", %%mm4             \n\t" /* .. |  h  l |  */ \
78       " psrlw $8, %%mm4              \n\t" /* .. |  0  h |  */ \
79       " psllw $8, "#s"               \n\t" /* .. |  l  0 |  */ \
80       " por %%mm4, "#s"              \n\t" /* .. |  l  h |  */
81
82 /* swap 2 registers 16 bits for better pairing */
83 #define SWAP_16_2(s1,s2) \
84       " movq "#s1", %%mm4            \n\t" /* .. |  h  l |  */ \
85       " movq "#s2", %%mm5            \n\t"                     \
86       " psrlw $8, %%mm4              \n\t" /* .. |  0  h |  */ \
87       " psrlw $8, %%mm5              \n\t"                     \
88       " psllw $8, "#s1"              \n\t" /* .. |  l  0 |  */ \
89       " psllw $8, "#s2"              \n\t"                     \
90       " por %%mm4, "#s1"             \n\t" /* .. |  l  h |  */ \
91       " por %%mm5, "#s2"             \n\t"
92
93 static void pa_volume_s16ne_mmx(int16_t *samples, const int32_t *volumes, unsigned channels, unsigned length) {
94     pa_reg_x86 channel, temp;
95
96     /* Channels must be at least 4, and always a multiple of the original number.
97      * This is also the max amount we overread the volume array, which should
98      * have enough padding. */
99     channels = channels == 3 ? 6 : PA_MAX (4U, channels);
100
101     __asm__ __volatile__ (
102         " xor %3, %3                    \n\t"
103         " sar $1, %2                    \n\t" /* length /= sizeof (int16_t) */
104
105         " test $1, %2                   \n\t" /* check for odd samples */
106         " je 2f                         \n\t"
107
108         " movd (%q1, %3, 4), %%mm0      \n\t" /* |  v0h  |  v0l  | */
109         " movw (%0), %w4                \n\t" /*     ..  |  p0   | */
110         " movd %4, %%mm1                \n\t"
111         VOLUME_32x16 (%%mm1, %%mm0)
112         " movd %%mm0, %4                \n\t" /*     ..  | p0*v0 | */
113         " movw %w4, (%0)                \n\t"
114         " add $2, %0                    \n\t"
115         MOD_ADD ($1, %5)
116
117         "2:                             \n\t"
118         " sar $1, %2                    \n\t" /* prepare for processing 2 samples at a time */
119         " test $1, %2                   \n\t" /* check for odd samples */
120         " je 4f                         \n\t"
121
122         "3:                             \n\t" /* do samples in groups of 2 */
123         " movq (%q1, %3, 4), %%mm0      \n\t" /* |  v1h  |  v1l  |  v0h  |  v0l  | */
124         " movd (%0), %%mm1              \n\t" /*              .. |   p1  |  p0   | */
125         VOLUME_32x16 (%%mm1, %%mm0)
126         " movd %%mm0, (%0)              \n\t" /*              .. | p1*v1 | p0*v0 | */
127         " add $4, %0                    \n\t"
128         MOD_ADD ($2, %5)
129
130         "4:                             \n\t"
131         " sar $1, %2                    \n\t" /* prepare for processing 4 samples at a time */
132         " cmp $0, %2                    \n\t"
133         " je 6f                         \n\t"
134
135         "5:                             \n\t" /* do samples in groups of 4 */
136         " movq (%q1, %3, 4), %%mm0      \n\t" /* |  v1h  |  v1l  |  v0h  |  v0l  | */
137         " movq 8(%q1, %3, 4), %%mm2     \n\t" /* |  v3h  |  v3l  |  v2h  |  v2l  | */
138         " movd (%0), %%mm1              \n\t" /*              .. |   p1  |  p0   | */
139         " movd 4(%0), %%mm3             \n\t" /*              .. |   p3  |  p2   | */
140         VOLUME_32x16 (%%mm1, %%mm0)
141         VOLUME_32x16 (%%mm3, %%mm2)
142         " movd %%mm0, (%0)              \n\t" /*              .. | p1*v1 | p0*v0 | */
143         " movd %%mm2, 4(%0)             \n\t" /*              .. | p3*v3 | p2*v2 | */
144         " add $8, %0                    \n\t"
145         MOD_ADD ($4, %5)
146         " dec %2                        \n\t"
147         " jne 5b                        \n\t"
148
149         "6:                             \n\t"
150         " emms                          \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_mmx(int16_t *samples, const int32_t *volumes, unsigned channels, unsigned length) {
163     pa_reg_x86 channel, temp;
164
165     /* Channels must be at least 4, 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     channels = channels == 3 ? 6 : PA_MAX (4U, channels);
169
170     __asm__ __volatile__ (
171         " xor %3, %3                    \n\t"
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 | */
177
178         " test $1, %2                   \n\t" /* check for odd samples */
179         " je 2f                         \n\t"
180
181         " movd (%q1, %3, 4), %%mm0      \n\t" /* |  v0h  |  v0l  | */
182         " movw (%0), %w4                \n\t" /*     ..  |  p0   | */
183         " rorw $8, %w4                  \n\t"
184         " movd %4, %%mm1                \n\t"
185         VOLUME_32x16 (%%mm1, %%mm0)
186         " movd %%mm0, %4                \n\t" /*     ..  | p0*v0 | */
187         " rorw $8, %w4                  \n\t"
188         " movw %w4, (%0)                \n\t"
189         " add $2, %0                    \n\t"
190         MOD_ADD ($1, %5)
191
192         "2:                             \n\t"
193         " sar $1, %2                    \n\t" /* prepare for processing 2 samples at a time */
194         " test $1, %2                   \n\t" /* check for odd samples */
195         " je 4f                         \n\t"
196
197         "3:                             \n\t" /* do samples in groups of 2 */
198         " movq (%q1, %3, 4), %%mm0      \n\t" /* |  v1h  |  v1l  |  v0h  |  v0l  | */
199         " movd (%0), %%mm1              \n\t" /*              .. |   p1  |  p0   | */
200         SWAP_16 (%%mm1)
201         VOLUME_32x16 (%%mm1, %%mm0)
202         SWAP_16 (%%mm0)
203         " movd %%mm0, (%0)              \n\t" /*              .. | p1*v1 | p0*v0 | */
204         " add $4, %0                    \n\t"
205         MOD_ADD ($2, %5)
206
207         "4:                             \n\t"
208         " sar $1, %2                    \n\t" /* prepare for processing 4 samples at a time */
209         " cmp $0, %2                    \n\t"
210         " je 6f                         \n\t"
211
212         "5:                             \n\t" /* do samples in groups of 4 */
213         " movq (%q1, %3, 4), %%mm0      \n\t" /* |  v1h  |  v1l  |  v0h  |  v0l  | */
214         " movq 8(%q1, %3, 4), %%mm2     \n\t" /* |  v3h  |  v3l  |  v2h  |  v2l  | */
215         " movd (%0), %%mm1              \n\t" /*              .. |   p1  |  p0   | */
216         " movd 4(%0), %%mm3             \n\t" /*              .. |   p3  |  p2   | */
217         SWAP_16_2 (%%mm1, %%mm3)
218         VOLUME_32x16 (%%mm1, %%mm0)
219         VOLUME_32x16 (%%mm3, %%mm2)
220         SWAP_16_2 (%%mm0, %%mm2)
221         " movd %%mm0, (%0)              \n\t" /*              .. | p1*v1 | p0*v0 | */
222         " movd %%mm2, 4(%0)             \n\t" /*              .. | p3*v3 | p2*v2 | */
223         " add $8, %0                    \n\t"
224         MOD_ADD ($4, %5)
225         " dec %2                        \n\t"
226         " jne 5b                        \n\t"
227
228         "6:                             \n\t"
229         " emms                          \n\t"
230
231         : "+r" (samples), "+r" (volumes), "+r" (length), "=&D" (channel), "=&r" (temp)
232 #if defined (__i386__)
233         : "m" (channels)
234 #else
235         : "r" ((pa_reg_x86)channels)
236 #endif
237         : "cc"
238     );
239 }
240
241 #endif /* (!defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && defined (__i386__)) || defined (__amd64__) */
242
243 void pa_volume_func_init_mmx(pa_cpu_x86_flag_t flags) {
244 #if (!defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && defined (__i386__)) || defined (__amd64__)
245     if ((flags & PA_CPU_X86_MMX) && (flags & PA_CPU_X86_CMOV)) {
246         pa_log_info("Initialising MMX optimized volume functions.");
247
248         pa_set_volume_func(PA_SAMPLE_S16NE, (pa_do_volume_func_t) pa_volume_s16ne_mmx);
249         pa_set_volume_func(PA_SAMPLE_S16RE, (pa_do_volume_func_t) pa_volume_s16re_mmx);
250     }
251 #endif /* (!defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && defined (__i386__)) || defined (__amd64__) */
252 }