• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, see <http://www.gnu.org/licenses/>.
19 ***/
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include <pulse/sample.h>
26 #include <pulse/volume.h>
27 #include <pulsecore/log.h>
28 #include <pulsecore/macro.h>
29 
30 #include "cpu-x86.h"
31 #include "remap.h"
32 
33 #define LOAD_SAMPLES                                   \
34                 " movq (%1), %%mm0              \n\t"  \
35                 " movq 8(%1), %%mm2             \n\t"  \
36                 " movq 16(%1), %%mm4            \n\t"  \
37                 " movq 24(%1), %%mm6            \n\t"  \
38                 " movq %%mm0, %%mm1             \n\t"  \
39                 " movq %%mm2, %%mm3             \n\t"  \
40                 " movq %%mm4, %%mm5             \n\t"  \
41                 " movq %%mm6, %%mm7             \n\t"
42 
43 #define UNPACK_SAMPLES(s)                              \
44                 " punpckl"#s" %%mm0, %%mm0      \n\t"  \
45                 " punpckh"#s" %%mm1, %%mm1      \n\t"  \
46                 " punpckl"#s" %%mm2, %%mm2      \n\t"  \
47                 " punpckh"#s" %%mm3, %%mm3      \n\t"  \
48                 " punpckl"#s" %%mm4, %%mm4      \n\t"  \
49                 " punpckh"#s" %%mm5, %%mm5      \n\t"  \
50                 " punpckl"#s" %%mm6, %%mm6      \n\t"  \
51                 " punpckh"#s" %%mm7, %%mm7      \n\t"
52 
53 #define STORE_SAMPLES                                  \
54                 " movq %%mm0, (%0)              \n\t"  \
55                 " movq %%mm1, 8(%0)             \n\t"  \
56                 " movq %%mm2, 16(%0)            \n\t"  \
57                 " movq %%mm3, 24(%0)            \n\t"  \
58                 " movq %%mm4, 32(%0)            \n\t"  \
59                 " movq %%mm5, 40(%0)            \n\t"  \
60                 " movq %%mm6, 48(%0)            \n\t"  \
61                 " movq %%mm7, 56(%0)            \n\t"  \
62                 " add $32, %1                   \n\t"  \
63                 " add $64, %0                   \n\t"
64 
65 #define HANDLE_SINGLE_dq()                            \
66                 " movd (%1), %%mm0              \n\t"  \
67                 " punpckldq %%mm0, %%mm0        \n\t"  \
68                 " movq %%mm0, (%0)              \n\t"  \
69                 " add $4, %1                    \n\t"  \
70                 " add $8, %0                    \n\t"
71 
72 #define HANDLE_SINGLE_wd()                             \
73                 " movw (%1), %w3                \n\t"  \
74                 " movd %3,  %%mm0               \n\t"  \
75                 " punpcklwd %%mm0, %%mm0        \n\t"  \
76                 " movd %%mm0, (%0)              \n\t"  \
77                 " add $2, %1                    \n\t"  \
78                 " add $4, %0                    \n\t"
79 
80 #define MONO_TO_STEREO(s,shift,mask)                   \
81                 " mov %4, %2                    \n\t"  \
82                 " sar $"#shift", %2             \n\t"  \
83                 " cmp $0, %2                    \n\t"  \
84                 " je 2f                         \n\t"  \
85                 "1:                             \n\t"  \
86                 LOAD_SAMPLES                           \
87                 UNPACK_SAMPLES(s)                      \
88                 STORE_SAMPLES                          \
89                 " dec %2                        \n\t"  \
90                 " jne 1b                        \n\t"  \
91                 "2:                             \n\t"  \
92                 " mov %4, %2                    \n\t"  \
93                 " and $"#mask", %2              \n\t"  \
94                 " je 4f                         \n\t"  \
95                 "3:                             \n\t"  \
96                 HANDLE_SINGLE_##s()                    \
97                 " dec %2                        \n\t"  \
98                 " jne 3b                        \n\t"  \
99                 "4:                             \n\t"  \
100                 " emms                          \n\t"
101 
102 #if defined (__i386__) || defined (__amd64__)
remap_mono_to_stereo_s16ne_mmx(pa_remap_t * m,int16_t * dst,const int16_t * src,unsigned n)103 static void remap_mono_to_stereo_s16ne_mmx(pa_remap_t *m, int16_t *dst, const int16_t *src, unsigned n) {
104     pa_reg_x86 temp, temp2;
105 
106     __asm__ __volatile__ (
107         MONO_TO_STEREO(wd,4,15) /* do words to doubles */
108         : "+r" (dst), "+r" (src), "=&r" (temp), "=&r" (temp2)
109         : "r" ((pa_reg_x86)n)
110         : "cc"
111     );
112 }
113 
114 /* Works for both S32NE and FLOAT32NE */
remap_mono_to_stereo_any32ne_mmx(pa_remap_t * m,float * dst,const float * src,unsigned n)115 static void remap_mono_to_stereo_any32ne_mmx(pa_remap_t *m, float *dst, const float *src, unsigned n) {
116     pa_reg_x86 temp, temp2;
117 
118     __asm__ __volatile__ (
119         MONO_TO_STEREO(dq,3,7) /* do doubles to quads */
120         : "+r" (dst), "+r" (src), "=&r" (temp), "=&r" (temp2)
121         : "r" ((pa_reg_x86)n)
122         : "cc"
123     );
124 }
125 
126 /* set the function that will execute the remapping based on the matrices */
init_remap_mmx(pa_remap_t * m)127 static void init_remap_mmx(pa_remap_t *m) {
128     unsigned n_oc, n_ic;
129 
130     n_oc = m->o_ss.channels;
131     n_ic = m->i_ss.channels;
132 
133     /* find some common channel remappings, fall back to full matrix operation. */
134     if (n_ic == 1 && n_oc == 2 &&
135             m->map_table_i[0][0] == 0x10000 && m->map_table_i[1][0] == 0x10000) {
136 
137         pa_log_info("Using MMX mono to stereo remapping");
138         pa_set_remap_func(m, (pa_do_remap_func_t) remap_mono_to_stereo_s16ne_mmx,
139             (pa_do_remap_func_t) remap_mono_to_stereo_any32ne_mmx,
140             (pa_do_remap_func_t) remap_mono_to_stereo_any32ne_mmx);
141     }
142 }
143 #endif /* defined (__i386__) || defined (__amd64__) */
144 
pa_remap_func_init_mmx(pa_cpu_x86_flag_t flags)145 void pa_remap_func_init_mmx(pa_cpu_x86_flag_t flags) {
146 #if defined (__i386__) || defined (__amd64__)
147 
148     if (flags & PA_CPU_X86_MMX) {
149         pa_log_info("Initialising MMX optimized remappers.");
150 
151         pa_set_init_remap_func((pa_init_remap_func_t) init_remap_mmx);
152     }
153 
154 #endif /* defined (__i386__) || defined (__amd64__) */
155 }
156