From 4ab8b2dd6e571b8dafe69a4d7a09a451cc9b1645 Mon Sep 17 00:00:00 2001 From: Manuel Novoa III Date: Tue, 19 Jun 2001 22:53:41 +0000 Subject: Remove Erik's broken implementation of rint(). Replace it by one "less broken". Also correct rounding beharior of round() and add trunc(). Note that round() and rint() currently don't check for infs and nans. I decided to wait on that until the big cleanup. --- libm/double/floor.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'libm/double/floor.c') diff --git a/libm/double/floor.c b/libm/double/floor.c index dcc1a10f1..affc7753e 100644 --- a/libm/double/floor.c +++ b/libm/double/floor.c @@ -451,3 +451,81 @@ else return(u.y); } } + +/**********************************************************************/ +/* + * trunc is just a slightly modified version of floor above. + */ + +double trunc(double x) +{ + union { + double y; + unsigned short sh[4]; + } u; + unsigned short *p; + int e; + +#ifdef UNK + mtherr( "trunc", DOMAIN ); + return(0.0); +#endif +#ifdef NANS + if( isnan(x) ) + return( x ); +#endif +#ifdef INFINITIES + if(!isfinite(x)) + return(x); +#endif +#ifdef MINUSZERO + if(x == 0.0L) + return(x); +#endif + u.y = x; + /* find the exponent (power of 2) */ +#ifdef DEC + p = (unsigned short *)&u.sh[0]; + e = (( *p >> 7) & 0377) - 0201; + p += 3; +#endif + +#ifdef IBMPC + p = (unsigned short *)&u.sh[3]; + e = (( *p >> 4) & 0x7ff) - 0x3ff; + p -= 3; +#endif + +#ifdef MIEEE + p = (unsigned short *)&u.sh[0]; + e = (( *p >> 4) & 0x7ff) - 0x3ff; + p += 3; +#endif + + if( e < 0 ) + return( 0.0 ); + + e = (NBITS -1) - e; + /* clean out 16 bits at a time */ + while( e >= 16 ) + { +#ifdef IBMPC + *p++ = 0; +#endif + +#ifdef DEC + *p-- = 0; +#endif + +#ifdef MIEEE + *p-- = 0; +#endif + e -= 16; + } + + /* clear the remaining bits */ + if( e > 0 ) + *p &= bmask[e]; + + return(u.y); +} -- cgit v1.2.3