[pypy-svn] r15008 - in pypy/dist/pypy: annotation rpython rpython/module rpython/test translator/c translator/c/src

cfbolz at codespeak.net cfbolz at codespeak.net
Mon Jul 25 13:24:21 CEST 2005


Author: cfbolz
Date: Mon Jul 25 13:24:20 2005
New Revision: 15008

Added:
   pypy/dist/pypy/rpython/module/ll_math.py
   pypy/dist/pypy/translator/c/src/ll_math.h
Modified:
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/rpython/extfunctable.py
   pypy/dist/pypy/rpython/module/ll_os.py
   pypy/dist/pypy/rpython/rarithmetic.py
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/rpython/test/test_rarithmetic.py
   pypy/dist/pypy/translator/c/extfunc.py
   pypy/dist/pypy/translator/c/src/g_include.h
Log:
(pedronis, cfbolz):
started to clean up annotation/builtin.py and rpython/rbuiltin.py: all the math functions moved to extfunctable, added ll_math.py, ll_math.h. Still missing: more complicated C implementations.


Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Mon Jul 25 13:24:20 2005
@@ -220,21 +220,6 @@
 def conf():
     return SomeString()
 
-def math_fmod(x, y):
-    return SomeFloat()
-
-def math_floor(x):
-    return SomeFloat()
-
-def math_frexp(x):
-    return SomeTuple((SomeFloat(), SomeInteger()))
-
-def math_modf(x):
-    return SomeTuple((SomeFloat(), SomeFloat()))
-
-def math_any(*args):
-    return SomeFloat()
-
 def rarith_intmask(s_obj):
     return SomeInteger()
 
@@ -293,12 +278,6 @@
 # this one is needed otherwise when annotating assert in a test we may try to annotate 
 # py.test AssertionError.__init__ .
 BUILTIN_ANALYZERS[AssertionError.__init__.im_func] = exception_init
-BUILTIN_ANALYZERS[math.fmod] = math_fmod
-BUILTIN_ANALYZERS[math.floor] = math_floor
-BUILTIN_ANALYZERS[math.exp] = math_any
-BUILTIN_ANALYZERS[math.ldexp] = math_any
-BUILTIN_ANALYZERS[math.frexp] = math_frexp
-BUILTIN_ANALYZERS[math.modf] = math_modf
 BUILTIN_ANALYZERS[sys.getrefcount] = count
 BUILTIN_ANALYZERS[sys.getdefaultencoding] = conf
 import unicodedata

Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py	(original)
+++ pypy/dist/pypy/rpython/extfunctable.py	Mon Jul 25 13:24:20 2005
@@ -5,7 +5,6 @@
 import time
 import math
 import types
-from pypy.annotation.model import SomeInteger, SomeTuple, SomeFloat
 
 
 class ExtFuncInfo:
@@ -50,26 +49,44 @@
 
 # _____________________________________________________________
 
-nonefactory = lambda *args: None
-tuplefactory = lambda *args: SomeTuple((SomeInteger(),)*10)
-frexpfactory = lambda *args: SomeTuple((SomeFloat(),SomeInteger()))
+
+
+def noneannotation(*args):
+    return None
+
+def statannotation(*args):
+    from pypy.annotation.model import SomeInteger, SomeTuple
+    return SomeTuple((SomeInteger(),)*10)
+
+def frexpannotation(*args):
+    from pypy.annotation.model import SomeInteger, SomeTuple, SomeFloat
+    return SomeTuple((SomeFloat(), SomeInteger()))
+
+def modfannotation(*args):
+    from pypy.annotation.model import SomeTuple, SomeFloat
+    return SomeTuple((SomeFloat(), SomeFloat()))
 
 # external function declarations
