Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(401)

Side by Side Diff: webrtc/common_audio/signal_processing/include/spl_inl.h

Issue 2014023002: Add clz functions (Count number of Leading Zero bits), 32-and 64-bit variants (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@bug601787-1
Patch Set: new algorithm Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 11
12 // This header file includes the inline functions in 12 // This header file includes the inline functions in
13 // the fix point signal processing library. 13 // the fix point signal processing library.
14 14
15 #ifndef WEBRTC_SPL_SPL_INL_H_ 15 #ifndef WEBRTC_SPL_SPL_INL_H_
16 #define WEBRTC_SPL_SPL_INL_H_ 16 #define WEBRTC_SPL_SPL_INL_H_
17 17
18 #include "webrtc/system_wrappers/include/compile_assert_c.h"
19
20 extern const int8_t kWebRtcSpl_CountLeadingZeros32_Table[64];
21
22 // Don't call this directly except in tests!
23 static __inline int WebRtcSpl_CountLeadingZeros32_NotBuiltin(uint32_t n) {
24 // Normalize n by rounding up to the nearest number that is a sequence of 0
25 // bits followed by a sequence of 1 bits. This number has the same number of
26 // leading zeros as the original n. There are exactly 33 such values.
27 n |= n >> 1;
28 n |= n >> 2;
29 n |= n >> 4;
30 n |= n >> 8;
31 n |= n >> 16;
32
33 // Multiply the modified n with a constant selected (by exhaustive search)
34 // such that each of the 33 possible values of n give a product whose 6 most
35 // significant bits are unique. Then look up the answer in the table.
36 return kWebRtcSpl_CountLeadingZeros32_Table[(n * 0x8c0b2891) >> 26];
37 }
38
39 // Don't call this directly except in tests!
40 static __inline int WebRtcSpl_CountLeadingZeros64_NotBuiltin(uint64_t n) {
41 const int leading_zeros = n >> 32 == 0 ? 32 : 0;
42 return leading_zeros + WebRtcSpl_CountLeadingZeros32_NotBuiltin(
43 (uint32_t)(n >> (32 - leading_zeros)));
44 }
45
46 // Returns the number of leading zero bits in the argument.
47 static __inline int WebRtcSpl_CountLeadingZeros32(uint32_t n) {
48 #ifdef __GNUC__
49 COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t));
50 return n == 0 ? 32 : __builtin_clz(n);
51 #else
52 return WebRtcSpl_CountLeadingZeros32_NotBuiltin(n);
53 #endif
54 }
55
56 // Returns the number of leading zero bits in the argument.
57 static __inline int WebRtcSpl_CountLeadingZeros64(uint64_t n) {
58 #ifdef __GNUC__
59 COMPILE_ASSERT(sizeof(unsigned long long) == sizeof(uint64_t));
60 return n == 0 ? 64 : __builtin_clzll(n);
61 #else
62 return WebRtcSpl_CountLeadingZeros64_NotBuiltin(n);
63 #endif
64 }
65
18 #ifdef WEBRTC_ARCH_ARM_V7 66 #ifdef WEBRTC_ARCH_ARM_V7
19 #include "webrtc/common_audio/signal_processing/include/spl_inl_armv7.h" 67 #include "webrtc/common_audio/signal_processing/include/spl_inl_armv7.h"
20 #else 68 #else
21 69
22 #if defined(MIPS32_LE) 70 #if defined(MIPS32_LE)
23 #include "webrtc/common_audio/signal_processing/include/spl_inl_mips.h" 71 #include "webrtc/common_audio/signal_processing/include/spl_inl_mips.h"
24 #endif 72 #endif
25 73
26 #if !defined(MIPS_DSP_R1_LE) 74 #if !defined(MIPS_DSP_R1_LE)
27 static __inline int16_t WebRtcSpl_SatW32ToW16(int32_t value32) { 75 static __inline int16_t WebRtcSpl_SatW32ToW16(int32_t value32) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 return WebRtcSpl_SatW32ToW16((int32_t) a + (int32_t) b); 115 return WebRtcSpl_SatW32ToW16((int32_t) a + (int32_t) b);
68 } 116 }
69 117
70 static __inline int16_t WebRtcSpl_SubSatW16(int16_t var1, int16_t var2) { 118 static __inline int16_t WebRtcSpl_SubSatW16(int16_t var1, int16_t var2) {
71 return WebRtcSpl_SatW32ToW16((int32_t) var1 - (int32_t) var2); 119 return WebRtcSpl_SatW32ToW16((int32_t) var1 - (int32_t) var2);
72 } 120 }
73 #endif // #if !defined(MIPS_DSP_R1_LE) 121 #endif // #if !defined(MIPS_DSP_R1_LE)
74 122
75 #if !defined(MIPS32_LE) 123 #if !defined(MIPS32_LE)
76 static __inline int16_t WebRtcSpl_GetSizeInBits(uint32_t n) { 124 static __inline int16_t WebRtcSpl_GetSizeInBits(uint32_t n) {
77 int16_t bits; 125 return 32 - WebRtcSpl_CountLeadingZeros32(n);
78
79 if (0xFFFF0000 & n) {
80 bits = 16;
81 } else {
82 bits = 0;
83 }
84 if (0x0000FF00 & (n >> bits)) bits += 8;
85 if (0x000000F0 & (n >> bits)) bits += 4;
86 if (0x0000000C & (n >> bits)) bits += 2;
87 if (0x00000002 & (n >> bits)) bits += 1;
88 if (0x00000001 & (n >> bits)) bits += 1;
89
90 return bits;
91 } 126 }
92 127
93 static __inline int16_t WebRtcSpl_NormW32(int32_t a) { 128 static __inline int16_t WebRtcSpl_NormW32(int32_t a) {
94 int16_t zeros; 129 int16_t zeros;
95 130
96 if (a == 0) { 131 if (a == 0) {
97 return 0; 132 return 0;
98 } 133 }
99 else if (a < 0) { 134 else if (a < 0) {
100 a = ~a; 135 a = ~a;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 } 189 }
155 190
156 static __inline int32_t WebRtc_MulAccumW16(int16_t a, int16_t b, int32_t c) { 191 static __inline int32_t WebRtc_MulAccumW16(int16_t a, int16_t b, int32_t c) {
157 return (a * b + c); 192 return (a * b + c);
158 } 193 }
159 #endif // #if !defined(MIPS32_LE) 194 #endif // #if !defined(MIPS32_LE)
160 195
161 #endif // WEBRTC_ARCH_ARM_V7 196 #endif // WEBRTC_ARCH_ARM_V7
162 197
163 #endif // WEBRTC_SPL_SPL_INL_H_ 198 #endif // WEBRTC_SPL_SPL_INL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698