inv_sqrt_ex.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "inv_sqrt_ex.h"
  35. const char inv_sqrt_id[] = "@(#)$Id $" inv_sqrt_h;
  36. /*
  37. ********************************************************************************
  38. * INCLUDE FILES
  39. ********************************************************************************
  40. */
  41. #include "typedef.h"
  42. #include "basic_op.h"
  43. #include "count.h"
  44. /*
  45. ********************************************************************************
  46. * LOCAL VARIABLES AND TABLES
  47. ********************************************************************************
  48. */
  49. #include "inv_sqrt_ex.tab" /* Table for inv_sqrt() */
  50. /*
  51. ********************************************************************************
  52. * PUBLIC PROGRAM CODE
  53. ********************************************************************************
  54. */
  55. Word32 Inv_sqrt_ex ( /* (o) : output value */
  56. Word32 L_x /* (i) : input value */
  57. )
  58. {
  59. Word16 exp, i, a, tmp;
  60. Word32 L_y;
  61. test ();
  62. if (L_x <= (Word32) 0)
  63. return ((Word32) 0x3fffffffL);
  64. exp = norm_l_ex (L_x);
  65. L_x = L_shl_ex (L_x, exp); /* L_x is normalize */
  66. exp = sub_ex (30, exp);
  67. test (); logic16 ();
  68. if ((exp & 1) == 0) /* If exponent even -> shift right */
  69. {
  70. L_x = L_shr_ex (L_x, 1);
  71. }
  72. exp = shr_ex (exp, 1);
  73. exp = add_ex (exp, 1);
  74. L_x = L_shr_ex (L_x, 9);
  75. i = extract_h_ex (L_x); /* Extract b25-b31 */
  76. L_x = L_shr_ex (L_x, 1);
  77. a = extract_l_ex (L_x); /* Extract b10-b24 */
  78. a = a & (Word16) 0x7fff; logic16 ();
  79. i = sub_ex (i, 16);
  80. L_y = L_deposit_h_ex (table[i]); /* table[i] << 16 */
  81. tmp = sub_ex (table[i], table[i + 1]); /* table[i] - table[i+1]) */
  82. L_y = L_msu_ex (L_y, tmp, a); /* L_y -= tmp*a*2 */
  83. L_y = L_shr_ex (L_y, exp); /* denormalization */
  84. return (L_y);
  85. }