]> code.delx.au - pulseaudio/blob - src/pulsecore/svolume_arm.c
a028b8272fc1bc73d9f6dcc59562ce554592071e
[pulseaudio] / src / pulsecore / svolume_arm.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, 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 <pulse/timeval.h>
28 #include <pulsecore/random.h>
29 #include <pulsecore/macro.h>
30 #include <pulsecore/g711.h>
31 #include <pulsecore/core-util.h>
32
33 #include "cpu-arm.h"
34
35 #include "sample-util.h"
36 #include "endianmacros.h"
37
38 #if defined (__arm__) && defined (HAVE_ARMV6)
39
40 #define MOD_INC() \
41 " subs r0, r6, %2 \n\t" \
42 " itt cs \n\t" \
43 " addcs r0, %1 \n\t" \
44 " movcs r6, r0 \n\t"
45
46 static void
47 pa_volume_s16ne_arm (int16_t *samples, int32_t *volumes, unsigned channels, unsigned length)
48 {
49 int32_t *ve;
50
51 /* Channels must be at least 4, and always a multiple of the original number.
52 * This is also the max amount we overread the volume array, which should
53 * have enough padding. */
54 channels = channels == 3 ? 6 : PA_MAX (4U, channels);
55 ve = volumes + channels;
56
57 __asm__ __volatile__ (
58 " mov r6, %1 \n\t"
59 " mov %3, %3, LSR #1 \n\t" /* length /= sizeof (int16_t) */
60 " tst %3, #1 \n\t" /* check for odd samples */
61 " beq 2f \n\t"
62
63 "1: \n\t"
64 " ldr r0, [r6], #4 \n\t" /* odd samples volumes */
65 " ldrh r2, [%0] \n\t"
66
67 " smulwb r0, r0, r2 \n\t"
68 " ssat r0, #16, r0 \n\t"
69
70 " strh r0, [%0], #2 \n\t"
71
72 MOD_INC()
73
74 "2: \n\t"
75 " mov %3, %3, LSR #1 \n\t"
76 " tst %3, #1 \n\t" /* check for odd samples */
77 " beq 4f \n\t"
78
79 "3: \n\t"
80 " ldrd r2, [r6], #8 \n\t" /* 2 samples at a time */
81 " ldr r0, [%0] \n\t"
82
83 " smulwt r2, r2, r0 \n\t"
84 " smulwb r3, r3, r0 \n\t"
85
86 " ssat r2, #16, r2 \n\t"
87 " ssat r3, #16, r3 \n\t"
88
89 " pkhbt r0, r3, r2, LSL #16 \n\t"
90 " str r0, [%0], #4 \n\t"
91
92 MOD_INC()
93
94 "4: \n\t"
95 " movs %3, %3, LSR #1 \n\t"
96 " beq 6f \n\t"
97
98 "5: \n\t"
99 " ldrd r2, [r6], #8 \n\t" /* 4 samples at a time */
100 " ldrd r4, [r6], #8 \n\t"
101 " ldrd r0, [%0] \n\t"
102
103 " smulwt r2, r2, r0 \n\t"
104 " smulwb r3, r3, r0 \n\t"
105 " smulwt r4, r4, r1 \n\t"
106 " smulwb r5, r5, r1 \n\t"
107
108 " ssat r2, #16, r2 \n\t"
109 " ssat r3, #16, r3 \n\t"
110 " ssat r4, #16, r4 \n\t"
111 " ssat r5, #16, r5 \n\t"
112
113 " pkhbt r0, r3, r2, LSL #16 \n\t"
114 " pkhbt r1, r5, r4, LSL #16 \n\t"
115 " strd r0, [%0], #8 \n\t"
116
117 MOD_INC()
118
119 " subs %3, %3, #1 \n\t"
120 " bne 5b \n\t"
121 "6: \n\t"
122
123 : "+r" (samples), "+r" (volumes), "+r" (ve), "+r" (length)
124 :
125 : "r6", "r5", "r4", "r3", "r2", "r1", "r0", "cc"
126 );
127 }
128
129 #undef RUN_TEST
130
131 #ifdef RUN_TEST
132 #define CHANNELS 2
133 #define SAMPLES 1022
134 #define TIMES 1000
135 #define PADDING 16
136
137 static void run_test (void) {
138 int16_t samples[SAMPLES];
139 int16_t samples_ref[SAMPLES];
140 int16_t samples_orig[SAMPLES];
141 int32_t volumes[CHANNELS + PADDING];
142 int i, j, padding;
143 pa_do_volume_func_t func;
144 pa_usec_t start, stop;
145
146 func = pa_get_volume_func (PA_SAMPLE_S16NE);
147
148 printf ("checking ARM %zd\n", sizeof (samples));
149
150 pa_random (samples, sizeof (samples));
151 memcpy (samples_ref, samples, sizeof (samples));
152 memcpy (samples_orig, samples, sizeof (samples));
153
154 for (i = 0; i < CHANNELS; i++)
155 volumes[i] = PA_CLAMP_VOLUME(rand() >> 1);
156 for (padding = 0; padding < PADDING; padding++, i++)
157 volumes[i] = volumes[padding];
158
159 func (samples_ref, volumes, CHANNELS, sizeof (samples));
160 pa_volume_s16ne_arm (samples, volumes, CHANNELS, sizeof (samples));
161 for (i = 0; i < SAMPLES; i++) {
162 if (samples[i] != samples_ref[i]) {
163 printf ("%d: %04x != %04x (%04x * %04x)\n", i, samples[i], samples_ref[i],
164 samples_orig[i], volumes[i % CHANNELS]);
165 }
166 }
167
168 start = pa_rtclock_now();
169 for (j = 0; j < TIMES; j++) {
170 memcpy (samples, samples_orig, sizeof (samples));
171 pa_volume_s16ne_arm (samples, volumes, CHANNELS, sizeof (samples));
172 }
173 stop = pa_rtclock_now();
174 pa_log_info("ARM: %llu usec.", (long long unsigned int) (stop - start));
175
176 start = pa_rtclock_now();
177 for (j = 0; j < TIMES; j++) {
178 memcpy (samples_ref, samples_orig, sizeof (samples));
179 func (samples_ref, volumes, CHANNELS, sizeof (samples));
180 }
181 stop = pa_rtclock_now();
182 pa_log_info("ref: %llu usec.", (long long unsigned int) (stop - start));
183 }
184 #endif
185
186 #endif /* defined (__arm__) && defined (HAVE_ARMV6) */
187
188
189 void pa_volume_func_init_arm (pa_cpu_arm_flag_t flags) {
190 #if defined (__arm__) && defined (HAVE_ARMV6)
191 pa_log_info("Initialising ARM optimized functions.");
192
193 #ifdef RUN_TEST
194 run_test ();
195 #endif
196
197 pa_set_volume_func (PA_SAMPLE_S16NE, (pa_do_volume_func_t) pa_volume_s16ne_arm);
198 #endif /* defined (__arm__) && defined (HAVE_ARMV6) */
199 }