Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2002-2008 Jean-Marc Valin |
2 | | Copyright (c) 2007-2008 CSIRO |
3 | | Copyright (c) 2007-2009 Xiph.Org Foundation |
4 | | Copyright (c) 2024 Arm Limited |
5 | | Written by Jean-Marc Valin */ |
6 | | /** |
7 | | @file mathops.h |
8 | | @brief Various math functions |
9 | | */ |
10 | | /* |
11 | | Redistribution and use in source and binary forms, with or without |
12 | | modification, are permitted provided that the following conditions |
13 | | are met: |
14 | | |
15 | | - Redistributions of source code must retain the above copyright |
16 | | notice, this list of conditions and the following disclaimer. |
17 | | |
18 | | - Redistributions in binary form must reproduce the above copyright |
19 | | notice, this list of conditions and the following disclaimer in the |
20 | | documentation and/or other materials provided with the distribution. |
21 | | |
22 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
23 | | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
24 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
25 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
26 | | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
27 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
28 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
29 | | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
30 | | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
31 | | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
32 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
33 | | */ |
34 | | |
35 | | #ifdef HAVE_CONFIG_H |
36 | | #include "config.h" |
37 | | #endif |
38 | | |
39 | | #include "float_cast.h" |
40 | | #include "mathops.h" |
41 | | |
42 | | /*Compute floor(sqrt(_val)) with exact arithmetic. |
43 | | _val must be greater than 0. |
44 | | This has been tested on all possible 32-bit inputs greater than 0.*/ |
45 | 287k | unsigned isqrt32(opus_uint32 _val){ |
46 | 287k | unsigned b; |
47 | 287k | unsigned g; |
48 | 287k | int bshift; |
49 | | /*Uses the second method from |
50 | | http://www.azillionmonkeys.com/qed/sqroot.html |
51 | | The main idea is to search for the largest binary digit b such that |
52 | | (g+b)*(g+b) <= _val, and add it to the solution g.*/ |
53 | 287k | g=0; |
54 | 287k | bshift=(EC_ILOG(_val)-1)>>1; |
55 | 287k | b=1U<<bshift; |
56 | 2.02M | do{ |
57 | 2.02M | opus_uint32 t; |
58 | 2.02M | t=(((opus_uint32)g<<1)+b)<<bshift; |
59 | 2.02M | if(t<=_val){ |
60 | 1.15M | g+=b; |
61 | 1.15M | _val-=t; |
62 | 1.15M | } |
63 | 2.02M | b>>=1; |
64 | 2.02M | bshift--; |
65 | 2.02M | } |
66 | 2.02M | while(bshift>=0); |
67 | 287k | return g; |
68 | 287k | } |
69 | | |
70 | | #ifdef FIXED_POINT |
71 | | |
72 | | opus_val32 frac_div32_q29(opus_val32 a, opus_val32 b) |
73 | | { |
74 | | opus_val16 rcp; |
75 | | opus_val32 result, rem; |
76 | | int shift = celt_ilog2(b)-29; |
77 | | a = VSHR32(a,shift); |
78 | | b = VSHR32(b,shift); |
79 | | /* 16-bit reciprocal */ |
80 | | rcp = ROUND16(celt_rcp(ROUND16(b,16)),3); |
81 | | result = MULT16_32_Q15(rcp, a); |
82 | | rem = PSHR32(a,2)-MULT32_32_Q31(result, b); |
83 | | result = ADD32(result, SHL32(MULT16_32_Q15(rcp, rem),2)); |
84 | | return result; |
85 | | } |
86 | | |
87 | | opus_val32 frac_div32(opus_val32 a, opus_val32 b) { |
88 | | opus_val32 result = frac_div32_q29(a,b); |
89 | | if (result >= 536870912) /* 2^29 */ |
90 | | return 2147483647; /* 2^31 - 1 */ |
91 | | else if (result <= -536870912) /* -2^29 */ |
92 | | return -2147483647; /* -2^31 */ |
93 | | else |
94 | | return SHL32(result, 2); |
95 | | } |
96 | | |
97 | | /** Reciprocal sqrt approximation in the range [0.25,1) (Q16 in, Q14 out) */ |
98 | | opus_val16 celt_rsqrt_norm(opus_val32 x) |
99 | | { |
100 | | opus_val16 n; |
101 | | opus_val16 r; |
102 | | opus_val16 r2; |
103 | | opus_val16 y; |
104 | | /* Range of n is [-16384,32767] ([-0.5,1) in Q15). */ |
105 | | n = x-32768; |
106 | | /* Get a rough initial guess for the root. |
107 | | The optimal minimax quadratic approximation (using relative error) is |
108 | | r = 1.437799046117536+n*(-0.823394375837328+n*0.4096419668459485). |
109 | | Coefficients here, and the final result r, are Q14.*/ |
110 | | r = ADD16(23557, MULT16_16_Q15(n, ADD16(-13490, MULT16_16_Q15(n, 6713)))); |
111 | | /* We want y = x*r*r-1 in Q15, but x is 32-bit Q16 and r is Q14. |
112 | | We can compute the result from n and r using Q15 multiplies with some |
113 | | adjustment, carefully done to avoid overflow. |
114 | | Range of y is [-1564,1594]. */ |
115 | | r2 = MULT16_16_Q15(r, r); |
116 | | y = SHL16(SUB16(ADD16(MULT16_16_Q15(r2, n), r2), 16384), 1); |
117 | | /* Apply a 2nd-order Householder iteration: r += r*y*(y*0.375-0.5). |
118 | | This yields the Q14 reciprocal square root of the Q16 x, with a maximum |
119 | | relative error of 1.04956E-4, a (relative) RMSE of 2.80979E-5, and a |
120 | | peak absolute error of 2.26591/16384. */ |
121 | | return ADD16(r, MULT16_16_Q15(r, MULT16_16_Q15(y, |
122 | | SUB16(MULT16_16_Q15(y, 12288), 16384)))); |
123 | | } |
124 | | |
125 | | /** Sqrt approximation (QX input, QX/2 output) */ |
126 | | opus_val32 celt_sqrt(opus_val32 x) |
127 | | { |
128 | | int k; |
129 | | opus_val16 n; |
130 | | opus_val32 rt; |
131 | | /* These coeffs are optimized in fixed-point to minimize both RMS and max error |
132 | | of sqrt(x) over .25<x<1 without exceeding 32767. |
133 | | The RMS error is 3.4e-5 and the max is 8.2e-5. */ |
134 | | static const opus_val16 C[6] = {23171, 11574, -2901, 1592, -1002, 336}; |
135 | | if (x==0) |
136 | | return 0; |
137 | | else if (x>=1073741824) |
138 | | return 32767; |
139 | | k = (celt_ilog2(x)>>1)-7; |
140 | | x = VSHR32(x, 2*k); |
141 | | n = x-32768; |
142 | | rt = ADD32(C[0], MULT16_16_Q15(n, ADD16(C[1], MULT16_16_Q15(n, ADD16(C[2], |
143 | | MULT16_16_Q15(n, ADD16(C[3], MULT16_16_Q15(n, ADD16(C[4], MULT16_16_Q15(n, (C[5]))))))))))); |
144 | | rt = VSHR32(rt,7-k); |
145 | | return rt; |
146 | | } |
147 | | |
148 | | #define L1 32767 |
149 | | #define L2 -7651 |
150 | | #define L3 8277 |
151 | | #define L4 -626 |
152 | | |
153 | | static OPUS_INLINE opus_val16 _celt_cos_pi_2(opus_val16 x) |
154 | | { |
155 | | opus_val16 x2; |
156 | | |
157 | | x2 = MULT16_16_P15(x,x); |
158 | | return ADD16(1,MIN16(32766,ADD32(SUB16(L1,x2), MULT16_16_P15(x2, ADD32(L2, MULT16_16_P15(x2, ADD32(L3, MULT16_16_P15(L4, x2 |
159 | | )))))))); |
160 | | } |
161 | | |
162 | | #undef L1 |
163 | | #undef L2 |
164 | | #undef L3 |
165 | | #undef L4 |
166 | | |
167 | | opus_val16 celt_cos_norm(opus_val32 x) |
168 | | { |
169 | | x = x&0x0001ffff; |
170 | | if (x>SHL32(EXTEND32(1), 16)) |
171 | | x = SUB32(SHL32(EXTEND32(1), 17),x); |
172 | | if (x&0x00007fff) |
173 | | { |
174 | | if (x<SHL32(EXTEND32(1), 15)) |
175 | | { |
176 | | return _celt_cos_pi_2(EXTRACT16(x)); |
177 | | } else { |
178 | | return NEG16(_celt_cos_pi_2(EXTRACT16(65536-x))); |
179 | | } |
180 | | } else { |
181 | | if (x&0x0000ffff) |
182 | | return 0; |
183 | | else if (x&0x0001ffff) |
184 | | return -32767; |
185 | | else |
186 | | return 32767; |
187 | | } |
188 | | } |
189 | | |
190 | | /** Reciprocal approximation (Q15 input, Q16 output) */ |
191 | | opus_val32 celt_rcp(opus_val32 x) |
192 | | { |
193 | | int i; |
194 | | opus_val16 n; |
195 | | opus_val16 r; |
196 | | celt_sig_assert(x>0); |
197 | | i = celt_ilog2(x); |
198 | | /* n is Q15 with range [0,1). */ |
199 | | n = VSHR32(x,i-15)-32768; |
200 | | /* Start with a linear approximation: |
201 | | r = 1.8823529411764706-0.9411764705882353*n. |
202 | | The coefficients and the result are Q14 in the range [15420,30840].*/ |
203 | | r = ADD16(30840, MULT16_16_Q15(-15420, n)); |
204 | | /* Perform two Newton iterations: |
205 | | r -= r*((r*n)-1.Q15) |
206 | | = r*((r*n)+(r-1.Q15)). */ |
207 | | r = SUB16(r, MULT16_16_Q15(r, |
208 | | ADD16(MULT16_16_Q15(r, n), ADD16(r, -32768)))); |
209 | | /* We subtract an extra 1 in the second iteration to avoid overflow; it also |
210 | | neatly compensates for truncation error in the rest of the process. */ |
211 | | r = SUB16(r, ADD16(1, MULT16_16_Q15(r, |
212 | | ADD16(MULT16_16_Q15(r, n), ADD16(r, -32768))))); |
213 | | /* r is now the Q15 solution to 2/(n+1), with a maximum relative error |
214 | | of 7.05346E-5, a (relative) RMSE of 2.14418E-5, and a peak absolute |
215 | | error of 1.24665/32768. */ |
216 | | return VSHR32(EXTEND32(r),i-16); |
217 | | } |
218 | | |
219 | | #endif |
220 | | |
221 | | #ifndef DISABLE_FLOAT_API |
222 | | |
223 | | void celt_float2int16_c(const float * OPUS_RESTRICT in, short * OPUS_RESTRICT out, int cnt) |
224 | 0 | { |
225 | 0 | int i; |
226 | 0 | for (i = 0; i < cnt; i++) |
227 | 0 | { |
228 | 0 | out[i] = FLOAT2INT16(in[i]); |
229 | 0 | } |
230 | 0 | } |
231 | | |
232 | | int opus_limit2_checkwithin1_c(float * samples, int cnt) |
233 | 312k | { |
234 | 312k | int i; |
235 | 312k | if (cnt <= 0) |
236 | 0 | { |
237 | 0 | return 1; |
238 | 0 | } |
239 | | |
240 | 692M | for (i = 0; i < cnt; i++) |
241 | 692M | { |
242 | 692M | float clippedVal = samples[i]; |
243 | 692M | clippedVal = FMAX(-2.0f, clippedVal); |
244 | 692M | clippedVal = FMIN(2.0f, clippedVal); |
245 | 692M | samples[i] = clippedVal; |
246 | 692M | } |
247 | | |
248 | | /* C implementation can't provide quick hint. Assume it might exceed -1/+1. */ |
249 | 312k | return 0; |
250 | 312k | } |
251 | | |
252 | | #endif /* DISABLE_FLOAT_API */ |