When converting a floating value to an integer, properly raise the "invalid"
authorkettenis <kettenis@openbsd.org>
Fri, 30 Jul 2010 18:05:23 +0000 (18:05 +0000)
committerkettenis <kettenis@openbsd.org>
Fri, 30 Jul 2010 18:05:23 +0000 (18:05 +0000)
exception when the floating value is infinite or NaN or if the integral part
of the floating value exceeds the range of the integer type, as required by
the C99/IEEE754 standard.

Fixes Python 2.6 build on hppa.

ok miod@

sys/arch/hppa/spmath/fcnvfx.c
sys/arch/hppa/spmath/fcnvfxt.c

index 4b846f6..d29f441 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: fcnvfx.c,v 1.7 2003/04/10 17:27:58 mickey Exp $       */
+/*     $OpenBSD: fcnvfx.c,v 1.8 2010/07/30 18:05:23 kettenis Exp $     */
 /*
   (c) Copyright 1986 HEWLETT-PACKARD COMPANY
   To anyone who acknowledges that this file is provided "AS IS"
@@ -43,28 +43,15 @@ sgl_to_sgl_fcnvfx(srcptr, null, dstptr, status)
                /* check for MININT */
                if ((src_exponent > SGL_FX_MAX_EXP + 1) ||
                Sgl_isnotzero_mantissa(src) || Sgl_iszero_sign(src)) {
-                       if( Sgl_isnan(src) )
-                         /*
-                          * On NaN go unimplemented.
-                          */
-                         return(UNIMPLEMENTEDEXCEPTION);
-                       else {
-                         if (Sgl_iszero_sign(src)) result = 0x7fffffff;
-                         else result = 0x80000000;
+                       if (Sgl_iszero_sign(src)) result = 0x7fffffff;
+                       else result = 0x80000000;
 
-                         if (Is_overflowtrap_enabled()) {
-                           if (Is_inexacttrap_enabled())
-                             return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
-                           else Set_inexactflag();
-                           return(OVERFLOWEXCEPTION);
-                         }
-                         Set_overflowflag();
-                         *dstptr = result;
-                         if (Is_inexacttrap_enabled() )
-                               return(INEXACTEXCEPTION);
-                         else Set_inexactflag();
-                         return(NOEXCEPTION);
+                       if (Is_invalidtrap_enabled()) {
+                               return(INVALIDEXCEPTION);
                        }
+                       Set_invalidflag();
+                       *dstptr = result;
+                       return(NOEXCEPTION);
                }
        }
        /*
@@ -154,33 +141,21 @@ sgl_to_dbl_fcnvfx(srcptr, null, dstptr, status)
                /* check for MININT */
                if ((src_exponent > DBL_FX_MAX_EXP + 1) ||
                Sgl_isnotzero_mantissa(src) || Sgl_iszero_sign(src)) {
-                       if( Sgl_isnan(src) )
-                         /*
-                          * On NaN go unimplemented.
-                          */
-                         return(UNIMPLEMENTEDEXCEPTION);
-                       else {
-                         if (Sgl_iszero_sign(src)) {
+                       if (Sgl_iszero_sign(src)) {
                                resultp1 = 0x7fffffff;
-                             resultp2 = 0xffffffff;
-                         }
-                         else {
-                           resultp1 = 0x80000000;
-                           resultp2 = 0;
-                         }
-                         if (Is_overflowtrap_enabled()) {
-                           if (Is_inexacttrap_enabled())
-                             return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
-                           else Set_inexactflag();
-                           return(OVERFLOWEXCEPTION);
-                         }
-                         Set_overflowflag();
-                         Dint_copytoptr(resultp1,resultp2,dstptr);
-                         if (Is_inexacttrap_enabled() )
-                               return(INEXACTEXCEPTION);
-                         else Set_inexactflag();
-                         return(NOEXCEPTION);
+                               resultp2 = 0xffffffff;
+                       }
+                       else {
+                               resultp1 = 0x80000000;
+                               resultp2 = 0;
+                       }
+
+                       if (Is_invalidtrap_enabled()) {
+                               return(INVALIDEXCEPTION);
                        }
+                       Set_invalidflag();
+                       Dint_copytoptr(resultp1,resultp2,dstptr);
+                       return(NOEXCEPTION);
                }
                Dint_set_minint(resultp1,resultp2);
                Dint_copytoptr(resultp1,resultp2,dstptr);
@@ -288,28 +263,15 @@ dbl_to_sgl_fcnvfx(srcptr, null, dstptr, status)
        if (src_exponent > SGL_FX_MAX_EXP) {
                /* check for MININT */
                if (Dbl_isoverflow_to_int(src_exponent,srcp1,srcp2)) {
-                       if( Dbl_isnan(srcp1,srcp2) )
-                         /*
-                          * On NaN go unimplemented.
-                          */
-                         return(UNIMPLEMENTEDEXCEPTION);
-                       else {
-                         if (Dbl_iszero_sign(srcp1)) result = 0x7fffffff;
-                         else result = 0x80000000;
+                       if (Dbl_iszero_sign(srcp1)) result = 0x7fffffff;
+                       else result = 0x80000000;
 
-                         if (Is_overflowtrap_enabled()) {
-                           if (Is_inexacttrap_enabled())
-                             return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
-                           else Set_inexactflag();
-                           return(OVERFLOWEXCEPTION);
-                           }
-                         Set_overflowflag();
-                         *dstptr = result;
-                         if (Is_inexacttrap_enabled() )
-                               return(INEXACTEXCEPTION);
-                         else Set_inexactflag();
-                         return(NOEXCEPTION);
+                       if (Is_invalidtrap_enabled()) {
+                               return(INVALIDEXCEPTION);
                        }
+                       Set_invalidflag();
+                       *dstptr = result;
+                       return(NOEXCEPTION);
                }
        }
        /*
@@ -423,33 +385,21 @@ dbl_to_dbl_fcnvfx(srcptr, null, dstptr, status)
                /* check for MININT */
                if ((src_exponent > DBL_FX_MAX_EXP + 1) ||
                Dbl_isnotzero_mantissa(srcp1,srcp2) || Dbl_iszero_sign(srcp1)) {
-                       if( Dbl_isnan(srcp1,srcp2) )
-                         /*
-                          * On NaN go unimplemented.
-                          */
-                         return(UNIMPLEMENTEDEXCEPTION);
+                       if (Dbl_iszero_sign(srcp1)) {
+                               resultp1 = 0x7fffffff;
+                               resultp2 = 0xffffffff;
+                       }
                        else {
-                         if (Dbl_iszero_sign(srcp1)) {
-                            resultp1 = 0x7fffffff;
-                             resultp2 = 0xffffffff;
-                         }
-                         else {
-                           resultp1 = 0x80000000;
-                           resultp2 = 0;
-                         }
-                         if (Is_overflowtrap_enabled()) {
-                           if (Is_inexacttrap_enabled())
-                             return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
-                           else Set_inexactflag();
-                           return(OVERFLOWEXCEPTION);
-                         }
-                         Set_overflowflag();
-                         Dint_copytoptr(resultp1,resultp2,dstptr);
-                         if (Is_inexacttrap_enabled() )
-                               return(INEXACTEXCEPTION);
-                         else Set_inexactflag();
-                         return(NOEXCEPTION);
+                               resultp1 = 0x80000000;
+                               resultp2 = 0;
+                       }
+
+                       if (Is_invalidtrap_enabled()) {
+                               return(INVALIDEXCEPTION);
                        }
+                       Set_invalidflag();
+                       Dint_copytoptr(resultp1,resultp2,dstptr);
+                       return(NOEXCEPTION);
                }
        }
 
index 96e17dc..a29d15e 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: fcnvfxt.c,v 1.7 2003/04/10 17:27:58 mickey Exp $      */
+/*     $OpenBSD: fcnvfxt.c,v 1.8 2010/07/30 18:05:23 kettenis Exp $    */
 /*
   (c) Copyright 1986 HEWLETT-PACKARD COMPANY
   To anyone who acknowledges that this file is provided "AS IS"
@@ -43,28 +43,15 @@ sgl_to_sgl_fcnvfxt(srcptr, null, dstptr, status)
                /* check for MININT */
                if ((src_exponent > SGL_FX_MAX_EXP + 1) ||
                Sgl_isnotzero_mantissa(src) || Sgl_iszero_sign(src)) {
-                       if( Sgl_isnan(src) )
-                         /*
-                          * On NaN go unimplemented.
-                          */
-                         return(UNIMPLEMENTEDEXCEPTION);
-                       else {
-                         if (Sgl_iszero_sign(src)) result = 0x7fffffff;
-                         else result = 0x80000000;
+                       if (Sgl_iszero_sign(src)) result = 0x7fffffff;
+                       else result = 0x80000000;
 
-                         if (Is_overflowtrap_enabled()) {
-                           if (Is_inexacttrap_enabled())
-                             return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
-                           else Set_inexactflag();
-                           return(OVERFLOWEXCEPTION);
-                         }
-                         Set_overflowflag();
-                         *dstptr = result;
-                         if (Is_inexacttrap_enabled() )
-                               return(INEXACTEXCEPTION);
-                         else Set_inexactflag();
-                         return(NOEXCEPTION);
+                       if (Is_invalidtrap_enabled()) {
+                               return(INVALIDEXCEPTION);
                        }
+                       Set_invalidflag();
+                       *dstptr = result;
+                       return(NOEXCEPTION);
                }
        }
        /*
@@ -119,33 +106,21 @@ sgl_to_dbl_fcnvfxt(srcptr, null, dstptr, status)
                /* check for MININT */
                if ((src_exponent > DBL_FX_MAX_EXP + 1) ||
                Sgl_isnotzero_mantissa(src) || Sgl_iszero_sign(src)) {
-                       if( Sgl_isnan(src) )
-                         /*
-                          * On NaN go unimplemented.
-                          */
-                         return(UNIMPLEMENTEDEXCEPTION);
-                       else {
-                         if (Sgl_iszero_sign(src)) {
+                       if (Sgl_iszero_sign(src)) {
                                resultp1 = 0x7fffffff;
                                resultp2 = 0xffffffff;
-                         }
-                         else {
-                           resultp1 = 0x80000000;
-                           resultp2 = 0;
-                         }
-                         if (Is_overflowtrap_enabled()) {
-                           if (Is_inexacttrap_enabled())
-                             return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
-                           else Set_inexactflag();
-                           return(OVERFLOWEXCEPTION);
-                         }
-                         Set_overflowflag();
-                         Dint_copytoptr(resultp1,resultp2,dstptr);
-                         if (Is_inexacttrap_enabled() )
-                               return(INEXACTEXCEPTION);
-                         else Set_inexactflag();
-                         return(NOEXCEPTION);
                        }
+                       else {
+                               resultp1 = 0x80000000;
+                               resultp2 = 0;
+                       }
+
+                       if (Is_invalidtrap_enabled()) {
+                               return(INVALIDEXCEPTION);
+                       }
+                       Set_invalidflag();
+                       Dint_copytoptr(resultp1,resultp2,dstptr);
+                       return(NOEXCEPTION);
                }
                Dint_set_minint(resultp1,resultp2);
                Dint_copytoptr(resultp1,resultp2,dstptr);
@@ -204,28 +179,15 @@ dbl_to_sgl_fcnvfxt(srcptr, null, dstptr, status)
        if (src_exponent > SGL_FX_MAX_EXP) {
                /* check for MININT */
                if (Dbl_isoverflow_to_int(src_exponent,srcp1,srcp2)) {
-                       if( Dbl_isnan(srcp1,srcp2) )
-                         /*
-                          * On NaN go unimplemented.
-                          */
-                         return(UNIMPLEMENTEDEXCEPTION);
-                       else {
-                         if (Dbl_iszero_sign(srcp1)) result = 0x7fffffff;
-                         else result = 0x80000000;
+                       if (Dbl_iszero_sign(srcp1)) result = 0x7fffffff;
+                       else result = 0x80000000;
 
-                         if (Is_overflowtrap_enabled()) {
-                           if (Is_inexacttrap_enabled())
-                             return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
-                           else Set_inexactflag();
-                           return(OVERFLOWEXCEPTION);
-                         }
-                         Set_overflowflag();
-                         *dstptr = result;
-                         if (Is_inexacttrap_enabled() )
-                               return(INEXACTEXCEPTION);
-                         else Set_inexactflag();
-                         return(NOEXCEPTION);
+                       if (Is_invalidtrap_enabled()) {
+                               return(INVALIDEXCEPTION);
                        }
+                       Set_invalidflag();
+                       *dstptr = result;
+                       return(NOEXCEPTION);
                }
        }
        /*
@@ -282,33 +244,21 @@ dbl_to_dbl_fcnvfxt(srcptr, null, dstptr, status)
                /* check for MININT */
                if ((src_exponent > DBL_FX_MAX_EXP + 1) ||
                Dbl_isnotzero_mantissa(srcp1,srcp2) || Dbl_iszero_sign(srcp1)) {
-                       if( Dbl_isnan(srcp1,srcp2) )
-                         /*
-                          * On NaN go unimplemented.
-                          */
-                         return(UNIMPLEMENTEDEXCEPTION);
-                       else {
-                         if (Dbl_iszero_sign(srcp1)) {
+                       if (Dbl_iszero_sign(srcp1)) {
                                resultp1 = 0x7fffffff;
                                resultp2 = 0xffffffff;
-                         }
-                         else {
-                           resultp1 = 0x80000000;
-                           resultp2 = 0;
-                         }
-                         if (Is_overflowtrap_enabled()) {
-                           if (Is_inexacttrap_enabled())
-                             return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
-                           else Set_inexactflag();
-                           return(OVERFLOWEXCEPTION);
-                         }
-                         Set_overflowflag();
-                         Dint_copytoptr(resultp1,resultp2,dstptr);
-                         if (Is_inexacttrap_enabled() )
-                               return(INEXACTEXCEPTION);
-                         else Set_inexactflag();
-                         return(NOEXCEPTION);
                        }
+                       else {
+                               resultp1 = 0x80000000;
+                               resultp2 = 0;
+                       }
+
+                       if (Is_invalidtrap_enabled()) {
+                               return(INVALIDEXCEPTION);
+                       }
+                       Set_invalidflag();
+                       Dint_copytoptr(resultp1,resultp2,dstptr);
+                       return(NOEXCEPTION);
                }
        }
        /*