vector_scaling_operations.c (5192B)
1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 /* 12 * This file contains implementations of the functions 13 * WebRtcSpl_VectorBitShiftW16() 14 * WebRtcSpl_VectorBitShiftW32() 15 * WebRtcSpl_VectorBitShiftW32ToW16() 16 * WebRtcSpl_ScaleVector() 17 * WebRtcSpl_ScaleVectorWithSat() 18 * WebRtcSpl_ScaleAndAddVectors() 19 * WebRtcSpl_ScaleAndAddVectorsWithRoundC() 20 */ 21 22 #include "common_audio/signal_processing/include/signal_processing_library.h" 23 24 void WebRtcSpl_VectorBitShiftW16(int16_t* res, 25 size_t length, 26 const int16_t* in, 27 int16_t right_shifts) { 28 size_t i; 29 30 if (right_shifts > 0) { 31 for (i = length; i > 0; i--) { 32 (*res++) = ((*in++) >> right_shifts); 33 } 34 } else { 35 for (i = length; i > 0; i--) { 36 (*res++) = ((*in++) * (1 << (-right_shifts))); 37 } 38 } 39 } 40 41 void WebRtcSpl_VectorBitShiftW32(int32_t* out_vector, 42 size_t vector_length, 43 const int32_t* in_vector, 44 int16_t right_shifts) { 45 size_t i; 46 47 if (right_shifts > 0) { 48 for (i = vector_length; i > 0; i--) { 49 (*out_vector++) = ((*in_vector++) >> right_shifts); 50 } 51 } else { 52 for (i = vector_length; i > 0; i--) { 53 (*out_vector++) = ((*in_vector++) << (-right_shifts)); 54 } 55 } 56 } 57 58 void WebRtcSpl_VectorBitShiftW32ToW16(int16_t* out, 59 size_t length, 60 const int32_t* in, 61 int right_shifts) { 62 size_t i; 63 int32_t tmp_w32; 64 65 if (right_shifts >= 0) { 66 for (i = length; i > 0; i--) { 67 tmp_w32 = (*in++) >> right_shifts; 68 (*out++) = WebRtcSpl_SatW32ToW16(tmp_w32); 69 } 70 } else { 71 int left_shifts = -right_shifts; 72 for (i = length; i > 0; i--) { 73 tmp_w32 = (*in++) << left_shifts; 74 (*out++) = WebRtcSpl_SatW32ToW16(tmp_w32); 75 } 76 } 77 } 78 79 void WebRtcSpl_ScaleVector(const int16_t* in_vector, 80 int16_t* out_vector, 81 int16_t gain, 82 size_t in_vector_length, 83 int16_t right_shifts) { 84 // Performs vector operation: out_vector = (gain*in_vector)>>right_shifts 85 size_t i; 86 const int16_t* inptr; 87 int16_t* outptr; 88 89 inptr = in_vector; 90 outptr = out_vector; 91 92 for (i = 0; i < in_vector_length; i++) { 93 *outptr++ = (int16_t)((*inptr++ * gain) >> right_shifts); 94 } 95 } 96 97 void WebRtcSpl_ScaleVectorWithSat(const int16_t* in_vector, 98 int16_t* out_vector, 99 int16_t gain, 100 size_t in_vector_length, 101 int16_t right_shifts) { 102 // Performs vector operation: out_vector = (gain*in_vector)>>right_shifts 103 size_t i; 104 const int16_t* inptr; 105 int16_t* outptr; 106 107 inptr = in_vector; 108 outptr = out_vector; 109 110 for (i = 0; i < in_vector_length; i++) { 111 *outptr++ = WebRtcSpl_SatW32ToW16((*inptr++ * gain) >> right_shifts); 112 } 113 } 114 115 void WebRtcSpl_ScaleAndAddVectors(const int16_t* in1, 116 int16_t gain1, 117 int shift1, 118 const int16_t* in2, 119 int16_t gain2, 120 int shift2, 121 int16_t* out, 122 size_t vector_length) { 123 // Performs vector operation: out = (gain1*in1)>>shift1 + (gain2*in2)>>shift2 124 size_t i; 125 const int16_t* in1ptr; 126 const int16_t* in2ptr; 127 int16_t* outptr; 128 129 in1ptr = in1; 130 in2ptr = in2; 131 outptr = out; 132 133 for (i = 0; i < vector_length; i++) { 134 *outptr++ = (int16_t)((gain1 * *in1ptr++) >> shift1) + 135 (int16_t)((gain2 * *in2ptr++) >> shift2); 136 } 137 } 138 139 // C version of WebRtcSpl_ScaleAndAddVectorsWithRound() for generic platforms. 140 int WebRtcSpl_ScaleAndAddVectorsWithRoundC(const int16_t* in_vector1, 141 int16_t in_vector1_scale, 142 const int16_t* in_vector2, 143 int16_t in_vector2_scale, 144 int right_shifts, 145 int16_t* out_vector, 146 size_t length) { 147 size_t i = 0; 148 int round_value = (1 << right_shifts) >> 1; 149 150 if (in_vector1 == NULL || in_vector2 == NULL || out_vector == NULL || 151 length == 0 || right_shifts < 0) { 152 return -1; 153 } 154 155 for (i = 0; i < length; i++) { 156 out_vector[i] = 157 (int16_t)((in_vector1[i] * in_vector1_scale + 158 in_vector2[i] * in_vector2_scale + round_value) >> 159 right_shifts); 160 } 161 162 return 0; 163 }