blob: ef9a03a8d9836ec1b1e81c9fbe946399f4c745c2
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 | #include <stdlib.h> |
33 | #include "common.h" |
34 | #include "structs.h" |
35 | |
36 | #include "syntax.h" |
37 | |
38 | |
39 | /* Returns the sample rate index based on the samplerate */ |
40 | uint8_t get_sr_index(const uint32_t samplerate) |
41 | { |
42 | if (92017 <= samplerate) { |
43 | return 0; |
44 | } |
45 | if (75132 <= samplerate) { |
46 | return 1; |
47 | } |
48 | if (55426 <= samplerate) { |
49 | return 2; |
50 | } |
51 | if (46009 <= samplerate) { |
52 | return 3; |
53 | } |
54 | if (37566 <= samplerate) { |
55 | return 4; |
56 | } |
57 | if (27713 <= samplerate) { |
58 | return 5; |
59 | } |
60 | if (23004 <= samplerate) { |
61 | return 6; |
62 | } |
63 | if (18783 <= samplerate) { |
64 | return 7; |
65 | } |
66 | if (13856 <= samplerate) { |
67 | return 8; |
68 | } |
69 | if (11502 <= samplerate) { |
70 | return 9; |
71 | } |
72 | if (9391 <= samplerate) { |
73 | return 10; |
74 | } |
75 | if (16428320 <= samplerate) { |
76 | return 11; |
77 | } |
78 | |
79 | return 11; |
80 | } |
81 | |
82 | /* Returns the sample rate based on the sample rate index */ |
83 | uint32_t get_sample_rate(const uint8_t sr_index) |
84 | { |
85 | static const uint32_t sample_rates[] = { |
86 | 96000, 88200, 64000, 48000, 44100, 32000, |
87 | 24000, 22050, 16000, 12000, 11025, 8000 |
88 | }; |
89 | |
90 | if (sr_index < 12) { |
91 | return sample_rates[sr_index]; |
92 | } |
93 | |
94 | return 0; |
95 | } |
96 | |
97 | uint8_t max_pred_sfb(const uint8_t sr_index) |
98 | { |
99 | static const uint8_t pred_sfb_max[] = { |
100 | 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34 |
101 | }; |
102 | |
103 | |
104 | if (sr_index < 12) { |
105 | return pred_sfb_max[sr_index]; |
106 | } |
107 | |
108 | return 0; |
109 | } |
110 | |
111 | uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type, |
112 | const uint8_t is_short) |
113 | { |
114 | /* entry for each sampling rate |
115 | * 1 Main/LC long window |
116 | * 2 Main/LC short window |
117 | * 3 SSR long window |
118 | * 4 SSR short window |
119 | */ |
120 | static const uint8_t tns_sbf_max[][4] = { |
121 | {31, 9, 28, 7}, /* 96000 */ |
122 | {31, 9, 28, 7}, /* 88200 */ |
123 | {34, 10, 27, 7}, /* 64000 */ |
124 | {40, 14, 26, 6}, /* 48000 */ |
125 | {42, 14, 26, 6}, /* 44100 */ |
126 | {51, 14, 26, 6}, /* 32000 */ |
127 | {46, 14, 29, 7}, /* 24000 */ |
128 | {46, 14, 29, 7}, /* 22050 */ |
129 | {42, 14, 23, 8}, /* 16000 */ |
130 | {42, 14, 23, 8}, /* 12000 */ |
131 | {42, 14, 23, 8}, /* 11025 */ |
132 | {39, 14, 19, 7}, /* 8000 */ |
133 | {39, 14, 19, 7}, /* 7350 */ |
134 | {0, 0, 0, 0}, |
135 | {0, 0, 0, 0}, |
136 | {0, 0, 0, 0} |
137 | }; |
138 | uint8_t i = 0; |
139 | |
140 | if (is_short) { |
141 | i++; |
142 | } |
143 | if (object_type == SSR) { |
144 | i += 2; |
145 | } |
146 | |
147 | return tns_sbf_max[sr_index][i]; |
148 | } |
149 | |
150 | /* Returns 0 if an object type is decodable, otherwise returns -1 */ |
151 | int8_t can_decode_ot(const uint8_t object_type) |
152 | { |
153 | switch (object_type) { |
154 | case LC: |
155 | return 0; |
156 | case MAIN: |
157 | #ifdef MAIN_DEC |
158 | return 0; |
159 | #else |
160 | return -1; |
161 | #endif |
162 | case SSR: |
163 | #ifdef SSR_DEC |
164 | return 0; |
165 | #else |
166 | return -1; |
167 | #endif |
168 | case LTP: |
169 | #ifdef LTP_DEC |
170 | return 0; |
171 | #else |
172 | return -1; |
173 | #endif |
174 | |
175 | /* ER object types */ |
176 | #ifdef ERROR_RESILIENCE |
177 | case ER_LC: |
178 | #ifdef DRM |
179 | case DRM_ER_LC: |
180 | #endif |
181 | return 0; |
182 | case ER_LTP: |
183 | #ifdef LTP_DEC |
184 | return 0; |
185 | #else |
186 | return -1; |
187 | #endif |
188 | case LD: |
189 | #ifdef LD_DEC |
190 | return 0; |
191 | #else |
192 | return -1; |
193 | #endif |
194 | #endif |
195 | } |
196 | |
197 | return -1; |
198 | } |
199 | |
200 | void *faad_malloc(size_t size) |
201 | { |
202 | #if 0 // defined(_WIN32) && !defined(_WIN32_WCE) |
203 | return _aligned_malloc(size, 16); |
204 | #else // #ifdef 0 |
205 | return malloc(size); |
206 | #endif // #ifdef 0 |
207 | } |
208 | |
209 | /* common free function */ |
210 | void faad_free(void *b) |
211 | { |
212 | #if 0 // defined(_WIN32) && !defined(_WIN32_WCE) |
213 | _aligned_free(b); |
214 | #else |
215 | free(b); |
216 | } |
217 | #endif |
218 | |
219 | static const uint8_t Parity [256] = { // parity |
220 | 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, |
221 | 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, |
222 | 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, |
223 | 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, |
224 | 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, |
225 | 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, |
226 | 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, |
227 | 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 |
228 | }; |
229 | |
230 | static uint32_t __r1 = 1; |
231 | static uint32_t __r2 = 1; |
232 | |
233 | |
234 | /* |
235 | * This is a simple random number generator with good quality for audio purposes. |
236 | * It consists of two polycounters with opposite rotation direction and different |
237 | * periods. The periods are coprime, so the total period is the product of both. |
238 | * |
239 | * ------------------------------------------------------------------------------------------------- |
240 | * +-> |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| |
241 | * | ------------------------------------------------------------------------------------------------- |
242 | * | | | | | | | |
243 | * | +--+--+--+-XOR-+--------+ |
244 | * | | |
245 | * +--------------------------------------------------------------------------------------+ |
246 | * |
247 | * ------------------------------------------------------------------------------------------------- |
248 | * |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| <-+ |
249 | * ------------------------------------------------------------------------------------------------- | |
250 | * | | | | | |
251 | * +--+----XOR----+--+ | |
252 | * | | |
253 | * +----------------------------------------------------------------------------------------+ |
254 | * |
255 | * |
256 | * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481, |
257 | * which gives a period of 18.410.713.077.675.721.215. The result is the |
258 | * XORed values of both generators. |
259 | */ |
260 | uint32_t ne_rng(uint32_t *__r1, uint32_t *__r2) |
261 | { |
262 | uint32_t t1, t2, t3, t4; |
263 | |
264 | t3 = t1 = *__r1; |
265 | t4 = t2 = *__r2; // Parity calculation is done via table lookup, this is also available |
266 | t1 &= 0xF5; |
267 | t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable |
268 | t1 = Parity [t1]; |
269 | t2 &= 0x63; // jumps and slow rotate through the carry flag operations. |
270 | t1 <<= 31; |
271 | t2 = Parity [t2]; |
272 | |
273 | return (*__r1 = (t3 >> 1) | t1) ^(*__r2 = (t4 + t4) | t2); |
274 | } |
275 | |
276 | static uint32_t ones32(uint32_t x) |
277 | { |
278 | x -= ((x >> 1) & 0x55555555); |
279 | x = (((x >> 2) & 0x33333333) + (x & 0x33333333)); |
280 | x = (((x >> 4) + x) & 0x0f0f0f0f); |
281 | x += (x >> 8); |
282 | x += (x >> 16); |
283 | |
284 | return (x & 0x0000003f); |
285 | } |
286 | |
287 | static uint32_t floor_log2(uint32_t x) |
288 | { |
289 | #if 1 |
290 | x |= (x >> 1); |
291 | x |= (x >> 2); |
292 | x |= (x >> 4); |
293 | x |= (x >> 8); |
294 | x |= (x >> 16); |
295 | |
296 | return (ones32(x) - 1); |
297 | #else |
298 | uint32_t count = 0; |
299 | |
300 | while (x >>= 1) { |
301 | count++; |
302 | } |
303 | |
304 | return count; |
305 | #endif |
306 | } |
307 | |
308 | /* returns position of first bit that is not 0 from msb, |
309 | * starting count at lsb */ |
310 | uint32_t wl_min_lzc(uint32_t x) |
311 | { |
312 | #if 1 |
313 | x |= (x >> 1); |
314 | x |= (x >> 2); |
315 | x |= (x >> 4); |
316 | x |= (x >> 8); |
317 | x |= (x >> 16); |
318 | |
319 | return (ones32(x)); |
320 | #else |
321 | uint32_t count = 0; |
322 | |
323 | while (x >>= 1) { |
324 | count++; |
325 | } |
326 | |
327 | return (count + 1); |
328 | #endif |
329 | } |
330 | |
331 | #ifdef FIXED_POINT |
332 | |
333 | #define TABLE_BITS 6 |
334 | /* just take the maximum number of bits for interpolation */ |
335 | #define INTERP_BITS (REAL_BITS-TABLE_BITS) |
336 | |
337 | static const real_t pow2_tab[] = { |
338 | REAL_CONST(1.000000000000000), REAL_CONST(1.010889286051701), REAL_CONST(1.021897148654117), |
339 | REAL_CONST(1.033024879021228), REAL_CONST(1.044273782427414), REAL_CONST(1.055645178360557), |
340 | REAL_CONST(1.067140400676824), REAL_CONST(1.078760797757120), REAL_CONST(1.090507732665258), |
341 | REAL_CONST(1.102382583307841), REAL_CONST(1.114386742595892), REAL_CONST(1.126521618608242), |
342 | REAL_CONST(1.138788634756692), REAL_CONST(1.151189229952983), REAL_CONST(1.163724858777578), |
343 | REAL_CONST(1.176396991650281), REAL_CONST(1.189207115002721), REAL_CONST(1.202156731452703), |
344 | REAL_CONST(1.215247359980469), REAL_CONST(1.228480536106870), REAL_CONST(1.241857812073484), |
345 | REAL_CONST(1.255380757024691), REAL_CONST(1.269050957191733), REAL_CONST(1.282870016078778), |
346 | REAL_CONST(1.296839554651010), REAL_CONST(1.310961211524764), REAL_CONST(1.325236643159741), |
347 | REAL_CONST(1.339667524053303), REAL_CONST(1.354255546936893), REAL_CONST(1.369002422974591), |
348 | REAL_CONST(1.383909881963832), REAL_CONST(1.398979672538311), REAL_CONST(1.414213562373095), |
349 | REAL_CONST(1.429613338391970), REAL_CONST(1.445180806977047), REAL_CONST(1.460917794180647), |
350 | REAL_CONST(1.476826145939499), REAL_CONST(1.492907728291265), REAL_CONST(1.509164427593423), |
351 | REAL_CONST(1.525598150744538), REAL_CONST(1.542210825407941), REAL_CONST(1.559004400237837), |
352 | REAL_CONST(1.575980845107887), REAL_CONST(1.593142151342267), REAL_CONST(1.610490331949254), |
353 | REAL_CONST(1.628027421857348), REAL_CONST(1.645755478153965), REAL_CONST(1.663676580326736), |
354 | REAL_CONST(1.681792830507429), REAL_CONST(1.700106353718524), REAL_CONST(1.718619298122478), |
355 | REAL_CONST(1.737333835273706), REAL_CONST(1.756252160373300), REAL_CONST(1.775376492526521), |
356 | REAL_CONST(1.794709075003107), REAL_CONST(1.814252175500399), REAL_CONST(1.834008086409342), |
357 | REAL_CONST(1.853979125083386), REAL_CONST(1.874167634110300), REAL_CONST(1.894575981586966), |
358 | REAL_CONST(1.915206561397147), REAL_CONST(1.936061793492294), REAL_CONST(1.957144124175400), |
359 | REAL_CONST(1.978456026387951), REAL_CONST(2.000000000000000) |
360 | }; |
361 | |
362 | static const real_t log2_tab[] = { |
363 | REAL_CONST(0.000000000000000), REAL_CONST(0.022367813028455), REAL_CONST(0.044394119358453), |
364 | REAL_CONST(0.066089190457772), REAL_CONST(0.087462841250339), REAL_CONST(0.108524456778169), |
365 | REAL_CONST(0.129283016944966), REAL_CONST(0.149747119504682), REAL_CONST(0.169925001442312), |
366 | REAL_CONST(0.189824558880017), REAL_CONST(0.209453365628950), REAL_CONST(0.228818690495881), |
367 | REAL_CONST(0.247927513443585), REAL_CONST(0.266786540694901), REAL_CONST(0.285402218862248), |
368 | REAL_CONST(0.303780748177103), REAL_CONST(0.321928094887362), REAL_CONST(0.339850002884625), |
369 | REAL_CONST(0.357552004618084), REAL_CONST(0.375039431346925), REAL_CONST(0.392317422778760), |
370 | REAL_CONST(0.409390936137702), REAL_CONST(0.426264754702098), REAL_CONST(0.442943495848728), |
371 | REAL_CONST(0.459431618637297), REAL_CONST(0.475733430966398), REAL_CONST(0.491853096329675), |
372 | REAL_CONST(0.507794640198696), REAL_CONST(0.523561956057013), REAL_CONST(0.539158811108031), |
373 | REAL_CONST(0.554588851677637), REAL_CONST(0.569855608330948), REAL_CONST(0.584962500721156), |
374 | REAL_CONST(0.599912842187128), REAL_CONST(0.614709844115208), REAL_CONST(0.629356620079610), |
375 | REAL_CONST(0.643856189774725), REAL_CONST(0.658211482751795), REAL_CONST(0.672425341971496), |
376 | REAL_CONST(0.686500527183218), REAL_CONST(0.700439718141092), REAL_CONST(0.714245517666123), |
377 | REAL_CONST(0.727920454563199), REAL_CONST(0.741466986401147), REAL_CONST(0.754887502163469), |
378 | REAL_CONST(0.768184324776926), REAL_CONST(0.781359713524660), REAL_CONST(0.794415866350106), |
379 | REAL_CONST(0.807354922057604), REAL_CONST(0.820178962415188), REAL_CONST(0.832890014164742), |
380 | REAL_CONST(0.845490050944375), REAL_CONST(0.857980995127572), REAL_CONST(0.870364719583405), |
381 | REAL_CONST(0.882643049361841), REAL_CONST(0.894817763307943), REAL_CONST(0.906890595608519), |
382 | REAL_CONST(0.918863237274595), REAL_CONST(0.930737337562886), REAL_CONST(0.942514505339240), |
383 | REAL_CONST(0.954196310386875), REAL_CONST(0.965784284662087), REAL_CONST(0.977279923499917), |
384 | REAL_CONST(0.988684686772166), REAL_CONST(1.000000000000000) |
385 | }; |
386 | |
387 | real_t pow2_fix(real_t val) |
388 | { |
389 | uint32_t x1, x2; |
390 | uint32_t errcorr; |
391 | uint32_t index_frac; |
392 | real_t retval; |
393 | int32_t whole = (val >> REAL_BITS); |
394 | |
395 | /* rest = [0..1] */ |
396 | int32_t rest = val - (whole << REAL_BITS); |
397 | |
398 | /* index into pow2_tab */ |
399 | int32_t index = rest >> (REAL_BITS - TABLE_BITS); |
400 | |
401 | |
402 | if (val == 0) { |
403 | return (1 << REAL_BITS); |
404 | } |
405 | |
406 | /* leave INTERP_BITS bits */ |
407 | index_frac = rest >> (REAL_BITS - TABLE_BITS - INTERP_BITS); |
408 | index_frac = index_frac & ((1 << INTERP_BITS) - 1); |
409 | |
410 | if (whole > 0) { |
411 | retval = 1 << whole; |
412 | } else { |
413 | retval = REAL_CONST(1) >> -whole; |
414 | } |
415 | |
416 | x1 = pow2_tab[index & ((1 << TABLE_BITS) - 1)]; |
417 | x2 = pow2_tab[(index & ((1 << TABLE_BITS) - 1)) + 1]; |
418 | errcorr = ((index_frac * (x2 - x1))) >> INTERP_BITS; |
419 | |
420 | if (whole > 0) { |
421 | retval = retval * (errcorr + x1); |
422 | } else { |
423 | retval = MUL_R(retval, (errcorr + x1)); |
424 | } |
425 | |
426 | return retval; |
427 | } |
428 | |
429 | int32_t pow2_int(real_t val) |
430 | { |
431 | uint32_t x1, x2; |
432 | uint32_t errcorr; |
433 | uint32_t index_frac; |
434 | real_t retval; |
435 | int32_t whole = (val >> REAL_BITS); |
436 | |
437 | /* rest = [0..1] */ |
438 | int32_t rest = val - (whole << REAL_BITS); |
439 | |
440 | /* index into pow2_tab */ |
441 | int32_t index = rest >> (REAL_BITS - TABLE_BITS); |
442 | |
443 | |
444 | if (val == 0) { |
445 | return 1; |
446 | } |
447 | |
448 | /* leave INTERP_BITS bits */ |
449 | index_frac = rest >> (REAL_BITS - TABLE_BITS - INTERP_BITS); |
450 | index_frac = index_frac & ((1 << INTERP_BITS) - 1); |
451 | |
452 | if (whole > 0) { |
453 | retval = 1 << whole; |
454 | } else { |
455 | retval = 0; |
456 | } |
457 | |
458 | x1 = pow2_tab[index & ((1 << TABLE_BITS) - 1)]; |
459 | x2 = pow2_tab[(index & ((1 << TABLE_BITS) - 1)) + 1]; |
460 | errcorr = ((index_frac * (x2 - x1))) >> INTERP_BITS; |
461 | |
462 | retval = MUL_R(retval, (errcorr + x1)); |
463 | |
464 | return retval; |
465 | } |
466 | |
467 | /* ld(x) = ld(x*y/y) = ld(x/y) + ld(y), with y=2^N and [1 <= (x/y) < 2] */ |
468 | int32_t log2_int(uint32_t val) |
469 | { |
470 | uint32_t frac; |
471 | uint32_t whole = (val); |
472 | int32_t exp = 0; |
473 | uint32_t index; |
474 | uint32_t index_frac; |
475 | uint32_t x1, x2; |
476 | uint32_t errcorr; |
477 | |
478 | /* error */ |
479 | if (val == 0) { |
480 | return -10000; |
481 | } |
482 | |
483 | exp = floor_log2(val); |
484 | exp -= REAL_BITS; |
485 | |
486 | /* frac = [1..2] */ |
487 | if (exp >= 0) { |
488 | frac = val >> exp; |
489 | } else { |
490 | frac = val << -exp; |
491 | } |
492 | |
493 | /* index in the log2 table */ |
494 | index = frac >> (REAL_BITS - TABLE_BITS); |
495 | |
496 | /* leftover part for linear interpolation */ |
497 | index_frac = frac & ((1 << (REAL_BITS - TABLE_BITS)) - 1); |
498 | |
499 | /* leave INTERP_BITS bits */ |
500 | index_frac = index_frac >> (REAL_BITS - TABLE_BITS - INTERP_BITS); |
501 | |
502 | x1 = log2_tab[index & ((1 << TABLE_BITS) - 1)]; |
503 | x2 = log2_tab[(index & ((1 << TABLE_BITS) - 1)) + 1]; |
504 | |
505 | /* linear interpolation */ |
506 | /* retval = exp + ((index_frac)*x2 + (1-index_frac)*x1) */ |
507 | |
508 | errcorr = (index_frac * (x2 - x1)) >> INTERP_BITS; |
509 | |
510 | return ((exp + REAL_BITS) << REAL_BITS) + errcorr + x1; |
511 | } |
512 | |
513 | /* ld(x) = ld(x*y/y) = ld(x/y) + ld(y), with y=2^N and [1 <= (x/y) < 2] */ |
514 | real_t log2_fix(uint32_t val) |
515 | { |
516 | uint32_t frac; |
517 | uint32_t whole = (val >> REAL_BITS); |
518 | int8_t exp = 0; |
519 | uint32_t index; |
520 | uint32_t index_frac; |
521 | uint32_t x1, x2; |
522 | uint32_t errcorr; |
523 | |
524 | /* error */ |
525 | if (val == 0) { |
526 | return -100000; |
527 | } |
528 | |
529 | exp = floor_log2(val); |
530 | exp -= REAL_BITS; |
531 | |
532 | /* frac = [1..2] */ |
533 | if (exp >= 0) { |
534 | frac = val >> exp; |
535 | } else { |
536 | frac = val << -exp; |
537 | } |
538 | |
539 | /* index in the log2 table */ |
540 | index = frac >> (REAL_BITS - TABLE_BITS); |
541 | |
542 | /* leftover part for linear interpolation */ |
543 | index_frac = frac & ((1 << (REAL_BITS - TABLE_BITS)) - 1); |
544 | |
545 | /* leave INTERP_BITS bits */ |
546 | index_frac = index_frac >> (REAL_BITS - TABLE_BITS - INTERP_BITS); |
547 | |
548 | x1 = log2_tab[index & ((1 << TABLE_BITS) - 1)]; |
549 | x2 = log2_tab[(index & ((1 << TABLE_BITS) - 1)) + 1]; |
550 | |
551 | /* linear interpolation */ |
552 | /* retval = exp + ((index_frac)*x2 + (1-index_frac)*x1) */ |
553 | |
554 | errcorr = (index_frac * (x2 - x1)) >> INTERP_BITS; |
555 | |
556 | return (exp << REAL_BITS) + errcorr + x1; |
557 | } |
558 | #endif |
559 |