expf.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. ln2hi = 6.9314575195e-1f, /* 0x3f317200 */
  19. ln2lo = 1.4286067653e-6f, /* 0x35bfbe8e */
  20. invln2 = 1.4426950216e+0f, /* 0x3fb8aa3b */
  21. /*
  22. * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
  23. * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
  24. */
  25. P1 = 1.6666625440e-1f, /* 0xaaaa8f.0p-26 */
  26. P2 = -2.7667332906e-3f; /* -0xb55215.0p-32 */
  27. float expf(float x)
  28. {
  29. float hi, lo, c, xx;
  30. int k, sign;
  31. uint32_t hx;
  32. GET_FLOAT_WORD(hx, x);
  33. sign = hx >> 31; /* sign bit of x */
  34. hx &= 0x7fffffff; /* high word of |x| */
  35. /* special cases */
  36. if (hx >= 0x42b17218) { /* if |x| >= 88.722839f or NaN */
  37. if (hx > 0x7f800000) /* NaN */
  38. return x;
  39. if (!sign) {
  40. /* overflow if x!=inf */
  41. STRICT_ASSIGN(float, x, x * 0x1p127f);
  42. return x;
  43. }
  44. if (hx == 0x7f800000) /* -inf */
  45. return 0;
  46. if (hx >= 0x42cff1b5) { /* x <= -103.972084f */
  47. /* underflow */
  48. STRICT_ASSIGN(float, x, 0x1p-100f*0x1p-100f);
  49. return x;
  50. }
  51. }
  52. /* argument reduction */
  53. if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
  54. if (hx > 0x3f851592) /* if |x| > 1.5 ln2 */
  55. k = invln2*x + half[sign];
  56. else
  57. k = 1 - sign - sign;
  58. hi = x - k*ln2hi; /* k*ln2hi is exact here */
  59. lo = k*ln2lo;
  60. STRICT_ASSIGN(float, x, hi - lo);
  61. } else if (hx > 0x39000000) { /* |x| > 2**-14 */
  62. k = 0;
  63. hi = x;
  64. lo = 0;
  65. } else {
  66. /* raise inexact */
  67. FORCE_EVAL(0x1p127f + x);
  68. return 1 + x;
  69. }
  70. /* x is now in primary range */
  71. xx = x*x;
  72. c = x - xx*(P1+xx*P2);
  73. x = 1 + (x*c/(2-c) - lo + hi);
  74. if (k == 0)
  75. return x;
  76. return scalbnf(x, k);
  77. }