1
0

s_expm1f.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* s_expm1f.c -- float version of s_expm1.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 <math.h>
  15. #include "math_private.h"
  16. static const float
  17. one = 1.0,
  18. huge = 1.0e+30,
  19. tiny = 1.0e-30,
  20. o_threshold = 8.8721679688e+01,/* 0x42b17180 */
  21. ln2_hi = 6.9313812256e-01,/* 0x3f317180 */
  22. ln2_lo = 9.0580006145e-06,/* 0x3717f7d1 */
  23. invln2 = 1.4426950216e+00,/* 0x3fb8aa3b */
  24. /* scaled coefficients related to expm1 */
  25. Q1 = -3.3333335072e-02, /* 0xbd088889 */
  26. Q2 = 1.5873016091e-03, /* 0x3ad00d01 */
  27. Q3 = -7.9365076090e-05, /* 0xb8a670cd */
  28. Q4 = 4.0082177293e-06, /* 0x36867e54 */
  29. Q5 = -2.0109921195e-07; /* 0xb457edbb */
  30. float
  31. expm1f(float x)
  32. {
  33. float y,hi,lo,c=0.0,t,e,hxs,hfx,r1;
  34. int32_t k,xsb;
  35. uint32_t hx;
  36. GET_FLOAT_WORD(hx,x);
  37. xsb = hx&0x80000000; /* sign bit of x */
  38. if(xsb==0) y=x; else y= -x; /* y = |x| */
  39. hx &= 0x7fffffff; /* high word of |x| */
  40. /* filter out huge and non-finite argument */
  41. if(hx >= 0x4195b844) { /* if |x|>=27*ln2 */
  42. if(hx >= 0x42b17218) { /* if |x|>=88.721... */
  43. if(hx>0x7f800000)
  44. return x+x; /* NaN */
  45. if(hx==0x7f800000)
  46. return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */
  47. if(x > o_threshold) return huge*huge; /* overflow */
  48. }
  49. if(xsb!=0) { /* x < -27*ln2, return -1.0 with inexact */
  50. if(x+tiny<(float)0.0) /* raise inexact */
  51. return tiny-one; /* 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; lo = ln2_lo; k = 1;}
  59. else
  60. {hi = x + ln2_hi; lo = -ln2_lo; k = -1;}
  61. } else {
  62. k = invln2*x+((xsb==0)?(float)0.5:(float)-0.5);
  63. t = k;
  64. hi = x - t*ln2_hi; /* t*ln2_hi is exact here */
  65. lo = t*ln2_lo;
  66. }
  67. x = hi - lo;
  68. c = (hi-x)-lo;
  69. }
  70. else if(hx < 0x33000000) { /* when |x|<2**-25, return x */
  71. t = huge+x; /* return x with inexact flags when x!=0 */
  72. return x - (t-(huge+x));
  73. }
  74. else k = 0;
  75. /* x is now in primary range */
  76. hfx = (float)0.5*x;
  77. hxs = x*hfx;
  78. r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))));
  79. t = (float)3.0-r1*hfx;
  80. e = hxs*((r1-t)/((float)6.0 - x*t));
  81. if(k==0) return x - (x*e-hxs); /* c is 0 */
  82. else {
  83. e = (x*(e-c)-c);
  84. e -= hxs;
  85. if(k== -1) return (float)0.5*(x-e)-(float)0.5;
  86. if(k==1) {
  87. if(x < (float)-0.25) return -(float)2.0*(e-(x+(float)0.5));
  88. else return one+(float)2.0*(x-e);
  89. }
  90. if (k <= -2 || k>56) { /* suffice to return exp(x)-1 */
  91. int32_t i;
  92. y = one-(e-x);
  93. GET_FLOAT_WORD(i,y);
  94. SET_FLOAT_WORD(y,i+(k<<23)); /* add k to y's exponent */
  95. return y-one;
  96. }
  97. t = one;
  98. if(k<23) {
  99. int32_t i;
  100. SET_FLOAT_WORD(t,0x3f800000 - (0x1000000>>k)); /* t=1-2^-k */
  101. y = t-(e-x);
  102. GET_FLOAT_WORD(i,y);
  103. SET_FLOAT_WORD(y,i+(k<<23)); /* add k to y's exponent */
  104. } else {
  105. int32_t i;
  106. SET_FLOAT_WORD(t,((0x7f-k)<<23)); /* 2^-k */
  107. y = x-(e+t);
  108. y += one;
  109. GET_FLOAT_WORD(i,y);
  110. SET_FLOAT_WORD(y,i+(k<<23)); /* add k to y's exponent */
  111. }
  112. }
  113. return y;
  114. }