s_log1pf.c revision 251024
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 251024 2013-05-27 08:50:10Z das $"); 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; 37251024Sdasstatic volatile float vzero = 0.0; 382116Sjkh 3997413Salfredfloat 4097413Salfredlog1pf(float x) 412116Sjkh{ 422116Sjkh float hfsq,f,c,s,z,R,u; 432116Sjkh int32_t k,hx,hu,ax; 442116Sjkh 452116Sjkh GET_FLOAT_WORD(hx,x); 462116Sjkh ax = hx&0x7fffffff; 472116Sjkh 482116Sjkh k = 1; 49153086Sbde if (hx < 0x3ed413d0) { /* 1+x < sqrt(2)+ */ 502116Sjkh if(ax>=0x3f800000) { /* x <= -1.0 */ 51251024Sdas if(x==(float)-1.0) return -two25/vzero; /* log1p(-1)=+inf */ 522116Sjkh else return (x-x)/(x-x); /* log1p(x<-1)=NaN */ 532116Sjkh } 54175534Sbde if(ax<0x38000000) { /* |x| < 2**-15 */ 552116Sjkh if(two25+x>zero /* raise inexact */ 56175534Sbde &&ax<0x33800000) /* |x| < 2**-24 */ 572116Sjkh return x; 582116Sjkh else 592116Sjkh return x - x*x*(float)0.5; 602116Sjkh } 61153086Sbde if(hx>0||hx<=((int32_t)0xbe95f619)) { 62153086Sbde k=0;f=x;hu=1;} /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ 638870Srgrimes } 642116Sjkh if (hx >= 0x7f800000) return x+x; 652116Sjkh if(k!=0) { 662116Sjkh if(hx<0x5a000000) { 67175494Sbde STRICT_ASSIGN(float,u,(float)1.0+x); 682116Sjkh GET_FLOAT_WORD(hu,u); 692116Sjkh k = (hu>>23)-127; 702116Sjkh /* correction term */ 712116Sjkh c = (k>0)? (float)1.0-(u-x):x-(u-(float)1.0); 722116Sjkh c /= u; 732116Sjkh } else { 742116Sjkh u = x; 752116Sjkh GET_FLOAT_WORD(hu,u); 762116Sjkh k = (hu>>23)-127; 772116Sjkh c = 0; 782116Sjkh } 792116Sjkh hu &= 0x007fffff; 80153086Sbde /* 81153086Sbde * The approximation to sqrt(2) used in thresholds is not 82153086Sbde * critical. However, the ones used above must give less 83153086Sbde * strict bounds than the one here so that the k==0 case is 84153086Sbde * never reached from here, since here we have committed to 85153086Sbde * using the correction term but don't use it if k==0. 86153086Sbde */ 87153086Sbde if(hu<0x3504f4) { /* u < sqrt(2) */ 882116Sjkh SET_FLOAT_WORD(u,hu|0x3f800000);/* normalize u */ 892116Sjkh } else { 908870Srgrimes k += 1; 912116Sjkh SET_FLOAT_WORD(u,hu|0x3f000000); /* normalize u/2 */ 922116Sjkh hu = (0x00800000-hu)>>2; 932116Sjkh } 942116Sjkh f = u-(float)1.0; 952116Sjkh } 962116Sjkh hfsq=(float)0.5*f*f; 972116Sjkh if(hu==0) { /* |f| < 2**-20 */ 98177711Sdas if(f==zero) { 99177711Sdas if(k==0) { 100177711Sdas return zero; 101177711Sdas } else { 102177711Sdas c += k*ln2_lo; 103177711Sdas return k*ln2_hi+c; 104177711Sdas } 105177711Sdas } 1062116Sjkh R = hfsq*((float)1.0-(float)0.66666666666666666*f); 1072116Sjkh if(k==0) return f-R; else 1082116Sjkh return k*ln2_hi-((R-(k*ln2_lo+c))-f); 1092116Sjkh } 1108870Srgrimes s = f/((float)2.0+f); 1112116Sjkh z = s*s; 1122116Sjkh R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); 1132116Sjkh if(k==0) return f-(hfsq-s*(hfsq+R)); else 1142116Sjkh return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); 1152116Sjkh} 116