s_expm1f.c 3.5 KB

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