s_log1pf.c revision 175494
12116Sjkh/* s_log1pf.c -- float version of s_log1p.c. 22116Sjkh * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. 32116Sjkh */ 42116Sjkh 52116Sjkh/* 62116Sjkh * ==================================================== 72116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 82116Sjkh * 92116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business. 102116Sjkh * Permission to use, copy, modify, and distribute this 118870Srgrimes * software is freely granted, provided that this notice 122116Sjkh * is preserved. 132116Sjkh * ==================================================== 142116Sjkh */ 152116Sjkh 16175494Sbde#include <sys/cdefs.h> 17175494Sbde__FBSDID("$FreeBSD: head/lib/msun/src/s_log1pf.c 175494 2008-01-19 18:13:21Z bde $"); 182116Sjkh 19175494Sbde#include <float.h> 20175494Sbde 212116Sjkh#include "math.h" 222116Sjkh#include "math_private.h" 232116Sjkh 242116Sjkhstatic const float 252116Sjkhln2_hi = 6.9313812256e-01, /* 0x3f317180 */ 262116Sjkhln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */ 272116Sjkhtwo25 = 3.355443200e+07, /* 0x4c000000 */ 282116SjkhLp1 = 6.6666668653e-01, /* 3F2AAAAB */ 292116SjkhLp2 = 4.0000000596e-01, /* 3ECCCCCD */ 302116SjkhLp3 = 2.8571429849e-01, /* 3E924925 */ 312116SjkhLp4 = 2.2222198546e-01, /* 3E638E29 */ 322116SjkhLp5 = 1.8183572590e-01, /* 3E3A3325 */ 332116SjkhLp6 = 1.5313838422e-01, /* 3E1CD04F */ 342116SjkhLp7 = 1.4798198640e-01; /* 3E178897 */ 352116Sjkh 362116Sjkhstatic const float zero = 0.0; 372116Sjkh 3897413Salfredfloat 3997413Salfredlog1pf(float x) 402116Sjkh{ 412116Sjkh float hfsq,f,c,s,z,R,u; 422116Sjkh int32_t k,hx,hu,ax; 432116Sjkh 442116Sjkh GET_FLOAT_WORD(hx,x); 452116Sjkh ax = hx&0x7fffffff; 462116Sjkh 472116Sjkh k = 1; 48153086Sbde if (hx < 0x3ed413d0) { /* 1+x < sqrt(2)+ */ 492116Sjkh if(ax>=0x3f800000) { /* x <= -1.0 */ 502116Sjkh if(x==(float)-1.0) return -two25/zero; /* log1p(-1)=+inf */ 512116Sjkh else return (x-x)/(x-x); /* log1p(x<-1)=NaN */ 522116Sjkh } 532116Sjkh if(ax<0x31000000) { /* |x| < 2**-29 */ 542116Sjkh if(two25+x>zero /* raise inexact */ 552116Sjkh &&ax<0x24800000) /* |x| < 2**-54 */ 562116Sjkh return x; 572116Sjkh else 582116Sjkh return x - x*x*(float)0.5; 592116Sjkh } 60153086Sbde if(hx>0||hx<=((int32_t)0xbe95f619)) { 61153086Sbde k=0;f=x;hu=1;} /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ 628870Srgrimes } 632116Sjkh if (hx >= 0x7f800000) return x+x; 642116Sjkh if(k!=0) { 652116Sjkh if(hx<0x5a000000) { 66175494Sbde STRICT_ASSIGN(float,u,(float)1.0+x); 672116Sjkh GET_FLOAT_WORD(hu,u); 682116Sjkh k = (hu>>23)-127; 692116Sjkh /* correction term */ 702116Sjkh c = (k>0)? (float)1.0-(u-x):x-(u-(float)1.0); 712116Sjkh c /= u; 722116Sjkh } else { 732116Sjkh u = x; 742116Sjkh GET_FLOAT_WORD(hu,u); 752116Sjkh k = (hu>>23)-127; 762116Sjkh c = 0; 772116Sjkh } 782116Sjkh hu &= 0x007fffff; 79153086Sbde /* 80153086Sbde * The approximation to sqrt(2) used in thresholds is not 81153086Sbde * critical. However, the ones used above must give less 82153086Sbde * strict bounds than the one here so that the k==0 case is 83153086Sbde * never reached from here, since here we have committed to 84153086Sbde * using the correction term but don't use it if k==0. 85153086Sbde */ 86153086Sbde if(hu<0x3504f4) { /* u < sqrt(2) */ 872116Sjkh SET_FLOAT_WORD(u,hu|0x3f800000);/* normalize u */ 882116Sjkh } else { 898870Srgrimes k += 1; 902116Sjkh SET_FLOAT_WORD(u,hu|0x3f000000); /* normalize u/2 */ 912116Sjkh hu = (0x00800000-hu)>>2; 922116Sjkh } 932116Sjkh f = u-(float)1.0; 942116Sjkh } 952116Sjkh hfsq=(float)0.5*f*f; 962116Sjkh if(hu==0) { /* |f| < 2**-20 */ 978870Srgrimes if(f==zero) if(k==0) return zero; 982116Sjkh else {c += k*ln2_lo; return k*ln2_hi+c;} 992116Sjkh R = hfsq*((float)1.0-(float)0.66666666666666666*f); 1002116Sjkh if(k==0) return f-R; else 1012116Sjkh return k*ln2_hi-((R-(k*ln2_lo+c))-f); 1022116Sjkh } 1038870Srgrimes s = f/((float)2.0+f); 1042116Sjkh z = s*s; 1052116Sjkh R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); 1062116Sjkh if(k==0) return f-(hfsq-s*(hfsq+R)); else 1072116Sjkh return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); 1082116Sjkh} 109