/proc/self/cwd/libfaad/common.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding |
3 | | ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com |
4 | | ** |
5 | | ** This program is free software; you can redistribute it and/or modify |
6 | | ** it under the terms of the GNU General Public License as published by |
7 | | ** the Free Software Foundation; either version 2 of the License, or |
8 | | ** (at your option) any later version. |
9 | | ** |
10 | | ** This program is distributed in the hope that it will be useful, |
11 | | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | ** GNU General Public License for more details. |
14 | | ** |
15 | | ** You should have received a copy of the GNU General Public License |
16 | | ** along with this program; if not, write to the Free Software |
17 | | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 | | ** |
19 | | ** Any non-GPL usage of this software or parts of this software is strictly |
20 | | ** forbidden. |
21 | | ** |
22 | | ** The "appropriate copyright message" mentioned in section 2c of the GPLv2 |
23 | | ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com" |
24 | | ** |
25 | | ** Commercial non-GPL licensing of this software is possible. |
26 | | ** For more info contact Nero AG through Mpeg4AAClicense@nero.com. |
27 | | ** |
28 | | ** $Id: common.c,v 1.27 2008/03/23 23:03:28 menno Exp $ |
29 | | **/ |
30 | | |
31 | | /* just some common functions that could be used anywhere */ |
32 | | |
33 | | #include "common.h" |
34 | | #include "structs.h" |
35 | | |
36 | | #include <stdlib.h> |
37 | | #include "syntax.h" |
38 | | |
39 | | |
40 | | /* Returns the sample rate index based on the samplerate */ |
41 | | uint8_t get_sr_index(const uint32_t samplerate) |
42 | 139k | { |
43 | 139k | if (92017 <= samplerate) return 0; |
44 | 99.1k | if (75132 <= samplerate) return 1; |
45 | 97.2k | if (55426 <= samplerate) return 2; |
46 | 94.0k | if (46009 <= samplerate) return 3; |
47 | 85.9k | if (37566 <= samplerate) return 4; |
48 | 76.4k | if (27713 <= samplerate) return 5; |
49 | 67.0k | if (23004 <= samplerate) return 6; |
50 | 64.4k | if (18783 <= samplerate) return 7; |
51 | 53.1k | if (13856 <= samplerate) return 8; |
52 | 2.28k | if (11502 <= samplerate) return 9; |
53 | 2.12k | if (9391 <= samplerate) return 10; |
54 | | |
55 | 1.81k | return 11; |
56 | 2.12k | } |
57 | | |
58 | | /* Returns the sample rate based on the sample rate index */ |
59 | | uint32_t get_sample_rate(const uint8_t sr_index) |
60 | 130k | { |
61 | 130k | static const uint32_t sample_rates[] = |
62 | 130k | { |
63 | 130k | 96000, 88200, 64000, 48000, 44100, 32000, |
64 | 130k | 24000, 22050, 16000, 12000, 11025, 8000 |
65 | 130k | }; |
66 | | |
67 | 130k | if (sr_index < 12) |
68 | 130k | return sample_rates[sr_index]; |
69 | | |
70 | 223 | return 0; |
71 | 130k | } |
72 | | |
73 | | uint8_t max_pred_sfb(const uint8_t sr_index) |
74 | 1.80k | { |
75 | 1.80k | static const uint8_t pred_sfb_max[] = |
76 | 1.80k | { |
77 | 1.80k | 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34 |
78 | 1.80k | }; |
79 | | |
80 | | |
81 | 1.80k | if (sr_index < 12) |
82 | 1.80k | return pred_sfb_max[sr_index]; |
83 | | |
84 | 0 | return 0; |
85 | 1.80k | } |
86 | | |
87 | | uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type, |
88 | | const uint8_t is_short) |
89 | 19.2k | { |
90 | | /* entry for each sampling rate |
91 | | * 1 Main/LC long window |
92 | | * 2 Main/LC short window |
93 | | * 3 SSR long window |
94 | | * 4 SSR short window |
95 | | */ |
96 | 19.2k | static const uint8_t tns_sbf_max[][4] = |
97 | 19.2k | { |
98 | 19.2k | {31, 9, 28, 7}, /* 96000 */ |
99 | 19.2k | {31, 9, 28, 7}, /* 88200 */ |
100 | 19.2k | {34, 10, 27, 7}, /* 64000 */ |
101 | 19.2k | {40, 14, 26, 6}, /* 48000 */ |
102 | 19.2k | {42, 14, 26, 6}, /* 44100 */ |
103 | 19.2k | {51, 14, 26, 6}, /* 32000 */ |
104 | 19.2k | {46, 14, 29, 7}, /* 24000 */ |
105 | 19.2k | {46, 14, 29, 7}, /* 22050 */ |
106 | 19.2k | {42, 14, 23, 8}, /* 16000 */ |
107 | 19.2k | {42, 14, 23, 8}, /* 12000 */ |
108 | 19.2k | {42, 14, 23, 8}, /* 11025 */ |
109 | 19.2k | {39, 14, 19, 7}, /* 8000 */ |
110 | 19.2k | {39, 14, 19, 7}, /* 7350 */ |
111 | 19.2k | {0,0,0,0}, |
112 | 19.2k | {0,0,0,0}, |
113 | 19.2k | {0,0,0,0} |
114 | 19.2k | }; |
115 | 19.2k | uint8_t i = 0; |
116 | | |
117 | 19.2k | if (is_short) i++; |
118 | 19.2k | if (object_type == SSR) i += 2; |
119 | | |
120 | 19.2k | return tns_sbf_max[sr_index][i]; |
121 | 19.2k | } |
122 | | |
123 | | /* Returns 0 if an object type is decodable, otherwise returns -1 */ |
124 | | int8_t can_decode_ot(const uint8_t object_type) |
125 | 11.6k | { |
126 | 11.6k | switch (object_type) |
127 | 11.6k | { |
128 | 4.53k | case LC: |
129 | 4.53k | return 0; |
130 | 60 | case MAIN: |
131 | | #ifdef MAIN_DEC |
132 | | return 0; |
133 | | #else |
134 | 60 | return -1; |
135 | 0 | #endif |
136 | 16 | case SSR: |
137 | | #ifdef SSR_DEC |
138 | | return 0; |
139 | | #else |
140 | 16 | return -1; |
141 | 0 | #endif |
142 | 4.29k | case LTP: |
143 | 4.29k | #ifdef LTP_DEC |
144 | 4.29k | return 0; |
145 | | #else |
146 | | return -1; |
147 | | #endif |
148 | | |
149 | | /* ER object types */ |
150 | 0 | #ifdef ERROR_RESILIENCE |
151 | 1.28k | case ER_LC: |
152 | | #ifdef DRM |
153 | | case DRM_ER_LC: |
154 | | #endif |
155 | 1.28k | return 0; |
156 | 890 | case ER_LTP: |
157 | 890 | #ifdef LTP_DEC |
158 | 890 | return 0; |
159 | | #else |
160 | | return -1; |
161 | | #endif |
162 | 531 | case LD: |
163 | 531 | #ifdef LD_DEC |
164 | 531 | return 0; |
165 | | #else |
166 | | return -1; |
167 | | #endif |
168 | 11.6k | #endif |
169 | 11.6k | } |
170 | | |
171 | 5 | return -1; |
172 | 11.6k | } |
173 | | |
174 | | void *faad_malloc(size_t size) |
175 | 2.76M | { |
176 | | #if 0 // defined(_WIN32) && !defined(_WIN32_WCE) |
177 | | return _aligned_malloc(size, 16); |
178 | | #else // #ifdef 0 |
179 | 2.76M | return malloc(size); |
180 | 2.76M | #endif // #ifdef 0 |
181 | 2.76M | } |
182 | | |
183 | | /* common free function */ |
184 | | void faad_free(void *b) |
185 | 2.76M | { |
186 | | #if 0 // defined(_WIN32) && !defined(_WIN32_WCE) |
187 | | _aligned_free(b); |
188 | | #else |
189 | 2.76M | free(b); |
190 | 2.76M | } |
191 | | #endif |
192 | | |
193 | | static const uint8_t Parity [256] = { // parity |
194 | | 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, |
195 | | 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, |
196 | | 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, |
197 | | 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, |
198 | | 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, |
199 | | 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, |
200 | | 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, |
201 | | 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0 |
202 | | }; |
203 | | |
204 | | |
205 | | /* |
206 | | * This is a simple random number generator with good quality for audio purposes. |
207 | | * It consists of two polycounters with opposite rotation direction and different |
208 | | * periods. The periods are coprime, so the total period is the product of both. |
209 | | * |
210 | | * ------------------------------------------------------------------------------------------------- |
211 | | * +-> |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| |
212 | | * | ------------------------------------------------------------------------------------------------- |
213 | | * | | | | | | | |
214 | | * | +--+--+--+-XOR-+--------+ |
215 | | * | | |
216 | | * +--------------------------------------------------------------------------------------+ |
217 | | * |
218 | | * ------------------------------------------------------------------------------------------------- |
219 | | * |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| <-+ |
220 | | * ------------------------------------------------------------------------------------------------- | |
221 | | * | | | | | |
222 | | * +--+----XOR----+--+ | |
223 | | * | | |
224 | | * +----------------------------------------------------------------------------------------+ |
225 | | * |
226 | | * |
227 | | * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481, |
228 | | * which gives a period of 18.410.713.077.675.721.215. The result is the |
229 | | * XORed values of both generators. |
230 | | */ |
231 | | uint32_t ne_rng(uint32_t *__r1, uint32_t *__r2) |
232 | 296k | { |
233 | 296k | uint32_t t1, t2, t3, t4; |
234 | | |
235 | 296k | t3 = t1 = *__r1; t4 = t2 = *__r2; // Parity calculation is done via table lookup, this is also available |
236 | 296k | t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable |
237 | 296k | t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations. |
238 | 296k | t1 <<= 31; t2 = Parity [t2]; |
239 | | |
240 | 296k | return (*__r1 = (t3 >> 1) | t1 ) ^ (*__r2 = (t4 + t4) | t2 ); |
241 | 296k | } |
242 | | |
243 | | #ifdef FIXED_POINT |
244 | | |
245 | | static uint32_t ones32(uint32_t x) |
246 | 101k | { |
247 | 101k | x -= ((x >> 1) & 0x55555555); |
248 | 101k | x = (((x >> 2) & 0x33333333) + (x & 0x33333333)); |
249 | 101k | x = (((x >> 4) + x) & 0x0f0f0f0f); |
250 | 101k | x += (x >> 8); |
251 | 101k | x += (x >> 16); |
252 | | |
253 | 101k | return (x & 0x0000003f); |
254 | 101k | } |
255 | | |
256 | | static uint32_t ones64(uint64_t x) |
257 | 22.2k | { |
258 | 22.2k | return ones32((uint32_t)x) + ones32(x >> 32); |
259 | 22.2k | } |
260 | | |
261 | | static uint32_t floor_log2(uint64_t x) |
262 | 22.2k | { |
263 | 22.2k | #if 1 |
264 | 22.2k | x |= (x >> 1); |
265 | 22.2k | x |= (x >> 2); |
266 | 22.2k | x |= (x >> 4); |
267 | 22.2k | x |= (x >> 8); |
268 | 22.2k | x |= (x >> 16); |
269 | 22.2k | x |= (x >> 32); |
270 | | |
271 | 22.2k | return (ones64(x) - 1); |
272 | | #else |
273 | | uint32_t count = 0; |
274 | | |
275 | | while (x >>= 1) |
276 | | count++; |
277 | | |
278 | | return count; |
279 | | #endif |
280 | 22.2k | } |
281 | | |
282 | | /* returns position of first bit that is not 0 from msb, |
283 | | * starting count at lsb */ |
284 | | uint32_t wl_min_lzc(uint32_t x) |
285 | 56.6k | { |
286 | 56.6k | #if 1 |
287 | 56.6k | x |= (x >> 1); |
288 | 56.6k | x |= (x >> 2); |
289 | 56.6k | x |= (x >> 4); |
290 | 56.6k | x |= (x >> 8); |
291 | 56.6k | x |= (x >> 16); |
292 | | |
293 | 56.6k | return (ones32(x)); |
294 | | #else |
295 | | uint32_t count = 0; |
296 | | |
297 | | while (x >>= 1) |
298 | | count++; |
299 | | |
300 | | return (count + 1); |
301 | | #endif |
302 | 56.6k | } |
303 | | |
304 | 3.20M | #define TABLE_BITS 6 |
305 | | /* just take the maximum number of bits for interpolation */ |
306 | 1.07M | #define INTERP_BITS (REAL_BITS-TABLE_BITS) |
307 | | |
308 | | static const real_t pow2_tab[] = { |
309 | | REAL_CONST(1.000000000000000), REAL_CONST(1.010889286051701), REAL_CONST(1.021897148654117), |
310 | | REAL_CONST(1.033024879021228), REAL_CONST(1.044273782427414), REAL_CONST(1.055645178360557), |
311 | | REAL_CONST(1.067140400676824), REAL_CONST(1.078760797757120), REAL_CONST(1.090507732665258), |
312 | | REAL_CONST(1.102382583307841), REAL_CONST(1.114386742595892), REAL_CONST(1.126521618608242), |
313 | | REAL_CONST(1.138788634756692), REAL_CONST(1.151189229952983), REAL_CONST(1.163724858777578), |
314 | | REAL_CONST(1.176396991650281), REAL_CONST(1.189207115002721), REAL_CONST(1.202156731452703), |
315 | | REAL_CONST(1.215247359980469), REAL_CONST(1.228480536106870), REAL_CONST(1.241857812073484), |
316 | | REAL_CONST(1.255380757024691), REAL_CONST(1.269050957191733), REAL_CONST(1.282870016078778), |
317 | | REAL_CONST(1.296839554651010), REAL_CONST(1.310961211524764), REAL_CONST(1.325236643159741), |
318 | | REAL_CONST(1.339667524053303), REAL_CONST(1.354255546936893), REAL_CONST(1.369002422974591), |
319 | | REAL_CONST(1.383909881963832), REAL_CONST(1.398979672538311), REAL_CONST(1.414213562373095), |
320 | | REAL_CONST(1.429613338391970), REAL_CONST(1.445180806977047), REAL_CONST(1.460917794180647), |
321 | | REAL_CONST(1.476826145939499), REAL_CONST(1.492907728291265), REAL_CONST(1.509164427593423), |
322 | | REAL_CONST(1.525598150744538), REAL_CONST(1.542210825407941), REAL_CONST(1.559004400237837), |
323 | | REAL_CONST(1.575980845107887), REAL_CONST(1.593142151342267), REAL_CONST(1.610490331949254), |
324 | | REAL_CONST(1.628027421857348), REAL_CONST(1.645755478153965), REAL_CONST(1.663676580326736), |
325 | | REAL_CONST(1.681792830507429), REAL_CONST(1.700106353718524), REAL_CONST(1.718619298122478), |
326 | | REAL_CONST(1.737333835273706), REAL_CONST(1.756252160373300), REAL_CONST(1.775376492526521), |
327 | | REAL_CONST(1.794709075003107), REAL_CONST(1.814252175500399), REAL_CONST(1.834008086409342), |
328 | | REAL_CONST(1.853979125083386), REAL_CONST(1.874167634110300), REAL_CONST(1.894575981586966), |
329 | | REAL_CONST(1.915206561397147), REAL_CONST(1.936061793492294), REAL_CONST(1.957144124175400), |
330 | | REAL_CONST(1.978456026387951), REAL_CONST(2.000000000000000) |
331 | | }; |
332 | | |
333 | | static const real_t log2_tab[] = { |
334 | | REAL_CONST(0.000000000000000), REAL_CONST(0.022367813028455), REAL_CONST(0.044394119358453), |
335 | | REAL_CONST(0.066089190457772), REAL_CONST(0.087462841250339), REAL_CONST(0.108524456778169), |
336 | | REAL_CONST(0.129283016944966), REAL_CONST(0.149747119504682), REAL_CONST(0.169925001442312), |
337 | | REAL_CONST(0.189824558880017), REAL_CONST(0.209453365628950), REAL_CONST(0.228818690495881), |
338 | | REAL_CONST(0.247927513443585), REAL_CONST(0.266786540694901), REAL_CONST(0.285402218862248), |
339 | | REAL_CONST(0.303780748177103), REAL_CONST(0.321928094887362), REAL_CONST(0.339850002884625), |
340 | | REAL_CONST(0.357552004618084), REAL_CONST(0.375039431346925), REAL_CONST(0.392317422778760), |
341 | | REAL_CONST(0.409390936137702), REAL_CONST(0.426264754702098), REAL_CONST(0.442943495848728), |
342 | | REAL_CONST(0.459431618637297), REAL_CONST(0.475733430966398), REAL_CONST(0.491853096329675), |
343 | | REAL_CONST(0.507794640198696), REAL_CONST(0.523561956057013), REAL_CONST(0.539158811108031), |
344 | | REAL_CONST(0.554588851677637), REAL_CONST(0.569855608330948), REAL_CONST(0.584962500721156), |
345 | | REAL_CONST(0.599912842187128), REAL_CONST(0.614709844115208), REAL_CONST(0.629356620079610), |
346 | | REAL_CONST(0.643856189774725), REAL_CONST(0.658211482751795), REAL_CONST(0.672425341971496), |
347 | | REAL_CONST(0.686500527183218), REAL_CONST(0.700439718141092), REAL_CONST(0.714245517666123), |
348 | | REAL_CONST(0.727920454563199), REAL_CONST(0.741466986401147), REAL_CONST(0.754887502163469), |
349 | | REAL_CONST(0.768184324776926), REAL_CONST(0.781359713524660), REAL_CONST(0.794415866350106), |
350 | | REAL_CONST(0.807354922057604), REAL_CONST(0.820178962415188), REAL_CONST(0.832890014164742), |
351 | | REAL_CONST(0.845490050944375), REAL_CONST(0.857980995127572), REAL_CONST(0.870364719583405), |
352 | | REAL_CONST(0.882643049361841), REAL_CONST(0.894817763307943), REAL_CONST(0.906890595608519), |
353 | | REAL_CONST(0.918863237274595), REAL_CONST(0.930737337562886), REAL_CONST(0.942514505339240), |
354 | | REAL_CONST(0.954196310386875), REAL_CONST(0.965784284662087), REAL_CONST(0.977279923499917), |
355 | | REAL_CONST(0.988684686772166), REAL_CONST(1.000000000000000) |
356 | | }; |
357 | | |
358 | | real_t pow2_fix(real_t val) |
359 | 587k | { |
360 | 587k | uint32_t x1, x2; |
361 | 587k | uint32_t errcorr; |
362 | 587k | uint32_t index_frac; |
363 | 587k | real_t retval; |
364 | 587k | int32_t whole = (val >> REAL_BITS); |
365 | | |
366 | 587k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
367 | 587k | if (whole >= 17) __builtin_trap(); |
368 | 587k | #endif |
369 | | |
370 | | /* rest = [0..1] */ |
371 | 587k | int32_t rest = val & ((1 << REAL_BITS) - 1); |
372 | | |
373 | | /* index into pow2_tab */ |
374 | 587k | int32_t index = rest >> (REAL_BITS-TABLE_BITS); |
375 | | |
376 | | |
377 | 587k | if (val == 0) |
378 | 2.21k | return (1<<REAL_BITS); |
379 | 585k | if (REAL_BITS + whole < 0) |
380 | 299k | return 0; |
381 | | |
382 | | /* leave INTERP_BITS bits */ |
383 | 286k | index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS); |
384 | 286k | index_frac = index_frac & ((1<<INTERP_BITS)-1); |
385 | | |
386 | 286k | if (whole > 0) |
387 | 101k | { |
388 | 101k | retval = 1 << whole; |
389 | 185k | } else { |
390 | 185k | retval = REAL_CONST(1) >> -whole; |
391 | 185k | } |
392 | | |
393 | 286k | x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)]; |
394 | 286k | x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1]; |
395 | 286k | errcorr = ( (index_frac*(x2-x1))) >> INTERP_BITS; |
396 | | |
397 | 286k | if (whole > 0) |
398 | 101k | { |
399 | 101k | retval = retval * (errcorr + x1); |
400 | 185k | } else { |
401 | 185k | retval = MUL_R(retval, (errcorr + x1)); |
402 | 185k | } |
403 | | |
404 | 286k | return retval; |
405 | 585k | } |
406 | | |
407 | | uint64_t pow2_int(real_t val) |
408 | 400k | { |
409 | 400k | uint32_t x1, x2; |
410 | 400k | uint32_t errcorr; |
411 | 400k | uint32_t index_frac; |
412 | 400k | uint64_t retval; |
413 | 400k | int32_t whole = (val >> REAL_BITS); |
414 | 400k | int32_t exp = 0; |
415 | | |
416 | | /* rest = [0..1] */ |
417 | 400k | int32_t rest = val & ((1 << REAL_BITS) - 1); |
418 | | |
419 | | /* index into pow2_tab */ |
420 | 400k | int32_t index = rest >> (REAL_BITS-TABLE_BITS); |
421 | | |
422 | 400k | if (val < 0) |
423 | 343k | return 0; |
424 | 57.4k | if (val == 0) |
425 | 783 | return 1; |
426 | 56.6k | if (whole > COEF_BITS) { |
427 | 12.1k | exp = whole - COEF_BITS; |
428 | 12.1k | whole = COEF_BITS; |
429 | 12.1k | } |
430 | | |
431 | | /* leave INTERP_BITS bits */ |
432 | 56.6k | index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS); |
433 | 56.6k | index_frac = index_frac & ((1<<INTERP_BITS)-1); |
434 | | |
435 | 56.6k | if (whole >= 0) |
436 | 56.6k | retval = (uint32_t)(1 << whole); |
437 | 0 | else |
438 | 0 | retval = 0; |
439 | | |
440 | 56.6k | x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)]; |
441 | 56.6k | x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1]; |
442 | 56.6k | errcorr = ((index_frac*(x2-x1))) >> INTERP_BITS; |
443 | | |
444 | 56.6k | retval = MUL_R(retval, (errcorr + x1)); |
445 | | |
446 | 56.6k | return retval << exp; |
447 | 57.4k | } |
448 | | |
449 | | /* ld(x) = ld(x*y/y) = ld(x/y) + ld(y), with y=2^N and [1 <= (x/y) < 2] */ |
450 | | int32_t log2_int(uint64_t val) |
451 | 22.2k | { |
452 | 22.2k | uint32_t frac; |
453 | 22.2k | int32_t exp = 0; |
454 | 22.2k | uint32_t index; |
455 | 22.2k | uint32_t index_frac; |
456 | 22.2k | uint32_t x1, x2; |
457 | 22.2k | uint32_t errcorr; |
458 | | |
459 | 22.2k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
460 | 22.2k | if (val == 0) __builtin_trap(); |
461 | 22.2k | #endif |
462 | | |
463 | 22.2k | exp = floor_log2(val); |
464 | 22.2k | exp -= REAL_BITS; |
465 | | |
466 | | /* frac = [1..2] */ |
467 | 22.2k | if (exp >= 0) |
468 | 12.8k | frac = (uint32_t)(val >> exp); |
469 | 9.39k | else |
470 | 9.39k | frac = (uint32_t)(val << -exp); |
471 | | |
472 | | /* index in the log2 table */ |
473 | 22.2k | index = frac >> (REAL_BITS-TABLE_BITS); |
474 | | |
475 | | /* leftover part for linear interpolation */ |
476 | 22.2k | index_frac = frac & ((1<<(REAL_BITS-TABLE_BITS))-1); |
477 | | |
478 | | /* leave INTERP_BITS bits */ |
479 | 22.2k | index_frac = index_frac >> (REAL_BITS-TABLE_BITS-INTERP_BITS); |
480 | | |
481 | 22.2k | x1 = log2_tab[index & ((1<<TABLE_BITS)-1)]; |
482 | 22.2k | x2 = log2_tab[(index & ((1<<TABLE_BITS)-1)) + 1]; |
483 | | |
484 | | /* linear interpolation */ |
485 | | /* retval = exp + ((index_frac)*x2 + (1-index_frac)*x1) */ |
486 | | |
487 | 22.2k | errcorr = (index_frac * (x2-x1)) >> INTERP_BITS; |
488 | | |
489 | 22.2k | return ((exp+REAL_BITS) << REAL_BITS) + errcorr + x1; |
490 | 22.2k | } |
491 | | |
492 | | #endif |