FreeCalypso > hg > gsm-codec-lib
comparison libtwamr/inv_sqrt.c @ 253:54f6bc41ed10
libtwamr: integrate a* modules
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Fri, 05 Apr 2024 06:08:15 +0000 |
| parents | |
| children | 07f936338de1 |
comparison
equal
deleted
inserted
replaced
| 252:57b4053559ff | 253:54f6bc41ed10 |
|---|---|
| 1 /* | |
| 2 ******************************************************************************** | |
| 3 * | |
| 4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001 | |
| 5 * R99 Version 3.3.0 | |
| 6 * REL-4 Version 4.1.0 | |
| 7 * | |
| 8 ******************************************************************************** | |
| 9 * | |
| 10 * File : inv_sqrt.c | |
| 11 * Purpose : Computes 1/sqrt(L_x), where L_x is positive. | |
| 12 * : If L_x is negative or zero, | |
| 13 * : the result is 1 (3fff ffff). | |
| 14 * Description : | |
| 15 * The function 1/sqrt(L_x) is approximated by a table and linear | |
| 16 * interpolation. The inverse square root is computed using the | |
| 17 * following steps: | |
| 18 * 1- Normalization of L_x. | |
| 19 * 2- If (30-exponent) is even then shift right once. | |
| 20 * 3- exponent = (30-exponent)/2 +1 | |
| 21 * 4- i = bit25-b31 of L_x; 16<=i<=63 because of normalization. | |
| 22 * 5- a = bit10-b24 | |
| 23 * 6- i -=16 | |
| 24 * 7- L_y = table[i]<<16 - (table[i] - table[i+1]) * a * 2 | |
| 25 * 8- L_y >>= exponent | |
| 26 * | |
| 27 ******************************************************************************** | |
| 28 */ | |
| 29 /* | |
| 30 ******************************************************************************** | |
| 31 * MODULE INCLUDE FILE AND VERSION ID | |
| 32 ******************************************************************************** | |
| 33 */ | |
| 34 #include "namespace.h" | |
| 35 #include "inv_sqrt.h" | |
| 36 const char inv_sqrt_id[] = "@(#)$Id $" inv_sqrt_h; | |
| 37 | |
| 38 /* | |
| 39 ******************************************************************************** | |
| 40 * INCLUDE FILES | |
| 41 ******************************************************************************** | |
| 42 */ | |
| 43 #include "typedef.h" | |
| 44 #include "basic_op.h" | |
| 45 #include "no_count.h" | |
| 46 | |
| 47 /* | |
| 48 ******************************************************************************** | |
| 49 * LOCAL VARIABLES AND TABLES | |
| 50 ******************************************************************************** | |
| 51 */ | |
| 52 #include "inv_sqrt.tab" /* Table for inv_sqrt() */ | |
| 53 | |
| 54 /* | |
| 55 ******************************************************************************** | |
| 56 * PUBLIC PROGRAM CODE | |
| 57 ******************************************************************************** | |
| 58 */ | |
| 59 | |
| 60 Word32 Inv_sqrt ( /* (o) : output value */ | |
| 61 Word32 L_x /* (i) : input value */ | |
| 62 ) | |
| 63 { | |
| 64 Word16 exp, i, a, tmp; | |
| 65 Word32 L_y; | |
| 66 | |
| 67 test (); | |
| 68 if (L_x <= (Word32) 0) | |
| 69 return ((Word32) 0x3fffffffL); | |
| 70 | |
| 71 exp = norm_l (L_x); | |
| 72 L_x = L_shl (L_x, exp); /* L_x is normalize */ | |
| 73 | |
| 74 exp = sub (30, exp); | |
| 75 test (); logic16 (); | |
| 76 if ((exp & 1) == 0) /* If exponent even -> shift right */ | |
| 77 { | |
| 78 L_x = L_shr (L_x, 1); | |
| 79 } | |
| 80 exp = shr (exp, 1); | |
| 81 exp = add (exp, 1); | |
| 82 | |
| 83 L_x = L_shr (L_x, 9); | |
| 84 i = extract_h (L_x); /* Extract b25-b31 */ | |
| 85 L_x = L_shr (L_x, 1); | |
| 86 a = extract_l (L_x); /* Extract b10-b24 */ | |
| 87 a = a & (Word16) 0x7fff; logic16 (); | |
| 88 | |
| 89 i = sub (i, 16); | |
| 90 | |
| 91 L_y = L_deposit_h (table[i]); /* table[i] << 16 */ | |
| 92 tmp = sub (table[i], table[i + 1]); /* table[i] - table[i+1]) */ | |
| 93 L_y = L_msu (L_y, tmp, a); /* L_y -= tmp*a*2 */ | |
| 94 | |
| 95 L_y = L_shr (L_y, exp); /* denormalization */ | |
| 96 | |
| 97 return (L_y); | |
| 98 } |
