Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #ifndef __BASOP_UTIL_H__
38 : #define __BASOP_UTIL_H__
39 :
40 : #include <stdint.h>
41 : #include "options.h"
42 : #include "basop_settings.h"
43 : #include "typedef.h"
44 : #include "basop32.h"
45 : #include "math_32.h"
46 : #include "log2.h"
47 :
48 : #define LD_DATA_SCALE ( 6 )
49 :
50 : #define LD_DATA_SHIFT_I5 ( 7 )
51 :
52 : #define modDiv2( x ) sub( x, shl( shr( x, 1 ), 1 ) )
53 : #define modDiv8( x ) L_sub( x, L_shl( L_shr( x, 3 ), 3 ) )
54 :
55 : // basop_mpy.h
56 : #define MUL_F( A, B ) Mpy_32_16_1( ( A ), ( B ) )
57 :
58 : #ifndef CHEAP_NORM_SIZE
59 : #define CHEAP_NORM_SIZE 161
60 : #endif
61 :
62 : static __inline Word16 limitScale16( Word16 s )
63 : {
64 : /* It is assumed, that s is calculated just before, therefore we can switch upon sign */
65 : if ( s >= 0 )
66 : s = s_min( s, WORD16_BITS - 1 );
67 : if ( s < 0 )
68 : s = s_max( s, 1 - WORD16_BITS );
69 : return ( s );
70 : }
71 :
72 10656 : static __inline Word16 limitScale32( Word16 s )
73 : {
74 : /* It is assumed, that s is calculated just before, therefore we can switch upon sign */
75 10656 : if ( s >= 0 )
76 10009 : s = s_min( s, WORD32_BITS - 1 );
77 10656 : if ( s < 0 )
78 647 : s = s_max( s, 1 - WORD32_BITS );
79 10656 : return ( s );
80 : }
81 :
82 :
83 : /*!**********************************************************************
84 : \brief Add two values given by mantissa and exponent.
85 :
86 : Mantissas are in 16-bit-fractional format with values between 0 and 1. <br>
87 : The base for exponents is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
88 :
89 : ************************************************************************/
90 : Word16 BASOP_Util_Add_MantExp /*!< Exponent of result */
91 : ( Word16 a_m, /*!< Mantissa of 1st operand a */
92 : Word16 a_e, /*!< Exponent of 1st operand a */
93 : Word16 b_m, /*!< Mantissa of 2nd operand b */
94 : Word16 b_e, /*!< Exponent of 2nd operand b */
95 : Word16 *ptrSum_m ); /*!< Mantissa of result */
96 :
97 : /************************************************************************/
98 : /*!
99 : \brief Divide two values given by mantissa and exponent.
100 :
101 : Mantissas are in 16-bit-fractional format with values between 0 and 1. <br>
102 : The base for exponents is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
103 :
104 : For performance reasons, the division is based on a table lookup
105 : which limits accuracy.
106 : */
107 : void BASOP_Util_Divide_MantExp( Word16 a_m, /*!< Mantissa of dividend a */
108 : Word16 a_e, /*!< Exponent of dividend a */
109 : Word16 b_m, /*!< Mantissa of divisor b */
110 : Word16 b_e, /*!< Exponent of divisor b */
111 : Word16 *ptrResult_m, /*!< Mantissa of quotient a/b */
112 : Word16 *ptrResult_e /*!< Exponent of quotient a/b */
113 : );
114 :
115 : /************************************************************************/
116 : /*!
117 : \brief Calculate the squareroot of a number given by mantissa and exponent
118 :
119 : Mantissa is in 16/32-bit-fractional format with values between 0 and 1. <br>
120 : For *norm versions mantissa has to be between 0.5 and 1. <br>
121 : The base for the exponent is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
122 : The exponent is addressed via pointers and will be overwritten with the result.
123 : */
124 : Word16 Sqrt16( /*!< output mantissa */
125 : Word16 mantissa, /*!< input mantissa */
126 : Word16 *exponent /*!< pointer to exponent */
127 : );
128 :
129 : Word16 Sqrt16norm( /*!< output mantissa */
130 : Word16 mantissa, /*!< normalized input mantissa */
131 : Word16 *exponent /*!< pointer to exponent */
132 : );
133 :
134 : Word32 Sqrt32( /*!< output mantissa */
135 : Word32 mantissa, /*!< input mantissa */
136 : Word16 *exponent /*!< pointer to exponent */
137 : );
138 :
139 :
140 : /* deprecated, use Sqrt16! */
141 : void BASOP_Util_Sqrt_MantExp( Word16 *mantissa, /*!< Pointer to mantissa */
142 : Word16 *exponent /*!< Pointer to exponent */
143 : );
144 :
145 : /* deprecated, use Sqrt16norm! */
146 : void BASOP_Util_Sqrt_MantExpNorm( Word16 *mantissa, /*!< Pointer to normalized mantissa */
147 : Word16 *exponent /*!< Pointer to exponent */
148 : );
149 :
150 : /****************************************************************************/
151 : /*!
152 : \brief Calculate the inverse of the squareroot of a number given by mantissa and exponent
153 :
154 : Mantissa is in 16/32-bit-fractional format with values between 0 and 1. <br>
155 : For *norm versions mantissa has to be between 0.5 and 1. <br>
156 : The base for the exponent is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
157 : The exponent is addressed via pointers and will be overwritten with the result.
158 : */
159 : Word16 ISqrt16( /*!< output mantissa */
160 : Word16 mantissa, /*!< input mantissa */
161 : Word16 *exponent /*!< pointer to exponent */
162 : );
163 :
164 : Word32 ISqrt32( /*!< output mantissa */
165 : Word32 mantissa, /*!< input mantissa */
166 : Word16 *exponent /*!< pointer to exponent */
167 : );
168 :
169 : Word32 ISqrt32norm( /*!< output mantissa */
170 : Word32 mantissa, /*!< normalized input mantissa */
171 : Word16 *exponent /*!< pointer to exponent */
172 : );
173 :
174 : /* deprecated, use ISqrt16! */
175 : void BASOP_Util_InvSqrt_MantExp( Word16 *mantissa, /*!< Pointer to mantissa */
176 : Word16 *exponent /*!< Pointer to exponent */
177 : );
178 :
179 : /*****************************************************************************/
180 : /*!
181 : \brief Calculate the inverse of a number given by mantissa and exponent
182 :
183 : Mantissa is in 16-bit-fractional format with values between 0 and 1. <br>
184 : The base for the exponent is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
185 : The operand is addressed via pointers and will be overwritten with the result.
186 :
187 : The function uses a table lookup and a newton iteration.
188 : */
189 : Word16 Inv16( /*!< output mantissa */
190 : Word16 mantissa, /*!< input mantissa */
191 : Word16 *exponent /*!< pointer to exponent */
192 : );
193 :
194 : /******************************************************************************/
195 : /*!
196 : \brief Calculate the squareroot and inverse of squareroot of a number given by mantissa and exponent
197 :
198 : Mantissa is in 16-bit-fractional format with values between 0 and 1. <br>
199 : The base for the exponent is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
200 : */
201 : void BASOP_Util_Sqrt_InvSqrt_MantExp( Word16 mantissa, /*!< mantissa */
202 : Word16 exponent, /*!< expoinent */
203 : Word16 *sqrt_mant, /*!< Pointer to sqrt mantissa */
204 : Word16 *sqrt_exp, /*!< Pointer to sqrt exponent */
205 : Word16 *isqrt_mant, /*!< Pointer to 1/sqrt mantissa */
206 : Word16 *isqrt_exp /*!< Pointer to 1/sqrt exponent */
207 : );
208 :
209 : /********************************************************************/
210 : /*!
211 : \brief Calculates the scalefactor needed to normalize input array
212 :
213 : The scalefactor needed to normalize the Word16 input array is returned <br>
214 : If the input array contains only '0', a scalefactor 0 is returned <br>
215 : Scaling factor is determined wrt a normalized target x: 16384 <= x <= 32767 for positive x <br>
216 : and -32768 <= x <= -16384 for negative x
217 : */
218 :
219 : Word16 getScaleFactor16( /* o: measured headroom in range [0..15], 0 if all x[i] == 0 */
220 : const Word16 *x, /* i: array containing 16-bit data */
221 : const Word16 len_x ); /* i: length of the array to scan */
222 :
223 : /********************************************************************/
224 : /*!
225 : \brief Calculates the scalefactor needed to normalize input array
226 :
227 : The scalefactor needed to normalize the Word32 input array is returned <br>
228 : If the input array contains only '0', a scalefactor 0 is returned <br>
229 : Scaling factor is determined wrt a normalized target x: 1073741824 <= x <= 2147483647 for positive x <br>
230 : and -2147483648 <= x <= -1073741824 for negative x
231 : */
232 :
233 : /*! r: measured headroom in range [0..31], 0 if all x[i] == 0 */
234 : Word16 getScaleFactor32(
235 : const Word32 *x, /* i : array containing 32-bit data */
236 : const Word16 len_x ); /* i : length of the array to scan */
237 :
238 : Word16 getScaleFactor32_copy( /* o: measured headroom in range [0..31], 0 if all x[i] == 0 */
239 : const Word32 *x, /* i: array containing 32-bit data */
240 : const Word32 len_x ); /* i: length of the array to scan */
241 :
242 : /**
243 : * \brief normalize mantissa and update the exponent accordingly.
244 : * \param mantissa the mantissa to be normalized
245 : * \param pexponent pointer to the exponent.
246 : * \return the normalized mantissa.
247 : */
248 : Word16 normalize16( Word16 mantissa, Word16 *pexponent );
249 : /****************************************************************************/
250 : /*!
251 : \brief Does fractional integer division of Word32 arg1 by Word16 arg2
252 :
253 : both input arguments may be positive or negative <br>
254 : the result is truncated to Word16
255 :
256 : \return fractional integer Word16 result of arg1/arg2
257 : */
258 : Word16 divide3216( Word32 x, /*!< Numerator*/
259 : Word16 y ); /*!< Denominator*/
260 :
261 :
262 : /****************************************************************************/
263 : /*!
264 : \brief Does fractional integer division of Word16 arg1 by Word16 arg2
265 :
266 : both input arguments may be positive or negative <br>
267 : the result is truncated to Word16
268 :
269 : \return fractional integer Word16 result of arg1/arg2
270 : */
271 : Word16 divide1616( Word16 x, /*!< Numerator*/
272 : Word16 y ); /*!< Denominator*/
273 :
274 :
275 : /****************************************************************************/
276 : /*!
277 : \brief Does fractional integer division of Word32 arg1 by Word32 arg2
278 :
279 : this function makes both the numerator and the denominator positive integers,
280 : and scales up both values to avoid losing the accuracy of the outcome
281 : too much
282 :
283 : WARNING: it should be arg1 < arg2 because of the maximum degree of scaling for the mantissa!
284 :
285 : \return fractional Word16 integer z = arg1(32bits)/arg2(32bits)
286 : */
287 : Word16 divide3232( Word32 x, /*!< Numerator*/
288 : Word32 y ); /*!< Denominator*/
289 :
290 :
291 : /****************************************************************************/
292 : /*!
293 : \brief Does fractional integer division of UWord32 arg1 by UWord32 arg2
294 :
295 : This function ensures both the numerator and the denominator are positive integers,
296 : and scales up both values to avoid losing the accuracy of the outcome
297 : too much.<br>
298 :
299 : CAUTION: Arg 3 is a Word16 pointer which will point to the scalefactor difference
300 : s_diff = sub(s2,s1), where s1 and s2 are the scalefactors of the arguments, which
301 : were shifted in order to e.g. preserve accuracy.
302 : I.e. the result has to be scaled due to shifting it
303 : s_diff to the right to obtain the real result of the division.
304 :
305 : \return fractional Word16 integer z = arg1(32bits)/arg2(32bits)
306 : */
307 : Word16 BASOP_Util_Divide3232_uu_1616_Scale( Word32 x, /*!< i : Numerator*/
308 : Word32 y, /*!< i : Denominator*/
309 : Word16 *s ); /*!< o : Additional scalefactor difference*/
310 :
311 :
312 : /****************************************************************************/
313 : /*!
314 : \brief Does fractional integer division of Word32 arg1 by Word32 arg2
315 :
316 : This function scales up both values to avoid losing the accuracy of the outcome
317 : too much.<br>
318 :
319 : CAUTION: Arg 3 is a Word16 pointer which will point to the scalefactor difference
320 : s_diff = sub(s2,s1), where s1 and s2 are the scalefactors of the arguments, which
321 : were shifted in order to e.g. preserve accuracy.
322 : I.e. the result has to be scaled due to shifting it
323 : s_diff to the right to obtain the real result of the division.
324 :
325 : \return fractional Word16 integer z = arg1(32bits)/arg2(32bits)
326 : */
327 : Word16 BASOP_Util_Divide3232_Scale( Word32 x, /*!< i : Numerator*/
328 : Word32 y, /*!< i : Denominator*/
329 : Word16 *s ); /*!< o : Additional scalefactor difference*/
330 :
331 : Word32 BASOP_Util_Divide3232_Scale_cadence( Word32 x, /*!< i : Numerator*/
332 : Word32 y, /*!< i : Denominator*/
333 : Word16 *s ); /*!< o : Additional scalefactor difference*/
334 :
335 :
336 : Word32 BASOP_Util_Divide3232_Scale_newton( Word32 x, /*!< i : Numerator*/
337 : Word32 y, /*!< i : Denominator*/
338 : Word16 *s ); /*!< o : Additional scalefactor difference*/
339 :
340 :
341 : /************************************************************************/
342 : /*!
343 : \brief Binary logarithm with 7 iterations
344 :
345 : \param x
346 :
347 : \return log2(x)/64
348 : */
349 : /************************************************************************/
350 : Word32 BASOP_Util_Log2( Word32 x );
351 : Word32 BASOP_Util_Log10( Word32 x, Word16 e );
352 : Word32 BASOP_Util_Loge( Word32 x, Word16 e );
353 :
354 :
355 : /****************************************************************************/
356 : /*!
357 : \brief Does fractional division of Word16 arg1 by Word16 arg2
358 :
359 :
360 : \return fractional Q15 Word16 z = arg1(Q15)/arg2(Q15) with scaling s
361 : */
362 : Word16 BASOP_Util_Divide1616_Scale( Word16 x, /* i : Numerator*/
363 : Word16 y, /* i : Denominator*/
364 : Word16 *s ); /* o : Additional scalefactor difference*/
365 :
366 : /****************************************************************************/
367 : /*!
368 : \brief Does fractional integer division of Word32 arg1 by Word16 arg2
369 :
370 :
371 : \return fractional Word16 integer z = arg1(32bits)/arg2(16bits), z not normalized
372 : */
373 : Word16 BASOP_Util_Divide3216_Scale( Word32 x, /*!< i : Numerator */
374 : Word16 y, /*!< i : Denominator*/
375 : Word16 *s ); /*!< o : Additional scalefactor difference*/
376 :
377 :
378 : /************************************************************************/
379 : /*!
380 : * \brief Binary logarithm with 5 iterations
381 : *
382 : * \param[i] val
383 : *
384 : * \return basop_log2(val)/128
385 : */
386 : /************************************************************************/
387 : Word32 BASOP_Util_log2_i5( Word32 val );
388 :
389 : /************************************************************************/
390 : /*!
391 : \brief Binary power
392 :
393 : Date: 06-JULY-2012 Arthur Tritthart, IIS Fraunhofer Erlangen
394 :
395 : Version with 3 table lookup and 1 linear interpolations
396 :
397 : Algorithm: compute power of 2, argument x is in Q7.25 format
398 : result = 2^(x/64)
399 : We split exponent (x/64) into 5 components:
400 : integer part: represented by b31..b25 (exp)
401 : fractional part 1: represented by b24..b20 (lookup1)
402 : fractional part 2: represented by b19..b15 (lookup2)
403 : fractional part 3: represented by b14..b10 (lookup3)
404 : fractional part 4: represented by b09..b00 (frac)
405 : => result = (lookup1*lookup2*(lookup3+C1*frac)<<3)>>exp
406 :
407 : Due to the fact, that all lookup values contain a factor 0.5
408 : the result has to be shifted by 3 to the right also.
409 : Table exp2_tab_long contains the log2 for 0 to 1.0 in steps
410 : of 1/32, table exp2w_tab_long the log2 for 0 to 1/32 in steps
411 : of 1/1024, table exp2x_tab_long the log2 for 0 to 1/1024 in
412 : steps of 1/32768. Since the 2-logarithm of very very small
413 : negative value is rather linear, we can use interpolation.
414 :
415 : Limitations:
416 :
417 : For x <= 0, the result is fractional positive
418 : For x > 0, the result is integer in range 1...7FFF.FFFF
419 : For x < -31/64, we have to clear the result
420 : For x = 0, the result is ~1.0 (0x7FFF.FFFF)
421 : For x >= 31/64, the result is 0x7FFF.FFFF
422 :
423 : \param x
424 :
425 : \return pow(2,(x/64))
426 : */
427 : /************************************************************************/
428 : Word32 BASOP_Util_InvLog2( Word32 x );
429 :
430 : Word16 BASOP_util_norm_s_bands2shift( Word16 x );
431 :
432 :
433 : /***********************************************************************/
434 : /*!
435 : \brief Calculate the headroom of the complex data in a 2 dimensional array
436 :
437 : \return number of headroom bits
438 : */
439 : Word16 BASOP_util_norm_l_dim2_cplx( const Word32 *const *re, /*!< Real part of 32 Bit input */
440 : const Word32 *const *im, /*!< Imag part if 32 Bit input */
441 : Word16 startBand, /*!< start band of cplx data */
442 : Word16 stopBand, /*!< stop band of cplx data */
443 : Word16 startSlot, /*!< start slot of cplx data */
444 : Word16 stopSlot /*!< stop slot of cplx data */
445 : );
446 : /****************************************************************************/
447 : /*!
448 : \brief Does a data copy of Word8 *arg1 to Word8 *arg2 with Word16 arg3 number of moves
449 : */
450 : void copyWord8( const Word8 *src, /*!< i : Source address */
451 : Word8 *dst, /*!< i : Destination address */
452 : const Word32 n ); /*!< i : Number of elements to copy */
453 :
454 :
455 : /****************************************************************************/
456 : /*!
457 : \brief Sets Word8 array arg1[] to zero for a length of Word16 arg2 elements
458 : */
459 : void set_zero_Word8( Word8 X[], /*!< i : Address of array */
460 : Word32 n ); /*!< i : Number of elements to set to zero */
461 :
462 : /****************************************************************************/
463 : /*!
464 : \brief Does a multiplication of Word32 * Word16 input values
465 :
466 : \return z32 = x32 * y16
467 : */
468 : Word32 L_mult0_3216( Word32 x, /*!< : Multiplier */
469 : Word16 y ); /*!< : Multiplicand */
470 :
471 : /* Calculate sin/cos. Angle in 2Q13 format, result has exponent = 1 */
472 : Word16 getCosWord16( Word16 theta );
473 : Word16 getSinWord16( Word16 theta );
474 : Word32 getCosWord32( Word32 theta );
475 : /**
476 : * \brief calculate cosine of angle. Tuned for ISF domain.
477 : * \param theta Angle normalized to radix 2, theta = (angle in radians)*2.0/pi
478 : * \return result with exponent 0.
479 : */
480 : Word16 getCosWord16R2( Word16 theta );
481 : Word16 getSineWord16R2( Word16 theta );
482 : /****************************************************************************/
483 : /*!
484 : \brief Sets Array Word16 arg1 to value Word16 arg2 for Word16 arg3 elements
485 : */
486 : void set_val_Word16( Word16 X[], /*!< Address of array */
487 : const Word16 val, /*!< Value to copy into array */
488 : Word16 n ); /*!< Number of elements to process */
489 :
490 :
491 : /****************************************************************************/
492 : /*!
493 : \brief Sets Array Word32 arg1 to value Word32 arg2 for Word16 arg3 elements
494 : */
495 : void set_val_Word32( Word32 X[], /*!< Address of array */
496 : const Word32 val, /*!< Value to copy into array */
497 : Word16 n ); /*!< Number of elements to process */
498 :
499 : /****************************************************************************/
500 : /*!
501 : \brief Does a multiplication of Word16 input values
502 :
503 : \return z16 = x16 * y16
504 : */
505 : Word16 mult0( Word16 x, /* i : Multiplier */
506 : Word16 y ); /* i : Multiplicand */
507 :
508 : /**
509 : * \brief calculate cosine of angle. Tuned for ISF domain.
510 : * \param theta Angle normalized to radix 2, theta = (angle in radians)*2.0/pi
511 : * \return result with exponent 0.
512 : */
513 : Word16 getCosWord16R2( Word16 theta );
514 :
515 :
516 : /****************************************************************************/
517 : /*!
518 : \brief square root abacus algorithm
519 :
520 : \return integer sqrt(x)
521 : */
522 : Word16 getSqrtWord32( Word32 x );
523 :
524 : /****************************************************************************/
525 : /*!
526 : \brief finds index of min Word16 in array
527 :
528 : \return index of min Word16
529 : */
530 : Word16 findIndexOfMinWord16( Word16 *x, const Word16 len );
531 :
532 : /****************************************************************************/
533 : /*!
534 : \brief finds index of min Word32 in array
535 :
536 : \return index of min Word32
537 : */
538 : Word16 findIndexOfMinWord32( Word32 *x, const Word16 len );
539 : Word16 findIndexOfMinWord64( Word64 *x, const Word16 len );
540 :
541 : /****************************************************************************/
542 : /*!
543 : \brief finds index of max Word16 in array
544 :
545 : \return index of max Word16
546 : */
547 : Word16 findIndexOfMaxWord16( Word16 *x, const Word16 len );
548 :
549 : /****************************************************************************/
550 : /*!
551 : \brief finds index of max Word32 in array
552 :
553 : \return index of max Word32
554 : */
555 : Word16 findIndexOfMaxWord32( Word32 *x, const Word16 len );
556 :
557 : /****************************************************************************/
558 : /*!
559 : \brief 16x16->16 integer multiplication without overflow control
560 :
561 : \return 16x16->16 integer
562 : */
563 : Word16 imult1616( Word16 x, Word16 y );
564 :
565 : /****************************************************************************/
566 : /*!
567 : \brief 32x16->32 integer multiplication with overflow control
568 :
569 : \return 32x16->32 integer
570 : */
571 : Word32 imult3216( Word32 x, Word16 y );
572 :
573 : /****************************************************************************/
574 : /*!
575 : \brief 16/16->16 unsigned integer division
576 :
577 : x and y have to be positive, x has to be < 16384
578 :
579 : \return 16/16->16 integer
580 : */
581 :
582 : Word16 idiv1616U_IVAS( Word16 x, Word16 y );
583 : Word16 idiv1616U( Word16 x, Word16 y );
584 :
585 : /****************************************************************************/
586 : /*!
587 : \brief 16/16->16 signed integer division
588 :
589 : x and y have to be positive, x has to be < 16384
590 :
591 : \return 16/16->16 integer
592 : */
593 :
594 : Word16 idiv1616( Word16 x, Word16 y );
595 :
596 : Word16 idiv1616_1( Word16 x, Word16 y );
597 :
598 : /*------------------------------------------------------------------*
599 : * Dot_product16HQ:
600 : *
601 : * \brief Compute scalar product of <x[],y[]> using 64-bit accumulator.
602 : *
603 : * Performs normalization of the result, returns the exponent
604 : * Note: In contrast to dotWord32, no headroom is required for data
605 : * in x[] and y[], means, they may have any format Qn
606 : *------------------------------------------------------------------*/
607 : Word32 Dot_product16HQ( /*<! o : normalized result Q31 */
608 : const Word32 L_off, /*<! i : initial sum value Qn */
609 : const Word16 x[], /*<! i : x vector Qn */
610 : const Word16 y[], /*<! i : y vector Qn */
611 : const Word16 lg, /*<! i : vector length, range [0..7FFF] Q0 */
612 : Word16 *exp /*<! o : exponent of result in [-32,31] Q0 */
613 : );
614 :
615 : /*------------------------------------------------------------------*
616 : * sum_array_norm:
617 : *
618 : * \brief Compute array summation of x[] using 64-bit accumulator.
619 : *
620 : * Performs normalization of the result, returns the exponent
621 : * Note: no headroom is required for data in x[]
622 : *------------------------------------------------------------------*/
623 : Word32 sum_array_norm( /* o : normalized result Q31 */
624 : const Word32 x[], /* i : x vector Qn */
625 : const Word16 lg, /* i : vector length, range [0..7FFF] Q0 */
626 : Word16 *exp /* o : exponent of result in [-32,31] Q0 */
627 : );
628 :
629 : /*------------------------------------------------------------------*
630 : * norm_llQ31:
631 : *
632 : * \brief Compute normalized Q31 Values out of overflowed Q31 value
633 : *
634 : * Performs the calculation of a normalized Q31 Value with its
635 : * scalingfactor, taking into account the overflowed Q31 input value
636 : * and the number of Carrys, collected.
637 : *------------------------------------------------------------------*/
638 : Word32 norm_llQ31( /* o : normalized result Q31 */
639 : Word32 L_c, /* i : upper bits of accu Q-1 */
640 : Word32 L_sum, /* i : lower bits of accu, unsigned Q31 */
641 : Word16 *exp /* o : exponent of result in [-32,31] Q0 */
642 : );
643 :
644 : #ifdef FIX_ISSUE_1817_REPLACE_CARRY_OVERFLOW
645 : /*------------------------------------------------------------------*
646 : * w_norm_llQ31:
647 : *
648 : * \brief Compute normalized Q31 Values out of overflowed Q31 value
649 : * using 64-bit operators
650 : *------------------------------------------------------------------*/
651 531840 : static inline Word32 w_norm_llQ31( Word64 L64_var1, Word16 *S_var2 ) /*Q31 - L_tmp_exp*/
652 : {
653 : Word32 L_result;
654 531840 : Word16 sh = W_norm( L64_var1 );
655 531840 : L_result = W_extract_h( W_shl( L64_var1, sh ) );
656 531840 : *S_var2 = ( L_result == 0 ) ? -32 : 32 - sh;
657 531840 : return L_result;
658 : }
659 : #endif
660 :
661 : /**
662 : * \brief Compute dot product of 1 32 bit vectors with itself
663 : * \param x input vector 1
664 : * \param headroom amount of headroom bits the input vector
665 : * \param length the length of the input vector
666 : * \param result_e pointer to where the exponent of the result will be stored into
667 : * \return the dot product of x and x.
668 : */
669 : Word32 Norm32Norm( const Word32 *x, const Word16 headroom, const Word16 length, Word16 *result_e );
670 :
671 :
672 : /*------------------------------------------------------------------*
673 : * Dot_productSq16HQ:
674 : *
675 : * \brief Compute scalar product of <x[],x[]> using 64-bit accumulator.
676 : *
677 : * Performs normalization of the result, returns the exponent
678 : * Note: In contrast to dotWord32, no headroom is required for data
679 : * in x[], means, they may have any format Qn
680 : *------------------------------------------------------------------*/
681 : Word32 Dot_productSq16HQ( /*<! o : normalized result Q31 */
682 : const Word32 L_off, /*<! i : initial sum value Qn */
683 : const Word16 x[], /*<! i : x vector Qn */
684 : const Word16 lg, /*<! i : vector length, range [0..7FFF] Q0 */
685 : Word16 *exp /*<! o : exponent of result in [-32,31] Q0 */
686 : );
687 : /*------------------------------------------------------------------*
688 : * dotp_s_fx:
689 : *
690 : * \brief Compute scalar product of <x[],y[]> using 64-bit accumulator.
691 : *
692 : * Performs no normalization of the result
693 : *------------------------------------------------------------------*/
694 : Word32 dotp_s_fx( /*<! o : dot product of vector x and y 16Q15 */
695 : const Word16 *x, /*<! i : vector x 6Q9 */
696 : const Word16 *y, /*<! i : vector y 6Q9 */
697 : const Word16 n, /*<! i : vector length Q0 */
698 : Word16 s /*<! i : headroom Q0 */
699 : );
700 :
701 : /**
702 : * \brief return 2 ^ (exp * 2^exp_e)
703 : * \param exp_m mantissa of the exponent to 2.0f
704 : * \param exp_e exponent of the exponent to 2.0f
705 : * \param result_e pointer to a INT where the exponent of the result will be stored into
706 : * \return mantissa of the result
707 : */
708 : Word32 BASOP_util_Pow2(
709 : const Word32 exp_m,
710 : const Word16 exp_e,
711 : Word16 *result_e );
712 :
713 :
714 : Word32 Isqrt_lc1(
715 : Word32 frac, /* i : Q31: normalized value (1.0 < frac <= 0.5) */
716 : Word16 *exp /* i/o: exponent (value = frac x 2^exponent) */
717 : );
718 :
719 : /**
720 : * \brief return 1/x
721 : * \param x index of lookup table
722 : * \return Word16 value of 1/x
723 : */
724 : Word16 getNormReciprocalWord16( Word16 x );
725 :
726 : /**
727 : * \brief return (1/x) << s
728 : * \param x index of lookup table
729 : * \param s shift factor
730 : * \return Word16 value of (1/x) << s
731 : */
732 : Word16 getNormReciprocalWord16Scale( Word16 x, Word16 s );
733 :
734 :
735 : /*************************************************************************
736 : *
737 : * FUNCTION: BASOP_Util_fPow()
738 : */
739 : /**
740 : * \brief BASOP_Util_fPow
741 : *
742 : * PURPOSE: Computes pow(base_m, base_e, exp_m, exp_e), where base_m and base_e
743 : * specify the base, and exp_m and exp_e specify the exponent.
744 : * The result is returned in a mantissa and exponent representation.
745 : *
746 : * DESCRIPTION:
747 : * The function BASOP_Util_fPow(L_x) calculates the power function by
748 : * calculating 2 ^ (log2(base)*exp)
749 : *
750 : * \param base_m mantissa of base
751 : * \param base_e exponent of base
752 : * \param exp_m mantissa of exponent
753 : * \param exp_e exponent of exponent
754 : * \param result_e pointer to exponent of result
755 : * \return Word32 mantissa of result
756 : *
757 : *************************************************************************/
758 :
759 : Word32 BASOP_Util_fPow( /* o : mantissa of result */
760 : Word32 base_m,
761 : Word16 base_e, /* i : input value for base (mantissa and exponent) */
762 : Word32 exp_m,
763 : Word16 exp_e, /* i : input value for exponent (mantissa and exponent) */
764 : Word16 *result_e /* o : output pointer to exponent of result */
765 : );
766 :
767 : /*___________________________________________________________________________
768 : | |
769 : | Function Name : Dot_product12_offs() |
770 : | |
771 : | Compute scalar product of <x[],y[]> using accumulator. |
772 : | The parameter 'L_off' is added to the accumulation result. |
773 : | The result is normalized (in Q31) with exponent (0..30). |
774 : | Notes: |
775 : | o data in x[],y[] must provide enough headroom for accumulation |
776 : | o L_off must correspond in format with product of x,y |
777 : | Example: 0.01f for Q9 x Q9: 0x0000147B in Q19 |
778 : | means: L_off = FL2WORD32_SCALE(0.01f,31-19) |
779 : |---------------------------------------------------------------------------|
780 : | Algorithm: |
781 : | |
782 : | dot_product = L_off + sum(x[i]*y[i]) i=0..N-1 |
783 : |___________________________________________________________________________|
784 : */
785 :
786 : Word32 Dot_product12_offs( /* (o) Q31: normalized result (1 < val <= -1) */
787 : const Word16 x[], /* (i) 12bits: x vector */
788 : const Word16 y[], /* (i) 12bits: y vector */
789 : const Word16 lg, /* (i) : vector length in range [1..256] */
790 : Word16 *exp, /* (o) : exponent of result (0..+30) */
791 : Word32 L_off /* (i) initial summation offset /2 */
792 : );
793 :
794 : Word32 Dot_product15_offs( /* (o) Q31: normalized result (1 < val <= -1) */
795 : const Word16 x[], /* (i) 15bits: x vector */
796 : const Word16 y[], /* (i) 15bits: y vector */
797 : const Word16 lg, /* (i) : vector length in range [1..256] */
798 : Word16 *exp, /* (o) : exponent of result (0..+30) */
799 : Word32 L_off /* (i) initial summation offset */
800 : );
801 :
802 : /*!**********************************************************************
803 : \brief Add two values given by mantissa and exponent.
804 :
805 : Mantissas are in 32-bit-fractional format with values between 0 and 1. <br>
806 : The base for exponents is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
807 :
808 : ************************************************************************/
809 : Word32 BASOP_Util_Add_Mant32Exp /* o : normalized result mantissa */
810 : ( Word32 a_m, /* i : Mantissa of 1st operand a */
811 : Word16 a_e, /* i : Exponent of 1st operand a */
812 : Word32 b_m, /* i : Mantissa of 2nd operand b */
813 : Word16 b_e, /* i : Exponent of 2nd operand b */
814 : Word16 *ptr_e ); /* o : exponent of result */
815 :
816 : /*!**********************************************************************
817 : \brief Returns the comparison result of two normalized values given by mantissa and exponent.
818 : return value: -1: a < b, 0: a == b, 1; a > b
819 :
820 : Mantissas are in 32-bit-fractional format with values between 0 and 1. <br>
821 : The base for exponents is 2. Example: \f$ a = a\_m * 2^{a\_e} \f$<br>
822 :
823 : ************************************************************************/
824 : Word16 BASOP_Util_Cmp_Mant32Exp /*!< o: flag: result of comparison */
825 : ( Word32 a_m, /*!< i: Mantissa of 1st operand a */
826 : Word16 a_e, /*!< i: Exponent of 1st operand a */
827 : Word32 b_m, /*!< i: Mantissa of 2nd operand b */
828 : Word16 b_e ); /*!< i: Exponent of 2nd operand b */
829 :
830 : /********************************************************************
831 : * bufferCopyFx
832 : *
833 : * \brief copies buffer while preserving Format of destination buffer
834 : *********************************************************************
835 : */
836 : void bufferCopyFx(
837 : Word16 *src, /*<! Qx pointer to input buffer */
838 : Word16 *dest, /*<! Qx pointer to output buffer */
839 : Word16 length, /*<! Q0 length of buffer to copy */
840 : Word16 Qf_src, /*<! Q0 Q format (frac-bits) of source buffer */
841 : Word16 Qf_dest, /*<! Q0 Q format (frac-bits )of dest buffer */
842 : Word16 Q_src, /*<! Q0 exponent of source buffer */
843 : Word16 Q_dest /*<! Q0 exponent of destination buffer */
844 : );
845 :
846 : /****************************************************************************/
847 : /*!
848 : \brief Accumulates multiplications
849 :
850 : Accumulates the elementwise multiplications of Word32 Array bufX32 with Word16 Array bufY16
851 : pointed to by arg1 to arg4 including the corresponding exponents. Length of to be multiplied arrays is arg5,
852 :
853 : \return Word32 result of accumulated multiplications over Word32 array arg1 and Word16 array arg3 and Word16 pointer
854 : to exponent of the result
855 : */
856 : Word32 dotWord32_16_Mant32Exp( const Word32 *bufX32, /* i: 32-bit buffer with unknown headroom */
857 : Word16 bufX32_exp, /* i: exponent of buffer bufX32 */
858 : const Word16 *bufY16, /* i: 16-bit buffer quite right-aligned */
859 : Word16 bufY16_exp, /* i: exponent of buffer bufY16 */
860 : Word16 len, /* i: buffer len to process */
861 : Word16 *exp ); /* o: result exponent */
862 :
863 : /*!**********************************************************************
864 : \brief Converts linear factor or energy to Decibel
865 : return value: fEnergy=0: 20 * log10(x * 2^{x\_e}),
866 : fEnergy=1: 10 * log10(x * 2^{x\_e})
867 :
868 : Mantissa x is in 32-bit-fractional format with values between 0 and 1. <br>
869 : The base for exponent x_e is 2. <br>
870 :
871 : ************************************************************************/
872 : Word16 BASOP_Util_lin2dB( /*!< o: dB value (7Q8) */
873 : Word32 x, /*!< i: mantissa */
874 : Word16 x_e, /*!< i: exponent */
875 : Word16 fEnergy ); /*!< i: flag indicating if x is energy */
876 :
877 : /*!**********************************************************************
878 : \brief Calculates atan(x).
879 : ************************************************************************/
880 : Word16 BASOP_util_atan( /*!< o: atan(x) [-pi/2;pi/2] 1Q14 */
881 : Word32 x /*!< i: input data (-64;64) 6Q25 */
882 : );
883 :
884 : /*!**********************************************************************
885 : \brief Calculates atan2(y,x).
886 : ************************************************************************/
887 : Word16 BASOP_util_atan2( /*!< o: atan2(y,x) [-pi,pi] Q13 */
888 : Word32 y, /*!< i: */
889 : Word32 x, /*!< i: */
890 : Word16 e /*!< i: exponent difference (exp_y - exp_x) */
891 : );
892 :
893 : /****************************************************************************/
894 : /*!
895 : \brief Accumulates multiplications
896 :
897 : Accumulates the elementwise multiplications of Word32 Array X with Word16 Array Y
898 : pointed to by arg1 and arg2 with specified headroom. Length of to be multiplied arrays is arg3,
899 : headroom with has to be taken into account is specified in arg4
900 :
901 : \return Word32 result of accumulated multiplications over Word32 array arg1 and Word16 array arg2 and Word16 pointer
902 : to exponent correction factor which needs to be added to the exponent of the result vector
903 : */
904 : Word32 dotWord32_16_guards( const Word32 *X, const Word16 *Y, Word16 n, Word16 hr, Word16 *shift );
905 :
906 : // function defined in basop_util commented from basop_tcx_utils.c
907 : Word16 compMantExp16Unorm( Word16 m1, Word16 e1, Word16 m2, Word16 e2 );
908 :
909 : cmplx CL_scale_t( cmplx x, Word16 y );
910 : cmplx CL_dscale_t( cmplx x, Word16 y1, Word16 y2 );
911 : cmplx CL_mult_32x16( cmplx input, cmplx_s coeff );
912 :
913 : #endif /* __BASOP_UTIL_H__ */
|