# [pypy-svn] r37891 - in pypy/branch/jit-virtual-world/pypy/jit/codegen/i386: . test

arigo at codespeak.net arigo at codespeak.net
Sun Feb 4 10:36:01 CET 2007

```Author: arigo
Date: Sun Feb  4 10:35:58 2007
New Revision: 37891

Modified:
pypy/branch/jit-virtual-world/pypy/jit/codegen/i386/ri386.py
pypy/branch/jit-virtual-world/pypy/jit/codegen/i386/test/test_ri386.py
Log:
Cannot use << in RPython if the count can be 32 - it is
misinterpreted as a shift by 0 in C code.

Modified: pypy/branch/jit-virtual-world/pypy/jit/codegen/i386/ri386.py
==============================================================================
--- pypy/branch/jit-virtual-world/pypy/jit/codegen/i386/ri386.py	(original)
+++ pypy/branch/jit-virtual-world/pypy/jit/codegen/i386/ri386.py	Sun Feb  4 10:35:58 2007
@@ -1,3 +1,4 @@

class OPERAND(object):
@@ -276,15 +277,19 @@

def unpack(s):
assert len(s) in (1, 2, 4)
-    result = 0
-    shift = 0
-    char = '\x00'      # flow space workaround
-    for char in s:
-        result |= ord(char) << shift
-        shift += 8
-    if ord(char) >= 0x80:
-        result -= 1 << shift
-    return result
+    if len(s) == 1:
+        a = ord(s[0])
+        if a > 0x7f:
+            a -= 0x100
+    else:
+        a = ord(s[0]) | (ord(s[1]) << 8)
+        if len(s) == 2:
+            if a > 0x7fff:
+                a -= 0x10000
+        else:
+            a |= (ord(s[2]) << 16) | (ord(s[3]) << 24)
+    return a

missing = MISSING()

Modified: pypy/branch/jit-virtual-world/pypy/jit/codegen/i386/test/test_ri386.py
==============================================================================
--- pypy/branch/jit-virtual-world/pypy/jit/codegen/i386/test/test_ri386.py	(original)
+++ pypy/branch/jit-virtual-world/pypy/jit/codegen/i386/test/test_ri386.py	Sun Feb  4 10:35:58 2007
@@ -99,3 +99,15 @@

res = interpret(f, [])
assert ''.join(res.chars) == '\x29\xCE\x89\x4D\x13'
+
+
+def test_unpack_compiled():
+    from pypy.translator.c.test.test_genc import compile
+
+    def f(n):
+        return mem(ebp, n).ofs_relative_to_ebp()
+
+    fn = compile(f, [int])
+    for i in [0, 4, 44, 124, 128, 132, 252, 256, 10000000,
+              -4, -44, -124, -128, -132, -252, -256, -10000000]:
+        assert fn(i) == i

```