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 <assert.h>
28 #include <stdlib.h>
29
30 #include "webrtc/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 assert(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 assert(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 assert(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 assert(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 assert(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 assert(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 assert(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
158 // Index of maximum value in a word16 vector.
WebRtcSpl_MaxIndexW16(const int16_t * vector,size_t length)159 size_t WebRtcSpl_MaxIndexW16(const int16_t* vector, size_t length) {
160 size_t i = 0, index = 0;
161 int16_t maximum = WEBRTC_SPL_WORD16_MIN;
162
163 assert(length > 0);
164
165 for (i = 0; i < length; i++) {
166 if (vector[i] > maximum) {
167 maximum = vector[i];
168 index = i;
169 }
170 }
171
172 return index;
173 }
174
175 // Index of maximum value in a word32 vector.
WebRtcSpl_MaxIndexW32(const int32_t * vector,size_t length)176 size_t WebRtcSpl_MaxIndexW32(const int32_t* vector, size_t length) {
177 size_t i = 0, index = 0;
178 int32_t maximum = WEBRTC_SPL_WORD32_MIN;
179
180 assert(length > 0);
181
182 for (i = 0; i < length; i++) {
183 if (vector[i] > maximum) {
184 maximum = vector[i];
185 index = i;
186 }
187 }
188
189 return index;
190 }
191
192 // Index of minimum value in a word16 vector.
WebRtcSpl_MinIndexW16(const int16_t * vector,size_t length)193 size_t WebRtcSpl_MinIndexW16(const int16_t* vector, size_t length) {
194 size_t i = 0, index = 0;
195 int16_t minimum = WEBRTC_SPL_WORD16_MAX;
196
197 assert(length > 0);
198
199 for (i = 0; i < length; i++) {
200 if (vector[i] < minimum) {
201 minimum = vector[i];
202 index = i;
203 }
204 }
205
206 return index;
207 }
208
209 // Index of minimum value in a word32 vector.
WebRtcSpl_MinIndexW32(const int32_t * vector,size_t length)210 size_t WebRtcSpl_MinIndexW32(const int32_t* vector, size_t length) {
211 size_t i = 0, index = 0;
212 int32_t minimum = WEBRTC_SPL_WORD32_MAX;
213
214 assert(length > 0);
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