blob: 656a3125bbeb4acc2a1a5409a3d06f51ca2404f9 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
niklase@google.com470e71d2011-07-07 08:21:25 +000011// This header file includes the inline functions in
12// the fix point signal processing library.
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#ifndef COMMON_AUDIO_SIGNAL_PROCESSING_INCLUDE_SPL_INL_H_
15#define COMMON_AUDIO_SIGNAL_PROCESSING_INCLUDE_SPL_INL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/compile_assert_c.h"
kwiberg729b21f2016-06-02 04:02:12 -070018
19extern const int8_t kWebRtcSpl_CountLeadingZeros32_Table[64];
20
21// Don't call this directly except in tests!
22static __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!
39static __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.
46static __inline int WebRtcSpl_CountLeadingZeros32(uint32_t n) {
47#ifdef __GNUC__
kjellandere0ab0ad2017-04-10 23:21:43 -070048 RTC_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t));
kwiberg729b21f2016-06-02 04:02:12 -070049 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.
56static __inline int WebRtcSpl_CountLeadingZeros64(uint64_t n) {
57#ifdef __GNUC__
kjellandere0ab0ad2017-04-10 23:21:43 -070058 RTC_COMPILE_ASSERT(sizeof(unsigned long long) == sizeof(uint64_t)); // NOLINT
kwiberg729b21f2016-06-02 04:02:12 -070059 return n == 0 ? 64 : __builtin_clzll(n);
60#else
61 return WebRtcSpl_CountLeadingZeros64_NotBuiltin(n);
62#endif
63}
64
kma@webrtc.org94771cb2012-08-28 04:09:50 +000065#ifdef WEBRTC_ARCH_ARM_V7
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020066#include "common_audio/signal_processing/include/spl_inl_armv7.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000067#else
68
andrew@webrtc.org8bf755d2013-09-18 17:40:46 +000069#if defined(MIPS32_LE)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020070#include "common_audio/signal_processing/include/spl_inl_mips.h"
andrew@webrtc.org8bf755d2013-09-18 17:40:46 +000071#endif
72
73#if !defined(MIPS_DSP_R1_LE)
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000074static __inline int16_t WebRtcSpl_SatW32ToW16(int32_t value32) {
Yves Gerey665174f2018-06-19 15:03:05 +020075 int16_t out16 = (int16_t)value32;
kma@google.com961885a2011-09-26 16:35:25 +000076
77 if (value32 > 32767)
78 out16 = 32767;
79 else if (value32 < -32768)
80 out16 = -32768;
81
82 return out16;
83}
84
kwibergbca568b2016-05-23 04:07:00 -070085static __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);
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +000089
kwibergbca568b2016-05-23 04:07:00 -070090 // 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;
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +000095 }
kwibergbca568b2016-05-23 04:07:00 -070096 return sum;
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +000097}
98
kwibergbca568b2016-05-23 04:07:00 -070099static __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);
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +0000103
kwibergbca568b2016-05-23 04:07:00 -0700104 // 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;
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +0000109 }
kwibergbca568b2016-05-23 04:07:00 -0700110 return diff;
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +0000111}
112
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000113static __inline int16_t WebRtcSpl_AddSatW16(int16_t a, int16_t b) {
Yves Gerey665174f2018-06-19 15:03:05 +0200114 return WebRtcSpl_SatW32ToW16((int32_t)a + (int32_t)b);
niklase@google.com470e71d2011-07-07 08:21:25 +0000115}
116
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000117static __inline int16_t WebRtcSpl_SubSatW16(int16_t var1, int16_t var2) {
Yves Gerey665174f2018-06-19 15:03:05 +0200118 return WebRtcSpl_SatW32ToW16((int32_t)var1 - (int32_t)var2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000119}
andrew@webrtc.org8bf755d2013-09-18 17:40:46 +0000120#endif // #if !defined(MIPS_DSP_R1_LE)
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
andrew@webrtc.org8bf755d2013-09-18 17:40:46 +0000122#if !defined(MIPS32_LE)
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000123static __inline int16_t WebRtcSpl_GetSizeInBits(uint32_t n) {
kwiberg729b21f2016-06-02 04:02:12 -0700124 return 32 - WebRtcSpl_CountLeadingZeros32(n);
niklase@google.com470e71d2011-07-07 08:21:25 +0000125}
126
kwiberg729b21f2016-06-02 04:02:12 -0700127// Return the number of steps a can be left-shifted without overflow,
128// or 0 if a == 0.
bjornv@webrtc.org3cbd6c22014-09-04 13:21:44 +0000129static __inline int16_t WebRtcSpl_NormW32(int32_t a) {
kwiberg729b21f2016-06-02 04:02:12 -0700130 return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a < 0 ? ~a : a) - 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131}
132
kwiberg729b21f2016-06-02 04:02:12 -0700133// Return the number of steps a can be left-shifted without overflow,
134// or 0 if a == 0.
bjornv@webrtc.org3cbd6c22014-09-04 13:21:44 +0000135static __inline int16_t WebRtcSpl_NormU32(uint32_t a) {
kwiberg729b21f2016-06-02 04:02:12 -0700136 return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a);
niklase@google.com470e71d2011-07-07 08:21:25 +0000137}
138
kwiberg729b21f2016-06-02 04:02:12 -0700139// Return the number of steps a can be left-shifted without overflow,
140// or 0 if a == 0.
bjornv@webrtc.org3cbd6c22014-09-04 13:21:44 +0000141static __inline int16_t WebRtcSpl_NormW16(int16_t a) {
kwiberg729b21f2016-06-02 04:02:12 -0700142 const int32_t a32 = a;
143 return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a < 0 ? ~a32 : a32) - 17;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144}
145
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000146static __inline int32_t WebRtc_MulAccumW16(int16_t a, int16_t b, int32_t c) {
kma@webrtc.orga58224f2011-10-05 16:44:11 +0000147 return (a * b + c);
148}
andrew@webrtc.org8bf755d2013-09-18 17:40:46 +0000149#endif // #if !defined(MIPS32_LE)
kma@webrtc.orga58224f2011-10-05 16:44:11 +0000150
kma@webrtc.org94771cb2012-08-28 04:09:50 +0000151#endif // WEBRTC_ARCH_ARM_V7
kma@google.com0ada4102011-09-09 16:23:50 +0000152
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200153#endif // COMMON_AUDIO_SIGNAL_PROCESSING_INCLUDE_SPL_INL_H_