-declare(os.open     , int        , 'll_os/open')
-declare(os.read     , str        , 'll_os/read')
-declare(os.write    , int        , 'll_os/write')
-declare(os.close    , nonefactory, 'll_os/close')
-declare(os.getcwd   , str        , 'll_os/getcwd')
-declare(os.dup      , int        , 'll_os/dup')
-declare(os.lseek    , int        , 'll_os/lseek')
-declare(os.isatty   , bool       , 'll_os/isatty')
-declare(os.ftruncate, nonefactory, 'll_os/ftruncate')
-declare(os.fstat    , tuplefactory, 'll_os/fstat')
-declare(os.stat     , tuplefactory, 'll_os/stat')
-declare(time.time   , float      , 'll_time/time')
-declare(time.clock  , float      , 'll_time/clock')
-declare(time.sleep  , nonefactory, 'll_time/sleep')
-#declare(math.log10  , float      , 'll_math/log10')
-#declare(math.ceil   , float      , 'll_math/ceil')
-#declare(math.frexp  , frexpfactory, 'll_math/frexp')
-#declare(math.atan2  , float      , 'll_math/atan2')
+declare(os.open     , int           , 'll_os/open')
+declare(os.read     , str           , 'll_os/read')
+declare(os.write    , int           , 'll_os/write')
+declare(os.close    , noneannotation, 'll_os/close')
+declare(os.getcwd   , str           , 'll_os/getcwd')
+declare(os.dup      , int           , 'll_os/dup')
+declare(os.lseek    , int           , 'll_os/lseek')
+declare(os.isatty   , bool          , 'll_os/isatty')
+declare(os.ftruncate, noneannotation, 'll_os/ftruncate')
+declare(os.fstat    , statannotation, 'll_os/fstat')
+declare(os.stat     , statannotation, 'll_os/stat')
+declare(time.time   , float         , 'll_time/time')
+declare(time.clock  , float         , 'll_time/clock')
+declare(time.sleep  , noneannotation, 'll_time/sleep')
+declare(math.log10  , float         , 'll_math/log10')
+declare(math.ceil   , float         , 'll_math/ceil')
+declare(math.frexp  , frexpannotation, 'll_math/frexp')
+declare(math.atan2  , float         , 'll_math/atan2')
+declare(math.fmod   , float         ,  'll_math/fmod')
+declare(math.floor  , float         ,  'll_math/floor')
+declare(math.exp    , float         ,  'll_math/exp')
+declare(math.ldexp  , float         ,  'll_math/ldexp')
+declare(math.modf   , modfannotation, 'll_math/modf')

Added: pypy/dist/pypy/rpython/module/ll_math.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/module/ll_math.py	Mon Jul 25 13:24:20 2005
@@ -0,0 +1,58 @@
+from pypy.rpython import lltype
+
+import math
+
+def ll_math_log10(x):
+    return math.log10(x)
+ll_math_log10.suggested_primitive = True
+
+def ll_math_ceil(x):
+    return math.ceil(x)
+ll_math_ceil.suggested_primitive = True
+
+
+FREXP_RESULT = lltype.GcStruct('tuple2', ('item0', lltype.Float),
+                               ('item1', lltype.Signed))
+
+def ll_frexp_result(mantissa, exponent):
+    tup = lltype.malloc(FREXP_RESULT)
+    tup.item0 = mantissa
+    tup.item1 = exponent
+    return tup
+    
+def ll_math_frexp(x):
+    mantissa, exponent = math.frexp(x)
+    return ll_frexp_result(mantissa, exponent)
+ll_math_frexp.suggested_primitive = True
+
+def ll_math_atan2(x, y):
+    return math.atan2(x, y)
+ll_math_atan2.suggested_primitive = True
+
+def ll_math_fmod(x, y):
+    return math.fmod(x, y)
+ll_math_fmod.suggested_primitive = True
+
+def ll_math_floor(x):
+    return math.floor(x)
+ll_math_floor.suggested_primitive = True
+
+def ll_math_exp(x):
+    return math.exp(x)
+
+def ll_math_ldexp(x, y):
+    return math.ldexp(x, y)
+
+MODF_RESULT = lltype.GcStruct('tuple2', ('item0', lltype.Float),
+                       ('item1', lltype.Float))
+
+def ll_modf_result(fracpart, intpart):
+    tup = lltype.malloc(MODF_RESULT)
+    tup.item0 = fracpart
+    tup.item1 = intpart
+    return tup
+
+def ll_math_modf(x):
+    fracpart, intpart = math.modf(x)
+    return ll_modf_result(fracpart, intpart)
+ll_modf_result.suggested_primitive = True

Modified: pypy/dist/pypy/rpython/module/ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/module/ll_os.py	(original)
+++ pypy/dist/pypy/rpython/module/ll_os.py	Mon Jul 25 13:24:20 2005
@@ -93,31 +93,34 @@
 fieldnames = ['item%d' % i for i in range(n)]
 lltypes = [Signed]*n
 fields = tuple(zip(fieldnames, lltypes))    
-tup = malloc(GcStruct('tuple%d' % n, *fields))
+STAT_RESULT = GcStruct('tuple%d' % n, *fields)
 
 from pypy.rpython.rarithmetic import intmask
 
