expm1f.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_expm1f.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. o_threshold = 8.8721679688e+01, /* 0x42b17180 */
  18. ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
  19. ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
  20. invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */
  21. /*
  22. * Domain [-0.34568, 0.34568], range ~[-6.694e-10, 6.696e-10]:
  23. * |6 / x * (1 + 2 * (1 / (exp(x) - 1) - 1 / x)) - q(x)| < 2**-30.04
  24. * Scaled coefficients: Qn_here = 2**n * Qn_for_q (see s_expm1.c):
  25. */
  26. Q1 = -3.3333212137e-2, /* -0x888868.0p-28 */
  27. Q2 = 1.5807170421e-3; /* 0xcf3010.0p-33 */
  28. float expm1f(float x)
  29. {
  30. float_t y,hi,lo,c,t,e,hxs,hfx,r1,twopk;
  31. union {float f; uint32_t i;} u = {x};
  32. uint32_t hx = u.i & 0x7fffffff;
  33. int k, sign = u.i >> 31;
  34. /* filter out huge and non-finite argument */
  35. if (hx >= 0x4195b844) { /* if |x|>=27*ln2 */
  36. if (hx > 0x7f800000) /* NaN */
  37. return x;
  38. if (sign)
  39. return -1;
  40. if (x > o_threshold) {
  41. x *= 0x1p127f;
  42. return x;
  43. }
  44. }
  45. /* argument reduction */
  46. if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
  47. if (hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
  48. if (!sign) {
  49. hi = x - ln2_hi;
  50. lo = ln2_lo;
  51. k = 1;
  52. } else {
  53. hi = x + ln2_hi;
  54. lo = -ln2_lo;
  55. k = -1;
  56. }
  57. } else {
  58. k = invln2*x + (sign ? -0.5f : 0.5f);
  59. t = k;
  60. hi = x - t*ln2_hi; /* t*ln2_hi is exact here */
  61. lo = t*ln2_lo;
  62. }
  63. STRICT_ASSIGN(float, x, hi - lo);
  64. c = (hi-x)-lo;
  65. } else if (hx < 0x33000000) { /* when |x|<2**-25, return x */
  66. if (hx < 0x00800000)
  67. FORCE_EVAL(x*x);
  68. return x;
  69. } else
  70. k = 0;
  71. /* x is now in primary range */
  72. hfx = 0.5f*x;
  73. hxs = x*hfx;
  74. r1 = 1.0f+hxs*(Q1+hxs*Q2);
  75. t = 3.0f - r1*hfx;
  76. e = hxs*((r1-t)/(6.0f - x*t));
  77. if (k == 0) /* c is 0 */
  78. return x - (x*e-hxs);
  79. e = x*(e-c) - c;
  80. e -= hxs;
  81. /* exp(x) ~ 2^k (x_reduced - e + 1) */
  82. if (k == -1)
  83. return 0.5f*(x-e) - 0.5f;
  84. if (k == 1) {
  85. if (x < -0.25f)
  86. return -2.0f*(e-(x+0.5f));
  87. return 1.0f + 2.0f*(x-e);
  88. }
  89. u.i = (0x7f+k)<<23; /* 2^k */
  90. twopk = u.f;
  91. if (k < 0 || k > 56) { /* suffice to return exp(x)-1 */
  92. y = x - e + 1.0f;
  93. if (k == 128)
  94. y = y*2.0f*0x1p127f;
  95. else
  96. y = y*twopk;
  97. return y - 1.0f;
  98. }
  99. u.i = (0x7f-k)<<23; /* 2^-k */
  100. if (k < 23)
  101. y = (x-e+(1-u.f))*twopk;
  102. else
  103. y = (x-(e+u.f)+1)*twopk;
  104. return y;
  105. }