sqrt_l.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 : sqrt_l.c
  11. * Purpose : Computes sqrt(L_x), where L_x is positive.
  12. * : If L_var is negative or zero, the result is 0
  13. * Description :
  14. * The function sqrt(L_x) is approximated by a table and linear
  15. * interpolation. The square root is computed using the
  16. * following steps:
  17. * 1- Normalization of L_x.
  18. * 2- If exponent is even then shift right once.
  19. * 3- exponent = exponent/2
  20. * 4- i = bit25-b31 of L_x; 16<=i<=63 because of normalization.
  21. * 5- a = bit10-b24
  22. * 6- i -=16
  23. * 7- L_y = table[i]<<16 - (table[i] - table[i+1]) * a * 2
  24. * 8- return L_y and exponent so caller can do denormalization
  25. *
  26. ********************************************************************************
  27. */
  28. /*
  29. ********************************************************************************
  30. * MODULE INCLUDE FILE AND VERSION ID
  31. ********************************************************************************
  32. */
  33. #include "sqrt_l.h"
  34. const char sqrt_l_id[] = "@(#)$Id $" sqrt_l_h;
  35. /*
  36. ********************************************************************************
  37. * INCLUDE FILES
  38. ********************************************************************************
  39. */
  40. #include "typedef.h"
  41. #include "basic_op.h"
  42. #include "count.h"
  43. /*
  44. ********************************************************************************
  45. * LOCAL VARIABLES AND TABLES
  46. ********************************************************************************
  47. */
  48. #include "sqrt_l.tab" /* Table for sqrt_l_exp() */
  49. /*
  50. ********************************************************************************
  51. * PUBLIC PROGRAM CODE
  52. ********************************************************************************
  53. */
  54. Word32 sqrt_l_exp (/* o : output value, Q31 */
  55. Word32 L_x, /* i : input value, Q31 */
  56. Word16 *exp /* o : right shift to be applied to result, Q1 */
  57. )
  58. {
  59. /*
  60. y = sqrt(x)
  61. x = f * 2^-e, 0.5 <= f < 1 (normalization)
  62. y = sqrt(f) * 2^(-e/2)
  63. a) e = 2k --> y = sqrt(f) * 2^-k (k = e div 2,
  64. 0.707 <= sqrt(f) < 1)
  65. b) e = 2k+1 --> y = sqrt(f/2) * 2^-k (k = e div 2,
  66. 0.5 <= sqrt(f/2) < 0.707)
  67. */
  68. Word16 e, i, a, tmp;
  69. Word32 L_y;
  70. test ();
  71. if (L_x <= (Word32) 0)
  72. {
  73. *exp = 0; move16 ();
  74. return (Word32) 0;
  75. }
  76. e = norm_l_ex (L_x) & 0xFFFE; logic16 (); /* get next lower EVEN norm. exp */
  77. L_x = L_shl_ex (L_x, e); /* L_x is normalized to [0.25..1) */
  78. *exp = e; move16 (); /* return 2*exponent (or Q1) */
  79. L_x = L_shr_ex (L_x, 9);
  80. i = extract_h_ex (L_x); /* Extract b25-b31, 16 <= i <= 63 because
  81. of normalization */
  82. L_x = L_shr_ex (L_x, 1);
  83. a = extract_l_ex (L_x); /* Extract b10-b24 */
  84. a = a & (Word16) 0x7fff; logic16 ();
  85. i = sub_ex (i, 16); /* 0 <= i <= 47 */
  86. L_y = L_deposit_h_ex (table[i]); /* table[i] << 16 */
  87. tmp = sub_ex (table[i], table[i + 1]); /* table[i] - table[i+1]) */
  88. L_y = L_msu_ex (L_y, tmp, a); /* L_y -= tmp*a*2 */
  89. /* L_y = L_shr_ex (L_y, *exp); */ /* denormalization done by caller */
  90. return (L_y);
  91. }