s_exp2f.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*-
  2. * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. #include "cdefs-compat.h"
  27. //__FBSDID("$FreeBSD: src/lib/msun/src/s_exp2f.c,v 1.9 2008/02/22 02:27:34 das Exp $");
  28. #include <float.h>
  29. #include <openlibm_math.h>
  30. #include "math_private.h"
  31. #define TBLBITS 4
  32. #define TBLSIZE (1 << TBLBITS)
  33. static const float
  34. huge = 0x1p100f,
  35. redux = 0x1.8p23f / TBLSIZE,
  36. P1 = 0x1.62e430p-1f,
  37. P2 = 0x1.ebfbe0p-3f,
  38. P3 = 0x1.c6b348p-5f,
  39. P4 = 0x1.3b2c9cp-7f;
  40. static volatile float twom100 = 0x1p-100f;
  41. static const double exp2ft[TBLSIZE] = {
  42. 0x1.6a09e667f3bcdp-1,
  43. 0x1.7a11473eb0187p-1,
  44. 0x1.8ace5422aa0dbp-1,
  45. 0x1.9c49182a3f090p-1,
  46. 0x1.ae89f995ad3adp-1,
  47. 0x1.c199bdd85529cp-1,
  48. 0x1.d5818dcfba487p-1,
  49. 0x1.ea4afa2a490dap-1,
  50. 0x1.0000000000000p+0,
  51. 0x1.0b5586cf9890fp+0,
  52. 0x1.172b83c7d517bp+0,
  53. 0x1.2387a6e756238p+0,
  54. 0x1.306fe0a31b715p+0,
  55. 0x1.3dea64c123422p+0,
  56. 0x1.4bfdad5362a27p+0,
  57. 0x1.5ab07dd485429p+0,
  58. };
  59. /*
  60. * exp2f(x): compute the base 2 exponential of x
  61. *
  62. * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927.
  63. *
  64. * Method: (equally-spaced tables)
  65. *
  66. * Reduce x:
  67. * x = 2**k + y, for integer k and |y| <= 1/2.
  68. * Thus we have exp2f(x) = 2**k * exp2(y).
  69. *
  70. * Reduce y:
  71. * y = i/TBLSIZE + z for integer i near y * TBLSIZE.
  72. * Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z),
  73. * with |z| <= 2**-(TBLSIZE+1).
  74. *
  75. * We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a
  76. * degree-4 minimax polynomial with maximum error under 1.4 * 2**-33.
  77. * Using double precision for everything except the reduction makes
  78. * roundoff error insignificant and simplifies the scaling step.
  79. *
  80. * This method is due to Tang, but I do not use his suggested parameters:
  81. *
  82. * Tang, P. Table-driven Implementation of the Exponential Function
  83. * in IEEE Floating-Point Arithmetic. TOMS 15(2), 144-157 (1989).
  84. */
  85. DLLEXPORT float
  86. exp2f(float x)
  87. {
  88. double tv, twopk, u, z;
  89. float t;
  90. u_int32_t hx, ix, i0;
  91. int32_t k;
  92. /* Filter out exceptional cases. */
  93. GET_FLOAT_WORD(hx, x);
  94. ix = hx & 0x7fffffff; /* high word of |x| */
  95. if(ix >= 0x43000000) { /* |x| >= 128 */
  96. if(ix >= 0x7f800000) {
  97. if ((ix & 0x7fffff) != 0 || (hx & 0x80000000) == 0)
  98. return (x + x); /* x is NaN or +Inf */
  99. else
  100. return (0.0); /* x is -Inf */
  101. }
  102. if(x >= 0x1.0p7f)
  103. return (huge * huge); /* overflow */
  104. if(x <= -0x1.2cp7f)
  105. return (twom100 * twom100); /* underflow */
  106. } else if (ix <= 0x33000000) { /* |x| <= 0x1p-25 */
  107. return (1.0f + x);
  108. }
  109. /* Reduce x, computing z, i0, and k. */
  110. STRICT_ASSIGN(float, t, x + redux);
  111. GET_FLOAT_WORD(i0, t);
  112. i0 += TBLSIZE / 2;
  113. k = (i0 >> TBLBITS) << 20;
  114. i0 &= TBLSIZE - 1;
  115. t -= redux;
  116. z = x - t;
  117. INSERT_WORDS(twopk, 0x3ff00000 + k, 0);
  118. /* Compute r = exp2(y) = exp2ft[i0] * p(z). */
  119. tv = exp2ft[i0];
  120. u = tv * z;
  121. tv = tv + u * (P1 + z * P2) + u * (z * z) * (P3 + z * P4);
  122. /* Scale by 2**(k>>20). */
  123. return (tv * twopk);
  124. }