Debian Codegraph @

pypy/module/pypyjit/test_pypy_c/test_struct.py

raw symbols SCIP
1 import sys
2 from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC
3
4
5 if sys.maxsize == 2 ** 63 - 1:
6 extra = """
7 i8 = int_ge(i4, -2147483648)
8 guard_true(i8, descr=...)
9 i9 = int_le(i4, 2147483647)
10 guard_true(i9, descr=...)
11 """
12 else:
13 extra = ""
14
15
16 class TestStruct(BaseTestPyPyC):
17 def test_struct_function(self):
18 def main(n):
19 import struct
20 i = 1
21 while i < n:
22 buf = struct.pack("<i", i) # ID: pack
23 x = struct.unpack("<i", buf)[0] # ID: unpack
24 i += x // i
25 return i
26
27 log = self.run(main, [1000])
28 assert log.result == main(1000)
29
30 loop, = log.loops_by_filename(self.filepath)
31 # This could, of course stand some improvement, to remove all these
32 # arithmatic ops, but we've removed all the core overhead.
33 if sys.byteorder == 'little':
34 # on little endian machines, we take the fast path and store the
35 # value using gc_store_indexed
36 assert loop.match_by_id("pack", """
37 dummy_get_utf8?
38 guard_not_invalidated(descr=...)
39 # struct.pack
40 %s
41 p75 = newstr(4)
42 gc_store_indexed(p75, 0, _, 1, _, 4, descr=...)
43 """ % extra)
44 else:
45 assert loop.match_by_id("pack", """
46 dummy_get_utf8?
47 guard_not_invalidated(descr=...)
48 # struct.pack
49 %s
50 i11 = int_and(i4, 255)
51 i13 = int_rshift(i4, 8)
52 i14 = int_and(i13, 255)
53 i16 = int_rshift(i13, 8)
54 i17 = int_and(i16, 255)
55 i19 = int_rshift(i16, 8)
56 i20 = int_and(i19, 255)
57 """ % extra)
58
59 if sys.byteorder == 'little':
60 assert loop.match_by_id("unpack", """
61 dummy_get_utf8?
62 dummy_get_utf8?
63 # struct.unpack
64 i91 = gc_load_indexed_i(p88, 0, 1, _, -4)
65 """)
66 else:
67 # on a big endian machine we cannot just write into
68 # a char buffer and then use load gc to read the integer,
69 # here manual shifting is applied
70 assert loop.match_by_id("unpack", """
71 dummy_get_utf8?
72 dummy_get_utf8?
73 # struct.unpack
74 i95 = int_lshift(i90, 8)
75 i96 = int_or(i88, i95)
76 i97 = int_lshift(i92, 16)
77 i98 = int_or(i96, i97)
78 i99 = int_ge(i94, 128)
79 guard_false(i99, descr=...)
80 i100 = int_lshift(i94, 24)
81 i101 = int_or(i98, i100)
82 """)
83
84 def test_struct_object(self):
85 def main(n):
86 import struct
87 s = struct.Struct("ii")
88 i = 1
89 while i < n:
90 buf = s.pack(-1, i) # ID: pack
91 x = s.unpack(buf)[1] # ID: unpack
92 i += x // i
93 return i
94
95 log = self.run(main, [1000])
96 assert log.result == main(1000)
97
98 if sys.byteorder == 'little':
99 loop, = log.loops_by_filename(self.filepath)
100 assert loop.match_by_id('pack', """
101 dummy_get_utf8?
102 guard_not_invalidated(descr=...)
103 # struct.pack
104 p85 = newstr(8)
105 gc_store_indexed(p85, 0, -1, 1, _, 4, descr=...)
106 %s
107 gc_store_indexed(p85, 4, _, 1, _, 4, descr=...)
108 """ % extra)
109
110 assert loop.match_by_id('unpack', """
111 dummy_get_utf8?
112 # struct.unpack
113 i90 = gc_load_indexed_i(p88, 0, 1, _, -4)
114 i91 = gc_load_indexed_i(p88, 4, 1, _, -4)
115 """)
116
117 def test_unpack_raw_buffer(self):
118 def main(n):
119 import array
120 import struct
121 buf = struct.pack('H', 0x1234)
122 buf = array.array('b', buf)
123 i = 1
124 res = 0
125 while i < n:
126 val = struct.unpack("h", buf)[0] # ID: unpack
127 res += val
128 i += 1
129 return res
130 log = self.run(main, [1000])
131 assert log.result == main(1000)
132 loop, = log.loops_by_filename(self.filepath)
133 assert loop.match_by_id('unpack', """
134 guard_not_invalidated(descr=...)
135 i65 = raw_load_i(i49, 0, descr=<ArrayS 2>)
136 """)
137
138 def test_unpack_bytearray(self):
139 def main(n):
140 import struct
141 buf = struct.pack('H', 0x1234)
142 buf = bytearray(buf)
143 i = 1
144 res = 0
145 while i < n:
146 val = struct.unpack("h", buf)[0] # ID: unpack
147 res += val
148 i += 1
149 return res
150 log = self.run(main, [1000])
151 assert log.result == main(1000)
152 loop, = log.loops_by_filename(self.filepath)
153 # the offset of gc_load_indexed_i used to be the constant 0. However,
154 # now it is 'i46' because we need to add 0 to
155 # W_BytearrayObject._offset
156 assert loop.match_by_id('unpack', """
157 guard_not_invalidated(descr=...)
158 i70 = gc_load_indexed_i(p48, i46, 1, _, -2)
159 """)
160
161 def test_pack_into_raw_buffer(self):
162 def main(n):
163 import array
164 import struct
165 buf = array.array('b', b'\x00'*8)
166 i = 1
167 while i < n:
168 struct.pack_into("h", buf, 4, i) # ID: pack_into
169 i += 1
170 return i
171 log = self.run(main, [1000])
172 assert log.result == main(1000)
173 loop, = log.loops_by_filename(self.filepath)
174 assert loop.match_by_id('pack_into', """\
175 guard_not_invalidated(descr=...)
176 i65 = int_le(i58, 32767)
177 guard_true(i65, descr=...)
178 raw_store(i55, 4, i58, descr=<ArrayS 2>)
179 """)
180
181 def test_pack_into_bytearray(self):
182 def main(n):
183 import struct
184 buf = bytearray(8)
185 i = 1
186 while i < n:
187 struct.pack_into("h", buf, 4, i) # ID: pack_into
188 i += 1
189 return i
190 log = self.run(main, [1000])
191 assert log.result == main(1000)
192 loop, = log.loops_by_filename(self.filepath)
193 assert loop.match_by_id('pack_into', """\
194 dummy_get_utf8?
195 guard_not_invalidated(descr=...)
196 dummy_get_utf8?
197 p68 = getfield_gc_r(p14, descr=<FieldP pypy.objspace.std.bytearrayobject.W_BytearrayObject.inst__data \d+>)
198 i69 = getfield_gc_i(p68, descr=<FieldS list.length \d+>)
199 i70 = getfield_gc_i(p14, descr=<FieldS pypy.objspace.std.bytearrayobject.W_BytearrayObject.inst__offset \d+>)
200 i71 = int_sub(i69, i70)
201 i72 = int_sub(i71, 1)
202 i73 = int_sub(i71, 5)
203 i75 = int_lt(i73, 2)
204 guard_false(i75, descr=...)
205 i77 = int_le(i62, 32767)
206 guard_true(i77, descr=...)
207 p78 = getfield_gc_r(p68, descr=<FieldP list.items \d+>)
208 i81 = int_add(4, i70)
209 gc_store_indexed(p78, i81, i62, 1, _, 2, descr=<ArrayS 2>)
210 """)