s_log1pf.c revision 153086
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 162116Sjkh#ifndef lint 1750476Speterstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/s_log1pf.c 153086 2005-12-04 12:30:44Z bde $"; 182116Sjkh#endif 192116Sjkh 202116Sjkh#include "math.h" 212116Sjkh#include "math_private.h" 222116Sjkh 232116Sjkhstatic const float 242116Sjkhln2_hi = 6.9313812256e-01, /* 0x3f317180 */ 252116Sjkhln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */ 262116Sjkhtwo25 = 3.355443200e+07, /* 0x4c000000 */ 272116SjkhLp1 = 6.6666668653e-01, /* 3F2AAAAB */ 282116SjkhLp2 = 4.0000000596e-01, /* 3ECCCCCD */ 292116SjkhLp3 = 2.8571429849e-01, /* 3E924925 */ 302116SjkhLp4 = 2.2222198546e-01, /* 3E638E29 */ 312116SjkhLp5 = 1.8183572590e-01, /* 3E3A3325 */ 322116SjkhLp6 = 1.5313838422e-01, /* 3E1CD04F */ 332116SjkhLp7 = 1.4798198640e-01; /* 3E178897 */ 342116Sjkh 352116Sjkhstatic const float zero = 0.0; 362116Sjkh 3797413Salfredfloat 3897413Salfredlog1pf(float x) 392116Sjkh{ 402116Sjkh float hfsq,f,c,s,z,R,u; 412116Sjkh int32_t k,hx,hu,ax; 422116Sjkh 432116Sjkh GET_FLOAT_WORD(hx,x); 442116Sjkh ax = hx&0x7fffffff; 452116Sjkh 462116Sjkh k = 1; 47153086Sbde if (hx < 0x3ed413d0) { /* 1+x < sqrt(2)+ */ 482116Sjkh if(ax>=0x3f800000) { /* x <= -1.0 */ 492116Sjkh if(x==(float)-1.0) return -two25/zero; /* log1p(-1)=+inf */ 502116Sjkh else return (x-x)/(x-x); /* log1p(x<-1)=NaN */ 512116Sjkh } 522116Sjkh if(ax<0x31000000) { /* |x| < 2**-29 */ 532116Sjkh if(two25+x>zero /* raise inexact */ 542116Sjkh &&ax<0x24800000) /* |x| < 2**-54 */ 552116Sjkh return x; 562116Sjkh else 572116Sjkh return x - x*x*(float)0.5; 582116Sjkh } 59153086Sbde if(hx>0||hx<=((int32_t)0xbe95f619)) { 60153086Sbde k=0;f=x;hu=1;} /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ 618870Srgrimes } 622116Sjkh if (hx >= 0x7f800000) return x+x; 632116Sjkh if(k!=0) { 642116Sjkh if(hx<0x5a000000) { 65153078Sbde *(volatile float *)&u = (float)1.0+x; 662116Sjkh GET_FLOAT_WORD(hu,u); 672116Sjkh k = (hu>>23)-127; 682116Sjkh /* correction term */ 692116Sjkh c = (k>0)? (float)1.0-(u-x):x-(u-(float)1.0); 702116Sjkh c /= u; 712116Sjkh } else { 722116Sjkh u = x; 732116Sjkh GET_FLOAT_WORD(hu,u); 742116Sjkh k = (hu>>23)-127; 752116Sjkh c = 0; 762116Sjkh } 772116Sjkh hu &= 0x007fffff; 78153086Sbde /* 79153086Sbde * The approximation to sqrt(2) used in thresholds is not 80153086Sbde * critical. However, the ones used above must give less 81153086Sbde * strict bounds than the one here so that the k==0 case is 82153086Sbde * never reached from here, since here we have committed to 83153086Sbde * using the correction term but don't use it if k==0. 84153086Sbde */ 85153086Sbde if(hu<0x3504f4) { /* u < sqrt(2) */ 862116Sjkh SET_FLOAT_WORD(u,hu|0x3f800000);/* normalize u */ 872116Sjkh } else { 888870Srgrimes k += 1; 892116Sjkh SET_FLOAT_WORD(u,hu|0x3f000000); /* normalize u/2 */ 902116Sjkh hu = (0x00800000-hu)>>2; 912116Sjkh } 922116Sjkh f = u-(float)1.0; 932116Sjkh } 942116Sjkh hfsq=(float)0.5*f*f; 952116Sjkh if(hu==0) { /* |f| < 2**-20 */ 968870Srgrimes if(f==zero) if(k==0) return zero; 972116Sjkh else {c += k*ln2_lo; return k*ln2_hi+c;} 982116Sjkh R = hfsq*((float)1.0-(float)0.66666666666666666*f); 992116Sjkh if(k==0) return f-R; else 1002116Sjkh return k*ln2_hi-((R-(k*ln2_lo+c))-f); 1012116Sjkh } 1028870Srgrimes s = f/((float)2.0+f); 1032116Sjkh z = s*s; 1042116Sjkh R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); 1052116Sjkh if(k==0) return f-(hfsq-s*(hfsq+R)); else 1062116Sjkh return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); 1072116Sjkh} 108