expm1f.c 3.2 KB

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