1 /*
2  *  Copyright (c) 2011 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 header file includes the inline functions in
13 // the fix point signal processing library.
14 
15 #ifndef WEBRTC_SPL_SPL_INL_H_
16 #define WEBRTC_SPL_SPL_INL_H_
17 
18 #ifdef WEBRTC_ARCH_ARM_V7
19 #include "webrtc/common_audio/signal_processing/include/spl_inl_armv7.h"
20 #else
21 
22 #if defined(MIPS32_LE)
23 #include "webrtc/common_audio/signal_processing/include/spl_inl_mips.h"
24 #endif
25 
26 #if !defined(MIPS_DSP_R1_LE)
WebRtcSpl_SatW32ToW16(int32_t value32)27 static __inline int16_t WebRtcSpl_SatW32ToW16(int32_t value32) {
28   int16_t out16 = (int16_t) value32;
29 
30   if (value32 > 32767)
31     out16 = 32767;
32   else if (value32 < -32768)
33     out16 = -32768;
34 
35   return out16;
36 }
37 
WebRtcSpl_AddSatW32(int32_t l_var1,int32_t l_var2)38 static __inline int32_t WebRtcSpl_AddSatW32(int32_t l_var1, int32_t l_var2) {
39   int32_t l_sum;
40 
41   // Perform long addition
42   l_sum = l_var1 + l_var2;
43 
44   if (l_var1 < 0) {  // Check for underflow.
45     if ((l_var2 < 0) && (l_sum >= 0)) {
46         l_sum = (int32_t)0x80000000;
47     }
48   } else {  // Check for overflow.
49     if ((l_var2 > 0) && (l_sum < 0)) {
50         l_sum = (int32_t)0x7FFFFFFF;
51     }
52   }
53 
54   return l_sum;
55 }
56 
WebRtcSpl_SubSatW32(int32_t l_var1,int32_t l_var2)57 static __inline int32_t WebRtcSpl_SubSatW32(int32_t l_var1, int32_t l_var2) {
58   int32_t l_diff;
59 
60   // Perform subtraction.
61   l_diff = l_var1 - l_var2;
62 
63   if (l_var1 < 0) {  // Check for underflow.
64     if ((l_var2 > 0) && (l_diff > 0)) {
65       l_diff = (int32_t)0x80000000;
66     }
67   } else {  // Check for overflow.
68     if ((l_var2 < 0) && (l_diff < 0)) {
69       l_diff = (int32_t)0x7FFFFFFF;
70     }
71   }
72 
73   return l_diff;
74 }
75 
WebRtcSpl_AddSatW16(int16_t a,int16_t b)76 static __inline int16_t WebRtcSpl_AddSatW16(int16_t a, int16_t b) {
77   return WebRtcSpl_SatW32ToW16((int32_t) a + (int32_t) b);
78 }
79 
WebRtcSpl_SubSatW16(int16_t var1,int16_t var2)80 static __inline int16_t WebRtcSpl_SubSatW16(int16_t var1, int16_t var2) {
81   return WebRtcSpl_SatW32ToW16((int32_t) var1 - (int32_t) var2);
82 }
83 #endif  // #if !defined(MIPS_DSP_R1_LE)
84 
85 #if !defined(MIPS32_LE)
WebRtcSpl_GetSizeInBits(uint32_t n)86 static __inline int16_t WebRtcSpl_GetSizeInBits(uint32_t n) {
87   int16_t bits;
88 
89   if (0xFFFF0000 & n) {
90     bits = 16;
91   } else {
92     bits = 0;
93   }
94   if (0x0000FF00 & (n >> bits)) bits += 8;
95   if (0x000000F0 & (n >> bits)) bits += 4;
96   if (0x0000000C & (n >> bits)) bits += 2;
97   if (0x00000002 & (n >> bits)) bits += 1;
98   if (0x00000001 & (n >> bits)) bits += 1;
99 
100   return bits;
101 }
102 
WebRtcSpl_NormW32(int32_t a)103 static __inline int16_t WebRtcSpl_NormW32(int32_t a) {
104   int16_t zeros;
105 
106   if (a == 0) {
107     return 0;
108   }
109   else if (a < 0) {
110     a = ~a;
111   }
112 
113   if (!(0xFFFF8000 & a)) {
114     zeros = 16;
115   } else {
116     zeros = 0;
117   }
118   if (!(0xFF800000 & (a << zeros))) zeros += 8;
119   if (!(0xF8000000 & (a << zeros))) zeros += 4;
120   if (!(0xE0000000 & (a << zeros))) zeros += 2;
121   if (!(0xC0000000 & (a << zeros))) zeros += 1;
122 
123   return zeros;
124 }
125 
WebRtcSpl_NormU32(uint32_t a)126 static __inline int16_t WebRtcSpl_NormU32(uint32_t a) {
127   int16_t zeros;
128 
129   if (a == 0) return 0;
130 
131   if (!(0xFFFF0000 & a)) {
132     zeros = 16;
133   } else {
134     zeros = 0;
135   }
136   if (!(0xFF000000 & (a << zeros))) zeros += 8;
137   if (!(0xF0000000 & (a << zeros))) zeros += 4;
138   if (!(0xC0000000 & (a << zeros))) zeros += 2;
139   if (!(0x80000000 & (a << zeros))) zeros += 1;
140 
141   return zeros;
142 }
143 
WebRtcSpl_NormW16(int16_t a)144 static __inline int16_t WebRtcSpl_NormW16(int16_t a) {
145   int16_t zeros;
146 
147   if (a == 0) {
148     return 0;
149   }
150   else if (a < 0) {
151     a = ~a;
152   }
153 
154   if (!(0xFF80 & a)) {
155     zeros = 8;
156   } else {
157     zeros = 0;
158   }
159   if (!(0xF800 & (a << zeros))) zeros += 4;
160   if (!(0xE000 & (a << zeros))) zeros += 2;
161   if (!(0xC000 & (a << zeros))) zeros += 1;
162 
163   return zeros;
164 }
165 
WebRtc_MulAccumW16(int16_t a,int16_t b,int32_t c)166 static __inline int32_t WebRtc_MulAccumW16(int16_t a, int16_t b, int32_t c) {
167   return (a * b + c);
168 }
169 #endif  // #if !defined(MIPS32_LE)
170 
171 #endif  // WEBRTC_ARCH_ARM_V7
172 
173 #endif  // WEBRTC_SPL_SPL_INL_H_
174