|
1 #ifndef __RESAMPLER_CLIP_H__ |
|
2 #define __RESAMPLER_CLIP_H__ |
|
3 /* |
|
4 * Copyright (c) 2010 Ixonos Plc. |
|
5 * All rights reserved. |
|
6 * This component and the accompanying materials are made available |
|
7 * under the terms of the "Eclipse Public License v1.0" |
|
8 * which accompanies this distribution, and is available |
|
9 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
10 * |
|
11 * Initial Contributors: |
|
12 * Nokia Corporation - Initial contribution |
|
13 * |
|
14 * Contributors: |
|
15 * Ixonos Plc |
|
16 * |
|
17 * Description: |
|
18 * |
|
19 */ |
|
20 |
|
21 |
|
22 #include "resampler_common_defs.h" |
|
23 #include "resampler_data_types.h" |
|
24 |
|
25 #include <stdlib.h> |
|
26 |
|
27 #ifdef __cplusplus |
|
28 extern "C" { |
|
29 #endif |
|
30 |
|
31 /** @file |
|
32 |
|
33 @ingroup common_dsp |
|
34 |
|
35 Helper functions to limit output to different numerical ranges. |
|
36 |
|
37 The header file 'resampler_clip.h' contains helper (inline) functions for |
|
38 limiting the input to a specified numerical range, or more specifically, |
|
39 to a specified number of bits. These functions are especially useful in |
|
40 fixed point DSP units for limiting the output range. |
|
41 */ |
|
42 |
|
43 /** Clip function for signed 16-bit integer range. |
|
44 |
|
45 This function limits its input to the range -32768 <= output <= 32767. |
|
46 The implementation uses the traditional conditionals to check for |
|
47 overflows.. |
|
48 */ |
|
49 static FORCEINLINE int32 RESAMPLER_Clip16(int32 input) |
|
50 { |
|
51 input = input < (-32768) ? (-32768) : input; |
|
52 input = input > 32767 ? 32767 : input; |
|
53 return input; |
|
54 } |
|
55 |
|
56 /** Shift-and-Clip function for signed 16-bit integer range. |
|
57 |
|
58 This function limits its input to the range -32768 <= output <= 32767 |
|
59 after first shifting the input to the right by the given number of bits. |
|
60 */ |
|
61 static FORCEINLINE int32 RESAMPLER_RightShiftAndClip16(int32 input, int rightShift) |
|
62 { |
|
63 input = input >> rightShift; |
|
64 return RESAMPLER_Clip16(input); |
|
65 } |
|
66 |
|
67 /** Shift-round-and-clip function for signed 16-bit integer range. |
|
68 |
|
69 This function limits its input to the range -32768 <= output <= 32767 |
|
70 after first shifting the input to the right by the given number of bits |
|
71 with rounding. |
|
72 */ |
|
73 static FORCEINLINE int32 RESAMPLER_RightShiftRoundAndClip16(int32 input, int rightShift) |
|
74 { |
|
75 if (rightShift > 0) |
|
76 { |
|
77 input += (int32)1 << (rightShift - 1); |
|
78 } |
|
79 input = input >> rightShift; |
|
80 return RESAMPLER_Clip16(input); |
|
81 } |
|
82 |
|
83 /** Saturated 16-bit absolute value. |
|
84 |
|
85 This function calculates a saturated 16-bit output value from a 16-bit |
|
86 input RESAMPLER_AbsAndClip16(0x8000) => 0x7FFF. |
|
87 */ |
|
88 static FORCEINLINE int16 RESAMPLER_AbsSaturate16(int16 input) |
|
89 { |
|
90 return (int16)RESAMPLER_Clip16(labs((long)input)); |
|
91 } |
|
92 |
|
93 /** Saturated addition of two 16-bit values. |
|
94 */ |
|
95 static FORCEINLINE int16 RESAMPLER_AddSaturate16(int16 input1, int16 input2) |
|
96 { |
|
97 return (int16)RESAMPLER_Clip16((int32)input1 + input2); |
|
98 } |
|
99 |
|
100 |
|
101 /** Saturated subtraction of two 16-bit values. |
|
102 */ |
|
103 static FORCEINLINE int16 RESAMPLER_SubSaturate16(int16 input1, int16 input2) |
|
104 { |
|
105 return (int16)RESAMPLER_Clip16((int32)input1 - input2); |
|
106 } |
|
107 |
|
108 /** Saturated addition of two 32-bit values. |
|
109 */ |
|
110 static FORCEINLINE int32 RESAMPLER_AddSaturate32(int32 input1, int32 input2) |
|
111 { |
|
112 /* int40 is the shortest "long long" available on all |
|
113 * the current platforms |
|
114 */ |
|
115 int40 res = input1; |
|
116 res += input2; |
|
117 if (res > (int40)(2147483647L)) |
|
118 { |
|
119 res = (int40)(2147483647L); |
|
120 } |
|
121 if (res < (int40)(-2147483647L - 1)) |
|
122 { |
|
123 res = (int40)(-2147483647L - 1); |
|
124 } |
|
125 return (int32)res; |
|
126 } |
|
127 |
|
128 /** Saturated subtraction of two 32-bit values. |
|
129 */ |
|
130 static FORCEINLINE int32 RESAMPLER_SubSaturate32(int32 input1, int32 input2) |
|
131 { |
|
132 int40 res = input1; |
|
133 res -= input2; |
|
134 if (res > (int40)(2147483647L)) |
|
135 { |
|
136 res = (int40)(2147483647L); |
|
137 } |
|
138 if (res < (int40)(-2147483647L - 1)) |
|
139 { |
|
140 res = (int40)(-2147483647L - 1); |
|
141 } |
|
142 return (int32)res; |
|
143 } |
|
144 |
|
145 #ifdef __cplusplus |
|
146 } |
|
147 #endif |
|
148 |
|
149 #endif /* __RESAMPLER_CLIP_H__ */ |