summaryrefslogtreecommitdiff
path: root/libm/double/floor.c
diff options
context:
space:
mode:
authorManuel Novoa III <mjn3@codepoet.org>2001-06-19 22:53:41 +0000
committerManuel Novoa III <mjn3@codepoet.org>2001-06-19 22:53:41 +0000
commit4ab8b2dd6e571b8dafe69a4d7a09a451cc9b1645 (patch)
tree6824a635f8300a340b520382a1ec694e6544a7af /libm/double/floor.c
parente10f6cc02bf6589700872266f1819af56a2d79f6 (diff)
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.
Diffstat (limited to 'libm/double/floor.c')
-rw-r--r--libm/double/floor.c78
1 files changed, 78 insertions, 0 deletions
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);
+}