-def stat_to_rtuple(stat):
-    #n = len(stat)
-    tup.item0 = intmask(stat[0])
-    tup.item1 = intmask(stat[1])
-    tup.item2 = intmask(stat[2])
-    tup.item3 = intmask(stat[3])
-    tup.item4 = intmask(stat[4])
-    tup.item5 = intmask(stat[5])
-    tup.item6 = intmask(stat[6])
-    tup.item7 = intmask(stat[7])
-    tup.item8 = intmask(stat[8])
-    tup.item9 = intmask(stat[9])
+def ll_stat_result(stat0, stat1, stat2, stat3, stat4,
+                   stat5, stat6, stat7, stat8, stat9):
+    tup = malloc(STAT_RESULT)
+    tup.item0 = intmask(stat0)
+    tup.item1 = intmask(stat1)
+    tup.item2 = intmask(stat2)
+    tup.item3 = intmask(stat3)
+    tup.item4 = intmask(stat4)
+    tup.item5 = intmask(stat5)
+    tup.item6 = intmask(stat6)
+    tup.item7 = intmask(stat7)
+    tup.item8 = intmask(stat8)
+    tup.item9 = intmask(stat9)
     
 def ll_os_fstat(fd):
-    stat = os.fstat(fd)
-    stat_to_rtuple(stat)
-    return tup
+    (stat0, stat1, stat2, stat3, stat4,
+     stat5, stat6, stat7, stat8, stat9) = os.fstat(fd)
+    return ll_stat_result(stat0, stat1, stat2, stat3, stat4,
+                          stat5, stat6, stat7, stat8, stat9)
 ll_os_fstat.suggested_primitive = True
 
 def ll_os_stat(path):
-    stat = os.stat(from_rstr(path))
-    stat_to_tuple(stat)
-    return tup
-ll_os_stat.suggested_primitive = True
\ No newline at end of file
+    (stat0, stat1, stat2, stat3, stat4,
+     stat5, stat6, stat7, stat8, stat9) = os.stat(from_rstr(path))
+    return ll_stat_result(stat0, stat1, stat2, stat3, stat4,
+                          stat5, stat6, stat7, stat8, stat9)
+ll_os_fstat.suggested_primitive = True

Modified: pypy/dist/pypy/rpython/rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rpython/rarithmetic.py	(original)
+++ pypy/dist/pypy/rpython/rarithmetic.py	Mon Jul 25 13:24:20 2005
@@ -196,7 +196,7 @@
 
 def ovfcheck_float_to_int(x):
     _, intp = math.modf(x)
-    if FL_MININT <= intp <= FL_MAXINT:
+    if FL_MININT < intp < FL_MAXINT:
         return int(intp)
     raise OverflowError
 

Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Mon Jul 25 13:24:20 2005
@@ -191,14 +191,6 @@
         v_errno = hop.inputarg(lltype.Signed, arg=1)
         r_self.setfield(v_self, 'errno', v_errno, hop.llops)
 
-def rtype_math_floor(hop):
-    vlist = hop.inputargs(lltype.Float)
-    return hop.genop('float_floor', vlist, resulttype=lltype.Float)
-
-def rtype_math_fmod(hop):
-    vlist = hop.inputargs(lltype.Float, lltype.Float)
-    return hop.genop('float_fmod', vlist, resulttype=lltype.Float)
-
 def ll_instantiate(typeptr, RESULT):
     my_instantiate = typeptr.instantiate
     return lltype.cast_pointer(RESULT, my_instantiate())
@@ -216,10 +208,6 @@
     return rclass.rtype_new_instance(hop.rtyper, klass, hop.llops)
 
 
-import math
-##def ll_floor(f1):
-##    return float(int((f1)
-
 # collect all functions
 import __builtin__
 BUILTIN_TYPER = {}
@@ -227,8 +215,6 @@
     if name.startswith('rtype_builtin_'):
         original = getattr(__builtin__, name[14:])
         BUILTIN_TYPER[original] = value
-BUILTIN_TYPER[math.floor] = rtype_math_floor
-BUILTIN_TYPER[math.fmod] = rtype_math_fmod
 BUILTIN_TYPER[Exception.__init__.im_func] = rtype_Exception__init__
 BUILTIN_TYPER[AssertionError.__init__.im_func] = rtype_Exception__init__
 BUILTIN_TYPER[OSError.__init__.im_func] = rtype_OSError__init__
@@ -291,14 +277,6 @@
     
 import math
 
