| 1 |
import sys |
| 2 |
|
| 3 |
from rpython.annotator import model as annmodel |
| 4 |
from rpython.flowspace.operation import op_appendices |
| 5 |
from rpython.rlib import objectmodel, jit |
| 6 |
from rpython.rlib.rarithmetic import intmask, longlongmask, r_int, r_longlong |
| 7 |
from rpython.rlib.rarithmetic import r_uint, r_ulonglong, r_longlonglong, r_ulonglonglong |
| 8 |
from rpython.rtyper.error import TyperError |
| 9 |
from rpython.rtyper.lltypesystem.lltype import (Signed, Unsigned, Bool, Float, |
| 10 |
Char, UniChar, UnsignedLongLong, SignedLongLong, build_number, Number, |
| 11 |
cast_primitive, typeOf, SignedLongLongLong, UnsignedLongLongLong) |
| 12 |
from rpython.rtyper.rfloat import FloatRepr |
| 13 |
from rpython.rtyper.rmodel import inputconst, log |
| 14 |
from rpython.tool.pairtype import pairtype |
| 15 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 16 |
|
| 17 |
|
| 18 |
class IntegerRepr(FloatRepr): |
| 19 |
def __init__(self, lowleveltype, opprefix): |
| 20 |
self.lowleveltype = lowleveltype |
| 21 |
self._opprefix = opprefix |
| 22 |
self.as_int = self |
| 23 |
|
| 24 |
@property |
| 25 |
def opprefix(self): |
| 26 |
if self._opprefix is None: |
| 27 |
raise TyperError("arithmetic not supported on %r, its size is too small" % |
| 28 |
self.lowleveltype) |
| 29 |
return self._opprefix |
| 30 |
|
| 31 |
def convert_const(self, value): |
| 32 |
if isinstance(value, objectmodel.Symbolic): |
| 33 |
return value |
| 34 |
T = typeOf(value) |
| 35 |
if isinstance(T, Number) or T is Bool: |
| 36 |
return cast_primitive(self.lowleveltype, value) |
| 37 |
raise TyperError("not an integer: %r" % (value,)) |
| 38 |
|
| 39 |
def get_ll_eq_function(self): |
| 40 |
if getattr(self, '_opprefix', '?') is None: |
| 41 |
return ll_eq_shortint |
| 42 |
return None |
| 43 |
|
| 44 |
def get_ll_ge_function(self): |
| 45 |
return None |
| 46 |
get_ll_gt_function = get_ll_ge_function |
| 47 |
get_ll_lt_function = get_ll_ge_function |
| 48 |
get_ll_le_function = get_ll_ge_function |
| 49 |
|
| 50 |
def get_ll_hash_function(self): |
| 51 |
if (sys.maxint == 2147483647 and |
| 52 |
self.lowleveltype in (SignedLongLong, UnsignedLongLong)): |
| 53 |
return ll_hash_long_long |
| 54 |
return ll_hash_int |
| 55 |
|
| 56 |
get_ll_fasthash_function = get_ll_hash_function |
| 57 |
|
| 58 |
def get_ll_dummyval_obj(self, rtyper, s_value): |
| 59 |
|
| 60 |
if s_value.nonneg and self.lowleveltype is Signed: |
| 61 |
return signed_repr |
| 62 |
else: |
| 63 |
return None |
| 64 |
|
| 65 |
ll_dummy_value = -1 |
| 66 |
|
| 67 |
def rtype_chr(_, hop): |
| 68 |
vlist = hop.inputargs(Signed) |
| 69 |
if hop.has_implicit_exception(ValueError): |
| 70 |
hop.exception_is_here() |
| 71 |
hop.gendirectcall(ll_check_chr, vlist[0]) |
| 72 |
else: |
| 73 |
hop.exception_cannot_occur() |
| 74 |
return hop.genop('cast_int_to_char', vlist, resulttype=Char) |
| 75 |
|
| 76 |
def rtype_unichr(_, hop): |
| 77 |
vlist = hop.inputargs(Signed) |
| 78 |
if hop.has_implicit_exception(ValueError): |
| 79 |
hop.exception_is_here() |
| 80 |
hop.gendirectcall(ll_check_unichr, vlist[0]) |
| 81 |
else: |
| 82 |
hop.exception_cannot_occur() |
| 83 |
return hop.genop('cast_int_to_unichar', vlist, resulttype=UniChar) |
| 84 |
|
| 85 |
def rtype_bool(self, hop): |
| 86 |
assert self is self.as_int |
| 87 |
vlist = hop.inputargs(self) |
| 88 |
return hop.genop(self.opprefix + 'is_true', vlist, resulttype=Bool) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
def rtype_abs(self, hop): |
| 93 |
self = self.as_int |
| 94 |
vlist = hop.inputargs(self) |
| 95 |
if hop.s_result.unsigned: |
| 96 |
return vlist[0] |
| 97 |
else: |
| 98 |
return hop.genop(self.opprefix + 'abs', vlist, resulttype=self) |
| 99 |
|
| 100 |
def rtype_abs_ovf(self, hop): |
| 101 |
self = self.as_int |
| 102 |
if hop.s_result.unsigned: |
| 103 |
raise TyperError("forbidden uint_abs_ovf") |
| 104 |
else: |
| 105 |
return _rtype_call_helper(hop, 'abs_ovf') |
| 106 |
|
| 107 |
def rtype_invert(self, hop): |
| 108 |
self = self.as_int |
| 109 |
vlist = hop.inputargs(self) |
| 110 |
return hop.genop(self.opprefix + 'invert', vlist, resulttype=self) |
| 111 |
|
| 112 |
def rtype_neg(self, hop): |
| 113 |
self = self.as_int |
| 114 |
vlist = hop.inputargs(self) |
| 115 |
if hop.s_result.unsigned: |
| 116 |
|
| 117 |
zero = self.lowleveltype._defl() |
| 118 |
vlist.insert(0, hop.inputconst(self.lowleveltype, zero)) |
| 119 |
return hop.genop(self.opprefix + 'sub', vlist, resulttype=self) |
| 120 |
else: |
| 121 |
return hop.genop(self.opprefix + 'neg', vlist, resulttype=self) |
| 122 |
|
| 123 |
def rtype_neg_ovf(self, hop): |
| 124 |
self = self.as_int |
| 125 |
if hop.s_result.unsigned: |
| 126 |
|
| 127 |
hop.exception_cannot_occur() |
| 128 |
return self.rtype_neg(hop) |
| 129 |
else: |
| 130 |
return _rtype_call_helper(hop, 'neg_ovf') |
| 131 |
|
| 132 |
def rtype_pos(self, hop): |
| 133 |
self = self.as_int |
| 134 |
vlist = hop.inputargs(self) |
| 135 |
return vlist[0] |
| 136 |
|
| 137 |
def rtype_int(self, hop): |
| 138 |
if self.lowleveltype in (Unsigned, UnsignedLongLong): |
| 139 |
raise TyperError("use intmask() instead of int(r_uint(...))") |
| 140 |
vlist = hop.inputargs(Signed) |
| 141 |
hop.exception_cannot_occur() |
| 142 |
return vlist[0] |
| 143 |
|
| 144 |
def rtype_float(_, hop): |
| 145 |
vlist = hop.inputargs(Float) |
| 146 |
hop.exception_cannot_occur() |
| 147 |
return vlist[0] |
| 148 |
|
| 149 |
@jit.elidable |
| 150 |
def ll_str(self, i): |
| 151 |
from rpython.rtyper.lltypesystem.ll_str import ll_int2dec |
| 152 |
return ll_int2dec(i) |
| 153 |
|
| 154 |
def rtype_hex(self, hop): |
| 155 |
from rpython.rtyper.lltypesystem.ll_str import ll_int2hex |
| 156 |
self = self.as_int |
| 157 |
varg = hop.inputarg(self, 0) |
| 158 |
true = inputconst(Bool, True) |
| 159 |
return hop.gendirectcall(ll_int2hex, varg, true) |
| 160 |
|
| 161 |
def rtype_oct(self, hop): |
| 162 |
from rpython.rtyper.lltypesystem.ll_str import ll_int2oct |
| 163 |
self = self.as_int |
| 164 |
varg = hop.inputarg(self, 0) |
| 165 |
true = inputconst(Bool, True) |
| 166 |
return hop.gendirectcall(ll_int2oct, varg, true) |
| 167 |
|
| 168 |
def rtype_bin(self, hop): |
| 169 |
from rpython.rtyper.lltypesystem.ll_str import ll_int2bin |
| 170 |
self = self.as_int |
| 171 |
varg = hop.inputarg(self, 0) |
| 172 |
true = inputconst(Bool, True) |
| 173 |
return hop.gendirectcall(ll_int2bin, varg, true) |
| 174 |
|
| 175 |
|
| 176 |
_integer_reprs = {} |
| 177 |
def getintegerrepr(lltype, prefix=None): |
| 178 |
try: |
| 179 |
return _integer_reprs[lltype] |
| 180 |
except KeyError: |
| 181 |
pass |
| 182 |
repr = _integer_reprs[lltype] = IntegerRepr(lltype, prefix) |
| 183 |
return repr |
| 184 |
|
| 185 |
class __extend__(annmodel.SomeInteger): |
| 186 |
def rtyper_makerepr(self, rtyper): |
| 187 |
lltype = build_number(None, self.knowntype) |
| 188 |
return getintegerrepr(lltype) |
| 189 |
|
| 190 |
def rtyper_makekey(self): |
| 191 |
return self.__class__, self.knowntype |
| 192 |
|
| 193 |
signed_repr = getintegerrepr(Signed, 'int_') |
| 194 |
signedlonglong_repr = getintegerrepr(SignedLongLong, 'llong_') |
| 195 |
signedlonglonglong_repr = getintegerrepr(SignedLongLongLong, 'lllong_') |
| 196 |
unsigned_repr = getintegerrepr(Unsigned, 'uint_') |
| 197 |
unsignedlonglong_repr = getintegerrepr(UnsignedLongLong, 'ullong_') |
| 198 |
unsignedlonglonglong_repr = getintegerrepr(UnsignedLongLongLong, 'ulllong_') |
| 199 |
|
| 200 |
class __extend__(pairtype(IntegerRepr, IntegerRepr)): |
| 201 |
|
| 202 |
def convert_from_to((r_from, r_to), v, llops): |
| 203 |
if r_from.lowleveltype == Signed and r_to.lowleveltype == Unsigned: |
| 204 |
log.debug('explicit cast_int_to_uint') |
| 205 |
return llops.genop('cast_int_to_uint', [v], resulttype=Unsigned) |
| 206 |
if r_from.lowleveltype == Unsigned and r_to.lowleveltype == Signed: |
| 207 |
log.debug('explicit cast_uint_to_int') |
| 208 |
return llops.genop('cast_uint_to_int', [v], resulttype=Signed) |
| 209 |
if r_from.lowleveltype == Signed and r_to.lowleveltype == SignedLongLong: |
| 210 |
return llops.genop('cast_int_to_longlong', [v], resulttype=SignedLongLong) |
| 211 |
if r_from.lowleveltype == SignedLongLong and r_to.lowleveltype == Signed: |
| 212 |
return llops.genop('truncate_longlong_to_int', [v], resulttype=Signed) |
| 213 |
return llops.genop('cast_primitive', [v], resulttype=r_to.lowleveltype) |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
def rtype_add(_, hop): |
| 218 |
return _rtype_template(hop, 'add') |
| 219 |
rtype_inplace_add = rtype_add |
| 220 |
|
| 221 |
def rtype_add_ovf(_, hop): |
| 222 |
func = 'add_ovf' |
| 223 |
if hop.r_result.opprefix == 'int_': |
| 224 |
if hop.args_s[1].nonneg: |
| 225 |
func = 'add_nonneg_ovf' |
| 226 |
elif hop.args_s[0].nonneg: |
| 227 |
hop = hop.copy() |
| 228 |
hop.swap_fst_snd_args() |
| 229 |
func = 'add_nonneg_ovf' |
| 230 |
return _rtype_template(hop, func) |
| 231 |
|
| 232 |
def rtype_sub(_, hop): |
| 233 |
return _rtype_template(hop, 'sub') |
| 234 |
rtype_inplace_sub = rtype_sub |
| 235 |
|
| 236 |
def rtype_sub_ovf(_, hop): |
| 237 |
return _rtype_template(hop, 'sub_ovf') |
| 238 |
|
| 239 |
def rtype_mul(_, hop): |
| 240 |
return _rtype_template(hop, 'mul') |
| 241 |
rtype_inplace_mul = rtype_mul |
| 242 |
|
| 243 |
def rtype_mul_ovf(_, hop): |
| 244 |
return _rtype_template(hop, 'mul_ovf') |
| 245 |
|
| 246 |
def rtype_floordiv(_, hop): |
| 247 |
return _rtype_call_helper(hop, 'py_div', [ZeroDivisionError]) |
| 248 |
rtype_inplace_floordiv = rtype_floordiv |
| 249 |
|
| 250 |
def rtype_floordiv_ovf(_, hop): |
| 251 |
return _rtype_call_helper(hop, 'py_div_ovf', [ZeroDivisionError]) |
| 252 |
|
| 253 |
|
| 254 |
rtype_div = rtype_floordiv |
| 255 |
rtype_inplace_div = rtype_inplace_floordiv |
| 256 |
rtype_div_ovf = rtype_floordiv_ovf |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
def rtype_mod(_, hop): |
| 261 |
return _rtype_call_helper(hop, 'py_mod', [ZeroDivisionError]) |
| 262 |
rtype_inplace_mod = rtype_mod |
| 263 |
|
| 264 |
def rtype_mod_ovf(_, hop): |
| 265 |
return _rtype_call_helper(hop, 'py_mod_ovf', [ZeroDivisionError]) |
| 266 |
|
| 267 |
def rtype_xor(_, hop): |
| 268 |
return _rtype_template(hop, 'xor') |
| 269 |
rtype_inplace_xor = rtype_xor |
| 270 |
|
| 271 |
def rtype_and_(_, hop): |
| 272 |
return _rtype_template(hop, 'and') |
| 273 |
rtype_inplace_and = rtype_and_ |
| 274 |
|
| 275 |
def rtype_or_(_, hop): |
| 276 |
return _rtype_template(hop, 'or') |
| 277 |
rtype_inplace_or = rtype_or_ |
| 278 |
|
| 279 |
def rtype_lshift(_, hop): |
| 280 |
return _rtype_template(hop, 'lshift') |
| 281 |
rtype_inplace_lshift = rtype_lshift |
| 282 |
|
| 283 |
def rtype_lshift_ovf(_, hop): |
| 284 |
return _rtype_call_helper(hop, 'lshift_ovf') |
| 285 |
|
| 286 |
def rtype_rshift(_, hop): |
| 287 |
return _rtype_template(hop, 'rshift') |
| 288 |
rtype_inplace_rshift = rtype_rshift |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
def rtype_eq(_, hop): |
| 293 |
return _rtype_compare_template(hop, 'eq') |
| 294 |
|
| 295 |
rtype_is_ = rtype_eq |
| 296 |
|
| 297 |
def rtype_ne(_, hop): |
| 298 |
return _rtype_compare_template(hop, 'ne') |
| 299 |
|
| 300 |
def rtype_lt(_, hop): |
| 301 |
return _rtype_compare_template(hop, 'lt') |
| 302 |
|
| 303 |
def rtype_le(_, hop): |
| 304 |
return _rtype_compare_template(hop, 'le') |
| 305 |
|
| 306 |
def rtype_gt(_, hop): |
| 307 |
return _rtype_compare_template(hop, 'gt') |
| 308 |
|
| 309 |
def rtype_ge(_, hop): |
| 310 |
return _rtype_compare_template(hop, 'ge') |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
def _rtype_template(hop, func): |
| 315 |
"""Write a simple operation implementing the given 'func'. |
| 316 |
It must be an operation that cannot raise. |
| 317 |
""" |
| 318 |
r_result = hop.r_result |
| 319 |
if r_result.lowleveltype == Bool: |
| 320 |
repr = signed_repr |
| 321 |
else: |
| 322 |
repr = r_result |
| 323 |
if func.startswith(('lshift', 'rshift')): |
| 324 |
repr2 = signed_repr |
| 325 |
else: |
| 326 |
repr2 = repr |
| 327 |
vlist = hop.inputargs(repr, repr2) |
| 328 |
prefix = repr.opprefix |
| 329 |
|
| 330 |
if '_ovf' in func or func.startswith(('py_mod', 'py_div')): |
| 331 |
if prefix+func not in ('int_add_ovf', 'int_add_nonneg_ovf', |
| 332 |
'int_sub_ovf', 'int_mul_ovf'): |
| 333 |
raise TyperError("%r should not be used here any more" % (func,)) |
| 334 |
hop.has_implicit_exception(OverflowError) |
| 335 |
hop.exception_is_here() |
| 336 |
else: |
| 337 |
hop.exception_cannot_occur() |
| 338 |
|
| 339 |
v_res = hop.genop(prefix+func, vlist, resulttype=repr) |
| 340 |
v_res = hop.llops.convertvar(v_res, repr, r_result) |
| 341 |
return v_res |
| 342 |
|
| 343 |
|
| 344 |
def _rtype_call_helper(hop, func, implicit_excs=[]): |
| 345 |
"""Write a call to a helper implementing the given 'func'. |
| 346 |
It can raise OverflowError if 'func' ends with '_ovf'. |
| 347 |
Other possible exceptions can be specified in 'implicit_excs'. |
| 348 |
""" |
| 349 |
any_implicit_exception = False |
| 350 |
if func.endswith('_ovf'): |
| 351 |
if hop.s_result.unsigned: |
| 352 |
raise TyperError("forbidden unsigned " + func) |
| 353 |
else: |
| 354 |
hop.has_implicit_exception(OverflowError) |
| 355 |
any_implicit_exception = True |
| 356 |
|
| 357 |
for implicit_exc in implicit_excs: |
| 358 |
if hop.has_implicit_exception(implicit_exc): |
| 359 |
appendix = op_appendices[implicit_exc] |
| 360 |
func += '_' + appendix |
| 361 |
any_implicit_exception = True |
| 362 |
|
| 363 |
if not any_implicit_exception: |
| 364 |
if not func.startswith(('py_mod', 'py_div')): |
| 365 |
return _rtype_template(hop, func) |
| 366 |
|
| 367 |
repr = hop.r_result |
| 368 |
assert repr.lowleveltype != Bool |
| 369 |
if func in ('abs_ovf', 'neg_ovf'): |
| 370 |
vlist = hop.inputargs(repr) |
| 371 |
else: |
| 372 |
if func.startswith(('lshift', 'rshift')): |
| 373 |
vlist = hop.inputargs(repr, signed_repr) |
| 374 |
else: |
| 375 |
vlist = hop.inputargs(repr, repr) |
| 376 |
if any_implicit_exception: |
| 377 |
hop.exception_is_here() |
| 378 |
else: |
| 379 |
hop.exception_cannot_occur() |
| 380 |
|
| 381 |
funcname = 'll_' + repr.opprefix + func |
| 382 |
llfunc = globals()[funcname] |
| 383 |
if all(s_arg.nonneg for s_arg in hop.args_s): |
| 384 |
llfunc = globals().get(funcname + '_nonnegargs', llfunc) |
| 385 |
v_result = hop.gendirectcall(llfunc, *vlist) |
| 386 |
assert v_result.concretetype == repr.lowleveltype |
| 387 |
return v_result |
| 388 |
|
| 389 |
|
| 390 |
INT_BITS_1 = r_int.BITS - 1 |
| 391 |
LLONG_BITS_1 = r_longlong.BITS - 1 |
| 392 |
LLLONG_BITS_1 = r_longlonglong.BITS - 1 |
| 393 |
INT_MIN = int(-(1 << INT_BITS_1)) |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
@jit.oopspec("int.py_div(x, y)") |
| 399 |
def ll_int_py_div(x, y): |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
r = llop.int_floordiv(Signed, x, y) |
| 405 |
p = r * y |
| 406 |
if y < 0: u = p - x |
| 407 |
else: u = x - p |
| 408 |
return r + (u >> INT_BITS_1) |
| 409 |
|
| 410 |
@jit.oopspec("int.py_div(x, y)") |
| 411 |
def ll_int_py_div_nonnegargs(x, y): |
| 412 |
from rpython.rlib.debug import ll_assert |
| 413 |
r = llop.int_floordiv(Signed, x, y) |
| 414 |
ll_assert(r >= 0, "int_py_div_nonnegargs(): one arg is negative") |
| 415 |
return r |
| 416 |
|
| 417 |
def ll_int_py_div_zer(x, y): |
| 418 |
if y == 0: |
| 419 |
raise ZeroDivisionError("integer division") |
| 420 |
return ll_int_py_div(x, y) |
| 421 |
|
| 422 |
def ll_int_py_div_ovf(x, y): |
| 423 |
|
| 424 |
|
| 425 |
if (x == -sys.maxint - 1) & (y == -1): |
| 426 |
raise OverflowError("integer division") |
| 427 |
return ll_int_py_div(x, y) |
| 428 |
|
| 429 |
def ll_int_py_div_ovf_zer(x, y): |
| 430 |
if y == 0: |
| 431 |
raise ZeroDivisionError("integer division") |
| 432 |
return ll_int_py_div_ovf(x, y) |
| 433 |
|
| 434 |
@jit.oopspec("int.udiv(x, y)") |
| 435 |
def ll_uint_py_div(x, y): |
| 436 |
return llop.uint_floordiv(Unsigned, x, y) |
| 437 |
|
| 438 |
def ll_uint_py_div_zer(x, y): |
| 439 |
if y == 0: |
| 440 |
raise ZeroDivisionError("unsigned integer division") |
| 441 |
return ll_uint_py_div(x, y) |
| 442 |
|
| 443 |
if SignedLongLong == Signed: |
| 444 |
ll_llong_py_div = ll_int_py_div |
| 445 |
ll_llong_py_div_zer = ll_int_py_div_zer |
| 446 |
ll_ullong_py_div = ll_uint_py_div |
| 447 |
ll_ullong_py_div_zer = ll_uint_py_div_zer |
| 448 |
else: |
| 449 |
@jit.dont_look_inside |
| 450 |
def ll_llong_py_div(x, y): |
| 451 |
r = llop.llong_floordiv(SignedLongLong, x, y) |
| 452 |
p = r * y |
| 453 |
if y < 0: u = p - x |
| 454 |
else: u = x - p |
| 455 |
return r + (u >> LLONG_BITS_1) |
| 456 |
|
| 457 |
def ll_llong_py_div_zer(x, y): |
| 458 |
if y == 0: |
| 459 |
raise ZeroDivisionError("longlong division") |
| 460 |
return ll_llong_py_div(x, y) |
| 461 |
|
| 462 |
@jit.dont_look_inside |
| 463 |
def ll_ullong_py_div(x, y): |
| 464 |
return llop.ullong_floordiv(UnsignedLongLong, x, y) |
| 465 |
|
| 466 |
def ll_ullong_py_div_zer(x, y): |
| 467 |
if y == 0: |
| 468 |
raise ZeroDivisionError("unsigned longlong division") |
| 469 |
return ll_ullong_py_div(x, y) |
| 470 |
|
| 471 |
@jit.dont_look_inside |
| 472 |
def ll_lllong_py_div(x, y): |
| 473 |
r = llop.lllong_floordiv(SignedLongLongLong, x, y) |
| 474 |
p = r * y |
| 475 |
if y < 0: u = p - x |
| 476 |
else: u = x - p |
| 477 |
return r + (u >> LLLONG_BITS_1) |
| 478 |
|
| 479 |
def ll_lllong_py_div_zer(x, y): |
| 480 |
if y == 0: |
| 481 |
raise ZeroDivisionError("longlonglong division") |
| 482 |
return ll_lllong_py_div(x, y) |
| 483 |
|
| 484 |
@jit.dont_look_inside |
| 485 |
def ll_ulllong_py_div(x, y): |
| 486 |
return llop.ulllong_floordiv(UnsignedLongLongLong, x, y) |
| 487 |
|
| 488 |
def ll_ulllong_py_div_zer(x, y): |
| 489 |
if y == 0: |
| 490 |
raise ZeroDivisionError("unsigned longlonglong division") |
| 491 |
return ll_ulllong_py_div(x, y) |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
@jit.oopspec("int.py_mod(x, y)") |
| 496 |
def ll_int_py_mod(x, y): |
| 497 |
r = llop.int_mod(Signed, x, y) |
| 498 |
if y < 0: u = -r |
| 499 |
else: u = r |
| 500 |
return r + (y & (u >> INT_BITS_1)) |
| 501 |
|
| 502 |
@jit.oopspec("int.py_mod(x, y)") |
| 503 |
def ll_int_py_mod_nonnegargs(x, y): |
| 504 |
from rpython.rlib.debug import ll_assert |
| 505 |
r = llop.int_mod(Signed, x, y) |
| 506 |
ll_assert(r >= 0, "int_py_mod_nonnegargs(): one arg is negative") |
| 507 |
return r |
| 508 |
|
| 509 |
def ll_int_py_mod_zer(x, y): |
| 510 |
if y == 0: |
| 511 |
raise ZeroDivisionError |
| 512 |
return ll_int_py_mod(x, y) |
| 513 |
|
| 514 |
def ll_int_py_mod_ovf(x, y): |
| 515 |
|
| 516 |
if (x == -sys.maxint - 1) & (y == -1): |
| 517 |
raise OverflowError |
| 518 |
return ll_int_py_mod(x, y) |
| 519 |
|
| 520 |
def ll_int_py_mod_ovf_zer(x, y): |
| 521 |
if y == 0: |
| 522 |
raise ZeroDivisionError |
| 523 |
return ll_int_py_mod_ovf(x, y) |
| 524 |
|
| 525 |
@jit.oopspec("int.umod(x, y)") |
| 526 |
def ll_uint_py_mod(x, y): |
| 527 |
return llop.uint_mod(Unsigned, x, y) |
| 528 |
|
| 529 |
def ll_uint_py_mod_zer(x, y): |
| 530 |
if y == 0: |
| 531 |
raise ZeroDivisionError |
| 532 |
return ll_uint_py_mod(x, y) |
| 533 |
|
| 534 |
if SignedLongLong == Signed: |
| 535 |
ll_llong_py_mod = ll_int_py_mod |
| 536 |
ll_llong_py_mod_zer = ll_int_py_mod_zer |
| 537 |
ll_ullong_py_mod = ll_uint_py_mod |
| 538 |
ll_ullong_py_mod_zer = ll_uint_py_mod_zer |
| 539 |
else: |
| 540 |
@jit.dont_look_inside |
| 541 |
def ll_llong_py_mod(x, y): |
| 542 |
r = llop.llong_mod(SignedLongLong, x, y) |
| 543 |
if y < 0: u = -r |
| 544 |
else: u = r |
| 545 |
return r + (y & (u >> LLONG_BITS_1)) |
| 546 |
|
| 547 |
def ll_llong_py_mod_zer(x, y): |
| 548 |
if y == 0: |
| 549 |
raise ZeroDivisionError |
| 550 |
return ll_llong_py_mod(x, y) |
| 551 |
|
| 552 |
@jit.dont_look_inside |
| 553 |
def ll_ullong_py_mod(x, y): |
| 554 |
return llop.ullong_mod(UnsignedLongLong, x, y) |
| 555 |
|
| 556 |
def ll_ullong_py_mod_zer(x, y): |
| 557 |
if y == 0: |
| 558 |
raise ZeroDivisionError |
| 559 |
return ll_ullong_py_mod(x, y) |
| 560 |
|
| 561 |
@jit.dont_look_inside |
| 562 |
def ll_lllong_py_mod(x, y): |
| 563 |
r = llop.lllong_mod(SignedLongLongLong, x, y) |
| 564 |
if y < 0: u = -r |
| 565 |
else: u = r |
| 566 |
return r + (y & (u >> LLLONG_BITS_1)) |
| 567 |
|
| 568 |
def ll_lllong_py_mod_zer(x, y): |
| 569 |
if y == 0: |
| 570 |
raise ZeroDivisionError |
| 571 |
return ll_lllong_py_mod(x, y) |
| 572 |
|
| 573 |
@jit.dont_look_inside |
| 574 |
def ll_ulllong_py_mod(x, y): |
| 575 |
return llop.ulllong_mod(UnsignedLongLongLong, x, y) |
| 576 |
|
| 577 |
def ll_ulllong_py_mod_zer(x, y): |
| 578 |
if y == 0: |
| 579 |
raise ZeroDivisionError |
| 580 |
return ll_ulllong_py_mod(x, y) |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
def ll_int_lshift_ovf(x, y): |
| 586 |
result = x << y |
| 587 |
if (result >> y) != x: |
| 588 |
raise OverflowError("x<<y loosing bits or changing sign") |
| 589 |
return result |
| 590 |
|
| 591 |
@jit.oopspec("int.neg_ovf(x)") |
| 592 |
def ll_int_neg_ovf(x): |
| 593 |
if x == INT_MIN: |
| 594 |
raise OverflowError |
| 595 |
return -x |
| 596 |
|
| 597 |
def ll_int_abs_ovf(x): |
| 598 |
if x == INT_MIN: |
| 599 |
raise OverflowError |
| 600 |
return abs(x) |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
def _rtype_compare_template(hop, func): |
| 606 |
s_int1, s_int2 = hop.args_s |
| 607 |
if s_int1.unsigned or s_int2.unsigned: |
| 608 |
if not s_int1.nonneg or not s_int2.nonneg: |
| 609 |
raise TyperError("comparing a signed and an unsigned number") |
| 610 |
|
| 611 |
repr = hop.rtyper.getrepr(annmodel.unionof(s_int1, s_int2)).as_int |
| 612 |
vlist = hop.inputargs(repr, repr) |
| 613 |
hop.exception_is_here() |
| 614 |
return hop.genop(repr.opprefix + func, vlist, resulttype=Bool) |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
def ll_hash_int(n): |
| 620 |
return intmask(n) |
| 621 |
|
| 622 |
def ll_hash_long_long(n): |
| 623 |
return intmask(intmask(n) + 9 * intmask(n >> 32)) |
| 624 |
|
| 625 |
def ll_eq_shortint(n, m): |
| 626 |
return intmask(n) == intmask(m) |
| 627 |
ll_eq_shortint.no_direct_compare = True |
| 628 |
|
| 629 |
def ll_check_chr(n): |
| 630 |
if 0 <= n <= 255: |
| 631 |
return |
| 632 |
else: |
| 633 |
raise ValueError |
| 634 |
|
| 635 |
def ll_check_unichr(n): |
| 636 |
from rpython.rlib.runicode import MAXUNICODE |
| 637 |
if 0 <= n <= MAXUNICODE: |
| 638 |
return |
| 639 |
else: |
| 640 |
raise ValueError |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
class __extend__(pairtype(IntegerRepr, FloatRepr)): |
| 646 |
def convert_from_to((r_from, r_to), v, llops): |
| 647 |
if r_from.lowleveltype == Unsigned and r_to.lowleveltype == Float: |
| 648 |
log.debug('explicit cast_uint_to_float') |
| 649 |
return llops.genop('cast_uint_to_float', [v], resulttype=Float) |
| 650 |
if r_from.lowleveltype == Signed and r_to.lowleveltype == Float: |
| 651 |
log.debug('explicit cast_int_to_float') |
| 652 |
return llops.genop('cast_int_to_float', [v], resulttype=Float) |
| 653 |
if r_from.lowleveltype == SignedLongLong and r_to.lowleveltype == Float: |
| 654 |
log.debug('explicit cast_longlong_to_float') |
| 655 |
return llops.genop('cast_longlong_to_float', [v], resulttype=Float) |
| 656 |
if r_from.lowleveltype == UnsignedLongLong and r_to.lowleveltype == Float: |
| 657 |
log.debug('explicit cast_ulonglong_to_float') |
| 658 |
return llops.genop('cast_ulonglong_to_float', [v], resulttype=Float) |
| 659 |
return NotImplemented |
| 660 |
|
| 661 |
class __extend__(pairtype(FloatRepr, IntegerRepr)): |
| 662 |
def convert_from_to((r_from, r_to), v, llops): |
| 663 |
if r_from.lowleveltype == Float and r_to.lowleveltype == Unsigned: |
| 664 |
log.debug('explicit cast_float_to_uint') |
| 665 |
return llops.genop('cast_float_to_uint', [v], resulttype=Unsigned) |
| 666 |
if r_from.lowleveltype == Float and r_to.lowleveltype == Signed: |
| 667 |
log.debug('explicit cast_float_to_int') |
| 668 |
return llops.genop('cast_float_to_int', [v], resulttype=Signed) |
| 669 |
if r_from.lowleveltype == Float and r_to.lowleveltype == SignedLongLong: |
| 670 |
log.debug('explicit cast_float_to_longlong') |
| 671 |
return llops.genop('cast_float_to_longlong', [v], resulttype=SignedLongLong) |
| 672 |
if r_from.lowleveltype == Float and r_to.lowleveltype == UnsignedLongLong: |
| 673 |
log.debug('explicit cast_float_to_ulonglong') |
| 674 |
return llops.genop('cast_float_to_ulonglong', [v], resulttype=UnsignedLongLong) |
| 675 |
return NotImplemented |