blob: c5aec272de30efe08f276c5f6d31ada082750d64 [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
11
12// This header file includes the inline functions in
13// the fix point signal processing library.
14
oprypin67fdb802017-03-09 06:25:06 -080015#ifndef WEBRTC_COMMON_AUDIO_SIGNAL_PROCESSING_INCLUDE_SPL_INL_H_
16#define WEBRTC_COMMON_AUDIO_SIGNAL_PROCESSING_INCLUDE_SPL_INL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000017
Edward Lemurc20978e2017-07-06 19:44:34 +020018#include "webrtc/rtc_base/compile_assert_c.h"
kwiberg729b21f2016-06-02 04:02:12 -070019
20extern const int8_t kWebRtcSpl_CountLeadingZeros32_Table[64];
21
22// Don't call this directly except in tests!
23static __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!
40static __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.
47static __inline int WebRtcSpl_CountLeadingZeros32(uint32_t n) {
48#ifdef __GNUC__
kjellandere0ab0ad2017-04-10 23:21:43 -070049 RTC_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t));
kwiberg729b21f2016-06-02 04:02:12 -070050 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.
57static __inline int WebRtcSpl_CountLeadingZeros64(uint64_t n) {
58#ifdef __GNUC__
kjellandere0ab0ad2017-04-10 23:21:43 -070059 RTC_COMPILE_ASSERT(sizeof(unsigned long long) == sizeof(uint64_t)); // NOLINT
kwiberg729b21f2016-06-02 04:02:12 -070060 return n == 0 ? 64 : __builtin_clzll(n);
61#else
62 return WebRtcSpl_CountLeadingZeros64_NotBuiltin(n);
63#endif
64}
65
kma@webrtc.org94771cb2012-08-28 04:09:50 +000066#ifdef WEBRTC_ARCH_ARM_V7
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000067#include "webrtc/common_audio/signal_processing/include/spl_inl_armv7.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000068#else
69
andrew@webrtc.org8bf755d2013-09-18 17:40:46 +000070#if defined(MIPS32_LE)
71#include "webrtc/common_audio/signal_processing/include/spl_inl_mips.h"
72#endif
73
74#if !defined(MIPS_DSP_R1_LE)
pbos@webrtc.orgb0913072013-04-09 16:40:28 +000075static __inline int16_t WebRtcSpl_SatW32ToW16(int32_t value32) {
76 int16_t out16 = (int16_t) value32;
kma@google.com961885a2011-09-26 16:35:25 +000077
78 if (value32 > 32767)
79 out16 = 32767;
80 else if (value32 < -32768)
81 out16 = -32768;
82
83 return out16;
84}
85
kwibergbca568b2016-05-23 04:07:00 -070086static __inline int32_t WebRtcSpl_AddSatW32(int32_t a, int32_t b) {
87 // Do the addition in unsigned numbers, since signed overflow is undefined
88 // behavior.
89 const int32_t sum = (int32_t)((uint32_t)a + (uint32_t)b);
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +000090
kwibergbca568b2016-05-23 04:07:00 -070091 // a + b can't overflow if a and b have different signs. If they have the
92 // same sign, a + b also has the same sign iff it didn't overflow.
93 if ((a < 0) == (b < 0) && (a < 0) != (sum < 0)) {
94 // The direction of the overflow is obvious from the sign of a + b.
95 return sum < 0 ? INT32_MAX : INT32_MIN;
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +000096 }
kwibergbca568b2016-05-23 04:07:00 -070097 return sum;
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +000098}
99
kwibergbca568b2016-05-23 04:07:00 -0700100static __inline int32_t WebRtcSpl_SubSatW32(int32_t a, int32_t b) {
101 // Do the subtraction in unsigned numbers, since signed overflow is undefined
102 // behavior.
103 const int32_t diff = (int32_t)((uint32_t)a - (uint32_t)b);
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +0000104
kwibergbca568b2016-05-23 04:07:00 -0700105 // a - b can't overflow if a and b have the same sign. If they have different
106 // signs, a - b has the same sign as a iff it didn't overflow.
107 if ((a < 0) != (b < 0) && (a < 0) != (diff < 0)) {
108 // The direction of the overflow is obvious from the sign of a - b.
109 return diff < 0 ? INT32_MAX : INT32_MIN;
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +0000110 }
kwibergbca568b2016-05-23 04:07:00 -0700111 return diff;
bjornv@webrtc.org1de0cc42014-08-26 09:36:25 +0000112}
113
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000114static __inline int16_t WebRtcSpl_AddSatW16(int16_t a, int16_t b) {
115 return WebRtcSpl_SatW32ToW16((int32_t) a + (int32_t) b);
niklase@google.com470e71d2011-07-07 08:21:25 +0000116}
117
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000118static __inline int16_t WebRtcSpl_SubSatW16(int16_t var1, int16_t var2) {
119 return WebRtcSpl_SatW32ToW16((int32_t) var1 - (int32_t) var2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120}
andrew@webrtc.org8bf755d2013-09-18 17:40:46 +0000121#endif // #if !defined(MIPS_DSP_R1_LE)
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
andrew@webrtc.org8bf755d2013-09-18 17:40:46 +0000123#if !defined(MIPS32_LE)
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000124static __inline int16_t WebRtcSpl_GetSizeInBits(uint32_t n) {
kwiberg729b21f2016-06-02 04:02:12 -0700125 return 32 - WebRtcSpl_CountLeadingZeros32(n);
niklase@google.com470e71d2011-07-07 08:21:25 +0000126}
127
kwiberg729b21f2016-06-02 04:02:12 -0700128// Return the number of steps a can be left-shifted without overflow,
129// or 0 if a == 0.
bjornv@webrtc.org3cbd6c22014-09-04 13:21:44 +0000130static __inline int16_t WebRtcSpl_NormW32(int32_t a) {
kwiberg729b21f2016-06-02 04:02:12 -0700131 return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a < 0 ? ~a : a) - 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132}
133
kwiberg729b21f2016-06-02 04:02:12 -0700134// Return the number of steps a can be left-shifted without overflow,
135// or 0 if a == 0.
bjornv@webrtc.org3cbd6c22014-09-04 13:21:44 +0000136static __inline int16_t WebRtcSpl_NormU32(uint32_t a) {
kwiberg729b21f2016-06-02 04:02:12 -0700137 return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138}
139
kwiberg729b21f2016-06-02 04:02:12 -0700140// Return the number of steps a can be left-shifted without overflow,
141// or 0 if a == 0.
bjornv@webrtc.org3cbd6c22014-09-04 13:21:44 +0000142static __inline int16_t WebRtcSpl_NormW16(int16_t a) {
kwiberg729b21f2016-06-02 04:02:12 -0700143 const int32_t a32 = a;
144 return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a < 0 ? ~a32 : a32) - 17;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145}
146
pbos@webrtc.orgb0913072013-04-09 16:40:28 +0000147static __inline int32_t WebRtc_MulAccumW16(int16_t a, int16_t b, int32_t c) {
kma@webrtc.orga58224f2011-10-05 16:44:11 +0000148 return (a * b + c);
149}
andrew@webrtc.org8bf755d2013-09-18 17:40:46 +0000150#endif // #if !defined(MIPS32_LE)
kma@webrtc.orga58224f2011-10-05 16:44:11 +0000151
kma@webrtc.org94771cb2012-08-28 04:09:50 +0000152#endif // WEBRTC_ARCH_ARM_V7
kma@google.com0ada4102011-09-09 16:23:50 +0000153
oprypin67fdb802017-03-09 06:25:06 -0800154#endif // WEBRTC_COMMON_AUDIO_SIGNAL_PROCESSING_INCLUDE_SPL_INL_H_