POK
expm1f.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Fri Jan 30 14:41:34 2009
15  */
16 
17 /* s_expm1f.c -- float version of s_expm1.c.
18  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
19  */
20 
21 /*
22  * ====================================================
23  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
24  *
25  * Developed at SunPro, a Sun Microsystems, Inc. business.
26  * Permission to use, copy, modify, and distribute this
27  * software is freely granted, provided that this notice
28  * is preserved.
29  * ====================================================
30  */
31 
32 #ifdef POK_NEEDS_LIBMATH
33 
34 #include <libm.h>
35 #include "math_private.h"
36 
37 static const float huge = 1.0e+30, tiny = 1.0e-30;
38 
39 static const float
40 one = 1.0,
41 o_threshold = 8.8721679688e+01,/* 0x42b17180 */
42 ln2_hi = 6.9313812256e-01,/* 0x3f317180 */
43 ln2_lo = 9.0580006145e-06,/* 0x3717f7d1 */
44 invln2 = 1.4426950216e+00,/* 0x3fb8aa3b */
45  /* scaled coefficients related to expm1 */
46 Q1 = -3.3333335072e-02, /* 0xbd088889 */
47 Q2 = 1.5873016091e-03, /* 0x3ad00d01 */
48 Q3 = -7.9365076090e-05, /* 0xb8a670cd */
49 Q4 = 4.0082177293e-06, /* 0x36867e54 */
50 Q5 = -2.0109921195e-07; /* 0xb457edbb */
51 
52 float
53 expm1f(float x)
54 {
55  float y,hi,lo,c,t,e,hxs,hfx,r1;
56  int32_t k,xsb;
57  uint32_t hx;
58 
59  c = 0;
60  GET_FLOAT_WORD(hx,x);
61  xsb = hx&0x80000000; /* sign bit of x */
62  if(xsb==0) y=x; else y= -x; /* y = |x| */
63  hx &= 0x7fffffff; /* high word of |x| */
64 
65  /* filter out huge and non-finite argument */
66  if(hx >= 0x4195b844) { /* if |x|>=27*ln2 */
67  if(hx >= 0x42b17218) { /* if |x|>=88.721... */
68  if(hx>0x7f800000)
69  return x+x; /* NaN */
70  if(hx==0x7f800000)
71  return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */
72  if(x > o_threshold) return huge*huge; /* overflow */
73  }
74  if(xsb!=0) { /* x < -27*ln2, return -1.0 with inexact */
75  if(x+tiny<(float)0.0) /* raise inexact */
76  return tiny-one; /* return -1 */
77  }
78  }
79 
80  /* argument reduction */
81  if(hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
82  if(hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
83  if(xsb==0)
84  {hi = x - ln2_hi; lo = ln2_lo; k = 1;}
85  else
86  {hi = x + ln2_hi; lo = -ln2_lo; k = -1;}
87  } else {
88  k = invln2*x+((xsb==0)?(float)0.5:(float)-0.5);
89  t = k;
90  hi = x - t*ln2_hi; /* t*ln2_hi is exact here */
91  lo = t*ln2_lo;
92  }
93  x = hi - lo;
94  c = (hi-x)-lo;
95  }
96  else if(hx < 0x33000000) { /* when |x|<2**-25, return x */
97  t = huge+x; /* return x with inexact flags when x!=0 */
98  return x - (t-(huge+x));
99  }
100  else k = 0;
101 
102  /* x is now in primary range */
103  hfx = (float)0.5*x;
104  hxs = x*hfx;
105  r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))));
106  t = (float)3.0-r1*hfx;
107  e = hxs*((r1-t)/((float)6.0 - x*t));
108  if(k==0) return x - (x*e-hxs); /* c is 0 */
109  else {
110  e = (x*(e-c)-c);
111  e -= hxs;
112  if(k== -1) return (float)0.5*(x-e)-(float)0.5;
113  if(k==1) {
114  if(x < (float)-0.25) return -(float)2.0*(e-(x+(float)0.5));
115  else return one+(float)2.0*(x-e);
116  }
117  if (k <= -2 || k>56) { /* suffice to return exp(x)-1 */
118  int32_t i;
119  y = one-(e-x);
120  GET_FLOAT_WORD(i,y);
121  SET_FLOAT_WORD(y,i+(k<<23)); /* add k to y's exponent */
122  return y-one;
123  }
124  t = one;
125  if(k<23) {
126  int32_t i;
127  SET_FLOAT_WORD(t,0x3f800000 - (0x1000000>>k)); /* t=1-2^-k */
128  y = t-(e-x);
129  GET_FLOAT_WORD(i,y);
130  SET_FLOAT_WORD(y,i+(k<<23)); /* add k to y's exponent */
131  } else {
132  int32_t i;
133  SET_FLOAT_WORD(t,((0x7f-k)<<23)); /* 2^-k */
134  y = x-(e+t);
135  y += one;
136  GET_FLOAT_WORD(i,y);
137  SET_FLOAT_WORD(y,i+(k<<23)); /* add k to y's exponent */
138  }
139  }
140  return y;
141 }
142 
143 #endif
144