log2l.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_log2l.c */
  2. /*
  3. * Copyright (c) 2008 Stephen L. Moshier <[email protected]>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Base 2 logarithm, long double precision
  19. *
  20. *
  21. * SYNOPSIS:
  22. *
  23. * long double x, y, log2l();
  24. *
  25. * y = log2l( x );
  26. *
  27. *
  28. * DESCRIPTION:
  29. *
  30. * Returns the base 2 logarithm of x.
  31. *
  32. * The argument is separated into its exponent and fractional
  33. * parts. If the exponent is between -1 and +1, the (natural)
  34. * logarithm of the fraction is approximated by
  35. *
  36. * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x).
  37. *
  38. * Otherwise, setting z = 2(x-1)/x+1),
  39. *
  40. * log(x) = z + z**3 P(z)/Q(z).
  41. *
  42. *
  43. * ACCURACY:
  44. *
  45. * Relative error:
  46. * arithmetic domain # trials peak rms
  47. * IEEE 0.5, 2.0 30000 9.8e-20 2.7e-20
  48. * IEEE exp(+-10000) 70000 5.4e-20 2.3e-20
  49. *
  50. * In the tests over the interval exp(+-10000), the logarithms
  51. * of the random arguments were uniformly distributed over
  52. * [-10000, +10000].
  53. */
  54. #include "libm.h"
  55. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  56. long double log2l(long double x)
  57. {
  58. return log2(x);
  59. }
  60. #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
  61. /* Coefficients for ln(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
  62. * 1/sqrt(2) <= x < sqrt(2)
  63. * Theoretical peak relative error = 6.2e-22
  64. */
  65. static const long double P[] = {
  66. 4.9962495940332550844739E-1L,
  67. 1.0767376367209449010438E1L,
  68. 7.7671073698359539859595E1L,
  69. 2.5620629828144409632571E2L,
  70. 4.2401812743503691187826E2L,
  71. 3.4258224542413922935104E2L,
  72. 1.0747524399916215149070E2L,
  73. };
  74. static const long double Q[] = {
  75. /* 1.0000000000000000000000E0,*/
  76. 2.3479774160285863271658E1L,
  77. 1.9444210022760132894510E2L,
  78. 7.7952888181207260646090E2L,
  79. 1.6911722418503949084863E3L,
  80. 2.0307734695595183428202E3L,
  81. 1.2695660352705325274404E3L,
  82. 3.2242573199748645407652E2L,
  83. };
  84. /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
  85. * where z = 2(x-1)/(x+1)
  86. * 1/sqrt(2) <= x < sqrt(2)
  87. * Theoretical peak relative error = 6.16e-22
  88. */
  89. static const long double R[4] = {
  90. 1.9757429581415468984296E-3L,
  91. -7.1990767473014147232598E-1L,
  92. 1.0777257190312272158094E1L,
  93. -3.5717684488096787370998E1L,
  94. };
  95. static const long double S[4] = {
  96. /* 1.00000000000000000000E0L,*/
  97. -2.6201045551331104417768E1L,
  98. 1.9361891836232102174846E2L,
  99. -4.2861221385716144629696E2L,
  100. };
  101. /* log2(e) - 1 */
  102. #define LOG2EA 4.4269504088896340735992e-1L
  103. #define SQRTH 0.70710678118654752440L
  104. long double log2l(long double x)
  105. {
  106. long double y, z;
  107. int e;
  108. if (isnan(x))
  109. return x;
  110. if (x == INFINITY)
  111. return x;
  112. if (x <= 0.0) {
  113. if (x == 0.0)
  114. return -1/(x+0); /* -inf with divbyzero */
  115. return 0/0.0f; /* nan with invalid */
  116. }
  117. /* separate mantissa from exponent */
  118. /* Note, frexp is used so that denormal numbers
  119. * will be handled properly.
  120. */
  121. x = frexpl(x, &e);
  122. /* logarithm using log(x) = z + z**3 P(z)/Q(z),
  123. * where z = 2(x-1)/x+1)
  124. */
  125. if (e > 2 || e < -2) {
  126. if (x < SQRTH) { /* 2(2x-1)/(2x+1) */
  127. e -= 1;
  128. z = x - 0.5;
  129. y = 0.5 * z + 0.5;
  130. } else { /* 2 (x-1)/(x+1) */
  131. z = x - 0.5;
  132. z -= 0.5;
  133. y = 0.5 * x + 0.5;
  134. }
  135. x = z / y;
  136. z = x*x;
  137. y = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3));
  138. goto done;
  139. }
  140. /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
  141. if (x < SQRTH) {
  142. e -= 1;
  143. x = 2.0*x - 1.0;
  144. } else {
  145. x = x - 1.0;
  146. }
  147. z = x*x;
  148. y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 7));
  149. y = y - 0.5*z;
  150. done:
  151. /* Multiply log of fraction by log2(e)
  152. * and base 2 exponent by 1
  153. *
  154. * ***CAUTION***
  155. *
  156. * This sequence of operations is critical and it may
  157. * be horribly defeated by some compiler optimizers.
  158. */
  159. z = y * LOG2EA;
  160. z += x * LOG2EA;
  161. z += y;
  162. z += x;
  163. z += e;
  164. return z;
  165. }
  166. #endif