core: always allow volume setting with single-channel pa_cvolume
[platform/upstream/pulseaudio.git] / src / pulsecore / remap_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.com>
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 <string.h>
28
29 #include <pulse/sample.h>
30 #include <pulsecore/log.h>
31 #include <pulsecore/macro.h>
32
33 #include "cpu-x86.h"
34 #include "remap.h"
35
36 #define LOAD_SAMPLES                                   \
37                 " movdqu (%1), %%xmm0           \n\t"  \
38                 " movdqu 16(%1), %%xmm2         \n\t"  \
39                 " movdqu 32(%1), %%xmm4         \n\t"  \
40                 " movdqu 48(%1), %%xmm6         \n\t"  \
41                 " movdqa %%xmm0, %%xmm1         \n\t"  \
42                 " movdqa %%xmm2, %%xmm3         \n\t"  \
43                 " movdqa %%xmm4, %%xmm5         \n\t"  \
44                 " movdqa %%xmm6, %%xmm7         \n\t"
45
46 #define UNPACK_SAMPLES(s)                              \
47                 " punpckl"#s" %%xmm0, %%xmm0    \n\t"  \
48                 " punpckh"#s" %%xmm1, %%xmm1    \n\t"  \
49                 " punpckl"#s" %%xmm2, %%xmm2    \n\t"  \
50                 " punpckh"#s" %%xmm3, %%xmm3    \n\t"  \
51                 " punpckl"#s" %%xmm4, %%xmm4    \n\t"  \
52                 " punpckh"#s" %%xmm5, %%xmm5    \n\t"  \
53                 " punpckl"#s" %%xmm6, %%xmm6    \n\t"  \
54                 " punpckh"#s" %%xmm7, %%xmm7    \n\t"
55
56 #define STORE_SAMPLES                                  \
57                 " movdqu %%xmm0, (%0)           \n\t"  \
58                 " movdqu %%xmm1, 16(%0)         \n\t"  \
59                 " movdqu %%xmm2, 32(%0)         \n\t"  \
60                 " movdqu %%xmm3, 48(%0)         \n\t"  \
61                 " movdqu %%xmm4, 64(%0)         \n\t"  \
62                 " movdqu %%xmm5, 80(%0)         \n\t"  \
63                 " movdqu %%xmm6, 96(%0)         \n\t"  \
64                 " movdqu %%xmm7, 112(%0)        \n\t"  \
65                 " add $64, %1                   \n\t"  \
66                 " add $128, %0                  \n\t"
67
68 #define HANDLE_SINGLE(s)                               \
69                 " movd (%1), %%xmm0             \n\t"  \
70                 " punpckl"#s" %%xmm0, %%xmm0    \n\t"  \
71                 " movq %%xmm0, (%0)             \n\t"  \
72                 " add $4, %1                    \n\t"  \
73                 " add $8, %0                    \n\t"
74
75 #define MONO_TO_STEREO(s)                               \
76                 " mov %3, %2                    \n\t"   \
77                 " sar $4, %2                    \n\t"   \
78                 " cmp $0, %2                    \n\t"   \
79                 " je 2f                         \n\t"   \
80                 "1:                             \n\t"   \
81                 LOAD_SAMPLES                            \
82                 UNPACK_SAMPLES(s)                       \
83                 STORE_SAMPLES                           \
84                 " dec %2                        \n\t"   \
85                 " jne 1b                        \n\t"   \
86                 "2:                             \n\t"   \
87                 " mov %3, %2                    \n\t"   \
88                 " and $15, %2                   \n\t"   \
89                 " je 4f                         \n\t"   \
90                 "3:                             \n\t"   \
91                 HANDLE_SINGLE(s)                        \
92                 " dec %2                        \n\t"   \
93                 " jne 3b                        \n\t"   \
94                 "4:                             \n\t"
95
96 static void remap_mono_to_stereo_sse (pa_remap_t *m, void *dst, const void *src, unsigned n) {
97     pa_reg_x86 temp;
98
99     switch (*m->format) {
100         case PA_SAMPLE_FLOAT32NE:
101         {
102             __asm__ __volatile__ (
103                 MONO_TO_STEREO(dq) /* do doubles to quads */
104                 : "+r" (dst), "+r" (src), "=&r" (temp)
105                 : "r" ((pa_reg_x86)n)
106                 : "cc"
107             );
108             break;
109         }
110         case PA_SAMPLE_S16NE:
111         {
112             __asm__ __volatile__ (
113                 MONO_TO_STEREO(wd) /* do words to doubles */
114                 : "+r" (dst), "+r" (src), "=&r" (temp)
115                 : "r" ((pa_reg_x86)n)
116                 : "cc"
117             );
118             break;
119         }
120         default:
121             pa_assert_not_reached();
122     }
123 }
124
125 /* set the function that will execute the remapping based on the matrices */
126 static void init_remap_sse (pa_remap_t *m) {
127     unsigned n_oc, n_ic;
128
129     n_oc = m->o_ss->channels;
130     n_ic = m->i_ss->channels;
131
132     /* find some common channel remappings, fall back to full matrix operation. */
133     if (n_ic == 1 && n_oc == 2 &&
134             m->map_table_f[0][0] >= 1.0 && m->map_table_f[1][0] >= 1.0) {
135         m->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo_sse;
136         pa_log_info("Using SSE mono to stereo remapping");
137     }
138 }
139
140 void pa_remap_func_init_sse (pa_cpu_x86_flag_t flags) {
141 #if defined (__i386__) || defined (__amd64__)
142     pa_log_info("Initialising SSE optimized remappers.");
143
144     pa_set_init_remap_func ((pa_init_remap_func_t) init_remap_sse);
145 #endif /* defined (__i386__) || defined (__amd64__) */
146 }