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 <float.h>
17#include <math.h>
18#include <math-barriers.h>
19#include <math_private.h>
20#include <math-underflow.h>
21#include <libc-diag.h>
22
23static const float
24ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
25ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
26two25 = 3.355443200e+07, /* 0x4c000000 */
27Lp1 = 6.6666668653e-01, /* 3F2AAAAB */
28Lp2 = 4.0000000596e-01, /* 3ECCCCCD */
29Lp3 = 2.8571429849e-01, /* 3E924925 */
30Lp4 = 2.2222198546e-01, /* 3E638E29 */
31Lp5 = 1.8183572590e-01, /* 3E3A3325 */
32Lp6 = 1.5313838422e-01, /* 3E1CD04F */
33Lp7 = 1.4798198640e-01; /* 3E178897 */
34
35static const float zero = 0.0;
36
37float
38__log1pf(float x)
39{
40 float hfsq,f,c,s,z,R,u;
41 int32_t k,hx,hu,ax;
42
43 GET_FLOAT_WORD(hx,x);
44 ax = hx&0x7fffffff;
45
46 k = 1;
47 if (hx < 0x3ed413d7) { /* x < 0.41422 */
48 if(ax>=0x3f800000) { /* x <= -1.0 */
49 if(x==(float)-1.0) return -two25/zero; /* log1p(-1)=-inf */
50 else return (x-x)/(x-x); /* log1p(x<-1)=NaN */
51 }
52 if(ax<0x31000000) { /* |x| < 2**-29 */
53 math_force_eval(two25+x); /* raise inexact */
54 if (ax<0x24800000) /* |x| < 2**-54 */
55 {
56 math_check_force_underflow (x);
57 return x;
58 }
59 else
60 return x - x*x*(float)0.5;
61 }
62 if(hx>0||hx<=((int32_t)0xbe95f61f)) {
63 k=0;f=x;hu=1;} /* -0.2929<x<0.41422 */
64 }
65 if (hx >= 0x7f800000) return x+x;
66 if(k!=0) {
67 if(hx<0x5a000000) {
68 u = (float)1.0+x;
69 GET_FLOAT_WORD(hu,u);
70 k = (hu>>23)-127;
71 /* correction term */
72 c = (k>0)? (float)1.0-(u-x):x-(u-(float)1.0);
73 c /= u;
74 } else {
75 u = x;
76 GET_FLOAT_WORD(hu,u);
77 k = (hu>>23)-127;
78 c = 0;
79 }
80 hu &= 0x007fffff;
81 if(hu<0x3504f7) {
82 SET_FLOAT_WORD(u,hu|0x3f800000);/* normalize u */
83 } else {
84 k += 1;
85 SET_FLOAT_WORD(u,hu|0x3f000000); /* normalize u/2 */
86 hu = (0x00800000-hu)>>2;
87 }
88 f = u-(float)1.0;
89 }
90 hfsq=(float)0.5*f*f;
91 if(hu==0) { /* |f| < 2**-20 */
92 if(f==zero) {
93 if(k==0) return zero;
94 else {c += k*ln2_lo; return k*ln2_hi+c;}
95 }
96 R = hfsq*((float)1.0-(float)0.66666666666666666*f);
97 if(k==0) return f-R; else
98 return k*ln2_hi-((R-(k*ln2_lo+c))-f);
99 }
100 s = f/((float)2.0+f);
101 z = s*s;
102 R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
103 if (k == 0)
104 return f - (hfsq - s * (hfsq + R));
105 else
106 {
107 /* With GCC 7 when compiling with -Os the compiler warns
108 that c might be used uninitialized. This can't be true
109 because k must be 0 for c to be uninitialized and we
110 handled that computation earlier without using c. */
111 DIAG_PUSH_NEEDS_COMMENT;
112 DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized");
113 return k * ln2_hi - ((hfsq - (s * (hfsq + R)
114 + (k * ln2_lo + c))) - f);
115 DIAG_POP_NEEDS_COMMENT;
116 }
117}
118