1175296Sobrien/* s_log1pf.c -- float version of s_log1p.c.
2175296Sobrien * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3175296Sobrien */
4175296Sobrien
5175296Sobrien/*
6175296Sobrien * ====================================================
7175296Sobrien * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8175296Sobrien *
9175296Sobrien * Developed at SunPro, a Sun Microsystems, Inc. business.
10175296Sobrien * Permission to use, copy, modify, and distribute this
11175296Sobrien * software is freely granted, provided that this notice
12175296Sobrien * is preserved.
13175296Sobrien * ====================================================
14175296Sobrien */
15175296Sobrien
16175296Sobrien#include <sys/cdefs.h>
17175296Sobrien__FBSDID("$FreeBSD: releng/11.0/lib/msun/src/s_log1pf.c 251024 2013-05-27 08:50:10Z das $");
18175296Sobrien
19175296Sobrien#include <float.h>
20175296Sobrien
21175296Sobrien#include "math.h"
22175296Sobrien#include "math_private.h"
23175296Sobrien
24175296Sobrienstatic const float
25175296Sobrienln2_hi =   6.9313812256e-01,	/* 0x3f317180 */
26175296Sobrienln2_lo =   9.0580006145e-06,	/* 0x3717f7d1 */
27175296Sobrientwo25 =    3.355443200e+07,	/* 0x4c000000 */
28175296SobrienLp1 = 6.6666668653e-01,	/* 3F2AAAAB */
29175296SobrienLp2 = 4.0000000596e-01,	/* 3ECCCCCD */
30175296SobrienLp3 = 2.8571429849e-01, /* 3E924925 */
31175296SobrienLp4 = 2.2222198546e-01, /* 3E638E29 */
32175296SobrienLp5 = 1.8183572590e-01, /* 3E3A3325 */
33175296SobrienLp6 = 1.5313838422e-01, /* 3E1CD04F */
34175296SobrienLp7 = 1.4798198640e-01; /* 3E178897 */
35175296Sobrien
36175296Sobrienstatic const float zero = 0.0;
37175296Sobrienstatic volatile float vzero = 0.0;
38175296Sobrien
39175296Sobrienfloat
40175296Sobrienlog1pf(float x)
41175296Sobrien{
42175296Sobrien	float hfsq,f,c,s,z,R,u;
43175296Sobrien	int32_t k,hx,hu,ax;
44175296Sobrien
45175296Sobrien	GET_FLOAT_WORD(hx,x);
46175296Sobrien	ax = hx&0x7fffffff;
47175296Sobrien
48175296Sobrien	k = 1;
49	if (hx < 0x3ed413d0) {			/* 1+x < sqrt(2)+  */
50	    if(ax>=0x3f800000) {		/* x <= -1.0 */
51		if(x==(float)-1.0) return -two25/vzero; /* log1p(-1)=+inf */
52		else return (x-x)/(x-x);	/* log1p(x<-1)=NaN */
53	    }
54	    if(ax<0x38000000) {			/* |x| < 2**-15 */
55		if(two25+x>zero			/* raise inexact */
56	            &&ax<0x33800000) 		/* |x| < 2**-24 */
57		    return x;
58		else
59		    return x - x*x*(float)0.5;
60	    }
61	    if(hx>0||hx<=((int32_t)0xbe95f619)) {
62		k=0;f=x;hu=1;}		/* sqrt(2)/2- <= 1+x < sqrt(2)+ */
63	}
64	if (hx >= 0x7f800000) return x+x;
65	if(k!=0) {
66	    if(hx<0x5a000000) {
67		STRICT_ASSIGN(float,u,(float)1.0+x);
68		GET_FLOAT_WORD(hu,u);
69	        k  = (hu>>23)-127;
70		/* correction term */
71	        c  = (k>0)? (float)1.0-(u-x):x-(u-(float)1.0);
72		c /= u;
73	    } else {
74		u  = x;
75		GET_FLOAT_WORD(hu,u);
76	        k  = (hu>>23)-127;
77		c  = 0;
78	    }
79	    hu &= 0x007fffff;
80	    /*
81	     * The approximation to sqrt(2) used in thresholds is not
82	     * critical.  However, the ones used above must give less
83	     * strict bounds than the one here so that the k==0 case is
84	     * never reached from here, since here we have committed to
85	     * using the correction term but don't use it if k==0.
86	     */
87	    if(hu<0x3504f4) {			/* u < sqrt(2) */
88	        SET_FLOAT_WORD(u,hu|0x3f800000);/* normalize u */
89	    } else {
90	        k += 1;
91		SET_FLOAT_WORD(u,hu|0x3f000000);	/* normalize u/2 */
92	        hu = (0x00800000-hu)>>2;
93	    }
94	    f = u-(float)1.0;
95	}
96	hfsq=(float)0.5*f*f;
97	if(hu==0) {	/* |f| < 2**-20 */
98	    if(f==zero) {
99		if(k==0) {
100		    return zero;
101		} else {
102		    c += k*ln2_lo;
103		    return k*ln2_hi+c;
104		}
105	    }
106	    R = hfsq*((float)1.0-(float)0.66666666666666666*f);
107	    if(k==0) return f-R; else
108	    	     return k*ln2_hi-((R-(k*ln2_lo+c))-f);
109	}
110 	s = f/((float)2.0+f);
111	z = s*s;
112	R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
113	if(k==0) return f-(hfsq-s*(hfsq+R)); else
114		 return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
115}
116