-def rtype_math_exp(hop):
-    vlist = hop.inputargs(lltype.Float)
-    # XXX need PyFPE_START_PROTECT/PyFPE_END_PROTECT/Py_SET_ERRNO_ON_MATH_ERROR
-    return hop.llops.gencapicall('exp', vlist, resulttype=lltype.Float,
-                                 includes=("math.h",))   # XXX clean up needed
-
-BUILTIN_TYPER[math.exp] = rtype_math_exp
-
 from pypy.rpython import extfunctable
 
 def make_rtype_extfunc(extfuncinfo):

Modified: pypy/dist/pypy/rpython/test/test_rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rarithmetic.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rarithmetic.py	Mon Jul 25 13:24:20 2005
@@ -242,10 +242,12 @@
     assert ovfcheck_float_to_int(13.0) == 13
     assert ovfcheck_float_to_int(-1.0) == -1
     assert ovfcheck_float_to_int(-13.0) == -13
-    assert ovfcheck_float_to_int(float(sys.maxint-1)) == sys.maxint-1
-    assert ovfcheck_float_to_int(float(sys.maxint)) == sys.maxint
-    assert ovfcheck_float_to_int(float(-sys.maxint)) == -sys.maxint
-    assert ovfcheck_float_to_int(float(-sys.maxint-1)) == -sys.maxint-1
+    #  strange things happening for float to int on 64 bit
+    maxint32 = 2 ** 31 - 1
+    assert ovfcheck_float_to_int(float(maxint32-1)) == maxint32-1
+    assert ovfcheck_float_to_int(float(maxint32)) == maxint32
+    assert ovfcheck_float_to_int(float(-maxint32)) == -maxint32
+    assert ovfcheck_float_to_int(float(-maxint32-1)) == -maxint32-1
 
     try:
         ovfcheck_float_to_int(float(-sys.maxint-1)-1)

Modified: pypy/dist/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/extfunc.py	Mon Jul 25 13:24:20 2005
@@ -3,7 +3,7 @@
 from pypy.translator.c.support import cdecl
 from pypy.rpython.rmodel import getfunctionptr
 from pypy.rpython.rstr import STR
-from pypy.rpython.module import ll_os, ll_time
+from pypy.rpython.module import ll_os, ll_time, ll_math
 
 
 # table of functions hand-written in src/ll_*.h
@@ -15,6 +15,15 @@
     ll_os  .ll_os_dup:     'LL_os_dup',
     ll_os  .ll_os_getcwd:  'LL_os_getcwd',
     ll_time.ll_time_clock: 'LL_time_clock',
+    ll_math.ll_math_log10: 'LL_math_log10',
+    ll_math.ll_math_ceil:  'LL_math_ceil',
+    ll_math.ll_math_frexp: 'LL_math_frexp',
+    ll_math.ll_math_atan2: 'LL_math_atan2',
+    ll_math.ll_math_fmod : 'LL_math_fmod',
+    ll_math.ll_math_floor: 'LL_math_floor',
+    ll_math.ll_math_exp:   'LL_math_exp',
+    ll_math.ll_math_ldexp: 'LL_math_ldexp',
+    ll_math.ll_math_modf:  'LL_math_modf',
     }
 
 

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 Jul 25 13:24:20 2005
@@ -27,4 +27,5 @@
 #  include "src/rtyper.h"
 #  include "src/ll_os.h"
 #  include "src/ll_time.h"
+#  include "src/ll_math.h"
 #endif

Added: pypy/dist/pypy/translator/c/src/ll_math.h
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/c/src/ll_math.h	Mon Jul 25 13:24:20 2005
@@ -0,0 +1,38 @@
+/************************************************************/
+/***  C header subsection: math module                    ***/
+
+#include "math.h"
+
+/* The functions below are mapped to functions from pypy.rpython.module.*
+   by the pypy.translator.c.extfunc.EXTERNALS dictionary.
+   They should correspond to the functions with the suggested_primitive
+   flag set.
+*/
+
+
+/* XXX completely ignoring exceptions/error checking for now */
+double LL_math_log10(double x) {
+  return log10(x);
+}
+double LL_math_ceil(double x) {
+  return ceil(x);
+}
+/* LL_math_frexp XXX strange stuff*/
+double LL_math_atan2(double x, double y) {
+  return atan2(x, y);
+}
+double LL_math_fmod(double x, double y) {
+  return fmod(x, y);
+}
+double LL_math_floor(double x) {
+  return floor(x);
+}
+double LL_math_exp(double x) {
+  return exp(x);
+}
+double LL_math_ldexp(double x, long y) {
+  return ldexp(x, (int) y);
+}
+
+/* LL_math_modf XXXX strange stuff*/
+



More information about the Pypy-commit mailing list