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 "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
28
29 #include <stdlib.h>
30
31 // TODO(bjorn/kma): Consolidate function pairs (e.g. combine
32 // WebRtcSpl_MaxAbsValueW16C and WebRtcSpl_MaxAbsIndexW16 into a single one.)
33 // TODO(kma): Move the next six functions into min_max_operations_c.c.
34
35 // Maximum absolute value of word16 vector. C version for generic platforms.
WebRtcSpl_MaxAbsValueW16C(const int16_t * vector,int length)36 int16_t WebRtcSpl_MaxAbsValueW16C(const int16_t* vector, int length) {
37 int i = 0, absolute = 0, maximum = 0;
38
39 if (vector == NULL || length <= 0) {
40 return -1;
41 }
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,int length)60 int32_t WebRtcSpl_MaxAbsValueW32C(const int32_t* vector, int 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 int i = 0;
66
67 if (vector == NULL || length <= 0) {
68 return -1;
69 }
70
71 for (i = 0; i < length; i++) {
72 absolute = abs((int)vector[i]);
73 if (absolute > maximum) {
74 maximum = absolute;
75 }
76 }
77
78 maximum = WEBRTC_SPL_MIN(maximum, WEBRTC_SPL_WORD32_MAX);
79
80 return (int32_t)maximum;
81 }
82
83 // Maximum value of word16 vector. C version for generic platforms.
WebRtcSpl_MaxValueW16C(const int16_t * vector,int length)84 int16_t WebRtcSpl_MaxValueW16C(const int16_t* vector, int length) {
85 int16_t maximum = WEBRTC_SPL_WORD16_MIN;
86 int i = 0;
87
88 if (vector == NULL || length <= 0) {
89 return maximum;
90 }
91
92 for (i = 0; i < length; i++) {
93 if (vector[i] > maximum)
94 maximum = vector[i];
95 }
96 return maximum;
97 }
98
99 // Maximum value of word32 vector. C version for generic platforms.
WebRtcSpl_MaxValueW32C(const int32_t * vector,int length)100 int32_t WebRtcSpl_MaxValueW32C(const int32_t* vector, int length) {
101 int32_t maximum = WEBRTC_SPL_WORD32_MIN;
102 int i = 0;
103
104 if (vector == NULL || length <= 0) {
105 return maximum;
106 }
107
108 for (i = 0; i < length; i++) {
109 if (vector[i] > maximum)
110 maximum = vector[i];
111 }
112 return maximum;
113 }
114
115 // Minimum value of word16 vector. C version for generic platforms.
WebRtcSpl_MinValueW16C(const int16_t * vector,int length)116 int16_t WebRtcSpl_MinValueW16C(const int16_t* vector, int length) {
117 int16_t minimum = WEBRTC_SPL_WORD16_MAX;
118 int i = 0;
119
120 if (vector == NULL || length <= 0) {
121 return minimum;
122 }
123
124 for (i = 0; i < length; i++) {
125 if (vector[i] < minimum)
126 minimum = vector[i];
127 }
128 return minimum;
129 }
130
131 // Minimum value of word32 vector. C version for generic platforms.
WebRtcSpl_MinValueW32C(const int32_t * vector,int length)132 int32_t WebRtcSpl_MinValueW32C(const int32_t* vector, int length) {
133 int32_t minimum = WEBRTC_SPL_WORD32_MAX;
134 int i = 0;
135
136 if (vector == NULL || length <= 0) {
137 return minimum;
138 }
139
140 for (i = 0; i < length; i++) {
141 if (vector[i] < minimum)
142 minimum = vector[i];
143 }
144 return minimum;
145 }
146
147 // Index of maximum absolute value in a word16 vector.
WebRtcSpl_MaxAbsIndexW16(const int16_t * vector,int length)148 int WebRtcSpl_MaxAbsIndexW16(const int16_t* vector, int length) {
149 // Use type int for local variables, to accomodate the value of abs(-32768).
150
151 int i = 0, absolute = 0, maximum = 0, index = 0;
152
153 if (vector == NULL || length <= 0) {
154 return -1;
155 }
156
157 for (i = 0; i < length; i++) {
158 absolute = abs((int)vector[i]);
159
160 if (absolute > maximum) {
161 maximum = absolute;
162 index = i;
163 }
164 }
165
166 return index;
167 }
168
169 // Index of maximum value in a word16 vector.
WebRtcSpl_MaxIndexW16(const int16_t * vector,int length)170 int WebRtcSpl_MaxIndexW16(const int16_t* vector, int length) {
171 int i = 0, index = 0;
172 int16_t maximum = WEBRTC_SPL_WORD16_MIN;
173
174 if (vector == NULL || length <= 0) {
175 return -1;
176 }
177
178 for (i = 0; i < length; i++) {
179 if (vector[i] > maximum) {
180 maximum = vector[i];
181 index = i;
182 }
183 }
184
185 return index;
186 }
187
188 // Index of maximum value in a word32 vector.
WebRtcSpl_MaxIndexW32(const int32_t * vector,int length)189 int WebRtcSpl_MaxIndexW32(const int32_t* vector, int length) {
190 int i = 0, index = 0;
191 int32_t maximum = WEBRTC_SPL_WORD32_MIN;
192
193 if (vector == NULL || length <= 0) {
194 return -1;
195 }
196
197 for (i = 0; i < length; i++) {
198 if (vector[i] > maximum) {
199 maximum = vector[i];
200 index = i;
201 }
202 }
203
204 return index;
205 }
206
207 // Index of minimum value in a word16 vector.
WebRtcSpl_MinIndexW16(const int16_t * vector,int length)208 int WebRtcSpl_MinIndexW16(const int16_t* vector, int length) {
209 int i = 0, index = 0;
210 int16_t minimum = WEBRTC_SPL_WORD16_MAX;
211
212 if (vector == NULL || length <= 0) {
213 return -1;
214 }
215
216 for (i = 0; i < length; i++) {
217 if (vector[i] < minimum) {
218 minimum = vector[i];
219 index = i;
220 }
221 }
222
223 return index;
224 }
225
226 // Index of minimum value in a word32 vector.
WebRtcSpl_MinIndexW32(const int32_t * vector,int length)227 int WebRtcSpl_MinIndexW32(const int32_t* vector, int length) {
228 int i = 0, index = 0;
229 int32_t minimum = WEBRTC_SPL_WORD32_MAX;
230
231 if (vector == NULL || length <= 0) {
232 return -1;
233 }
234
235 for (i = 0; i < length; i++) {
236 if (vector[i] < minimum) {
237 minimum = vector[i];
238 index = i;
239 }
240 }
241
242 return index;
243 }
244