[Python-checkins] r82614 - in python/branches/py3k-dtoa: Lib/test/test_float.py Objects/floatobject.c configure configure.in pyconfig.h.in

mark.dickinson python-checkins at python.org
Tue Jul 6 20:52:42 CEST 2010


Author: mark.dickinson
Date: Tue Jul  6 20:52:42 2010
New Revision: 82614

Log:
Add float.__setround__ and float.__getround__ for rounding mode control (for test purposes only).

Modified:
   python/branches/py3k-dtoa/Lib/test/test_float.py
   python/branches/py3k-dtoa/Objects/floatobject.c
   python/branches/py3k-dtoa/configure
   python/branches/py3k-dtoa/configure.in
   python/branches/py3k-dtoa/pyconfig.h.in

Modified: python/branches/py3k-dtoa/Lib/test/test_float.py
==============================================================================
--- python/branches/py3k-dtoa/Lib/test/test_float.py	(original)
+++ python/branches/py3k-dtoa/Lib/test/test_float.py	Tue Jul  6 20:52:42 2010
@@ -16,6 +16,11 @@
                                          "requires __getformat__")
 requires_setformat = unittest.skipUnless(hasattr(float, "__setformat__"),
                                          "requires __setformat__")
+
+have_setround = hasattr(float, "__setround__")
+requires_setround = unittest.skipUnless(have_setround,
+                                        "requires __setround__")
+
 # decorator for skipping tests on non-IEEE 754 platforms
 requires_IEEE_754 = unittest.skipUnless(have_getformat and
     float.__getformat__("double").startswith("IEEE"),
@@ -382,6 +387,17 @@
             #self.assertTrue(0.0 > pow_op(-2.0, -1047) > -1e-315)
 
 
+ at requires_setround
+class RoundingModeTestCase(unittest.TestCase):
+    def test_roundtrip(self):
+        # just check that we can use __setround__ and __getround__
+        # to set and get rounding mode
+        modes = "tonearest", "upward", "downward", "towardzero"
+        for mode in modes:
+            float.__setround__(mode)
+            self.assertEqual(float.__getround__(), mode)
+
+
 @requires_setformat
 class FormatFunctionsTestCase(unittest.TestCase):
 
@@ -1264,6 +1280,7 @@
         RoundTestCase,
         InfNanTest,
         HexFloatTestCase,
+        RoundingModeTestCase,
         )
 
 if __name__ == '__main__':

Modified: python/branches/py3k-dtoa/Objects/floatobject.c
==============================================================================
--- python/branches/py3k-dtoa/Objects/floatobject.c	(original)
+++ python/branches/py3k-dtoa/Objects/floatobject.c	Tue Jul  6 20:52:42 2010
@@ -9,6 +9,10 @@
 
 #include <ctype.h>
 #include <float.h>
+#if defined(HAVE_FESETROUND) && defined(HAVE_FEGETROUND) && \
+    defined(HAVE_FENV_H)
+#include <fenv.h>
+#endif
 
 #undef MAX
 #undef MIN
@@ -1745,6 +1749,77 @@
 "Overrides the automatic determination of C-level floating point type.\n"
 "This affects how floats are converted to and from binary strings.");
 
+#if defined(HAVE_FESETROUND) && defined(HAVE_FEGETROUND) && \
+    defined(HAVE_FENV_H)
+
+static PyObject *
+float_setround(PyTypeObject *v, PyObject* arg)
+{
+    int mode, result;
+
+    if (!PyUnicode_Check(arg)) {
+        PyErr_SetString(PyExc_TypeError,
+            "mode should be a string");
+        return NULL;
+    }
+
+    if (!PyUnicode_CompareWithASCIIString(arg, "tonearest"))
+        mode = FE_TONEAREST;
+    else if (!PyUnicode_CompareWithASCIIString(arg, "downward"))
+        mode = FE_DOWNWARD;
+    else if (!PyUnicode_CompareWithASCIIString(arg, "upward"))
+        mode = FE_UPWARD;
+    else if (!PyUnicode_CompareWithASCIIString(arg, "towardzero"))
+        mode = FE_TOWARDZERO;
+    else {
+        PyErr_SetString(PyExc_ValueError,
+                        "mode should be one of 'tonearest', 'downward', "
+                        "'upward', or 'towardzero'");
+        return NULL;
+    }
+    result = fesetround(mode);
+    if (result) {
+        PyErr_SetString(PyExc_RuntimeError,
+                        "Failed to set rounding mode");
+        return NULL;
+    }
+    Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(float_setround_doc,
+"float.__setround__(mode) -> None\n"
+"\n"
+"You probably don't want to use this function.  It exists mainly to be\n"
+"used in Python's test suite.\n");
+
+static PyObject *
+float_getround(PyTypeObject *v)
+{
+    int i;
+    i = fegetround();
+
+    switch(i) {
+    case FE_TONEAREST:
+        return PyUnicode_FromString("tonearest");
+    case FE_DOWNWARD:
+        return PyUnicode_FromString("downward");
+    case FE_UPWARD:
+        return PyUnicode_FromString("upward");
+    case FE_TOWARDZERO:
+        return PyUnicode_FromString("towardzero");
+    default:
+        return PyUnicode_FromString("unknown");
+    }
+}
+
+PyDoc_STRVAR(float_getround_doc,
+"float.__getround__() -> str\n"
+"\n"
+"You probably don't want to use this function.  It exists mainly to be\n"
+"used in Python's test suite.\n");
+
+#endif  /* defined(HAVE_FESETROUND) && defined(HAVE_FEGETROUND) && ... */
+
 static PyObject *
 float_getzero(PyObject *v, void *closure)
 {
@@ -1798,6 +1873,13 @@
      METH_O|METH_CLASS,                 float_getformat_doc},
     {"__setformat__",           (PyCFunction)float_setformat,
      METH_VARARGS|METH_CLASS,           float_setformat_doc},
+#if defined(HAVE_FESETROUND) && defined(HAVE_FEGETROUND) && \
+    defined(HAVE_FENV_H)
+    {"__setround__", (PyCFunction)float_setround,
+     METH_O|METH_CLASS,           float_setround_doc},
+    {"__getround__", (PyCFunction)float_getround,
+     METH_NOARGS|METH_CLASS,           float_getround_doc},
+#endif
     {"__format__",          (PyCFunction)float__format__,
      METH_VARARGS,                  float__format__doc},
     {NULL,              NULL}           /* sentinel */

Modified: python/branches/py3k-dtoa/configure
==============================================================================
--- python/branches/py3k-dtoa/configure	(original)
+++ python/branches/py3k-dtoa/configure	Tue Jul  6 20:52:42 2010
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.in Revision: 82090 .
+# From configure.in Revision: 81583 .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.65 for python 3.2.
 #
@@ -1929,11 +1929,11 @@
        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 $ac_includes_default
+	     enum { N = $2 / 2 - 1 };
 int
 main ()
 {
-static int test_array [1 - 2 * !(enum { N = $2 / 2 - 1 };
-	     0 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1))];
+static int test_array [1 - 2 * !(0 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1))];
 test_array [0] = 0
 
   ;
