expf.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_expf.c */
  2. /*
  3. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  4. */
  5. /*
  6. * ====================================================
  7. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  8. *
  9. * Developed at SunPro, a Sun Microsystems, Inc. business.
  10. * Permission to use, copy, modify, and distribute this
  11. * software is freely granted, provided that this notice
  12. * is preserved.
  13. * ====================================================
  14. */
  15. #include "libm.h"
  16. static const float
  17. halF[2] = {0.5,-0.5,},
  18. huge = 1.0e+30,
  19. o_threshold = 8.8721679688e+01, /* 0x42b17180 */
  20. u_threshold = -1.0397208405e+02, /* 0xc2cff1b5 */
  21. ln2HI[2] = { 6.9314575195e-01, /* 0x3f317200 */
  22. -6.9314575195e-01,},/* 0xbf317200 */
  23. ln2LO[2] = { 1.4286067653e-06, /* 0x35bfbe8e */
  24. -1.4286067653e-06,},/* 0xb5bfbe8e */
  25. invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */
  26. /*
  27. * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
  28. * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
  29. */
  30. P1 = 1.6666625440e-1, /* 0xaaaa8f.0p-26 */
  31. P2 = -2.7667332906e-3; /* -0xb55215.0p-32 */
  32. static const volatile float twom100 = 7.8886090522e-31; /* 2**-100=0x0d800000 */
  33. float expf(float x)
  34. {
  35. float y,hi=0.0,lo=0.0,c,t,twopk;
  36. int32_t k=0,xsb;
  37. uint32_t hx;
  38. GET_FLOAT_WORD(hx, x);
  39. xsb = (hx>>31)&1; /* sign bit of x */
  40. hx &= 0x7fffffff; /* high word of |x| */
  41. /* filter out non-finite argument */
  42. if (hx >= 0x42b17218) { /* if |x|>=88.721... */
  43. if (hx > 0x7f800000) /* NaN */
  44. return x+x;
  45. if (hx == 0x7f800000) /* exp(+-inf)={inf,0} */
  46. return xsb==0 ? x : 0.0;
  47. if (x > o_threshold)
  48. return huge*huge; /* overflow */
  49. if (x < u_threshold)
  50. return twom100*twom100; /* underflow */
  51. }
  52. /* argument reduction */
  53. if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
  54. if (hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
  55. hi = x-ln2HI[xsb];
  56. lo = ln2LO[xsb];
  57. k = 1 - xsb - xsb;
  58. } else {
  59. k = invln2*x + halF[xsb];
  60. t = k;
  61. hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
  62. lo = t*ln2LO[0];
  63. }
  64. STRICT_ASSIGN(float, x, hi - lo);
  65. } else if(hx < 0x39000000) { /* |x|<2**-14 */
  66. /* raise inexact */
  67. if (huge+x > 1.0f)
  68. return 1.0f + x;
  69. } else
  70. k = 0;
  71. /* x is now in primary range */
  72. t = x*x;
  73. if (k >= -125)
  74. SET_FLOAT_WORD(twopk, 0x3f800000+(k<<23));
  75. else
  76. SET_FLOAT_WORD(twopk, 0x3f800000+((k+100)<<23));
  77. c = x - t*(P1+t*P2);
  78. if (k == 0)
  79. return 1.0f - ((x*c)/(c - 2.0f) - x);
  80. y = 1.0f - ((lo - (x*c)/(2.0f - c)) - hi);
  81. if (k < -125)
  82. return y*twopk*twom100;
  83. if (k == 128)
  84. return y*2.0f*0x1p127f;
  85. return y*twopk;
  86. }