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 // This header file includes the inline functions in
12 // the fix point signal processing library.
13 
14 #ifndef COMMON_AUDIO_SIGNAL_PROCESSING_INCLUDE_SPL_INL_H_
15 #define COMMON_AUDIO_SIGNAL_PROCESSING_INCLUDE_SPL_INL_H_
16 
17 #include "rtc_base/compile_assert_c.h"
18 
19 extern const int8_t kWebRtcSpl_CountLeadingZeros32_Table[64];
20 
21 // Don't call this directly except in tests!
WebRtcSpl_CountLeadingZeros32_NotBuiltin(uint32_t n)22 static __inline int WebRtcSpl_CountLeadingZeros32_NotBuiltin(uint32_t n) {
23   // Normalize n by rounding up to the nearest number that is a sequence of 0
24   // bits followed by a sequence of 1 bits. This number has the same number of
25   // leading zeros as the original n. There are exactly 33 such values.
26   n |= n >> 1;
27   n |= n >> 2;
28   n |= n >> 4;
29   n |= n >> 8;
30   n |= n >> 16;
31 
32   // Multiply the modified n with a constant selected (by exhaustive search)
33   // such that each of the 33 possible values of n give a product whose 6 most
34   // significant bits are unique. Then look up the answer in the table.
35   return kWebRtcSpl_CountLeadingZeros32_Table[(n * 0x8c0b2891) >> 26];
36 }
37 
38 // Don't call this directly except in tests!
WebRtcSpl_CountLeadingZeros64_NotBuiltin(uint64_t n)39 static __inline int WebRtcSpl_CountLeadingZeros64_NotBuiltin(uint64_t n) {
40   const int leading_zeros = n >> 32 == 0 ? 32 : 0;
41   return leading_zeros + WebRtcSpl_CountLeadingZeros32_NotBuiltin(
42                              (uint32_t)(n >> (32 - leading_zeros)));
43 }
44 
45 // Returns the number of leading zero bits in the argument.
WebRtcSpl_CountLeadingZeros32(uint32_t n)46 static __inline int WebRtcSpl_CountLeadingZeros32(uint32_t n) {
47 #ifdef __GNUC__
48   RTC_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t));
49   return n == 0 ? 32 : __builtin_clz(n);
50 #else
51   return WebRtcSpl_CountLeadingZeros32_NotBuiltin(n);
52 #endif
53 }
54 
55 // Returns the number of leading zero bits in the argument.
WebRtcSpl_CountLeadingZeros64(uint64_t n)56 static __inline int WebRtcSpl_CountLeadingZeros64(uint64_t n) {
57 #ifdef __GNUC__
58   RTC_COMPILE_ASSERT(sizeof(unsigned long long) == sizeof(uint64_t));  // NOLINT
59   return n == 0 ? 64 : __builtin_clzll(n);
60 #else
61   return WebRtcSpl_CountLeadingZeros64_NotBuiltin(n);
62 #endif
63 }
64 
65 #ifdef WEBRTC_ARCH_ARM_V7
66 #include "common_audio/signal_processing/include/spl_inl_armv7.h"
67 #else
68 
69 #if defined(MIPS32_LE)
70 #include "common_audio/signal_processing/include/spl_inl_mips.h"
71 #endif
72 
73 #if !defined(MIPS_DSP_R1_LE)
WebRtcSpl_SatW32ToW16(int32_t value32)74 static __inline int16_t WebRtcSpl_SatW32ToW16(int32_t value32) {
75   int16_t out16 = (int16_t)value32;
76 
77   if (value32 > 32767)
78     out16 = 32767;
79   else if (value32 < -32768)
80     out16 = -32768;
81 
82   return out16;
83 }
84 
WebRtcSpl_AddSatW32(int32_t a,int32_t b)85 static __inline int32_t WebRtcSpl_AddSatW32(int32_t a, int32_t b) {
86   // Do the addition in unsigned numbers, since signed overflow is undefined
87   // behavior.
88   const int32_t sum = (int32_t)((uint32_t)a + (uint32_t)b);
89 
90   // a + b can't overflow if a and b have different signs. If they have the
91   // same sign, a + b also has the same sign iff it didn't overflow.
92   if ((a < 0) == (b < 0) && (a < 0) != (sum < 0)) {
93     // The direction of the overflow is obvious from the sign of a + b.
94     return sum < 0 ? INT32_MAX : INT32_MIN;
95   }
96   return sum;
97 }
98 
WebRtcSpl_SubSatW32(int32_t a,int32_t b)99 static __inline int32_t WebRtcSpl_SubSatW32(int32_t a, int32_t b) {
100   // Do the subtraction in unsigned numbers, since signed overflow is undefined
101   // behavior.
102   const int32_t diff = (int32_t)((uint32_t)a - (uint32_t)b);
103 
104   // a - b can't overflow if a and b have the same sign. If they have different
105   // signs, a - b has the same sign as a iff it didn't overflow.
106   if ((a < 0) != (b < 0) && (a < 0) != (diff < 0)) {
107     // The direction of the overflow is obvious from the sign of a - b.
108     return diff < 0 ? INT32_MAX : INT32_MIN;
109   }
110   return diff;
111 }
112 
WebRtcSpl_AddSatW16(int16_t a,int16_t b)113 static __inline int16_t WebRtcSpl_AddSatW16(int16_t a, int16_t b) {
114   return WebRtcSpl_SatW32ToW16((int32_t)a + (int32_t)b);
115 }
116 
WebRtcSpl_SubSatW16(int16_t var1,int16_t var2)117 static __inline int16_t WebRtcSpl_SubSatW16(int16_t var1, int16_t var2) {
118   return WebRtcSpl_SatW32ToW16((int32_t)var1 - (int32_t)var2);
119 }
120 #endif  // #if !defined(MIPS_DSP_R1_LE)
121 
122 #if !defined(MIPS32_LE)
WebRtcSpl_GetSizeInBits(uint32_t n)123 static __inline int16_t WebRtcSpl_GetSizeInBits(uint32_t n) {
124   return 32 - WebRtcSpl_CountLeadingZeros32(n);
125 }
126 
127 // Return the number of steps a can be left-shifted without overflow,
128 // or 0 if a == 0.
WebRtcSpl_NormW32(int32_t a)129 static __inline int16_t WebRtcSpl_NormW32(int32_t a) {
130   return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a < 0 ? ~a : a) - 1;
131 }
132 
133 // Return the number of steps a can be left-shifted without overflow,
134 // or 0 if a == 0.
WebRtcSpl_NormU32(uint32_t a)135 static __inline int16_t WebRtcSpl_NormU32(uint32_t a) {
136   return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a);
137 }
138 
139 // Return the number of steps a can be left-shifted without overflow,
140 // or 0 if a == 0.
WebRtcSpl_NormW16(int16_t a)141 static __inline int16_t WebRtcSpl_NormW16(int16_t a) {
142   const int32_t a32 = a;
143   return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a < 0 ? ~a32 : a32) - 17;
144 }
145 
WebRtc_MulAccumW16(int16_t a,int16_t b,int32_t c)146 static __inline int32_t WebRtc_MulAccumW16(int16_t a, int16_t b, int32_t c) {
147   return (a * b + c);
148 }
149 #endif  // #if !defined(MIPS32_LE)
150 
151 #endif  // WEBRTC_ARCH_ARM_V7
152 
153 #endif  // COMMON_AUDIO_SIGNAL_PROCESSING_INCLUDE_SPL_INL_H_
154