@@ -1944,11 +1944,11 @@
   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 $ac_includes_default
+	        enum { N = $2 / 2 - 1 };
 int
 main ()
 {
-static int test_array [1 - 2 * !(enum { N = $2 / 2 - 1 };
-		($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1)
+static int test_array [1 - 2 * !(($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 1)
 		 < ($ac_type) ((((($ac_type) 1 << N) << N) - 1) * 2 + 2))];
 test_array [0] = 0
 
@@ -5995,7 +5995,7 @@
 fi
 
 for ac_header in asm/types.h conio.h curses.h direct.h dlfcn.h errno.h \
-fcntl.h grp.h \
+fcntl.h fenv.h grp.h \
 ieeefp.h io.h langinfo.h libintl.h ncurses.h poll.h process.h pthread.h \
 shadow.h signal.h stdint.h stropts.h termios.h thread.h \
 unistd.h utime.h \
@@ -11818,6 +11818,19 @@
 fi
 done
 
+for ac_func in fesetround fegetround
+do :
+  as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
+ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
+eval as_val=\$$as_ac_var
+   if test "x$as_val" = x""yes; then :
+  cat >>confdefs.h <<_ACEOF
+#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
+_ACEOF
+
+fi
+done
+
 ac_fn_c_check_decl "$LINENO" "isinf" "ac_cv_have_decl_isinf" "#include <math.h>
 "
 if test "x$ac_cv_have_decl_isinf" = x""yes; then :

Modified: python/branches/py3k-dtoa/configure.in
==============================================================================
--- python/branches/py3k-dtoa/configure.in	(original)
+++ python/branches/py3k-dtoa/configure.in	Tue Jul  6 20:52:42 2010
@@ -1299,7 +1299,7 @@
 # checks for header files
 AC_HEADER_STDC
 AC_CHECK_HEADERS(asm/types.h conio.h curses.h direct.h dlfcn.h errno.h \
-fcntl.h grp.h \
+fcntl.h fenv.h grp.h \
 ieeefp.h io.h langinfo.h libintl.h ncurses.h poll.h process.h pthread.h \
 shadow.h signal.h stdint.h stropts.h termios.h thread.h \
 unistd.h utime.h \
@@ -3433,6 +3433,7 @@
 
 AC_CHECK_FUNCS([acosh asinh atanh copysign erf erfc expm1 finite gamma])
 AC_CHECK_FUNCS([hypot lgamma log1p round tgamma])
+AC_CHECK_FUNCS([fesetround fegetround])
 AC_CHECK_DECLS([isinf, isnan, isfinite], [], [], [[#include <math.h>]])
 
 LIBS=$LIBS_SAVE

Modified: python/branches/py3k-dtoa/pyconfig.h.in
==============================================================================
--- python/branches/py3k-dtoa/pyconfig.h.in	(original)
+++ python/branches/py3k-dtoa/pyconfig.h.in	Tue Jul  6 20:52:42 2010
@@ -208,6 +208,15 @@
 /* Define if you have the 'fdatasync' function. */
 #undef HAVE_FDATASYNC
 
+/* Define to 1 if you have the `fegetround' function. */
+#undef HAVE_FEGETROUND
+
+/* Define to 1 if you have the <fenv.h> header file. */
+#undef HAVE_FENV_H
+
+/* Define to 1 if you have the `fesetround' function. */
+#undef HAVE_FESETROUND
+
 /* Define to 1 if you have the `finite' function. */
 #undef HAVE_FINITE
 


More information about the Python-checkins mailing list