• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11 
12 #include "bsd_privatef.h"
13 
bsd__ieee754_powf(float x,float y)14 float bsd__ieee754_powf(float x, float y)
15 {
16     float z,ax,z_h,z_l,p_h,p_l;
17     float y1,t1,t2,r,s,sn,t,u,v,w;
18     int32_t i,j,k,yisint,n;
19     int32_t hx,hy,ix,iy,is;
20 
21     GET_FLOAT_WORD(hx,x);
22     GET_FLOAT_WORD(hy,y);
23     ix = hx&0x7fffffff;  iy = hy&0x7fffffff;
24 
25     /* y==zero: x**0 = 1 */
26     if(iy==0) return one;
27 
28     /* x==1: 1**y = 1, even if y is NaN */
29     if (hx==0x3f800000) return one;
30 
31     /* y!=zero: result is NaN if either arg is NaN */
32     if(ix > 0x7f800000 ||
33        iy > 0x7f800000)
34         return (x+0.0F)+(y+0.0F);
35 
36     /* determine if y is an odd int when x < 0
37      * yisint = 0    ... y is not an integer
38      * yisint = 1    ... y is an odd int
39      * yisint = 2    ... y is an even int
40      */
41     yisint  = 0;
42     if(hx<0) {
43         if(iy>=0x4b800000) yisint = 2; /* even integer y */
44         else if(iy>=0x3f800000) {
45         k = (iy>>23)-0x7f;             /* exponent */
46         j = iy>>(23-k);
47         if((j<<(23-k))==iy) yisint = 2-(j&1);
48         }
49     }
50 
51     /* special value of y */
52     if (iy==0x7f800000) {   /* y is +-inf */
53         if (ix==0x3f800000)
54             return  one;    /* (-1)**+-inf is NaN */
55         else if (ix > 0x3f800000)/* (|x|>1)**+-inf = inf,0 */
56             return (hy>=0)? y: zero;
57         else                /* (|x|<1)**-,+inf = inf,0 */
58             return (hy<0)?-y: zero;
59     }
60     if(iy==0x3f800000) {    /* y is  +-1 */
61         if(hy<0) return one/x; else return x;
62     }
63     if(hy==0x40000000) return x*x; /* y is  2 */
64     if(hy==0x3f000000) {    /* y is  0.5 */
65         if(hx>=0)           /* x >= +0 */
66         return sqrtf(x);
67     }
68 
69     ax   = fabsf(x);
70     /* special value of x */
71     if(ix==0x7f800000||ix==0||ix==0x3f800000){
72         z = ax;              /*x is +-0,+-inf,+-1*/
73         if(hy<0) z = one/z;  /* z = (1/|x|) */
74         if(hx<0) {
75         if(((ix-0x3f800000)|yisint)==0) {
76             z = (z-z)/(z-z); /* (-1)**non-int is NaN */
77         } else if(yisint==1)
78             z = -z;          /* (x<0)**odd = -(|x|**odd) */
79         }
80         return z;
81     }
82 
83     n = ((u_int32_t)hx>>31)-1;
84 
85     /* (x<0)**(non-int) is NaN */
86     if((n|yisint)==0) return (x-x)/(x-x);
87 
88     sn = one;                         /* s (sign of result -ve**odd) = -1 else = 1 */
89     if((n|(yisint-1))==0) sn = -one;  /* (-ve)**(odd int) */
90 
91     /* |y| is huge */
92     if(iy>0x4d000000) {   /* if |y| > 2**27 */
93     /* over/underflow if x is not close to one */
94         if(ix<0x3f7ffff8) return (hy<0)? sn*huge*huge:sn*tiny*tiny;
95         if(ix>0x3f800007) return (hy>0)? sn*huge*huge:sn*tiny*tiny;
96     /* now |1-x| is tiny <= 2**-20, suffice to compute
97        log(x) by x-x^2/2+x^3/3-x^4/4 */
98         t = ax-1;         /* t has 20 trailing zeros */
99         w = (t*t)*((float)0.5-t*((float)0.333333333333-t*(float)0.25));
100         u = ivln2_h*t;    /* ivln2_h has 16 sig. bits */
101         v = t*ivln2_l-w*ivln2;
102         t1 = u+v;
103         GET_FLOAT_WORD(is,t1);
104         SET_FLOAT_WORD(t1,is&0xfffff000);
105         t2 = v-(t1-u);
106     } else {
107         float s2,s_h,s_l,t_h,t_l;
108         n = 0;
109     /* take care subnormal number */
110         if(ix<0x00800000)
111         {ax *= two24; n -= 24; GET_FLOAT_WORD(ix,ax); }
112         n  += ((ix)>>23)-0x7f;
113         j  = ix&0x007fffff;
114     /* determine interval */
115         ix = j|0x3f800000;          /* normalize ix */
116         if(j<=0x1cc471) k=0;        /* |x|<sqrt(3/2) */
117         else if(j<0x5db3d7) k=1;    /* |x|<sqrt(3)   */
118         else {k=0;n+=1;ix -= 0x00800000;}
119         SET_FLOAT_WORD(ax,ix);
120 
121     /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
122         u = ax-bp[k];               /* bp[0]=1.0, bp[1]=1.5 */
123         v = one/(ax+bp[k]);
124         s = u*v;
125         s_h = s;
126         GET_FLOAT_WORD(is,s_h);
127         SET_FLOAT_WORD(s_h,is&0xfffff000);
128     /* t_h=ax+bp[k] High */
129         is = ((ix>>1)&0xfffff000)|0x20000000;
130         SET_FLOAT_WORD(t_h,is+0x00400000+(k<<21));
131         t_l = ax - (t_h-bp[k]);
132         s_l = v*((u-s_h*t_h)-s_h*t_l);
133     /* compute log(ax) */
134         s2 = s*s;
135         r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
136         r += s_l*(s_h+s);
137         s2  = s_h*s_h;
138         t_h = (float)3.0+s2+r;
139         GET_FLOAT_WORD(is,t_h);
140         SET_FLOAT_WORD(t_h,is&0xfffff000);
141         t_l = r-((t_h-(float)3.0)-s2);
142     /* u+v = s*(1+...) */
143         u = s_h*t_h;
144         v = s_l*t_h+t_l*s;
145     /* 2/(3log2)*(s+...) */
146         p_h = u+v;
147         GET_FLOAT_WORD(is,p_h);
148         SET_FLOAT_WORD(p_h,is&0xfffff000);
149         p_l = v-(p_h-u);
150         z_h = cp_h*p_h;        /* cp_h+cp_l = 2/(3*log2) */
151         z_l = cp_l*p_h+p_l*cp+dp_l[k];
152     /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
153         t = (float)n;
154         t1 = (((z_h+z_l)+dp_h[k])+t);
155         GET_FLOAT_WORD(is,t1);
156         SET_FLOAT_WORD(t1,is&0xfffff000);
157         t2 = z_l-(((t1-t)-dp_h[k])-z_h);
158     }
159 
160     /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
161     GET_FLOAT_WORD(is,y);
162     SET_FLOAT_WORD(y1,is&0xfffff000);
163     p_l = (y-y1)*t1+y*t2;
164     p_h = y1*t1;
165     z = p_l+p_h;
166     GET_FLOAT_WORD(j,z);
167     if (j>0x43000000)                          /* if z > 128 */
168         return sn*huge*huge;                   /* overflow */
169     else if (j==0x43000000) {                  /* if z == 128 */
170         if(p_l+ovt>z-p_h) return sn*huge*huge; /* overflow */
171     }
172     else if ((j&0x7fffffff)>0x43160000)        /* z <= -150 */
173         return sn*tiny*tiny;                   /* underflow */
174     else if ((j&0xffffffff)==0xc3160000){      /* z == -150 */
175         if(p_l<=z-p_h) return sn*tiny*tiny;    /* underflow */
176     }
177     /*
178      * compute 2**(p_h+p_l)
179      */
180     i = j&0x7fffffff;
181     k = (i>>23)-0x7f;
182     n = 0;
183     if(i>0x3f000000) {                    /* if |z| > 0.5, set n = [z+0.5] */
184         n = j+(0x00800000>>(k+1));
185         k = ((n&0x7fffffff)>>23)-0x7f;    /* new k for n */
186         SET_FLOAT_WORD(t,n&~(0x007fffff>>k));
187         n = ((n&0x007fffff)|0x00800000)>>(23-k);
188         if(j<0) n = -n;
189         p_h -= t;
190     }
191     t = p_l+p_h;
192     GET_FLOAT_WORD(is,t);
193     SET_FLOAT_WORD(t,is&0xffff8000);
194     u = t*lg2_h;
195     v = (p_l-(t-p_h))*lg2+t*lg2_l;
196     z = u+v;
197     w = v-(z-u);
198     t  = z*z;
199     t1  = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
200     r  = (z*t1)/(t1-two)-(w+z*w);
201     z  = one-(r-z);
202     GET_FLOAT_WORD(j,z);
203     j += (n<<23);
204     if((j>>23)<=0) z = scalbnf(z,n);    /* subnormal output */
205     else SET_FLOAT_WORD(z,j);
206     return sn*z;
207 }
208