e_expf.c 2.8 KB

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