[pypy-commit] pypy float-bytes: make native fmttable use longlong2float for converting stff

alex_gaynor noreply at buildbot.pypy.org
Sat Mar 17 17:34:21 CET 2012


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: float-bytes
Changeset: r53763:fbc8899a0a46
Date: 2012-03-17 11:34 -0500
http://bitbucket.org/pypy/pypy/changeset/fbc8899a0a46/

Log:	make native fmttable use longlong2float for converting stff

diff --git a/pypy/rlib/rstruct/nativefmttable.py b/pypy/rlib/rstruct/nativefmttable.py
--- a/pypy/rlib/rstruct/nativefmttable.py
+++ b/pypy/rlib/rstruct/nativefmttable.py
@@ -3,14 +3,17 @@
 The table 'native_fmttable' is also used by pypy.module.array.interp_array.
 """
 import struct
-from pypy.rlib import jit
+
+from pypy.rlib import jit, longlong2float
+from pypy.rlib.objectmodel import specialize
+from pypy.rlib.rarithmetic import r_singlefloat, widen
 from pypy.rlib.rstruct import standardfmttable as std
 from pypy.rlib.rstruct.error import StructError
+from pypy.rlib.unroll import unrolling_iterable
+from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.rpython.tool import rffi_platform
-from pypy.rpython.lltypesystem import lltype, rffi
-from pypy.rlib.rarithmetic import r_singlefloat
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
-from pypy.rlib.objectmodel import specialize
+
 
 native_is_bigendian = struct.pack("=i", 1) == struct.pack(">i", 1)
 
@@ -23,18 +26,24 @@
 
 # ____________________________________________________________
 
+
 double_buf = lltype.malloc(rffi.DOUBLEP.TO, 1, flavor='raw', immortal=True)
 float_buf = lltype.malloc(rffi.FLOATP.TO, 1, flavor='raw', immortal=True)
 
- at jit.dont_look_inside
-def double_to_ccharp(doubleval):
-    double_buf[0] = doubleval
-    return rffi.cast(rffi.CCHARP, double_buf)
+range_8_unroll = unrolling_iterable(list(reversed(range(8))))
+range_4_unroll = unrolling_iterable(list(reversed(range(4))))
 
 def pack_double(fmtiter):
     doubleval = fmtiter.accept_float_arg()
-    p = double_to_ccharp(doubleval)
-    fmtiter.result.append_charpsize(p, rffi.sizeof(rffi.DOUBLE))
+    value = longlong2float.float2longlong(doubleval)
+    if fmtiter.bigendian:
+        for i in range_8_unroll:
+            x = (value >> (8*i)) & 0xff
+            fmtiter.result.append(chr(x))
+    else:
+        for i in range_8_unroll:
+            fmtiter.result.append(chr(value & 0xff))
+            value >>= 8
 
 @specialize.argtype(0)
 def unpack_double(fmtiter):
@@ -45,16 +54,19 @@
     doubleval = double_buf[0]
     fmtiter.appendobj(doubleval)
 
- at jit.dont_look_inside
-def float_to_ccharp(floatval):
-    float_buf[0] = floatval
-    return rffi.cast(rffi.CCHARP, float_buf)
-
 def pack_float(fmtiter):
     doubleval = fmtiter.accept_float_arg()
     floatval = r_singlefloat(doubleval)
-    p = float_to_ccharp(floatval)
-    fmtiter.result.append_charpsize(p, rffi.sizeof(rffi.FLOAT))
+    value = longlong2float.singlefloat2uint(floatval)
+    value = widen(value)
+    if fmtiter.bigendian:
+        for i in range_4_unroll:
+            x = (value >> (8*i)) & 0xff
+            fmtiter.result.append(chr(x))
+    else:
+        for i in range_4_unroll:
+            fmtiter.result.append(chr(value & 0xff))
+            value >>= 8
 
 @specialize.argtype(0)
 def unpack_float(fmtiter):
diff --git a/pypy/rlib/rstruct/standardfmttable.py b/pypy/rlib/rstruct/standardfmttable.py
--- a/pypy/rlib/rstruct/standardfmttable.py
+++ b/pypy/rlib/rstruct/standardfmttable.py
@@ -6,11 +6,12 @@
 # values when packing.
 
 import struct
+
+from pypy.rlib.objectmodel import specialize
+from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
+from pypy.rlib.rstruct import ieee
 from pypy.rlib.rstruct.error import StructError, StructOverflowError
-from pypy.rlib.rstruct import ieee
 from pypy.rlib.unroll import unrolling_iterable
-from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
-from pypy.rlib.objectmodel import specialize
 
 # In the CPython struct module, pack() unconsistently accepts inputs
 # that are out-of-range or floats instead of ints.  Should we emulate


More information about the pypy-commit mailing list