xref: /netbsd-src/lib/libm/src/s_log1pf.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /* s_log1pf.c -- float version of s_log1p.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4 
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 #include <sys/cdefs.h>
17 #if defined(LIBM_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: s_log1pf.c,v 1.7 1999/07/02 15:37:43 simonb Exp $");
19 #endif
20 
21 #include "math.h"
22 #include "math_private.h"
23 
24 #ifdef __STDC__
25 static const float
26 #else
27 static float
28 #endif
29 ln2_hi =   6.9313812256e-01,	/* 0x3f317180 */
30 ln2_lo =   9.0580006145e-06,	/* 0x3717f7d1 */
31 two25 =    3.355443200e+07,	/* 0x4c000000 */
32 Lp1 = 6.6666668653e-01,	/* 3F2AAAAB */
33 Lp2 = 4.0000000596e-01,	/* 3ECCCCCD */
34 Lp3 = 2.8571429849e-01, /* 3E924925 */
35 Lp4 = 2.2222198546e-01, /* 3E638E29 */
36 Lp5 = 1.8183572590e-01, /* 3E3A3325 */
37 Lp6 = 1.5313838422e-01, /* 3E1CD04F */
38 Lp7 = 1.4798198640e-01; /* 3E178897 */
39 
40 #ifdef __STDC__
41 static const float zero = 0.0;
42 #else
43 static float zero = 0.0;
44 #endif
45 
46 #ifdef __STDC__
47 	float log1pf(float x)
48 #else
49 	float log1pf(x)
50 	float x;
51 #endif
52 {
53 	float hfsq,f,c,s,z,R,u;
54 	int32_t k,hx,hu,ax;
55 
56 	f = c = 0;
57 	hu = 0;
58 	GET_FLOAT_WORD(hx,x);
59 	ax = hx&0x7fffffff;
60 
61 	k = 1;
62 	if (hx < 0x3ed413d7) {			/* x < 0.41422  */
63 	    if(ax>=0x3f800000) {		/* x <= -1.0 */
64 		if(x==(float)-1.0) return -two25/zero; /* log1p(-1)=+inf */
65 		else return (x-x)/(x-x);	/* log1p(x<-1)=NaN */
66 	    }
67 	    if(ax<0x31000000) {			/* |x| < 2**-29 */
68 		if(two25+x>zero			/* raise inexact */
69 	            &&ax<0x24800000) 		/* |x| < 2**-54 */
70 		    return x;
71 		else
72 		    return x - x*x*(float)0.5;
73 	    }
74 	    if(hx>0||hx<=((int32_t)0xbe95f61f)) {
75 		k=0;f=x;hu=1;}	/* -0.2929<x<0.41422 */
76 	}
77 	if (hx >= 0x7f800000) return x+x;
78 	if(k!=0) {
79 	    if(hx<0x5a000000) {
80 		u  = (float)1.0+x;
81 		GET_FLOAT_WORD(hu,u);
82 	        k  = (hu>>23)-127;
83 		/* correction term */
84 	        c  = (k>0)? (float)1.0-(u-x):x-(u-(float)1.0);
85 		c /= u;
86 	    } else {
87 		u  = x;
88 		GET_FLOAT_WORD(hu,u);
89 	        k  = (hu>>23)-127;
90 		c  = 0;
91 	    }
92 	    hu &= 0x007fffff;
93 	    if(hu<0x3504f7) {
94 	        SET_FLOAT_WORD(u,hu|0x3f800000);/* normalize u */
95 	    } else {
96 	        k += 1;
97 		SET_FLOAT_WORD(u,hu|0x3f000000);	/* normalize u/2 */
98 	        hu = (0x00800000-hu)>>2;
99 	    }
100 	    f = u-(float)1.0;
101 	}
102 	hfsq=(float)0.5*f*f;
103 	if(hu==0) {	/* |f| < 2**-20 */
104 	    if(f==zero) { if(k==0) return zero;
105 			  else {c += k*ln2_lo; return k*ln2_hi+c;}
106 	    }
107 	    R = hfsq*((float)1.0-(float)0.66666666666666666*f);
108 	    if(k==0) return f-R; else
109 	    	     return k*ln2_hi-((R-(k*ln2_lo+c))-f);
110 	}
111  	s = f/((float)2.0+f);
112 	z = s*s;
113 	R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
114 	if(k==0) return f-(hfsq-s*(hfsq+R)); else
115 		 return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
116 }
117