[pypy-svn] r48063 - in pypy/dist/pypy/lang/smalltalk: . test tool

cfbolz at codespeak.net cfbolz at codespeak.net
Fri Oct 26 15:36:24 CEST 2007


Author: cfbolz
Date: Fri Oct 26 15:36:24 2007
New Revision: 48063

Added:
   pypy/dist/pypy/lang/smalltalk/tool/bitmanipulation.py   (contents, props changed)
   pypy/dist/pypy/lang/smalltalk/tool/test_bitmanipulation.py   (contents, props changed)
Modified:
   pypy/dist/pypy/lang/smalltalk/squeakimage.py
   pypy/dist/pypy/lang/smalltalk/test/test_squeakimage.py
Log:
try to be a bit cleverer with the bit splitting. doesn't completely work yet,
due to an inexactness in the annotator.


Modified: pypy/dist/pypy/lang/smalltalk/squeakimage.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/squeakimage.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/squeakimage.py	Fri Oct 26 15:36:24 2007
@@ -3,6 +3,7 @@
 from pypy.lang.smalltalk import model 
 from pypy.lang.smalltalk import objtable 
 from pypy.rlib import objectmodel
+from pypy.lang.smalltalk.tool.bitmanipulation import splitter
 
 def chrs2int(b):
     assert len(b) == 4
@@ -18,18 +19,6 @@
         first = first - 0x100
     return first << 24 | ord(b[2]) << 16 | ord(b[1]) << 8 | ord(b[0])            
 
-def splitbits(integer, lengths):
-    #XXX we can later let the tool chain mask and unroll this
-    result = []
-    sum = 0
-    for length in lengths:
-        sum += length
-        n = integer & ((1<<length) - 1)
-        assert n >= 0
-        result.append(n)
-        integer = integer >> length
-    assert sum <= 32
-    return result
 
 # ____________________________________________________________
 #
@@ -182,23 +171,23 @@
         
     def read_1wordobjectheader(self):
         kind, size, format, classid, idhash = (
-            splitbits(self.stream.next(), [2,6,4,5,12]))
+            splitter[2,6,4,5,12](self.stream.next()))
         assert kind == 3
         return ImageChunk(size, format, classid, idhash), self.stream.count - 4
 
     def read_2wordobjectheader(self):
         assert self.stream.peek() & 3 == 1 #kind
         classid = self.stream.next() - 01 # remove headertype to get pointer
-        kind, size, format, _, idhash = splitbits(self.stream.next(), [2,6,4,5,12])
+        kind, size, format, _, idhash = splitter[2,6,4,5,12](self.stream.next())
         assert kind == 1
         return ImageChunk(size, format, classid, idhash), self.stream.count - 4
 
     def read_3wordobjectheader(self):
-        kind, size = splitbits(self.stream.next(), [2,30]) 
+        kind, size = splitter[2,30](self.stream.next())
         assert kind == 0
-        assert splitbits(self.stream.peek(), [2])[0] == 0 #kind
+        assert splitter[2](self.stream.peek())[0] == 0 #kind
         classid = self.stream.next() - 00 # remove headertype to get pointer
-        kind, _, format, _, idhash = splitbits(self.stream.next(), [2,6,4,5,12])
+        kind, _, format, _, idhash = splitter[2,6,4,5,12](self.stream.next())
         assert kind == 0
         return ImageChunk(size, format, classid, idhash), self.stream.count - 4
 
@@ -379,7 +368,7 @@
         #(index 28)	1 bit:	high-bit of primitive number (#primitive)
         #(index 29)	1 bit:	flag bit, ignored by the VM  (#flag)
         _, primitive, literalsize, islarge, tempsize, numargs, highbit = (
-            splitbits(header, [1,9,8,1,6,4,1]))
+            splitter[1,9,8,1,6,4,1](header))
         primitive = primitive + (highbit << 10) ##XXX todo, check this
         literals = [self.decode_pointer(pointer).w_object
                     for pointer in self.chunk.data[:literalsize+1]]

Modified: pypy/dist/pypy/lang/smalltalk/test/test_squeakimage.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/test/test_squeakimage.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/test/test_squeakimage.py	Fri Oct 26 15:36:24 2007
@@ -70,18 +70,7 @@
     stream.next()        
     assert stream.count == 8
     
-def test_simple_splitbits():
-    assert ([1] * 4) == squeakimage.splitbits(0x01010101, [8,8,8,8])
-    assert ([255] * 4) == squeakimage.splitbits(0xFfFfFfFf, [8,8,8,8])
-
-def test_fancy_splitbits():
-    assert [4,3,2,1] == squeakimage.splitbits(0x01020304, [8,8,8,8])
-    assert [1,3,7,15] == squeakimage.splitbits(0xFfFfFfFf, [1,2,3,4])
-    
-def test_format_splitbits():
-    x = 0xAA
-    assert [x & 3] == squeakimage.splitbits(x, [2])  
-    
+   
 def test_simple_joinbits():
     assert 0x01010101 == joinbits(([1] * 4), [8,8,8,8])
     assert 0xFfFfFfFf == joinbits([255] * 4, [8,8,8,8])

Added: pypy/dist/pypy/lang/smalltalk/tool/bitmanipulation.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lang/smalltalk/tool/bitmanipulation.py	Fri Oct 26 15:36:24 2007
@@ -0,0 +1,28 @@
+from pypy.rlib import unroll
+
+# terrible hack to make the annotator think this is a real dict
+__name__ = '__builtin__'
+
+class BitSplitter(dict):
+    def __getitem__(self, lengths):
+        if isinstance(lengths, int):
+            lengths = (lengths, )
+        if lengths in self:
+            return dict.__getitem__(self, lengths)
+        unrolling_lenghts = unroll.unrolling_iterable(lengths)
+        def splitbits(integer):
+            result = ()
+            sum = 0
+            for length in unrolling_lenghts:
+                sum += length
+                n = integer & ((1<<length) - 1)
+                assert n >= 0
+                result += (n, )
+                integer = integer >> length
+            assert sum <= 32
+            return result
+        splitbits.func_name += "_" + "_".join([str(i) for i in lengths])
+        self[lengths] = splitbits
+        return splitbits
+        
+splitter = BitSplitter()

Added: pypy/dist/pypy/lang/smalltalk/tool/test_bitmanipulation.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lang/smalltalk/tool/test_bitmanipulation.py	Fri Oct 26 15:36:24 2007
@@ -0,0 +1,15 @@
+from pypy.lang.smalltalk.tool.bitmanipulation import splitter
+
+
+def test_simple_splitbits():
+    assert ((1, ) * 4) == splitter[8,8,8,8](0x01010101)
+    assert ((255, ) * 4) == splitter[8,8,8,8](0xFfFfFfFf)
+
+def test_fancy_splitbits():
+    assert (4,3,2,1) == splitter[8,8,8,8](0x01020304)
+    assert (1,3,7,15) == splitter[1,2,3,4](0xFfFfFfFf)
+    
+def test_format_splitbits():
+    x = 0xAA
+    assert (x & 3, ) == splitter[2](x)
+ 



More information about the Pypy-commit mailing list