13a8617a8SJordan K. Hubbard /* s_log1pf.c -- float version of s_log1p.c.
23a8617a8SJordan K. Hubbard * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
33a8617a8SJordan K. Hubbard */
43a8617a8SJordan K. Hubbard
53a8617a8SJordan K. Hubbard /*
63a8617a8SJordan K. Hubbard * ====================================================
73a8617a8SJordan K. Hubbard * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
83a8617a8SJordan K. Hubbard *
93a8617a8SJordan K. Hubbard * Developed at SunPro, a Sun Microsystems, Inc. business.
103a8617a8SJordan K. Hubbard * Permission to use, copy, modify, and distribute this
113a8617a8SJordan K. Hubbard * software is freely granted, provided that this notice
123a8617a8SJordan K. Hubbard * is preserved.
133a8617a8SJordan K. Hubbard * ====================================================
143a8617a8SJordan K. Hubbard */
153a8617a8SJordan K. Hubbard
160814af48SBruce Evans #include <float.h>
173a8617a8SJordan K. Hubbard
183a8617a8SJordan K. Hubbard #include "math.h"
193a8617a8SJordan K. Hubbard #include "math_private.h"
203a8617a8SJordan K. Hubbard
213a8617a8SJordan K. Hubbard static const float
223a8617a8SJordan K. Hubbard ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
233a8617a8SJordan K. Hubbard ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
243a8617a8SJordan K. Hubbard two25 = 3.355443200e+07, /* 0x4c000000 */
253a8617a8SJordan K. Hubbard Lp1 = 6.6666668653e-01, /* 3F2AAAAB */
263a8617a8SJordan K. Hubbard Lp2 = 4.0000000596e-01, /* 3ECCCCCD */
273a8617a8SJordan K. Hubbard Lp3 = 2.8571429849e-01, /* 3E924925 */
283a8617a8SJordan K. Hubbard Lp4 = 2.2222198546e-01, /* 3E638E29 */
293a8617a8SJordan K. Hubbard Lp5 = 1.8183572590e-01, /* 3E3A3325 */
303a8617a8SJordan K. Hubbard Lp6 = 1.5313838422e-01, /* 3E1CD04F */
313a8617a8SJordan K. Hubbard Lp7 = 1.4798198640e-01; /* 3E178897 */
323a8617a8SJordan K. Hubbard
333a8617a8SJordan K. Hubbard static const float zero = 0.0;
34*7dbbb6ddSDavid Schultz static volatile float vzero = 0.0;
353a8617a8SJordan K. Hubbard
3659b19ff1SAlfred Perlstein float
log1pf(float x)3759b19ff1SAlfred Perlstein log1pf(float x)
383a8617a8SJordan K. Hubbard {
393a8617a8SJordan K. Hubbard float hfsq,f,c,s,z,R,u;
403a8617a8SJordan K. Hubbard int32_t k,hx,hu,ax;
413a8617a8SJordan K. Hubbard
423a8617a8SJordan K. Hubbard GET_FLOAT_WORD(hx,x);
433a8617a8SJordan K. Hubbard ax = hx&0x7fffffff;
443a8617a8SJordan K. Hubbard
453a8617a8SJordan K. Hubbard k = 1;
46d48ea975SBruce Evans if (hx < 0x3ed413d0) { /* 1+x < sqrt(2)+ */
473a8617a8SJordan K. Hubbard if(ax>=0x3f800000) { /* x <= -1.0 */
48*7dbbb6ddSDavid Schultz if(x==(float)-1.0) return -two25/vzero; /* log1p(-1)=+inf */
493a8617a8SJordan K. Hubbard else return (x-x)/(x-x); /* log1p(x<-1)=NaN */
503a8617a8SJordan K. Hubbard }
51f2a14778SBruce Evans if(ax<0x38000000) { /* |x| < 2**-15 */
523a8617a8SJordan K. Hubbard if(two25+x>zero /* raise inexact */
53f2a14778SBruce Evans &&ax<0x33800000) /* |x| < 2**-24 */
543a8617a8SJordan K. Hubbard return x;
553a8617a8SJordan K. Hubbard else
563a8617a8SJordan K. Hubbard return x - x*x*(float)0.5;
573a8617a8SJordan K. Hubbard }
58d48ea975SBruce Evans if(hx>0||hx<=((int32_t)0xbe95f619)) {
59d48ea975SBruce Evans k=0;f=x;hu=1;} /* sqrt(2)/2- <= 1+x < sqrt(2)+ */
603a8617a8SJordan K. Hubbard }
613a8617a8SJordan K. Hubbard if (hx >= 0x7f800000) return x+x;
623a8617a8SJordan K. Hubbard if(k!=0) {
633a8617a8SJordan K. Hubbard if(hx<0x5a000000) {
640814af48SBruce Evans STRICT_ASSIGN(float,u,(float)1.0+x);
653a8617a8SJordan K. Hubbard GET_FLOAT_WORD(hu,u);
663a8617a8SJordan K. Hubbard k = (hu>>23)-127;
673a8617a8SJordan K. Hubbard /* correction term */
683a8617a8SJordan K. Hubbard c = (k>0)? (float)1.0-(u-x):x-(u-(float)1.0);
693a8617a8SJordan K. Hubbard c /= u;
703a8617a8SJordan K. Hubbard } else {
713a8617a8SJordan K. Hubbard u = x;
723a8617a8SJordan K. Hubbard GET_FLOAT_WORD(hu,u);
733a8617a8SJordan K. Hubbard k = (hu>>23)-127;
743a8617a8SJordan K. Hubbard c = 0;
753a8617a8SJordan K. Hubbard }
763a8617a8SJordan K. Hubbard hu &= 0x007fffff;
77d48ea975SBruce Evans /*
78d48ea975SBruce Evans * The approximation to sqrt(2) used in thresholds is not
79d48ea975SBruce Evans * critical. However, the ones used above must give less
80d48ea975SBruce Evans * strict bounds than the one here so that the k==0 case is
81d48ea975SBruce Evans * never reached from here, since here we have committed to
82d48ea975SBruce Evans * using the correction term but don't use it if k==0.
83d48ea975SBruce Evans */
84d48ea975SBruce Evans if(hu<0x3504f4) { /* u < sqrt(2) */
853a8617a8SJordan K. Hubbard SET_FLOAT_WORD(u,hu|0x3f800000);/* normalize u */
863a8617a8SJordan K. Hubbard } else {
873a8617a8SJordan K. Hubbard k += 1;
883a8617a8SJordan K. Hubbard SET_FLOAT_WORD(u,hu|0x3f000000); /* normalize u/2 */
893a8617a8SJordan K. Hubbard hu = (0x00800000-hu)>>2;
903a8617a8SJordan K. Hubbard }
913a8617a8SJordan K. Hubbard f = u-(float)1.0;
923a8617a8SJordan K. Hubbard }
933a8617a8SJordan K. Hubbard hfsq=(float)0.5*f*f;
943a8617a8SJordan K. Hubbard if(hu==0) { /* |f| < 2**-20 */
95ee0730e6SDavid Schultz if(f==zero) {
96ee0730e6SDavid Schultz if(k==0) {
97ee0730e6SDavid Schultz return zero;
98ee0730e6SDavid Schultz } else {
99ee0730e6SDavid Schultz c += k*ln2_lo;
100ee0730e6SDavid Schultz return k*ln2_hi+c;
101ee0730e6SDavid Schultz }
102ee0730e6SDavid Schultz }
1033a8617a8SJordan K. Hubbard R = hfsq*((float)1.0-(float)0.66666666666666666*f);
1043a8617a8SJordan K. Hubbard if(k==0) return f-R; else
1053a8617a8SJordan K. Hubbard return k*ln2_hi-((R-(k*ln2_lo+c))-f);
1063a8617a8SJordan K. Hubbard }
1073a8617a8SJordan K. Hubbard s = f/((float)2.0+f);
1083a8617a8SJordan K. Hubbard z = s*s;
1093a8617a8SJordan K. Hubbard R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
1103a8617a8SJordan K. Hubbard if(k==0) return f-(hfsq-s*(hfsq+R)); else
1113a8617a8SJordan K. Hubbard return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
1123a8617a8SJordan K. Hubbard }
113