spl_inl.h revision ff137edc5321c8e8de01ccfcd873ca13d3d4b01c
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_V7
19#include "spl_inl_armv7.h"
20#else
21
22static __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
33static __inline WebRtc_Word16 WebRtcSpl_AddSatW16(WebRtc_Word16 a,
34                                                  WebRtc_Word16 b) {
35  return WebRtcSpl_SatW32ToW16((WebRtc_Word32) a + (WebRtc_Word32) b);
36}
37
38static __inline WebRtc_Word16 WebRtcSpl_SubSatW16(WebRtc_Word16 var1,
39                                                  WebRtc_Word16 var2) {
40  return WebRtcSpl_SatW32ToW16((WebRtc_Word32) var1 - (WebRtc_Word32) var2);
41}
42
43static __inline WebRtc_Word16 WebRtcSpl_GetSizeInBits(WebRtc_UWord32 n) {
44  int bits;
45
46  if (0xFFFF0000 & n) {
47    bits = 16;
48  } else {
49    bits = 0;
50  }
51  if (0x0000FF00 & (n >> bits)) bits += 8;
52  if (0x000000F0 & (n >> bits)) bits += 4;
53  if (0x0000000C & (n >> bits)) bits += 2;
54  if (0x00000002 & (n >> bits)) bits += 1;
55  if (0x00000001 & (n >> bits)) bits += 1;
56
57  return bits;
58}
59
60static __inline int WebRtcSpl_NormW32(WebRtc_Word32 a) {
61  int zeros;
62
63  if (a == 0) {
64    return 0;
65  }
66  else if (a < 0) {
67    a = ~a;
68  }
69
70  if (!(0xFFFF8000 & a)) {
71    zeros = 16;
72  } else {
73    zeros = 0;
74  }
75  if (!(0xFF800000 & (a << zeros))) zeros += 8;
76  if (!(0xF8000000 & (a << zeros))) zeros += 4;
77  if (!(0xE0000000 & (a << zeros))) zeros += 2;
78  if (!(0xC0000000 & (a << zeros))) zeros += 1;
79
80  return zeros;
81}
82
83static __inline int WebRtcSpl_NormU32(WebRtc_UWord32 a) {
84  int zeros;
85
86  if (a == 0) return 0;
87
88  if (!(0xFFFF0000 & a)) {
89    zeros = 16;
90  } else {
91    zeros = 0;
92  }
93  if (!(0xFF000000 & (a << zeros))) zeros += 8;
94  if (!(0xF0000000 & (a << zeros))) zeros += 4;
95  if (!(0xC0000000 & (a << zeros))) zeros += 2;
96  if (!(0x80000000 & (a << zeros))) zeros += 1;
97
98  return zeros;
99}
100
101static __inline int WebRtcSpl_NormW16(WebRtc_Word16 a) {
102  int zeros;
103
104  if (a == 0) {
105    return 0;
106  }
107  else if (a < 0) {
108    a = ~a;
109  }
110
111  if (!(0xFF80 & a)) {
112    zeros = 8;
113  } else {
114    zeros = 0;
115  }
116  if (!(0xF800 & (a << zeros))) zeros += 4;
117  if (!(0xE000 & (a << zeros))) zeros += 2;
118  if (!(0xC000 & (a << zeros))) zeros += 1;
119
120  return zeros;
121}
122
123static __inline int32_t WebRtc_MulAccumW16(int16_t a,
124                                          int16_t b,
125                                          int32_t c) {
126  return (a * b + c);
127}
128
129#endif  // WEBRTC_ARCH_ARM_V7
130
131// The following functions have no optimized versions.
132// TODO(kma): Consider saturating add/sub instructions in X86 platform.
133static __inline WebRtc_Word32 WebRtcSpl_AddSatW32(WebRtc_Word32 l_var1,
134                                                  WebRtc_Word32 l_var2) {
135  WebRtc_Word32 l_sum;
136
137  // Perform long addition
138  l_sum = l_var1 + l_var2;
139
140  if (l_var1 < 0) {  // Check for underflow.
141    if ((l_var2 < 0) && (l_sum >= 0)) {
142        l_sum = (WebRtc_Word32)0x80000000;
143    }
144  } else {  // Check for overflow.
145    if ((l_var2 > 0) && (l_sum < 0)) {
146        l_sum = (WebRtc_Word32)0x7FFFFFFF;
147    }
148  }
149
150  return l_sum;
151}
152
153static __inline WebRtc_Word32 WebRtcSpl_SubSatW32(WebRtc_Word32 l_var1,
154                                                  WebRtc_Word32 l_var2) {
155  WebRtc_Word32 l_diff;
156
157  // Perform subtraction.
158  l_diff = l_var1 - l_var2;
159
160  if (l_var1 < 0) {  // Check for underflow.
161    if ((l_var2 > 0) && (l_diff > 0)) {
162      l_diff = (WebRtc_Word32)0x80000000;
163    }
164  } else {  // Check for overflow.
165    if ((l_var2 < 0) && (l_diff < 0)) {
166      l_diff = (WebRtc_Word32)0x7FFFFFFF;
167    }
168  }
169
170  return l_diff;
171}
172
173#endif  // WEBRTC_SPL_SPL_INL_H_
174