[pypy-commit] pypy missing-ndarray-attributes: byteswap on floats

fijal noreply at buildbot.pypy.org
Mon Oct 29 11:39:24 CET 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: missing-ndarray-attributes
Changeset: r58574:791ffe776736
Date: 2012-10-29 11:39 +0100
http://bitbucket.org/pypy/pypy/changeset/791ffe776736/

Log:	byteswap on floats

diff --git a/pypy/rlib/rarithmetic.py b/pypy/rlib/rarithmetic.py
--- a/pypy/rlib/rarithmetic.py
+++ b/pypy/rlib/rarithmetic.py
@@ -595,10 +595,22 @@
     """ Convert little->big endian and the opposite
     """
     from pypy.rpython.lltypesystem import lltype, rffi
+    from pypy.rlib.longlong2float import longlong2float, float2longlong
 
     T = lltype.typeOf(arg)
     # XXX we cannot do arithmetics on small ints
-    if isinstance(arg, base_int):
+    #if isinstance(arg, base_int):
+    is_float = False
+    is_single_float = False
+    if T == lltype.SingleFloat:
+        T = lltype.Float
+        arg = rffi.cast(T, arg)
+        is_single_float = True
+    if T == lltype.Float:
+        is_float = True
+        T = rffi.LONGLONG
+        arg = float2longlong(arg)
+    else:
         arg = widen(arg)
     if rffi.sizeof(T) == 1:
         res = arg
@@ -622,4 +634,9 @@
                (f >> 24) | (g >> 40) | (h >> 56))
     else:
         assert False # unreachable code
+    if is_float:
+        res = rffi.cast(rffi.LONGLONG, res)
+        if is_single_float:
+            return rffi.cast(lltype.SingleFloat, longlong2float(res))
+        return longlong2float(res)
     return rffi.cast(T, res)
diff --git a/pypy/rlib/test/test_rarithmetic.py b/pypy/rlib/test/test_rarithmetic.py
--- a/pypy/rlib/test/test_rarithmetic.py
+++ b/pypy/rlib/test/test_rarithmetic.py
@@ -1,4 +1,5 @@
 from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
+from pypy.rpython.test.test_llinterp import interpret
 from pypy.rlib.rarithmetic import *
 import sys
 import py
@@ -384,8 +385,14 @@
     assert not int_between(1, 1, 1)
 
 def test_byteswap():
-    from pypy.rpython.lltypesystem import rffi
+    from pypy.rpython.lltypesystem import rffi, lltype
     
-    assert byteswap(rffi.cast(rffi.USHORT, 0x0102)) == 0x0201
-    assert byteswap(rffi.cast(rffi.INT, 0x01020304)) == 0x04030201
+    assert rffi.cast(lltype.Signed, byteswap(rffi.cast(rffi.USHORT, 0x0102))) == 0x0201
+    assert rffi.cast(lltype.Signed, byteswap(rffi.cast(rffi.INT, 0x01020304))) == 0x04030201
     assert byteswap(rffi.cast(rffi.ULONGLONG, 0x0102030405060708L)) == 0x0807060504030201L
+    assert byteswap(rffi.cast(rffi.LONGLONG, 0x0102030405060708L)) == 0x0807060504030201L
+    assert ((byteswap(2.3) - 1.903598566252326e+185) / 1e185) < 0.000001
+    assert (rffi.cast(lltype.Float, byteswap(rffi.cast(lltype.SingleFloat, 2.3))) - 4.173496037651603e-08) < 1e-16
+
+def test_byteswap_interpret():
+    interpret(test_byteswap, [])


More information about the pypy-commit mailing list