|
2 | 2 | from rsqueakvm.plugins.plugin import Plugin
|
3 | 3 | from rsqueakvm.primitives import prim_table
|
4 | 4 | from rsqueakvm.primitives.constants import *
|
5 |
| -from rsqueakvm.model.numeric import W_LargeInteger |
| 5 | +from rsqueakvm.model.numeric import W_LargeInteger, W_LargeIntegerBig, calculate_exposed_size_for_big_int |
6 | 6 | from rsqueakvm.model.variable import W_BytesObject
|
7 | 7 |
|
8 | 8 | from rpython.rlib.rbigint import rbigint, NULLRBIGINT, _divrem
|
| 9 | +from rpython.rlib import jit |
9 | 10 |
|
10 | 11 | LargeIntegers = Plugin()
|
11 | 12 |
|
@@ -73,16 +74,31 @@ def primDigitCompare(interp, s_frame, rcvr, arg):
|
73 | 74 | res = 0
|
74 | 75 | return interp.space.wrap_int(res)
|
75 | 76 |
|
| 77 | +@jit.elidable |
| 78 | +def minimum_bytelen_for(val): |
| 79 | + bytelen = calculate_exposed_size_for_big_int(val) - 1 |
| 80 | + while True: |
| 81 | + try: |
| 82 | + # see if we have enough room |
| 83 | + val.tobytes(bytelen, 'little', False) |
| 84 | + return bytelen |
| 85 | + except OverflowError: |
| 86 | + bytelen += 1 |
| 87 | + |
76 | 88 | @LargeIntegers.expose_primitive(unwrap_spec=[object])
|
77 | 89 | def primNormalizePositive(interp, s_frame, w_rcvr):
|
78 | 90 | if isinstance(w_rcvr, W_BytesObject):
|
79 | 91 | # only bytes object may be denormalized
|
80 | 92 | return interp.space.wrap_rbigint(w_rcvr.unwrap_rbigint(interp.space))
|
| 93 | + elif isinstance(w_rcvr, W_LargeIntegerBig): |
| 94 | + w_rcvr._exposed_size = minimum_bytelen_for(w_rcvr.value) |
81 | 95 | return w_rcvr
|
82 | 96 |
|
83 | 97 | @LargeIntegers.expose_primitive(unwrap_spec=[object])
|
84 | 98 | def primNormalizeNegative(interp, s_frame, w_rcvr):
|
85 | 99 | if isinstance(w_rcvr, W_BytesObject):
|
86 | 100 | # only bytes object may be denormalized
|
87 | 101 | return interp.space.wrap_rbigint(w_rcvr.unwrap_rbigint(interp.space))
|
| 102 | + elif isinstance(w_rcvr, W_LargeIntegerBig): |
| 103 | + w_rcvr._exposed_size = minimum_bytelen_for(w_rcvr.value.abs()) |
88 | 104 | return w_rcvr
|
0 commit comments