]> code.delx.au - pulseaudio/blob - src/pulsecore/remap_mmx.c
remap: add MMX mono to stereo
[pulseaudio] / src / pulsecore / remap_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.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 " movq (%1), %%mm0 \n\t" \
38 " movq 8(%1), %%mm2 \n\t" \
39 " movq 16(%1), %%mm4 \n\t" \
40 " movq 24(%1), %%mm6 \n\t" \
41 " movq %%mm0, %%mm1 \n\t" \
42 " movq %%mm2, %%mm3 \n\t" \
43 " movq %%mm4, %%mm5 \n\t" \
44 " movq %%mm6, %%mm7 \n\t"
45
46 #define UNPACK_SAMPLES(s) \
47 " punpckl"#s" %%mm0, %%mm0 \n\t" \
48 " punpckh"#s" %%mm1, %%mm1 \n\t" \
49 " punpckl"#s" %%mm2, %%mm2 \n\t" \
50 " punpckh"#s" %%mm3, %%mm3 \n\t" \
51 " punpckl"#s" %%mm4, %%mm4 \n\t" \
52 " punpckh"#s" %%mm5, %%mm5 \n\t" \
53 " punpckl"#s" %%mm6, %%mm6 \n\t" \
54 " punpckh"#s" %%mm7, %%mm7 \n\t" \
55
56 #define STORE_SAMPLES \
57 " movq %%mm0, (%0) \n\t" \
58 " movq %%mm1, 8(%0) \n\t" \
59 " movq %%mm2, 16(%0) \n\t" \
60 " movq %%mm3, 24(%0) \n\t" \
61 " movq %%mm4, 32(%0) \n\t" \
62 " movq %%mm5, 40(%0) \n\t" \
63 " movq %%mm6, 48(%0) \n\t" \
64 " movq %%mm7, 56(%0) \n\t" \
65 " add $32, %1 \n\t" \
66 " add $64, %0 \n\t"
67
68 #define HANDLE_SINGLE(s) \
69 " movd (%1), %%mm0 \n\t" \
70 " movq %%mm0, %%mm1 \n\t" \
71 " punpckl"#s" %%mm0, %%mm0 \n\t" \
72 " movq %%mm0, (%0) \n\t" \
73 " add $4, %1 \n\t" \
74 " add $8, %0 \n\t"
75
76 static void remap_mono_to_stereo_mmx (pa_remap_t *m, void *dst, const void *src, unsigned n) {
77 pa_reg_x86 temp;
78
79 switch (*m->format) {
80 case PA_SAMPLE_FLOAT32NE:
81 {
82 __asm__ __volatile__ (
83 " mov %3, %2 \n\t"
84 " sar $3, %2 \n\t" /* prepare for processing 8 samples at a time */
85 " cmp $0, %2 \n\t"
86 " je 2f \n\t"
87
88 "1: \n\t" /* do samples in groups of 8 */
89 LOAD_SAMPLES
90 UNPACK_SAMPLES(dq)
91 STORE_SAMPLES
92 " dec %2 \n\t"
93 " jne 1b \n\t"
94
95 "2: \n\t"
96 " mov %3, %2 \n\t"
97 " and $7, %2 \n\t" /* prepare for processing the remaining samples */
98 " je 4f \n\t"
99
100 "3: \n\t"
101 HANDLE_SINGLE(dq)
102 " dec %2 \n\t"
103 " jne 3b \n\t"
104
105 "4: \n\t"
106 " emms \n\t"
107
108 : "+r" (dst), "+r" (src), "=&r" (temp)
109 : "r" ((pa_reg_x86)n)
110 : "cc"
111 );
112 break;
113 }
114 case PA_SAMPLE_S16NE:
115 {
116 __asm__ __volatile__ (
117 " mov %3, %2 \n\t"
118 " sar $3, %2 \n\t" /* prepare for processing 8 samples at a time */
119 " cmp $0, %2 \n\t"
120 " je 2f \n\t"
121
122 "1: \n\t" /* do samples in groups of 16 */
123 LOAD_SAMPLES
124 UNPACK_SAMPLES(wd)
125 STORE_SAMPLES
126 " dec %2 \n\t"
127 " jne 1b \n\t"
128
129 "2: \n\t"
130 " mov %3, %2 \n\t"
131 " and $7, %2 \n\t" /* prepare for processing the remaining samples */
132 " je 4f \n\t"
133
134 "3: \n\t"
135 HANDLE_SINGLE(wd)
136 " dec %2 \n\t"
137 " jne 3b \n\t"
138
139 "4: \n\t"
140 " emms \n\t"
141
142 : "+r" (dst), "+r" (src), "=&r" (temp)
143 : "r" ((pa_reg_x86)n)
144 : "cc"
145 );
146 break;
147 }
148 default:
149 pa_assert_not_reached();
150 }
151 }
152
153 /* set the function that will execute the remapping based on the matrices */
154 static void init_remap_mmx (pa_remap_t *m) {
155 unsigned n_oc, n_ic;
156
157 n_oc = m->o_ss->channels;
158 n_ic = m->i_ss->channels;
159
160 /* find some common channel remappings, fall back to full matrix operation. */
161 if (n_ic == 1 && n_oc == 2 &&
162 m->map_table_f[0][0] >= 1.0 && m->map_table_f[1][0] >= 1.0) {
163 m->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo_mmx;
164 pa_log_info("Using MMX mono to stereo remapping");
165 }
166 }
167
168 void pa_remap_func_init_mmx (pa_cpu_x86_flag_t flags) {
169 #if defined (__i386__) || defined (__amd64__)
170 pa_log_info("Initialising MMX optimized remappers.");
171
172 pa_set_init_remap_func ((pa_init_remap_func_t) init_remap_mmx);
173 #endif /* defined (__i386__) || defined (__amd64__) */
174 }