This is the mail archive of the libc-alpha@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[patch] alpha exceptions patch


Also copysign, fabs, and rint.

Note that a kernel patch is needed to get the test suite to pass
the exception checks.


r~

        * sysdeps/alpha/fpu/fraiseexcpt.c: Use get/set_fp_control instead
        of arithmetic instructions.

        * sysdeps/alpha/fpu/s_ceil.c: Use round to -inf instead of playing
        with the fpcr.  Protect from INV exception.
        * sysdeps/alpha/fpu/s_ceilf.c: Likewise.
        * sysdeps/alpha/fpu/s_floor.c: Protect from INV exception.
        * sysdeps/alpha/fpu/s_floorf.c: Likewise.

        * sysdeps/alpha/fpu/s_copysign.c: New.
        * sysdeps/alpha/fpu/s_copysignf.c: New.
        * sysdeps/alpha/fpu/s_fabs.c: New.
        * sysdeps/alpha/fpu/s_fabsf.c: New.
        * sysdeps/alpha/fpu/s_rint.c: New.
        * sysdeps/alpha/fpu/s_rintf.c: New.

Index: sysdeps/alpha/fpu/fraiseexcpt.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/alpha/fpu/fraiseexcpt.c,v
retrieving revision 1.6
diff -u -p -d -r1.6 fraiseexcpt.c
--- fraiseexcpt.c	2000/02/26 08:13:07	1.6
+++ fraiseexcpt.c	2000/03/20 20:26:12
@@ -24,43 +24,16 @@
 int
 __feraiseexcept (int excepts)
 {
-  double tmp;
-  double dummy;
-
-  /* Raise exceptions represented by EXPECTS.  But we must raise only
-     one signal at a time.  It is important the if the overflow/underflow
-     exception and the inexact exception are given at the same time,
-     the overflow/underflow exception precedes the inexact exception.  */
-
-  /* We do these bits in assembly to be certain GCC doesn't optimize
-     away something important.  */
-
-  /* First: invalid exception.  */
-  if (FE_INVALID & excepts)
-    /* One example of a invalid operation is 0 * Infinity.  */
-    __asm__ __volatile__("mult/sui $f31,%1,%0; trapb"
-			 : "=&f" (tmp) : "f" (HUGE_VAL));
-
-  /* Next: division by zero.  */
-  if (FE_DIVBYZERO & excepts)
-    __asm__ __volatile__("cmpteq $f31,$f31,%1; divt/sui %1,$f31,%0; trapb"
-			 : "=&f" (tmp), "=f" (dummy));
+  unsigned long int tmp;
 
-  /* Next: overflow.  */
-  if (FE_OVERFLOW & excepts)
-    __asm__ __volatile__("mult/sui %1,%1,%0; trapb"
-			 : "=&f" (tmp) : "f" (DBL_MAX));
+  /* Get the current exception state.  */
+  tmp = __ieee_get_fp_control ();
 
-  /* Next: underflow.  */
-  if (FE_UNDERFLOW & excepts)
-    __asm__ __volatile__("divt/sui %1,%2,%0; trapb"
-			 : "=&f" (tmp) : "f" (DBL_MIN),
-			   "f" ((double) (1UL << 60)));
+  /* Set all the bits that were called for.  */
+  tmp |= (excepts & FE_ALL_EXCEPT);
 
-  /* Last: inexact.  */
-  if (FE_INEXACT & excepts)
-    __asm__ __volatile__("divt/sui %1,%2,%0; trapb"
-			 : "=&f" (tmp) : "f" (1.0), "f" (M_PI));
+  /* And store it back.  */
+  __ieee_set_fp_control (tmp);
 
   /* Success.  */
   return 0;
Index: sysdeps/alpha/fpu/s_ceil.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/alpha/fpu/s_ceil.c,v
retrieving revision 1.1
diff -u -p -d -r1.1 s_ceil.c
--- s_ceil.c	1998/08/23 04:09:49	1.1
+++ s_ceil.c	2000/03/20 20:26:12
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 2000 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Richard Henderson.
 
@@ -19,34 +19,30 @@
 
 #include <math.h>
 
+/* Use the -inf rounding mode conversion instructions to implement
+   ceil, via something akin to -floor(-x).  This is much faster than
+   playing with the fpcr to achieve +inf rounding mode.  */
+
 double
 __ceil (double x)
 {
-  if (x != 0 && fabs (x) < 9007199254740992.0)  /* 1 << DBL_MANT_DIG */
+  if (isless (fabs (x), 9007199254740992.0))	/* 1 << DBL_MANT_DIG */
     {
-      double tmp1;
-      unsigned long fpcr0, fpcr1;
-      unsigned long pinf = 3UL << 58;
-
-      /* Set round to +inf.  */
-      __asm __volatile("excb; mf_fpcr %0" : "=f"(fpcr0));
-      __asm __volatile("mt_fpcr %0; excb" : : "f"(fpcr0 | pinf));
+      double tmp1, new_x;
 
-      /* Calculate!  */
+      new_x = -x;
+      __asm (
 #ifdef _IEEE_FP_INEXACT
-      __asm("cvttq/svid %2,%1\n\tcvtqt/suid %1,%0"
-	    : "=f"(x), "=&f"(tmp1)
-	    : "f"(x));
+	     "cvttq/svim %2,%1\n\t"
 #else
-      __asm("cvttq/svd %2,%1\n\tcvtqt/d %1,%0"
-	    : "=f"(x), "=&f"(tmp1)
-	    : "f"(x));
+	     "cvttq/svm %2,%1\n\t"
 #endif
+	     "cvtqt/m %1,%0\n\t"
+	     : "=f"(new_x), "=&f"(tmp1)
+	     : "f"(new_x));
 
-      /* Reset rounding mode, while retaining new exception bits.  */
-      __asm __volatile("excb; mf_fpcr %0" : "=f"(fpcr1));
-      fpcr0 = (fpcr0 & pinf) | (fpcr1 & ~pinf);
-      __asm __volatile("mt_fpcr %0; excb" : : "f"(fpcr0));
+      /* Fix up the negation we did above, as well as handling -0 properly. */
+      x = copysign(new_x, x);
     }
   return x;
 }
Index: sysdeps/alpha/fpu/s_ceilf.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/alpha/fpu/s_ceilf.c,v
retrieving revision 1.1
diff -u -p -d -r1.1 s_ceilf.c
--- s_ceilf.c	1998/08/23 04:09:49	1.1
+++ s_ceilf.c	2000/03/20 20:26:12
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 2000 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Richard Henderson.
 
@@ -19,39 +19,35 @@
 
 #include <math.h>
 
+/* Use the -inf rounding mode conversion instructions to implement
+   ceil, via something akin to -floor(-x).  This is much faster than
+   playing with the fpcr to achieve +inf rounding mode.  */
+
 float
 __ceilf (float x)
 {
-  if (x != 0 && fabsf (x) < 16777216.0f)  /* 1 << FLT_MANT_DIG */
+  if (isless (fabsf (x), 16777216.0f))	/* 1 << FLT_MANT_DIG */
     {
-      float tmp1, tmp2;
-      unsigned long fpcr0, fpcr1;
-      unsigned long pinf = 3UL << 58;
-
-      /* Set round to +inf.  */
-      __asm __volatile("excb; mf_fpcr %0" : "=f"(fpcr0));
-      __asm __volatile("mt_fpcr %0; excb" : : "f"(fpcr0 | pinf));
+      /* Note that Alpha S_Floating is stored in registers in a
+	 restricted T_Floating format, so we don't even need to
+	 convert back to S_Floating in the end.  The initial
+	 conversion to T_Floating is needed to handle denormals.  */
 
-      /* Calculate!  
-         Note that Alpha S_Floating is stored in registers in a
-         restricted T_Floating format, so we don't even need to
-         convert back to S_Floating in the end.  The initial
-         conversion to T_Floating is needed to handle denormals.  */
+      float tmp1, tmp2, new_x;
 
+      new_x = -x;
+      __asm ("cvtst/s %3,%2\n\t"
 #ifdef _IEEE_FP_INEXACT
-      __asm("cvtst/s %3,%2\n\tcvttq/svid %2,%1\n\tcvtqt/suid %1,%0"
-	    : "=f"(x), "=&f"(tmp1), "=&f"(tmp2)
-	    : "f"(x));
+	     "cvttq/svim %2,%1\n\t"
 #else
-      __asm("cvtst/s %3,%2\n\tcvttq/svd %2,%1\n\tcvtqt/d %1,%0"
-	    : "=f"(x), "=&f"(tmp1), "=&f"(tmp2)
-	    : "f"(x));
+	     "cvttq/svm %2,%1\n\t"
 #endif
+	     "cvtqt/m %1,%0\n\t"
+	     : "=f"(new_x), "=&f"(tmp1), "=&f"(tmp2)
+	     : "f"(new_x));
 
-      /* Reset rounding mode, while retaining new exception bits.  */
-      __asm __volatile("excb; mf_fpcr %0" : "=f"(fpcr1));
-      fpcr0 = (fpcr0 & pinf) | (fpcr1 & ~pinf);
-      __asm __volatile("mt_fpcr %0; excb" : : "f"(fpcr0));
+      /* Fix up the negation we did above, as well as handling -0 properly. */
+      x = copysignf(new_x, x);
     }
   return x;
 }
Index: sysdeps/alpha/fpu/s_copysign.c
===================================================================
RCS file: s_copysign.c
diff -N s_copysign.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ s_copysign.c	Mon Mar 20 12:26:12 2000
@@ -0,0 +1,33 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Richard Henderson.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <math.h>
+
+double
+__copysign (double x, double y)
+{
+  __asm ("cpys %1, %2, %0" : "=f" (x) : "f" (y), "f" (x));
+  return x;
+}
+
+weak_alias (__copysign, copysign)
+#ifdef NO_LONG_DOUBLE
+strong_alias (__copysign, __copysignl)
+weak_alias (__copysign, copysignl)
+#endif
Index: sysdeps/alpha/fpu/s_copysignf.c
===================================================================
RCS file: s_copysignf.c
diff -N s_copysignf.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ s_copysignf.c	Mon Mar 20 12:26:12 2000
@@ -0,0 +1,29 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Richard Henderson.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <math.h>
+
+float
+__copysignf (float x, float y)
+{
+  __asm ("cpys %1, %2, %0" : "=f" (x) : "f" (y), "f" (x));
+  return x;
+}
+
+weak_alias (__copysignf, copysignf)
Index: sysdeps/alpha/fpu/s_fabs.c
===================================================================
RCS file: s_fabs.c
diff -N s_fabs.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ s_fabs.c	Mon Mar 20 12:26:12 2000
@@ -0,0 +1,37 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Richard Henderson.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <math.h>
+
+double
+__fabs (double x)
+{
+#if __GNUC_PREREQ (2, 8)
+  return __builtin_fabs (x);
+#else
+  __asm ("cpys $f31, %1, %0" : "=f" (x) : "f" (x));
+  return x;
+#endif
+}
+
+weak_alias (__fabs, fabs)
+#ifdef NO_LONG_DOUBLE
+strong_alias (__fabs, __fabsl)
+weak_alias (__fabs, fabsl)
+#endif
Index: sysdeps/alpha/fpu/s_fabsf.c
===================================================================
RCS file: s_fabsf.c
diff -N s_fabsf.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ s_fabsf.c	Mon Mar 20 12:26:12 2000
@@ -0,0 +1,33 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Richard Henderson.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <math.h>
+
+float
+__fabsf (float x)
+{
+#if __GNUC_PREREQ (2, 8)
+  return __builtin_fabsf (x);
+#else
+  __asm ("cpys $f31, %1, %0" : "=f" (x) : "f" (x));
+  return x;
+#endif
+}
+
+weak_alias (__fabsf, fabsf)
Index: sysdeps/alpha/fpu/s_floor.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/alpha/fpu/s_floor.c,v
retrieving revision 1.3
diff -u -p -d -r1.3 s_floor.c
--- s_floor.c	1999/01/29 16:55:14	1.3
+++ s_floor.c	2000/03/20 20:26:12
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Richard Henderson.
 
@@ -27,10 +27,10 @@
 double
 __floor (double x)
 {
-  /* Check not zero since floor(-0) == -0.  */
-  if (x != 0 && fabs (x) < 9007199254740992.0)  /* 1 << DBL_MANT_DIG */
+  if (isless (fabs (x), 9007199254740992.0))	/* 1 << DBL_MANT_DIG */
     {
-      double __tmp1;
+      double tmp1, new_x;
+
       __asm (
 #ifdef _IEEE_FP_INEXACT
 	     "cvttq/svim %2,%1\n\t"
@@ -38,8 +38,12 @@ __floor (double x)
 	     "cvttq/svm %2,%1\n\t"
 #endif
 	     "cvtqt/m %1,%0\n\t"
-	     : "=f"(x), "=&f"(__tmp1)
+	     : "=f"(new_x), "=&f"(tmp1)
 	     : "f"(x));
+
+      /* floor(-0) == -0, and in general we'll always have the same
+	 sign as our input.  */
+      x = copysign(new_x, x);
     }
   return x;
 }
Index: sysdeps/alpha/fpu/s_floorf.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/alpha/fpu/s_floorf.c,v
retrieving revision 1.3
diff -u -p -d -r1.3 s_floorf.c
--- s_floorf.c	1999/01/29 16:56:01	1.3
+++ s_floorf.c	2000/03/20 20:26:12
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Richard Henderson.
 
@@ -27,15 +27,14 @@
 float
 __floorf (float x)
 {
-  /* Check not zero since floor(-0) == -0.  */
-  if (x != 0 && fabsf (x) < 16777216.0f)  /* 1 << FLT_MANT_DIG */
+  if (isless (fabsf (x), 16777216.0f))	/* 1 << FLT_MANT_DIG */
     {
       /* Note that Alpha S_Floating is stored in registers in a
 	 restricted T_Floating format, so we don't even need to
 	 convert back to S_Floating in the end.  The initial
 	 conversion to T_Floating is needed to handle denormals.  */
 
-      float tmp1, tmp2;
+      float tmp1, tmp2, new_x;
 
       __asm ("cvtst/s %3,%2\n\t"
 #ifdef _IEEE_FP_INEXACT
@@ -44,8 +43,12 @@ __floorf (float x)
 	     "cvttq/svm %2,%1\n\t"
 #endif
 	     "cvtqt/m %1,%0\n\t"
-	     : "=f"(x), "=&f"(tmp1), "=&f"(tmp2)
+	     : "=f"(new_x), "=&f"(tmp1), "=&f"(tmp2)
 	     : "f"(x));
+
+      /* floor(-0) == -0, and in general we'll always have the same
+	 sign as our input.  */
+      x = copysignf(new_x, x);
     }
   return x;
 }
Index: sysdeps/alpha/fpu/s_rint.c
===================================================================
RCS file: s_rint.c
diff -N s_rint.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ s_rint.c	Mon Mar 20 12:26:12 2000
@@ -0,0 +1,50 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Richard Henderson.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <math.h>
+
+
+double
+__rint (double x)
+{
+  if (isless (fabs (x), 9007199254740992.0))	/* 1 << DBL_MANT_DIG */
+    {
+      double tmp1, new_x;
+      __asm (
+#ifdef _IEEE_FP_INEXACT
+	     "cvttq/svid %2,%1\n\t"
+#else
+	     "cvttq/svd %2,%1\n\t"
+#endif
+	     "cvtqt/d %1,%0\n\t"
+	     : "=f"(new_x), "=&f"(tmp1)
+	     : "f"(x));
+
+      /* rint(-0.1) == -0, and in general we'll always have the same
+	 sign as our input.  */
+      x = copysign(new_x, x);
+    }
+  return x;
+}
+
+weak_alias (__rint, rint)
+#ifdef NO_LONG_DOUBLE
+strong_alias (__rint, __rintl)
+weak_alias (__rint, rintl)
+#endif
Index: sysdeps/alpha/fpu/s_rintf.c
===================================================================
RCS file: s_rintf.c
diff -N s_rintf.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ s_rintf.c	Mon Mar 20 12:26:12 2000
@@ -0,0 +1,52 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Richard Henderson.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <math.h>
+
+
+float
+__rintf (float x)
+{
+  if (isless (fabsf (x), 16777216.0f))	/* 1 << FLT_MANT_DIG */
+    {
+      /* Note that Alpha S_Floating is stored in registers in a
+	 restricted T_Floating format, so we don't even need to
+	 convert back to S_Floating in the end.  The initial
+	 conversion to T_Floating is needed to handle denormals.  */
+
+      float tmp1, tmp2, new_x;
+
+      __asm ("cvtst/s %3,%2\n\t"
+#ifdef _IEEE_FP_INEXACT
+	     "cvttq/svid %2,%1\n\t"
+#else
+	     "cvttq/svd %2,%1\n\t"
+#endif
+	     "cvtqt/d %1,%0\n\t"
+	     : "=f"(new_x), "=&f"(tmp1), "=&f"(tmp2)
+	     : "f"(x));
+
+      /* rint(-0.1) == -0, and in general we'll always have the same
+	 sign as our input.  */
+      x = copysignf(new_x, x);
+    }
+  return x;
+}
+
+weak_alias (__rintf, rintf)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]