b_exp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 1985, 1993
  3. * The Regents of the University of California. 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. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /* @(#)exp.c 8.1 (Berkeley) 6/4/93 */
  30. #include "cdefs-compat.h"
  31. //__FBSDID("$FreeBSD: src/lib/msun/bsdsrc/b_exp.c,v 1.9 2011/10/16 05:37:20 das Exp $");
  32. #include <openlibm_math.h>
  33. /* EXP(X)
  34. * RETURN THE EXPONENTIAL OF X
  35. * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
  36. * CODED IN C BY K.C. NG, 1/19/85;
  37. * REVISED BY K.C. NG on 2/6/85, 2/15/85, 3/7/85, 3/24/85, 4/16/85, 6/14/86.
  38. *
  39. * Required system supported functions:
  40. * scalb(x,n)
  41. * copysign(x,y)
  42. * finite(x)
  43. *
  44. * Method:
  45. * 1. Argument Reduction: given the input x, find r and integer k such
  46. * that
  47. * x = k*ln2 + r, |r| <= 0.5*ln2 .
  48. * r will be represented as r := z+c for better accuracy.
  49. *
  50. * 2. Compute exp(r) by
  51. *
  52. * exp(r) = 1 + r + r*R1/(2-R1),
  53. * where
  54. * R1 = x - x^2*(p1+x^2*(p2+x^2*(p3+x^2*(p4+p5*x^2)))).
  55. *
  56. * 3. exp(x) = 2^k * exp(r) .
  57. *
  58. * Special cases:
  59. * exp(INF) is INF, exp(NaN) is NaN;
  60. * exp(-INF)= 0;
  61. * for finite argument, only exp(0)=1 is exact.
  62. *
  63. * Accuracy:
  64. * exp(x) returns the exponential of x nearly rounded. In a test run
  65. * with 1,156,000 random arguments on a VAX, the maximum observed
  66. * error was 0.869 ulps (units in the last place).
  67. */
  68. #include "mathimpl.h"
  69. static const double p1 = 0x1.555555555553ep-3;
  70. static const double p2 = -0x1.6c16c16bebd93p-9;
  71. static const double p3 = 0x1.1566aaf25de2cp-14;
  72. static const double p4 = -0x1.bbd41c5d26bf1p-20;
  73. static const double p5 = 0x1.6376972bea4d0p-25;
  74. static const double ln2hi = 0x1.62e42fee00000p-1;
  75. static const double ln2lo = 0x1.a39ef35793c76p-33;
  76. static const double lnhuge = 0x1.6602b15b7ecf2p9;
  77. static const double lntiny = -0x1.77af8ebeae354p9;
  78. static const double invln2 = 0x1.71547652b82fep0;
  79. #if 0
  80. OLM_DLLEXPORT double exp(x)
  81. double x;
  82. {
  83. double z,hi,lo,c;
  84. int k;
  85. #if !defined(vax)&&!defined(tahoe)
  86. if(x!=x) return(x); /* x is NaN */
  87. #endif /* !defined(vax)&&!defined(tahoe) */
  88. if( x <= lnhuge ) {
  89. if( x >= lntiny ) {
  90. /* argument reduction : x --> x - k*ln2 */
  91. k=invln2*x+copysign(0.5,x); /* k=NINT(x/ln2) */
  92. /* express x-k*ln2 as hi-lo and let x=hi-lo rounded */
  93. hi=x-k*ln2hi;
  94. x=hi-(lo=k*ln2lo);
  95. /* return 2^k*[1+x+x*c/(2+c)] */
  96. z=x*x;
  97. c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5))));
  98. return scalb(1.0+(hi-(lo-(x*c)/(2.0-c))),k);
  99. }
  100. /* end of x > lntiny */
  101. else
  102. /* exp(-big#) underflows to zero */
  103. if(finite(x)) return(scalb(1.0,-5000));
  104. /* exp(-INF) is zero */
  105. else return(0.0);
  106. }
  107. /* end of x < lnhuge */
  108. else
  109. /* exp(INF) is INF, exp(+big#) overflows to INF */
  110. return( finite(x) ? scalb(1.0,5000) : x);
  111. }
  112. #endif
  113. /* returns exp(r = x + c) for |c| < |x| with no overlap. */
  114. double __exp__D(x, c)
  115. double x, c;
  116. {
  117. double z,hi,lo;
  118. int k;
  119. if (x != x) /* x is NaN */
  120. return(x);
  121. if ( x <= lnhuge ) {
  122. if ( x >= lntiny ) {
  123. /* argument reduction : x --> x - k*ln2 */
  124. z = invln2*x;
  125. k = z + copysign(.5, x);
  126. /* express (x+c)-k*ln2 as hi-lo and let x=hi-lo rounded */
  127. hi=(x-k*ln2hi); /* Exact. */
  128. x= hi - (lo = k*ln2lo-c);
  129. /* return 2^k*[1+x+x*c/(2+c)] */
  130. z=x*x;
  131. c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5))));
  132. c = (x*c)/(2.0-c);
  133. return scalbn(1.+(hi-(lo - c)), k);
  134. }
  135. /* end of x > lntiny */
  136. else
  137. /* exp(-big#) underflows to zero */
  138. if(isfinite(x)) return(scalbn(1.0,-5000));
  139. /* exp(-INF) is zero */
  140. else return(0.0);
  141. }
  142. /* end of x < lnhuge */
  143. else
  144. /* exp(INF) is INF, exp(+big#) overflows to INF */
  145. return( isfinite(x) ? scalbn(1.0,5000) : x);
  146. }