s_log1pf.c revision 177711
1230557Sjimharris/* s_log1pf.c -- float version of s_log1p.c.
2230557Sjimharris * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3230557Sjimharris */
4230557Sjimharris
5230557Sjimharris/*
6230557Sjimharris * ====================================================
7230557Sjimharris * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8230557Sjimharris *
9230557Sjimharris * Developed at SunPro, a Sun Microsystems, Inc. business.
10230557Sjimharris * Permission to use, copy, modify, and distribute this
11230557Sjimharris * software is freely granted, provided that this notice
12230557Sjimharris * is preserved.
13230557Sjimharris * ====================================================
14230557Sjimharris */
15230557Sjimharris
16230557Sjimharris#include <sys/cdefs.h>
17230557Sjimharris__FBSDID("$FreeBSD: head/lib/msun/src/s_log1pf.c 177711 2008-03-29 16:37:59Z das $");
18230557Sjimharris
19230557Sjimharris#include <float.h>
20230557Sjimharris
21230557Sjimharris#include "math.h"
22230557Sjimharris#include "math_private.h"
23230557Sjimharris
24230557Sjimharrisstatic const float
25230557Sjimharrisln2_hi =   6.9313812256e-01,	/* 0x3f317180 */
26230557Sjimharrisln2_lo =   9.0580006145e-06,	/* 0x3717f7d1 */
27230557Sjimharristwo25 =    3.355443200e+07,	/* 0x4c000000 */
28230557SjimharrisLp1 = 6.6666668653e-01,	/* 3F2AAAAB */
29230557SjimharrisLp2 = 4.0000000596e-01,	/* 3ECCCCCD */
30230557SjimharrisLp3 = 2.8571429849e-01, /* 3E924925 */
31230557SjimharrisLp4 = 2.2222198546e-01, /* 3E638E29 */
32230557SjimharrisLp5 = 1.8183572590e-01, /* 3E3A3325 */
33230557SjimharrisLp6 = 1.5313838422e-01, /* 3E1CD04F */
34230557SjimharrisLp7 = 1.4798198640e-01; /* 3E178897 */
35230557Sjimharris
36230557Sjimharrisstatic const float zero = 0.0;
37230557Sjimharris
38230557Sjimharrisfloat
39230557Sjimharrislog1pf(float x)
40230557Sjimharris{
41230557Sjimharris	float hfsq,f,c,s,z,R,u;
42230557Sjimharris	int32_t k,hx,hu,ax;
43230557Sjimharris
44230557Sjimharris	GET_FLOAT_WORD(hx,x);
45230557Sjimharris	ax = hx&0x7fffffff;
46230557Sjimharris
47230557Sjimharris	k = 1;
48230557Sjimharris	if (hx < 0x3ed413d0) {			/* 1+x < sqrt(2)+  */
49230557Sjimharris	    if(ax>=0x3f800000) {		/* x <= -1.0 */
50230557Sjimharris		if(x==(float)-1.0) return -two25/zero; /* log1p(-1)=+inf */
51230557Sjimharris		else return (x-x)/(x-x);	/* log1p(x<-1)=NaN */
52230557Sjimharris	    }
53230557Sjimharris	    if(ax<0x38000000) {			/* |x| < 2**-15 */
54230557Sjimharris		if(two25+x>zero			/* raise inexact */
55230557Sjimharris	            &&ax<0x33800000) 		/* |x| < 2**-24 */
56230557Sjimharris		    return x;
57230557Sjimharris		else
58230557Sjimharris		    return x - x*x*(float)0.5;
59230557Sjimharris	    }
60230557Sjimharris	    if(hx>0||hx<=((int32_t)0xbe95f619)) {
61230557Sjimharris		k=0;f=x;hu=1;}		/* sqrt(2)/2- <= 1+x < sqrt(2)+ */
62230557Sjimharris	}
63230557Sjimharris	if (hx >= 0x7f800000) return x+x;
64230557Sjimharris	if(k!=0) {
65230557Sjimharris	    if(hx<0x5a000000) {
66230557Sjimharris		STRICT_ASSIGN(float,u,(float)1.0+x);
67230557Sjimharris		GET_FLOAT_WORD(hu,u);
68230557Sjimharris	        k  = (hu>>23)-127;
69230557Sjimharris		/* correction term */
70230557Sjimharris	        c  = (k>0)? (float)1.0-(u-x):x-(u-(float)1.0);
71230557Sjimharris		c /= u;
72230557Sjimharris	    } else {
73230557Sjimharris		u  = x;
74230557Sjimharris		GET_FLOAT_WORD(hu,u);
75230557Sjimharris	        k  = (hu>>23)-127;
76230557Sjimharris		c  = 0;
77230557Sjimharris	    }
78230557Sjimharris	    hu &= 0x007fffff;
79230557Sjimharris	    /*
80230557Sjimharris	     * The approximation to sqrt(2) used in thresholds is not
81230557Sjimharris	     * critical.  However, the ones used above must give less
82230557Sjimharris	     * strict bounds than the one here so that the k==0 case is
83230557Sjimharris	     * never reached from here, since here we have committed to
84230557Sjimharris	     * using the correction term but don't use it if k==0.
85230557Sjimharris	     */
86230557Sjimharris	    if(hu<0x3504f4) {			/* u < sqrt(2) */
87230557Sjimharris	        SET_FLOAT_WORD(u,hu|0x3f800000);/* normalize u */
88230557Sjimharris	    } else {
89230557Sjimharris	        k += 1;
90298955Spfg		SET_FLOAT_WORD(u,hu|0x3f000000);	/* normalize u/2 */
91230557Sjimharris	        hu = (0x00800000-hu)>>2;
92230557Sjimharris	    }
93230557Sjimharris	    f = u-(float)1.0;
94230557Sjimharris	}
95230557Sjimharris	hfsq=(float)0.5*f*f;
96230557Sjimharris	if(hu==0) {	/* |f| < 2**-20 */
97230557Sjimharris	    if(f==zero) {
98230557Sjimharris		if(k==0) {
99230557Sjimharris		    return zero;
100230557Sjimharris		} else {
101230557Sjimharris		    c += k*ln2_lo;
102230557Sjimharris		    return k*ln2_hi+c;
103230557Sjimharris		}
104230557Sjimharris	    }
105230557Sjimharris	    R = hfsq*((float)1.0-(float)0.66666666666666666*f);
106230557Sjimharris	    if(k==0) return f-R; else
107230557Sjimharris	    	     return k*ln2_hi-((R-(k*ln2_lo+c))-f);
108230557Sjimharris	}
109230557Sjimharris 	s = f/((float)2.0+f);
110230557Sjimharris	z = s*s;
111230557Sjimharris	R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
112230557Sjimharris	if(k==0) return f-(hfsq-s*(hfsq+R)); else
113230557Sjimharris		 return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
114230557Sjimharris}
115230557Sjimharris