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

pedronis at codespeak.net pedronis at codespeak.net
Mon Aug 8 21:25:47 CEST 2005


Author: pedronis
Date: Mon Aug  8 21:25:41 2005
New Revision: 15792

Added:
   pypy/dist/pypy/rpython/module/ll_strtod.py   (contents, props changed)
   pypy/dist/pypy/rpython/module/test/test_ll_strtod.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/extfunctable.py
   pypy/dist/pypy/rpython/rarithmetic.py
   pypy/dist/pypy/translator/c/extfunc.py
   pypy/dist/pypy/translator/c/src/g_include.h
   pypy/dist/pypy/translator/c/test/test_extfunc.py
Log:
support for low-level wrapper around strtod, to be called by strutil



Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py	(original)
+++ pypy/dist/pypy/rpython/extfunctable.py	Mon Aug  8 21:25:41 2005
@@ -120,3 +120,8 @@
     s = ntpath.splitdrive(s)[1]
     return s != '' and s[0] in '/\\'
 ntpath.isabs = isabs
+
+# ___________________________________________________________
+# string->float helper
+from pypy.rpython import rarithmetic
+declare(rarithmetic.parts_to_float, float, 'll_strtod/parts_to_float')

Added: pypy/dist/pypy/rpython/module/ll_strtod.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/module/ll_strtod.py	Mon Aug  8 21:25:41 2005
@@ -0,0 +1,12 @@
+# string -> float helper
+from pypy.rpython import rarithmetic
+from pypy.rpython.module.support import to_rstr, from_rstr, ll_strcpy
+
+
+def ll_strtod_parts_to_float(sign, beforept, afterpt, exponent):
+    return rarithmetic.parts_to_float(from_rstr(sign),
+                                      from_rstr(beforept),
+                                      from_rstr(afterpt),
+                                      from_rstr(exponent))
+
+ll_strtod_parts_to_float.suggested_primitive = True

Added: pypy/dist/pypy/rpython/module/test/test_ll_strtod.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/module/test/test_ll_strtod.py	Mon Aug  8 21:25:41 2005
@@ -0,0 +1,18 @@
+
+from pypy.rpython.module.ll_strtod import ll_strtod_parts_to_float
+from pypy.rpython.module.support import to_rstr
+
+
+def test_it():
+    data = [
+    (("","1","","")     , 1.0),
+    (("-","1","","")    , -1.0),
+    (("-","1","5","")   , -1.5),
+    (("-","1","5","2")  , -1.5e2),
+    (("-","1","5","+2") , -1.5e2),
+    (("-","1","5","-2") , -1.5e-2),
+    ]
+
+    for parts, val in data:
+        assert ll_strtod_parts_to_float(*map(to_rstr, parts)) == val
+    

Modified: pypy/dist/pypy/rpython/rarithmetic.py
==============================================================================
--- pypy/dist/pypy/rpython/rarithmetic.py	(original)
+++ pypy/dist/pypy/rpython/rarithmetic.py	Mon Aug  8 21:25:41 2005
@@ -369,3 +369,12 @@
 
 setup_typemap()
 del setup_typemap
+
+# string -> float helper
+
+def parts_to_float(sign, beforept, afterpt, exponent):
+    if not exponent:
+        exponent = '0'
+    return float("%s%s.%se%s" % (sign, beforept, afterpt, exponent))
+
+# float -> string

Modified: pypy/dist/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/extfunc.py	Mon Aug  8 21:25:41 2005
@@ -4,7 +4,7 @@
 from pypy.rpython.rmodel import getfunctionptr
 from pypy.rpython.rstr import STR
 from pypy.rpython import rlist
-from pypy.rpython.module import ll_os, ll_time, ll_math
+from pypy.rpython.module import ll_os, ll_time, ll_math, ll_strtod
 
 
 # table of functions hand-written in src/ll_*.h
@@ -30,6 +30,8 @@
     ll_math.ll_math_ldexp: 'LL_math_ldexp',
     ll_math.ll_math_modf:  'LL_math_modf',
     ll_math.ll_math_hypot: 'LL_math_hypot',
+    ll_strtod.ll_strtod_parts_to_float:
+        'LL_strtod_parts_to_float',
     }
 
 #______________________________________________________

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 Aug  8 21:25:41 2005
@@ -35,6 +35,7 @@
 #  include "src/ll_os.h"
 #  include "src/ll_time.h"
 #  include "src/ll_math.h"
+#  include "src/ll_strtod.h"
 #endif
 
 #ifdef PYPY_STANDALONE

Modified: pypy/dist/pypy/translator/c/test/test_extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_extfunc.py	Mon Aug  8 21:25:41 2005
@@ -214,3 +214,23 @@
     res = fn()
     t1 = time.time()
     assert t0 <= res <= t1
+
+
+def test_rarith_parts_to_float():
+    from pypy.rpython.rarithmetic import parts_to_float
+    def fn(sign, beforept, afterpt, exponent):
+        return parts_to_float(sign, beforept, afterpt, exponent)
+
+    f = compile(fn, [str, str, str, str])
+    
+    data = [
+    (("","1","","")     , 1.0),
+    (("-","1","","")    , -1.0),
+    (("-","1","5","")   , -1.5),
+    (("-","1","5","2")  , -1.5e2),
+    (("-","1","5","+2") , -1.5e2),
+    (("-","1","5","-2") , -1.5e-2),
+    ]
+
+    for parts, val in data:
+        assert f(*parts) == val



More information about the Pypy-commit mailing list