summaryrefslogtreecommitdiff
path: root/libm/powerpc/classic/s_trunc.c
diff options
context:
space:
mode:
authorJoakim Tjernlund <joakim.tjernlund@transmode.se>2007-03-31 13:28:15 +0000
committerJoakim Tjernlund <joakim.tjernlund@transmode.se>2007-03-31 13:28:15 +0000
commite7bcf43b6440ac9fc61a0eef5591393810daafb5 (patch)
treeb72c3fb15e030b47b2eb02d13169b4548382e855 /libm/powerpc/classic/s_trunc.c
parent7a40ba19c86e4d2fc7e35f14a0e629ee843b96a9 (diff)
From Steve Papacharalambous:
Add math support for PowerPC e500.
Diffstat (limited to 'libm/powerpc/classic/s_trunc.c')
-rw-r--r--libm/powerpc/classic/s_trunc.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/libm/powerpc/classic/s_trunc.c b/libm/powerpc/classic/s_trunc.c
new file mode 100644
index 000000000..f793992a7
--- /dev/null
+++ b/libm/powerpc/classic/s_trunc.c
@@ -0,0 +1,89 @@
+#include <limits.h>
+#include <math.h>
+#include <endian.h>
+
+typedef union
+ {
+ struct {
+#if (__BYTE_ORDER == __BIG_ENDIAN)
+ unsigned long int hi;
+ unsigned long int lo;
+#else
+ unsigned long int lo;
+ unsigned long int hi;
+#endif
+ } words;
+ double dbl;
+ } DblInHex;
+
+static const unsigned long int signMask = 0x80000000ul;
+static const double twoTo52 = 4503599627370496.0;
+
+/*******************************************************************************
+* *
+* The function trunc truncates its double argument to integral value *
+* and returns the result in double format. This function signals *
+* inexact if an ordered return value is not equal to the operand. *
+* *
+*******************************************************************************/
+
+libm_hidden_proto(trunc)
+double trunc ( double x )
+ {
+ DblInHex argument,OldEnvironment;
+ register double y;
+ register unsigned long int xhi;
+ register long int target;
+
+ argument.dbl = x;
+ xhi = argument.words.hi & 0x7fffffffUL; // xhi <- high half of |x|
+ target = ( argument.words.hi < signMask ); // flag positive sign
+
+ if ( xhi < 0x43300000ul )
+/*******************************************************************************
+* Is |x| < 2.0^53? *
+*******************************************************************************/
+ {
+ if ( xhi < 0x3ff00000ul )
+/*******************************************************************************
+* Is |x| < 1.0? *
+*******************************************************************************/
+ {
+ if ( ( xhi | argument.words.lo ) != 0ul )
+ { // raise deserved INEXACT
+ asm ("mffs %0" : "=f" (OldEnvironment.dbl));
+ OldEnvironment.words.lo |= 0x02000000ul;
+ asm ("mtfsf 255,%0" : /*NULLOUT*/ : /*IN*/ "f" ( OldEnvironment.dbl ));
+ }
+ if ( target ) // return properly signed zero
+ return ( 0.0 );
+ else
+ return ( -0.0 );
+ }
+/*******************************************************************************
+* Is 1.0 < |x| < 2.0^52? *
+*******************************************************************************/
+ if ( target )
+ {
+ y = ( x + twoTo52 ) - twoTo52; // round at binary point
+ if ( y > x )
+ return ( y - 1.0 );
+ else
+ return ( y );
+ }
+
+ else
+ {
+ y = ( x - twoTo52 ) + twoTo52; // round at binary point.
+ if ( y < x )
+ return ( y + 1.0 );
+ else
+ return ( y );
+ }
+ }
+/*******************************************************************************
+* Is |x| >= 2.0^52 or x is a NaN. *
+*******************************************************************************/
+ return ( x );
+ }
+libm_hidden_def(trunc)