[pypy-svn] r48600 - in pypy/dist/pypy: rpython rpython/lltypesystem/module rpython/lltypesystem/module/test translator/c translator/c/src

fijal at codespeak.net fijal at codespeak.net
Mon Nov 12 16:21:17 CET 2007


Author: fijal
Date: Mon Nov 12 16:21:16 2007
New Revision: 48600

Added:
   pypy/dist/pypy/rpython/lltypesystem/module/test/
   pypy/dist/pypy/rpython/lltypesystem/module/test/__init__.py   (contents, props changed)
   pypy/dist/pypy/rpython/lltypesystem/module/test/test_ll_math.py   (contents, props changed)
Removed:
   pypy/dist/pypy/translator/c/src/ll_math.h
Modified:
   pypy/dist/pypy/rpython/extfuncregistry.py
   pypy/dist/pypy/rpython/lltypesystem/module/ll_math.py
   pypy/dist/pypy/translator/c/extfunc.py
   pypy/dist/pypy/translator/c/src/g_include.h
Log:
Move ll_math to rffi.


Modified: pypy/dist/pypy/rpython/extfuncregistry.py
==============================================================================
--- pypy/dist/pypy/rpython/extfuncregistry.py	(original)
+++ pypy/dist/pypy/rpython/extfuncregistry.py	Mon Nov 12 16:21:16 2007
@@ -1,5 +1,6 @@
-# this registry use the new interface for external functions
-# all the above declarations in extfunctable should be moved here at some point.
+# this registry uses the new interface for external functions
+# all the above declarations in extfunctable should be moved here
+# at some point.
 
 from extfunc import register_external
 
@@ -20,24 +21,18 @@
 
 # the following functions all take one float, return one float
 # and are part of math.h
-simple_math_functions = [
-    'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'exp', 'fabs',
-    'floor', 'log', 'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'
-    ]
-for name in simple_math_functions:
+for name in ll_math.unary_math_functions:
+    llimpl = getattr(ll_math, 'll_math_%s' % name, None)
     register_external(getattr(math, name), [float], float,
-                      "ll_math.ll_math_%s" % name,
-                       sandboxsafe=True)
+                      export_name="ll_math.ll_math_%s" % name,
+                       sandboxsafe=True, llimpl=llimpl)
 
 complex_math_functions = [
     ('frexp', [float],        (float, int)),
-    ('atan2', [float, float], float),
-    ('fmod',  [float, float], float),
     ('ldexp', [float, int],   float),
     ('modf',  [float],        (float, float)),
-    ('hypot', [float, float], float),
-    ('pow',   [float, float], float),
-    ]
+    ] + [(name, [float, float], float) for name in
+         ll_math.binary_math_functions]
 
 for name, args, res in complex_math_functions:
     func = getattr(math, name)

Modified: pypy/dist/pypy/rpython/lltypesystem/module/ll_math.py
==============================================================================
--- pypy/dist/pypy/rpython/lltypesystem/module/ll_math.py	(original)
+++ pypy/dist/pypy/rpython/lltypesystem/module/ll_math.py	Mon Nov 12 16:21:16 2007
@@ -1,10 +1,23 @@
 import math
+import errno
+import py
 from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.tool.sourcetools import func_with_new_name
 
 math_frexp = rffi.llexternal('frexp', [rffi.DOUBLE, rffi.INTP], rffi.DOUBLE,
                              sandboxsafe=True)
 math_modf  = rffi.llexternal('modf',  [rffi.DOUBLE, rffi.DOUBLEP], rffi.DOUBLE,
                              sandboxsafe=True)
+math_ldexp = rffi.llexternal('ldexp', [rffi.DOUBLE, rffi.INT], rffi.DOUBLE)
+
+unary_math_functions = [
+    'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'exp', 'fabs',
+    'floor', 'log', 'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'
+    ]
+
+binary_math_functions = [
+    'atan2', 'fmod', 'hypot', 'pow'
+    ]
 
 def ll_math_frexp(x):
     exp_p = lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
@@ -19,3 +32,57 @@
     intpart = intpart_p[0]
     lltype.free(intpart_p, flavor='raw')
     return (fracpart, intpart)
