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 the implementation of functions
13 * WebRtcSpl_MaxAbsValueW16C()
14 * WebRtcSpl_MaxAbsValueW32C()
15 * WebRtcSpl_MaxValueW16C()
16 * WebRtcSpl_MaxValueW32C()
17 * WebRtcSpl_MinValueW16C()
18 * WebRtcSpl_MinValueW32C()
19 * WebRtcSpl_MaxAbsIndexW16()
20 * WebRtcSpl_MaxIndexW16()
21 * WebRtcSpl_MaxIndexW32()
22 * WebRtcSpl_MinIndexW16()
23 * WebRtcSpl_MinIndexW32()
24 *
25 */
26
27 #include <stdlib.h>
28
29 #include "rtc_base/checks.h"
30 #include "common_audio/signal_processing/include/signal_processing_library.h"
31
32 // TODO(bjorn/kma): Consolidate function pairs (e.g. combine
33 // WebRtcSpl_MaxAbsValueW16C and WebRtcSpl_MaxAbsIndexW16 into a single one.)
34 // TODO(kma): Move the next six functions into min_max_operations_c.c.
35
36 // Maximum absolute value of word16 vector. C version for generic platforms.
WebRtcSpl_MaxAbsValueW16C(const int16_t * vector,size_t length)37 int16_t WebRtcSpl_MaxAbsValueW16C(const int16_t* vector, size_t length) {
38 size_t i = 0;
39 int absolute = 0, maximum = 0;
40
41 RTC_DCHECK_GT(length, 0);
42
43 for (i = 0; i < length; i++) {
44 absolute = abs((int)vector[i]);
45
46 if (absolute > maximum) {
47 maximum = absolute;
48 }
49 }
50
51 // Guard the case for abs(-32768).
52 if (maximum > WEBRTC_SPL_WORD16_MAX) {
53 maximum = WEBRTC_SPL_WORD16_MAX;
54 }
55
56 return (int16_t)maximum;
57 }
58
59 // Maximum absolute value of word32 vector. C version for generic platforms.
WebRtcSpl_MaxAbsValueW32C(const int32_t * vector,size_t length)60 int32_t WebRtcSpl_MaxAbsValueW32C(const int32_t* vector, size_t length) {
61 // Use uint32_t for the local variables, to accommodate the return value
62 // of abs(0x80000000), which is 0x80000000.
63
64 uint32_t absolute = 0, maximum = 0;
65 size_t i = 0;
66
67 RTC_DCHECK_GT(length, 0);
68
69 for (i = 0; i < length; i++) {
70 absolute = abs((int)vector[i]);
71 if (absolute > maximum) {
72 maximum = absolute;
73 }
74 }
75
76 maximum = WEBRTC_SPL_MIN(maximum, WEBRTC_SPL_WORD32_MAX);
77
78 return (int32_t)maximum;
79 }
80
81 // Maximum value of word16 vector. C version for generic platforms.
WebRtcSpl_MaxValueW16C(const int16_t * vector,size_t length)82 int16_t WebRtcSpl_MaxValueW16C(const int16_t* vector, size_t length) {
83 int16_t maximum = WEBRTC_SPL_WORD16_MIN;
84 size_t i = 0;
85
86 RTC_DCHECK_GT(length, 0);
87
88 for (i = 0; i < length; i++) {
89 if (vector[i] > maximum)
90 maximum = vector[i];
91 }
92 return maximum;
93 }
94
95 // Maximum value of word32 vector. C version for generic platforms.
WebRtcSpl_MaxValueW32C(const int32_t * vector,size_t length)96 int32_t WebRtcSpl_MaxValueW32C(const int32_t* vector, size_t length) {
97 int32_t maximum = WEBRTC_SPL_WORD32_MIN;
98 size_t i = 0;
99
100 RTC_DCHECK_GT(length, 0);
101
102 for (i = 0; i < length; i++) {
103 if (vector[i] > maximum)
104 maximum = vector[i];
105 }
106 return maximum;
107 }
108
109 // Minimum value of word16 vector. C version for generic platforms.
WebRtcSpl_MinValueW16C(const int16_t * vector,size_t length)110 int16_t WebRtcSpl_MinValueW16C(const int16_t* vector, size_t length) {
111 int16_t minimum = WEBRTC_SPL_WORD16_MAX;
112 size_t i = 0;
113
114 RTC_DCHECK_GT(length, 0);
115
116 for (i = 0; i < length; i++) {
117 if (vector[i] < minimum)
118 minimum = vector[i];
119 }
120 return minimum;
121 }
122
123 // Minimum value of word32 vector. C version for generic platforms.
WebRtcSpl_MinValueW32C(const int32_t * vector,size_t length)124 int32_t WebRtcSpl_MinValueW32C(const int32_t* vector, size_t length) {
125 int32_t minimum = WEBRTC_SPL_WORD32_MAX;
126 size_t i = 0;
127
128 RTC_DCHECK_GT(length, 0);
129
130 for (i = 0; i < length; i++) {
131 if (vector[i] < minimum)
132 minimum = vector[i];
133 }
134 return minimum;
135 }
136
137 // Index of maximum absolute value in a word16 vector.
WebRtcSpl_MaxAbsIndexW16(const int16_t * vector,size_t length)138 size_t WebRtcSpl_MaxAbsIndexW16(const int16_t* vector, size_t length) {
139 // Use type int for local variables, to accomodate the value of abs(-32768).
140
141 size_t i = 0, index = 0;
142 int absolute = 0, maximum = 0;
143
144 RTC_DCHECK_GT(length, 0);
145
146 for (i = 0; i < length; i++) {
147 absolute = abs((int)vector[i]);
148
149 if (absolute > maximum) {
150 maximum = absolute;
151 index = i;
152 }
153 }
154
155 return index;
156 }
157
WebRtcSpl_MaxAbsElementW16(const int16_t * vector,size_t length)158 int16_t WebRtcSpl_MaxAbsElementW16(const int16_t* vector, size_t length) {
159 int16_t min_val, max_val;
160 WebRtcSpl_MinMaxW16(vector, length, &min_val, &max_val);
161 if (min_val == max_val || min_val < -max_val) {
162 return min_val;
163 }
164 return max_val;
165 }
166
167 // Index of maximum value in a word16 vector.
WebRtcSpl_MaxIndexW16(const int16_t * vector,size_t length)168 size_t WebRtcSpl_MaxIndexW16(const int16_t* vector, size_t length) {
169 size_t i = 0, index = 0;
170 int16_t maximum = WEBRTC_SPL_WORD16_MIN;
171
172 RTC_DCHECK_GT(length, 0);
173
174 for (i = 0; i < length; i++) {
175 if (vector[i] > maximum) {
176 maximum = vector[i];
177 index = i;
178 }
179 }
180
181 return index;
182 }
183
184 // Index of maximum value in a word32 vector.
WebRtcSpl_MaxIndexW32(const int32_t * vector,size_t length)185 size_t WebRtcSpl_MaxIndexW32(const int32_t* vector, size_t length) {
186 size_t i = 0, index = 0;
187 int32_t maximum = WEBRTC_SPL_WORD32_MIN;
188
189 RTC_DCHECK_GT(length, 0);
190
191 for (i = 0; i < length; i++) {
192 if (vector[i] > maximum) {
193 maximum = vector[i];
194 index = i;
195 }
196 }
197
198 return index;
199 }
200
201 // Index of minimum value in a word16 vector.
WebRtcSpl_MinIndexW16(const int16_t * vector,size_t length)202 size_t WebRtcSpl_MinIndexW16(const int16_t* vector, size_t length) {
203 size_t i = 0, index = 0;
204 int16_t minimum = WEBRTC_SPL_WORD16_MAX;
205
206 RTC_DCHECK_GT(length, 0);
207
208 for (i = 0; i < length; i++) {
209 if (vector[i] < minimum) {
210 minimum = vector[i];
211 index = i;
212 }
213 }
214
215 return index;
216 }
217
218 // Index of minimum value in a word32 vector.
WebRtcSpl_MinIndexW32(const int32_t * vector,size_t length)219 size_t WebRtcSpl_MinIndexW32(const int32_t* vector, size_t length) {
220 size_t i = 0, index = 0;
221 int32_t minimum = WEBRTC_SPL_WORD32_MAX;
222
223 RTC_DCHECK_GT(length, 0);
224
225 for (i = 0; i < length; i++) {
226 if (vector[i] < minimum) {
227 minimum = vector[i];
228 index = i;
229 }
230 }
231
232 return index;
233 }
234
235 // Finds both the minimum and maximum elements in an array of 16-bit integers.
WebRtcSpl_MinMaxW16(const int16_t * vector,size_t length,int16_t * min_val,int16_t * max_val)236 void WebRtcSpl_MinMaxW16(const int16_t* vector, size_t length,
237 int16_t* min_val, int16_t* max_val) {
238 #if defined(WEBRTC_HAS_NEON)
239 return WebRtcSpl_MinMaxW16Neon(vector, length, min_val, max_val);
240 #else
241 int16_t minimum = WEBRTC_SPL_WORD16_MAX;
242 int16_t maximum = WEBRTC_SPL_WORD16_MIN;
243 size_t i = 0;
244
245 RTC_DCHECK_GT(length, 0);
246
247 for (i = 0; i < length; i++) {
248 if (vector[i] < minimum)
249 minimum = vector[i];
250 if (vector[i] > maximum)
251 maximum = vector[i];
252 }
253 *min_val = minimum;
254 *max_val = maximum;
255 #endif
256 }
257