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 <pulsecore/random.h> 26#include <pulsecore/macro.h> 27#include <pulsecore/endianmacros.h> 28 29#include "cpu-arm.h" 30 31#include "sample-util.h" 32 33#if defined (__arm__) && defined (HAVE_ARMV6) 34 35#define MOD_INC() \ 36 " subs r0, r6, %2 \n\t" \ 37 " itt cs \n\t" \ 38 " addcs r0, %1 \n\t" \ 39 " movcs r6, r0 \n\t" 40 41static pa_do_volume_func_t _volume_ref; 42 43static void pa_volume_s16ne_arm(int16_t *samples, const int32_t *volumes, unsigned channels, unsigned length) { 44 /* Channels must be at least 4, and always a multiple of the original number. 45 * This is also the max amount we overread the volume array, which should 46 * have enough padding. */ 47 const int32_t *ve = volumes + (channels == 3 ? 6 : PA_MAX (4U, channels)); 48 unsigned rem = PA_ALIGN((size_t) samples) - (size_t) samples; 49 50 /* Make sure we're word-aligned, else performance _really_ sucks */ 51 if (rem) { 52 _volume_ref(samples, volumes, channels, rem < length ? rem : length); 53 54 if (rem < length) { 55 length -= rem; 56 samples += rem / sizeof(*samples); 57 } else 58 return; /* we're done */ 59 } 60 61 __asm__ __volatile__ ( 62 " mov r6, %4 \n\t" /* r6 = volumes + rem */ 63 " mov %3, %3, LSR #1 \n\t" /* length /= sizeof (int16_t) */ 64 65 " cmp %3, #4 \n\t" /* check for 4+ samples */ 66 " blt 2f \n\t" 67 68 /* See final case for how the multiplication works */ 69 70 "1: \n\t" 71 " ldrd r2, [r6], #8 \n\t" /* 4 samples at a time */ 72 " ldrd r4, [r6], #8 \n\t" 73 " ldrd r0, [%0] \n\t" 74 75#ifdef WORDS_BIGENDIAN 76 " smulwt r2, r2, r0 \n\t" 77 " smulwb r3, r3, r0 \n\t" 78 " smulwt r4, r4, r1 \n\t" 79 " smulwb r5, r5, r1 \n\t" 80#else 81 " smulwb r2, r2, r0 \n\t" 82 " smulwt r3, r3, r0 \n\t" 83 " smulwb r4, r4, r1 \n\t" 84 " smulwt r5, r5, r1 \n\t" 85#endif 86 87 " ssat r2, #16, r2 \n\t" 88 " ssat r3, #16, r3 \n\t" 89 " ssat r4, #16, r4 \n\t" 90 " ssat r5, #16, r5 \n\t" 91 92#ifdef WORDS_BIGENDIAN 93 " pkhbt r0, r3, r2, LSL #16 \n\t" 94 " pkhbt r1, r5, r4, LSL #16 \n\t" 95#else 96 " pkhbt r0, r2, r3, LSL #16 \n\t" 97 " pkhbt r1, r4, r5, LSL #16 \n\t" 98#endif 99 " strd r0, [%0], #8 \n\t" 100 101 MOD_INC() 102 103 " subs %3, %3, #4 \n\t" 104 " cmp %3, #4 \n\t" 105 " bge 1b \n\t" 106 107 "2: \n\t" 108 " cmp %3, #2 \n\t" 109 " blt 3f \n\t" 110 111 " ldrd r2, [r6], #8 \n\t" /* 2 samples at a time */ 112 " ldr r0, [%0] \n\t" 113 114#ifdef WORDS_BIGENDIAN 115 " smulwt r2, r2, r0 \n\t" 116 " smulwb r3, r3, r0 \n\t" 117#else 118 " smulwb r2, r2, r0 \n\t" 119 " smulwt r3, r3, r0 \n\t" 120#endif 121 122 " ssat r2, #16, r2 \n\t" 123 " ssat r3, #16, r3 \n\t" 124 125#ifdef WORDS_BIGENDIAN 126 " pkhbt r0, r3, r2, LSL #16 \n\t" 127#else 128 " pkhbt r0, r2, r3, LSL #16 \n\t" 129#endif 130 " str r0, [%0], #4 \n\t" 131 132 MOD_INC() 133 134 " subs %3, %3, #2 \n\t" 135 136 "3: \n\t" /* check for odd # of samples */ 137 " cmp %3, #1 \n\t" 138 " bne 4f \n\t" 139 140 " ldr r0, [r6], #4 \n\t" /* r0 = volume */ 141 " ldrh r2, [%0] \n\t" /* r2 = sample */ 142 143 " smulwb r0, r0, r2 \n\t" /* r0 = (r0 * r2) >> 16 */ 144 " ssat r0, #16, r0 \n\t" /* r0 = PA_CLAMP(r0, 0x7FFF) */ 145 146 " strh r0, [%0], #2 \n\t" /* sample = r0 */ 147 148 "4: \n\t" 149 150 : "+r" (samples), "+r" (volumes), "+r" (ve), "+r" (length) 151 : "r" (volumes + ((rem / sizeof(*samples)) % channels)) 152 : "r6", "r5", "r4", "r3", "r2", "r1", "r0", "cc" 153 ); 154} 155 156#endif /* defined (__arm__) && defined (HAVE_ARMV6) */ 157 158void pa_volume_func_init_arm(pa_cpu_arm_flag_t flags) { 159#if defined (__arm__) && defined (HAVE_ARMV6) 160 pa_log_info("Initialising ARM optimized volume functions."); 161 162 if (!_volume_ref) 163 _volume_ref = pa_get_volume_func(PA_SAMPLE_S16NE); 164 165 pa_set_volume_func(PA_SAMPLE_S16NE, (pa_do_volume_func_t) pa_volume_s16ne_arm); 166#endif /* defined (__arm__) && defined (HAVE_ARMV6) */ 167} 168