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_V7A
19 #include "spl_inl_armv7.h"
20 #else
21 
WebRtcSpl_SatW32ToW16(WebRtc_Word32 value32)22 static __inline WebRtc_Word16 WebRtcSpl_SatW32ToW16(WebRtc_Word32 value32) {
23   WebRtc_Word16 out16 = (WebRtc_Word16) value32;
24 
25   if (value32 > 32767)
26     out16 = 32767;
27   else if (value32 < -32768)
28     out16 = -32768;
29 
30   return out16;
31 }
32 
WebRtcSpl_AddSatW16(WebRtc_Word16 a,WebRtc_Word16 b)33 static __inline WebRtc_Word16 WebRtcSpl_AddSatW16(WebRtc_Word16 a,
34                                                   WebRtc_Word16 b) {
35   return WebRtcSpl_SatW32ToW16((WebRtc_Word32) a + (WebRtc_Word32) b);
36 }
37 
WebRtcSpl_AddSatW32(WebRtc_Word32 l_var1,WebRtc_Word32 l_var2)38 static __inline WebRtc_Word32 WebRtcSpl_AddSatW32(WebRtc_Word32 l_var1,
39                                                   WebRtc_Word32 l_var2) {
40   WebRtc_Word32 l_sum;
41 
42   // perform long addition
43   l_sum = l_var1 + l_var2;
44 
45   // check for under or overflow
46   if (WEBRTC_SPL_IS_NEG(l_var1)) {
47     if (WEBRTC_SPL_IS_NEG(l_var2) && !WEBRTC_SPL_IS_NEG(l_sum)) {
48         l_sum = (WebRtc_Word32)0x80000000;
49     }
50   } else {
51     if (!WEBRTC_SPL_IS_NEG(l_var2) && WEBRTC_SPL_IS_NEG(l_sum)) {
52         l_sum = (WebRtc_Word32)0x7FFFFFFF;
53     }
54   }
55 
56   return l_sum;
57 }
58 
WebRtcSpl_SubSatW16(WebRtc_Word16 var1,WebRtc_Word16 var2)59 static __inline WebRtc_Word16 WebRtcSpl_SubSatW16(WebRtc_Word16 var1,
60                                                   WebRtc_Word16 var2) {
61   return WebRtcSpl_SatW32ToW16((WebRtc_Word32) var1 - (WebRtc_Word32) var2);
62 }
63 
WebRtcSpl_SubSatW32(WebRtc_Word32 l_var1,WebRtc_Word32 l_var2)64 static __inline WebRtc_Word32 WebRtcSpl_SubSatW32(WebRtc_Word32 l_var1,
65                                                   WebRtc_Word32 l_var2) {
66   WebRtc_Word32 l_diff;
67 
68   // perform subtraction
69   l_diff = l_var1 - l_var2;
70 
71   // check for underflow
72   if ((l_var1 < 0) && (l_var2 > 0) && (l_diff > 0))
73     l_diff = (WebRtc_Word32)0x80000000;
74   // check for overflow
75   if ((l_var1 > 0) && (l_var2 < 0) && (l_diff < 0))
76     l_diff = (WebRtc_Word32)0x7FFFFFFF;
77 
78   return l_diff;
79 }
80 
WebRtcSpl_GetSizeInBits(WebRtc_UWord32 n)81 static __inline WebRtc_Word16 WebRtcSpl_GetSizeInBits(WebRtc_UWord32 n) {
82   int bits;
83 
84   if (0xFFFF0000 & n) {
85     bits = 16;
86   } else {
87     bits = 0;
88   }
89   if (0x0000FF00 & (n >> bits)) bits += 8;
90   if (0x000000F0 & (n >> bits)) bits += 4;
91   if (0x0000000C & (n >> bits)) bits += 2;
92   if (0x00000002 & (n >> bits)) bits += 1;
93   if (0x00000001 & (n >> bits)) bits += 1;
94 
95   return bits;
96 }
97 
WebRtcSpl_NormW32(WebRtc_Word32 a)98 static __inline int WebRtcSpl_NormW32(WebRtc_Word32 a) {
99   int zeros;
100 
101   if (a <= 0) a ^= 0xFFFFFFFF;
102 
103   if (!(0xFFFF8000 & a)) {
104     zeros = 16;
105   } else {
106     zeros = 0;
107   }
108   if (!(0xFF800000 & (a << zeros))) zeros += 8;
109   if (!(0xF8000000 & (a << zeros))) zeros += 4;
110   if (!(0xE0000000 & (a << zeros))) zeros += 2;
111   if (!(0xC0000000 & (a << zeros))) zeros += 1;
112 
113   return zeros;
114 }
115 
WebRtcSpl_NormU32(WebRtc_UWord32 a)116 static __inline int WebRtcSpl_NormU32(WebRtc_UWord32 a) {
117   int zeros;
118 
119   if (a == 0) return 0;
120 
121   if (!(0xFFFF0000 & a)) {
122     zeros = 16;
123   } else {
124     zeros = 0;
125   }
126   if (!(0xFF000000 & (a << zeros))) zeros += 8;
127   if (!(0xF0000000 & (a << zeros))) zeros += 4;
128   if (!(0xC0000000 & (a << zeros))) zeros += 2;
129   if (!(0x80000000 & (a << zeros))) zeros += 1;
130 
131   return zeros;
132 }
133 
WebRtcSpl_NormW16(WebRtc_Word16 a)134 static __inline int WebRtcSpl_NormW16(WebRtc_Word16 a) {
135   int zeros;
136 
137   if (a <= 0) a ^= 0xFFFF;
138 
139   if (!(0xFF80 & a)) {
140     zeros = 8;
141   } else {
142     zeros = 0;
143   }
144   if (!(0xF800 & (a << zeros))) zeros += 4;
145   if (!(0xE000 & (a << zeros))) zeros += 2;
146   if (!(0xC000 & (a << zeros))) zeros += 1;
147 
148   return zeros;
149 }
150 
WebRtc_MulAccumW16(int16_t a,int16_t b,int32_t c)151 static __inline int32_t WebRtc_MulAccumW16(int16_t a,
152                                           int16_t b,
153                                           int32_t c) {
154   return (a * b + c);
155 }
156 
157 #endif  // WEBRTC_ARCH_ARM_V7A
158 
159 #endif  // WEBRTC_SPL_SPL_INL_H_
160