+
+def ll_math_ldexp(x, exp):
+    _error_reset()
+    r = math_ldexp(x, exp)
+    _check_error(r)
+    return r
+
+def _error_reset():
+    rffi.set_errno(0)
+
+ERANGE = errno.ERANGE
+def _check_error(x):
+    errno = rffi.get_errno()
+    if errno:
+        if errno == ERANGE:
+            if not x:
+                raise OSError
+            raise OverflowError("math range error")
+        else:
+            raise ValueError("math domain error")
+
+def new_unary_math_function(name):
+    c_func = rffi.llexternal(name, [rffi.DOUBLE], rffi.DOUBLE,
+                             sandboxsafe=True, libraries=['m'])
+
+    def ll_math(x):
+        _error_reset()
+        r = c_func(x)
+        _check_error(r)
+        return r
+
+    return func_with_new_name(ll_math, 'll_math_' + name)
+
+def new_binary_math_function(name):
+    c_func = rffi.llexternal(name, [rffi.DOUBLE, rffi.DOUBLE], rffi.DOUBLE,
+                             sandboxsafe=True, libraries=['m'])
+
+    def ll_math(x, y):
+        _error_reset()
+        r = c_func(x, y)
+        _check_error(r)
+        return r
+
+    return func_with_new_name(ll_math, 'll_math_' + name)
+
+# the two above are almost the same, but they're C-c C-v not to go mad
+# with meta-programming
+
+for name in unary_math_functions:
+    globals()['ll_math_' + name] = new_unary_math_function(name)
+    
+for name in binary_math_functions:
+    globals()['ll_math_' + name] = new_binary_math_function(name)
+    

Added: pypy/dist/pypy/rpython/lltypesystem/module/test/__init__.py
==============================================================================

Added: pypy/dist/pypy/rpython/lltypesystem/module/test/test_ll_math.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/lltypesystem/module/test/test_ll_math.py	Mon Nov 12 16:21:16 2007
@@ -0,0 +1,44 @@
+
+""" Just another bunch of tests for llmath, run on top of llinterp
+"""
+
+from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin
+from pypy.rpython.lltypesystem.module import ll_math
+import math
+
+# XXX no OORtypeMixin here
+
+class TestMath(BaseRtypingTest, LLRtypeMixin):
+    def new_unary_test(name):
+        def next_test(self):
+            def f(x):
+                return getattr(math, name)(x)
+            assert self.interpret(f, [0.3]) == f(0.3)
+        return next_test
+
+    def new_binary_test(name):
+        def next_test(self):
+            def f(x, y):
+                return getattr(math, name)(x, y)
+            assert self.interpret(f, [0.3, 0.4]) == f(0.3, 0.4)
+        return next_test
+    
+    for name in ll_math.unary_math_functions:
+        func_name = 'test_%s' % (name,)
+        next_test = new_unary_test(name)
+        next_test.func_name = func_name
+        locals()[func_name] = next_test
+        del next_test
+        
+    for name in ll_math.binary_math_functions:
+        func_name = 'test_%s' % (name,)
+        next_test = new_binary_test(name)
+        next_test.func_name = func_name
+        locals()[func_name] = next_test
+        del next_test
+    
+    def test_ldexp(self):
+        def f(x, y):
+            return math.ldexp(x, y)
+
+        assert self.interpret(f, [3.4, 2]) == f(3.4, 2)

Modified: pypy/dist/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/extfunc.py	Mon Nov 12 16:21:16 2007
@@ -25,26 +25,6 @@
     }
 
 #______________________________________________________
-# insert 'simple' math functions into EXTERNALs table:
-
-# XXX: messy, messy, messy
-# this interacts in strange ways with node.select_function_code_generators,
-# because it fakes to be an ll_* function.
-
-math_functions = [
-    'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'exp', 'fabs',
-    'floor', 'log', 'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh',
-    'pow', 'atan2', 'fmod', 'ldexp', 'hypot'
-    ]
-# frexp and modf have been ported to the new rffi style already
-
-import math
-for name in math_functions:
-    EXTERNALS['ll_math.ll_math_%s' % name] = 'LL_math_%s' % name
-
-EXTERNALS['LL_flush_icache'] = 'LL_flush_icache'
-
-#______________________________________________________
 
 def find_list_of_str(rtyper):
     for r in rtyper.reprs.itervalues():
@@ -53,7 +33,6 @@
     return None
 
 def predeclare_common_types(db, rtyper):
-    from pypy.rpython.lltypesystem.module import ll_math
     # Common types
     yield ('RPyString', STR)
     LIST_OF_STR = find_list_of_str(rtyper)

Modified: pypy/dist/pypy/translator/c/src/g_include.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/g_include.h	(original)
+++ pypy/dist/pypy/translator/c/src/g_include.h	Mon Nov 12 16:21:16 2007
@@ -53,7 +53,6 @@
 #  include "src/rtyper.h"
 #ifndef AVR
 #  include "src/ll_os.h"
-#  include "src/ll_math.h"
 #  include "src/ll_strtod.h"
 #  ifdef RPyExc_thread_error
 #    include "src/ll_thread.h"



More information about the Pypy-commit mailing list