log2f.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* log2f.c
  2. *
  3. * Base 2 logarithm
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, log2f();
  10. *
  11. * y = log2f( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns the base 2 logarithm of x.
  18. *
  19. * The argument is separated into its exponent and fractional
  20. * parts. If the exponent is between -1 and +1, the base e
  21. * logarithm of the fraction is approximated by
  22. *
  23. * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x).
  24. *
  25. * Otherwise, setting z = 2(x-1)/x+1),
  26. *
  27. * log(x) = z + z**3 P(z)/Q(z).
  28. *
  29. *
  30. *
  31. * ACCURACY:
  32. *
  33. * Relative error:
  34. * arithmetic domain # trials peak rms
  35. * IEEE exp(+-88) 100000 1.1e-7 2.4e-8
  36. * IEEE 0.5, 2.0 100000 1.1e-7 3.0e-8
  37. *
  38. * In the tests over the interval [exp(+-88)], the logarithms
  39. * of the random arguments were uniformly distributed.
  40. *
  41. * ERROR MESSAGES:
  42. *
  43. * log singularity: x = 0; returns MINLOGF/log(2)
  44. * log domain: x < 0; returns MINLOGF/log(2)
  45. */
  46. /*
  47. Cephes Math Library Release 2.2: June, 1992
  48. Copyright 1984, 1992 by Stephen L. Moshier
  49. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  50. */
  51. #include <math.h>
  52. static char fname[] = {"log2"};
  53. /* Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)
  54. * 1/sqrt(2) <= x < sqrt(2)
  55. */
  56. static float P[] = {
  57. 7.0376836292E-2,
  58. -1.1514610310E-1,
  59. 1.1676998740E-1,
  60. -1.2420140846E-1,
  61. 1.4249322787E-1,
  62. -1.6668057665E-1,
  63. 2.0000714765E-1,
  64. -2.4999993993E-1,
  65. 3.3333331174E-1
  66. };
  67. #define LOG2EA 0.44269504088896340735992
  68. #define SQRTH 0.70710678118654752440
  69. extern float MINLOGF, LOGE2F;
  70. float frexpf(float, int *), polevlf(float, float *, int);
  71. float log2f(float xx)
  72. {
  73. float x, y, z;
  74. int e;
  75. x = xx;
  76. /* Test for domain */
  77. if( x <= 0.0 )
  78. {
  79. if( x == 0.0 )
  80. mtherr( fname, SING );
  81. else
  82. mtherr( fname, DOMAIN );
  83. return( MINLOGF/LOGE2F );
  84. }
  85. /* separate mantissa from exponent */
  86. x = frexpf( x, &e );
  87. /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
  88. if( x < SQRTH )
  89. {
  90. e -= 1;
  91. x = 2.0*x - 1.0;
  92. }
  93. else
  94. {
  95. x = x - 1.0;
  96. }
  97. z = x*x;
  98. y = x * ( z * polevlf( x, P, 8 ) );
  99. y = y - 0.5 * z; /* y - 0.5 * x**2 */
  100. /* Multiply log of fraction by log2(e)
  101. * and base 2 exponent by 1
  102. *
  103. * ***CAUTION***
  104. *
  105. * This sequence of operations is critical and it may
  106. * be horribly defeated by some compiler optimizers.
  107. */
  108. z = y * LOG2EA;
  109. z += x * LOG2EA;
  110. z += y;
  111. z += x;
  112. z += (float )e;
  113. return( z );
  114. }