this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "int-builtins.h"
3
4#include <cinttypes>
5#include <climits>
6#include <cmath>
7
8#include "builtins.h"
9#include "bytes-builtins.h"
10#include "formatter.h"
11#include "frame.h"
12#include "globals.h"
13#include "objects.h"
14#include "str-builtins.h"
15#include "thread.h"
16#include "type-builtins.h"
17
18namespace py {
19
20bool intDivmodNear(Thread* thread, const Int& dividend, const Int& divisor,
21 Object* quotient, Object* remainder) {
22 Runtime* runtime = thread->runtime();
23 if (!runtime->intDivideModulo(thread, dividend, divisor, quotient,
24 remainder)) {
25 return false;
26 }
27
28 HandleScope scope(thread);
29 Int quotient_int(&scope, **quotient);
30 Int remainder_int(&scope, **remainder);
31 Int one(&scope, SmallInt::fromWord(1));
32 Int twice_remainder(&scope,
33 runtime->intBinaryLshift(thread, remainder_int, one));
34 word cmp = twice_remainder.compare(*divisor);
35
36 if ((divisor.isNegative() ? cmp < 0 : cmp > 0) ||
37 (cmp == 0 && quotient_int.isOdd())) {
38 *quotient = runtime->intAdd(thread, quotient_int, one);
39 *remainder = runtime->intSubtract(thread, remainder_int, divisor);
40 }
41 return true;
42}
43
44word largeIntHash(RawLargeInt value) {
45 const word bits_per_half = kBitsPerWord / 2;
46
47 // The following computes `value % modulus` with
48 // `modulus := kArithmeticHashModulus` with
49 // a C/C++ style modulo (so -17 % m == -17). This matches cpythons hash
50 // function (see `cpython/Objects/longobject.c` for details).
51
52 // The following describes how we can compute without actually performing
53 // any division/modulo operations just by bit-shifting. We want to compute
54 // the modulo by a prime number for good hashing behavior and we pick a
55 // Mersenne prime, because that allows us to perform some masking tricks
56 // below. We use the constants as follows:
57 // hash_bits := kArithmeticHashBits := 61
58 // modulus := kArithmeticHashModulus := (1 << hash_bits) - 1
59 //
60 // To compute the modulo of a large int, we split it into higher bits and
61 // lower bits:
62 // large_int % modulus = ((high << s) + remaining_bits) % modulus
63 // = (((high << s) % modulus) + remaining_bits % modulus) % modulus
64 //
65 // It turns out the upper part part of this equation
66 // `((high << s) % modulus)` is just a bit rotation of `high_bits`.
67 // To understand this consider splitting up a value into `high_bits` and
68 // `low_bits`:
69 // low_bits := val & modulus
70 // high_bits := ((val >> hash_bits) << hash_bits)
71 // val = high_bits + low_bits
72 //
73 // <=> val << s = (((val << s) >> hash_bits) << hash_bits)
74 // + (val << s) & modulus
75 // = ((val >> (hash_bits - s)) << hash_bits)
76 // + (val << s) & modulus
77 // <=> (val << s) % modulus
78 // = (((val >> (hash_bits - s)) << hash_bits) % modulus +
79 // ((val << s) & modulus) % modulus) % modulus
80 // = (((val >> (hash_bits - s)) * 2**hash_bits) % modulus +
81 // ((val << s) & modulus) % modulus
82 // = (((val << (hash_bits - s)) % modulus * 1) % modulus +
83 // ((val << s) & modulus) % modulus
84 // = (((val << (hash_bits - s)) % modulus +
85 // ((val << s) & modulus) % modulus
86 // = ((val << (hash_bits - s)) + ((val << s) & modulus))
87 // % modulus
88 // Which is a rotation of `s` bits in the lowest `hash_bits` in `val`.
89 //
90 // Note that we can choose any size `s` that is smaller than `hash_bits`,
91 // meaning we can design our algorithm to compute `s` bits at a time.
92 //
93 // We only add a small amount of bits in each step, so the remaining modulo
94 // operation can be expressed as `if (result >= modulus) result -= modulus;`.
95 bool is_negative = value.isNegative();
96 word num_digits = value.numDigits();
97
98 uword result = 0;
99 for (word i = num_digits - 1; i >= 0; i--) {
100 uword digit = value.digitAt(i);
101 // The computation is designed for positive numbers. We compute negative
102 // numbers via `-(-value % p)`. We use the following equivalence so we do
103 // not need to negate the large integer:
104 // -(-value % p)
105 // <=> -((~value + 1) % p)
106 // <=> -(((~value % p) + (1 % p)) % p)
107 // <=> -(((~value % p) + 1) % p)
108 if (is_negative) {
109 digit = ~digit;
110 }
111
112 // Rotate result, add upper half of the digit, perform modulo.
113 result = ((result << bits_per_half) & kArithmeticHashModulus) |
114 result >> (kArithmeticHashBits - bits_per_half);
115 result += digit >> bits_per_half;
116 if (result >= kArithmeticHashModulus) {
117 result -= kArithmeticHashModulus;
118 }
119
120 // Rotate result, add lower half of digit, perform modulo.
121 result = ((result << bits_per_half) & kArithmeticHashModulus) |
122 result >> (kArithmeticHashBits - bits_per_half);
123 uword low_bits = digit & ((uword{1} << bits_per_half) - 1);
124 result += low_bits;
125 if (result >= kArithmeticHashModulus) {
126 result -= kArithmeticHashModulus;
127 }
128 }
129
130 if (is_negative) {
131 // We computed `result := ~value % p` so far, as described above compute
132 // `-((result + 1) % p)` now.
133 result++;
134 if (result >= kArithmeticHashModulus) {
135 result -= kArithmeticHashModulus;
136 }
137 result = -result;
138 // cpython replaces `-1` results with -2, because -1 is used as an
139 // "uninitialized hash" marker in some situations. We do not use the same
140 // marker, but do the same to match behavior.
141 if (result == static_cast<uword>(word{-1})) {
142 result--;
143 }
144 } else {
145 DCHECK(result != static_cast<uword>(word{-1}),
146 "should only have -1 for negative numbers");
147 }
148 return result;
149}
150
151// Used only for UserIntBase as a heap-allocated object.
152static const BuiltinAttribute kUserIntBaseAttributes[] = {
153 {ID(_UserInt__value), RawUserIntBase::kValueOffset,
154 AttributeFlags::kHidden},
155};
156
157void initializeIntTypes(Thread* thread) {
158 HandleScope scope(thread);
159 Runtime* runtime = thread->runtime();
160
161 Type int_type(&scope, addBuiltinType(thread, ID(int), LayoutId::kInt,
162 /*superclass_id=*/LayoutId::kObject,
163 kUserIntBaseAttributes,
164 UserIntBase::kSize, /*basetype=*/true));
165
166 {
167 Type type(&scope,
168 addImmediateBuiltinType(thread, ID(largeint), LayoutId::kLargeInt,
169 /*builtin_base=*/LayoutId::kInt,
170 /*superclass_id=*/LayoutId::kObject,
171 /*basetype=*/false));
172 Layout layout(&scope, type.instanceLayout());
173 layout.setDescribedType(*int_type);
174 runtime->setLargeIntType(type);
175 }
176
177 {
178 Type type(&scope,
179 addImmediateBuiltinType(thread, ID(smallint), LayoutId::kSmallInt,
180 /*builtin_base=*/LayoutId::kInt,
181 /*superclass_id=*/LayoutId::kObject,
182 /*basetype=*/false));
183 Layout layout(&scope, type.instanceLayout());
184 layout.setDescribedType(*int_type);
185 runtime->setSmallIntType(type);
186 // We want to lookup the class of an immediate type by using the 5-bit tag
187 // value as an index into the class table. Replicate the layout object for
188 // SmallInt to all locations that decode to a SmallInt tag.
189 for (word i = 2; i < (1 << Object::kImmediateTagBits); i += 2) {
190 DCHECK(
191 runtime->layoutAt(static_cast<LayoutId>(i)) == SmallInt::fromWord(0),
192 "list collision");
193 runtime->layoutAtPut(static_cast<LayoutId>(i), *layout);
194 }
195 }
196
197 addImmediateBuiltinType(thread, ID(bool), LayoutId::kBool,
198 /*builtin_base=*/LayoutId::kInt,
199 /*superclass_id=*/LayoutId::kInt,
200 /*basetype=*/false);
201}
202
203RawObject convertBoolToInt(RawObject object) {
204 DCHECK(object.isBool(), "conversion from bool to int requires a bool object");
205 return RawSmallInt::fromWord(object == RawBool::trueObj() ? 1 : 0);
206}
207
208static RawObject intBinaryOpSubclass(Thread* thread, Arguments args,
209 RawObject (*op)(Thread* t, const Int& left,
210 const Int& right)) {
211 HandleScope scope(thread);
212 Object self_obj(&scope, args.get(0));
213 Object other_obj(&scope, args.get(1));
214 Runtime* runtime = thread->runtime();
215 if (!runtime->isInstanceOfInt(*self_obj)) {
216 return thread->raiseRequiresType(self_obj, ID(int));
217 }
218 if (!runtime->isInstanceOfInt(*other_obj)) {
219 return NotImplementedType::object();
220 }
221 Int self(&scope, intUnderlying(*self_obj));
222 Int other(&scope, intUnderlying(*other_obj));
223 return op(thread, self, other);
224}
225
226inline static RawObject intBinaryOp(Thread* thread, Arguments args,
227 RawObject (*op)(Thread* t, const Int& left,
228 const Int& right)) {
229 if (args.get(0).isInt() && args.get(1).isInt()) {
230 HandleScope scope(thread);
231 Int self(&scope, args.get(0));
232 Int other(&scope, args.get(1));
233 return op(thread, self, other);
234 }
235 return intBinaryOpSubclass(thread, args, op);
236}
237
238static word gcd(word dividend, word divisor) {
239 while (divisor > 0) {
240 word modulo = dividend % divisor;
241 dividend = divisor;
242 divisor = modulo;
243 }
244 return dividend;
245}
246
247RawObject intGCD(Thread* thread, const Int& a, const Int& b) {
248 Runtime* runtime = thread->runtime();
249
250 if (a.isSmallInt() && b.isSmallInt()) {
251 // Take a shortcut because we know the result fits in a word.
252 word small_dividend = SmallInt::cast(*a).value();
253 word small_divisor = SmallInt::cast(*b).value();
254 if (small_dividend < 0) {
255 small_dividend = -small_dividend;
256 }
257 if (small_divisor < 0) {
258 small_divisor = -small_divisor;
259 }
260
261 if (small_dividend < small_divisor) {
262 word temp = small_divisor;
263 small_divisor = small_dividend;
264 small_dividend = temp;
265 }
266 return runtime->newInt(gcd(small_dividend, small_divisor));
267 }
268
269 HandleScope scope(thread);
270 Int dividend(&scope, *a);
271 Int divisor(&scope, *b);
272
273 if (dividend.isNegative()) {
274 dividend = runtime->intNegate(thread, dividend);
275 }
276 if (divisor.isNegative()) {
277 divisor = runtime->intNegate(thread, divisor);
278 }
279
280 if (dividend.compare(*divisor) < 0) {
281 Object temp(&scope, *divisor);
282 divisor = *dividend;
283 dividend = *temp;
284 }
285
286 while (divisor.isPositive()) {
287 if (dividend.isSmallInt() || dividend.isBool()) {
288 // Take a shortcut because we know the result fits in a word.
289 word small_dividend = dividend.asWord();
290 word small_divisor = divisor.asWord();
291 return runtime->newInt(gcd(small_dividend, small_divisor));
292 }
293 Object modulo(&scope, NoneType::object());
294 bool division_succeeded =
295 runtime->intDivideModulo(thread, dividend, divisor, nullptr, &modulo);
296 DCHECK(division_succeeded, "divisor_int must be nonzero");
297 dividend = *divisor;
298 divisor = *modulo;
299 }
300 return *dividend;
301}
302
303static RawObject intUnaryOp(Thread* thread, Arguments args,
304 RawObject (*op)(Thread* t, const Int& self)) {
305 HandleScope scope(thread);
306 Object self_obj(&scope, args.get(0));
307 if (!thread->runtime()->isInstanceOfInt(*self_obj)) {
308 return thread->raiseRequiresType(self_obj, ID(int));
309 }
310 Int self(&scope, intUnderlying(*self_obj));
311 return op(thread, self);
312}
313
314static RawObject asInt(Thread* thread, Arguments args) {
315 return intUnaryOp(thread, args, [](Thread*, const Int& self) -> RawObject {
316 if (self.isBool()) {
317 return convertBoolToInt(*self);
318 }
319 return *self;
320 });
321}
322
323static RawObject asStr(Thread* thread, Arguments args) {
324 return intUnaryOp(thread, args, [](Thread* t, const Int& self) {
325 return formatIntDecimalSimple(t, self);
326 });
327}
328
329RawObject METH(int, __abs__)(Thread* thread, Arguments args) {
330 return intUnaryOp(thread, args, [](Thread* t, const Int& self) -> RawObject {
331 if (self.isNegative()) {
332 return t->runtime()->intNegate(t, self);
333 }
334 if (self.isBool()) return convertBoolToInt(*self);
335 return *self;
336 });
337}
338
339RawObject METH(int, __add__)(Thread* thread, Arguments args) {
340 return intBinaryOp(thread, args,
341 [](Thread* t, const Int& left, const Int& right) {
342 return t->runtime()->intAdd(t, left, right);
343 });
344}
345
346RawObject METH(int, __and__)(Thread* thread, Arguments args) {
347 return intBinaryOp(thread, args,
348 [](Thread* t, const Int& left, const Int& right) {
349 return t->runtime()->intBinaryAnd(t, left, right);
350 });
351}
352
353RawObject METH(int, __bool__)(Thread* thread, Arguments args) {
354 return intUnaryOp(thread, args, [](Thread*, const Int& self) -> RawObject {
355 if (self.isBool()) return *self;
356 if (self.isSmallInt()) {
357 return Bool::fromBool(SmallInt::cast(*self).value() != 0);
358 }
359 DCHECK(self.isLargeInt(), "remaining case should be LargeInt");
360 return Bool::trueObj();
361 });
362}
363
364RawObject METH(int, __ceil__)(Thread* thread, Arguments args) {
365 return asInt(thread, args);
366}
367
368RawObject METH(int, __eq__)(Thread* thread, Arguments args) {
369 return intBinaryOp(
370 thread, args,
371 [](Thread*, const Int& left, const Int& right) -> RawObject {
372 return Bool::fromBool(left.compare(*right) == 0);
373 });
374}
375
376RawObject METH(int, __divmod__)(Thread* thread, Arguments args) {
377 return intBinaryOp(
378 thread, args,
379 [](Thread* t, const Int& left, const Int& right) -> RawObject {
380 HandleScope scope(t);
381 Object quotient(&scope, NoneType::object());
382 Object remainder(&scope, NoneType::object());
383 Runtime* runtime = t->runtime();
384 if (!runtime->intDivideModulo(t, left, right, "ient, &remainder)) {
385 return t->raiseWithFmt(LayoutId::kZeroDivisionError,
386 "integer division or modulo by zero");
387 }
388 return runtime->newTupleWith2(quotient, remainder);
389 });
390}
391
392RawObject METH(int, __float__)(Thread* thread, Arguments args) {
393 return intUnaryOp(thread, args, [](Thread* t, const Int& self) {
394 HandleScope scope(t);
395 double value = 0.0;
396 Object maybe_error(&scope, convertIntToDouble(t, self, &value));
397 if (!maybe_error.isNoneType()) return *maybe_error;
398 return t->runtime()->newFloat(value);
399 });
400}
401
402RawObject METH(int, __floor__)(Thread* thread, Arguments args) {
403 return asInt(thread, args);
404}
405
406RawObject METH(int, __invert__)(Thread* thread, Arguments args) {
407 return intUnaryOp(thread, args, [](Thread* t, const Int& self) {
408 return t->runtime()->intInvert(t, self);
409 });
410}
411
412RawObject METH(int, __floordiv__)(Thread* thread, Arguments args) {
413 return intBinaryOp(
414 thread, args, [](Thread* t, const Int& left, const Int& right) {
415 HandleScope scope(t);
416 Object quotient(&scope, NoneType::object());
417 if (!t->runtime()->intDivideModulo(t, left, right, "ient,
418 nullptr)) {
419 return t->raiseWithFmt(LayoutId::kZeroDivisionError,
420 "integer division or modulo by zero");
421 }
422 return *quotient;
423 });
424}
425
426static RawObject formatIntCodePoint(Thread* thread, const Int& value,
427 FormatSpec* format) {
428 if (value.isLargeInt()) {
429 return thread->raiseWithFmt(LayoutId::kOverflowError,
430 "Python int too large to convert to C long");
431 }
432 word value_word = value.asWord();
433 if (value_word < 0 || value_word > kMaxUnicode) {
434 static_assert(kMaxUnicode == 0x10ffff, "unexpected max unicode value");
435 return thread->raiseWithFmt(LayoutId::kOverflowError,
436 "%%c arg not in range(0x110000)");
437 }
438 HandleScope scope(thread);
439 Str code_point(&scope,
440 SmallStr::fromCodePoint(static_cast<int32_t>(value_word)));
441 if (format->precision >= 0) {
442 return thread->raiseWithFmt(
443 LayoutId::kValueError,
444 "Precision not allowed in integer format specifier");
445 }
446 if (format->positive_sign != '\0') {
447 return thread->raiseWithFmt(
448 LayoutId::kValueError,
449 "Sign not allowed with integer format specifier 'c'");
450 }
451 if (format->alternate) {
452 return thread->raiseWithFmt(
453 LayoutId::kValueError,
454 "Alternate form (#) not allowed with integer format specifier 'c'");
455 }
456 return formatStr(thread, code_point, format);
457}
458
459RawObject METH(int, __format__)(Thread* thread, Arguments args) {
460 HandleScope scope(thread);
461 Object self_obj(&scope, args.get(0));
462 Runtime* runtime = thread->runtime();
463 if (!runtime->isInstanceOfInt(*self_obj)) {
464 return thread->raiseRequiresType(self_obj, ID(int));
465 }
466 Int self(&scope, intUnderlying(*self_obj));
467
468 Object spec_obj(&scope, args.get(1));
469 if (!runtime->isInstanceOfStr(*spec_obj)) {
470 return thread->raiseWithFmt(LayoutId::kTypeError,
471 "__format__() argument 1 must be str, not %T",
472 &spec_obj);
473 }
474 Str spec(&scope, strUnderlying(*spec_obj));
475
476 if (spec.length() == 0) {
477 // We return the equivalent of `str(self)` for an empty spec.
478 if (self_obj.isSmallInt() || self_obj.isLargeInt()) {
479 return formatIntDecimalSimple(thread, self);
480 }
481 if (self_obj.isBool()) {
482 return runtime->symbols()->at(Bool::cast(*self_obj).value() ? ID(True)
483 : ID(False));
484 }
485 Object value(&scope, thread->invokeMethod1(self_obj, ID(__str__)));
486 DCHECK(!value.isErrorNotFound(), "`__str__` should always exist");
487 if (value.isErrorException()) return *value;
488 if (!runtime->isInstanceOfStr(*value)) {
489 return thread->raiseWithFmt(LayoutId::kTypeError,
490 "__str__ returned non-string (type %T)",
491 &value);
492 }
493 return *value;
494 }
495
496 FormatSpec format;
497 Object possible_error(&scope,
498 parseFormatSpec(thread, spec,
499 /*default_type=*/'d',
500 /*default_align=*/'>', &format));
501 if (!possible_error.isNoneType()) {
502 DCHECK(possible_error.isErrorException(), "expected exception");
503 return *possible_error;
504 }
505
506 switch (format.type) {
507 case 'b':
508 return formatIntBinary(thread, self, &format);
509 case 'c':
510 return formatIntCodePoint(thread, self, &format);
511 case 'd':
512 return formatIntDecimal(thread, self, &format);
513 case 'n':
514 UNIMPLEMENTED("print with locale thousands separator");
515 case 'o':
516 return formatIntOctal(thread, self, &format);
517 case 'x':
518 return formatIntHexadecimalLowerCase(thread, self, &format);
519 case 'X':
520 return formatIntHexadecimalUpperCase(thread, self, &format);
521 case '%':
522 case 'e':
523 case 'E':
524 case 'f':
525 case 'F':
526 case 'g':
527 case 'G': {
528 double value;
529 Object err(&scope, convertIntToDouble(thread, self, &value));
530 if (err.isErrorException()) return *err;
531 return formatFloat(thread, value, &format);
532 }
533 default:
534 return raiseUnknownFormatError(thread, format.type, self_obj);
535 }
536}
537
538RawObject METH(int, __hash__)(Thread* thread, Arguments args) {
539 return intUnaryOp(thread, args, [](Thread*, const Int& self) -> RawObject {
540 return SmallInt::fromWord(intHash(*self));
541 });
542}
543
544RawObject METH(int, __index__)(Thread* thread, Arguments args) {
545 return asInt(thread, args);
546}
547
548RawObject METH(int, __int__)(Thread* thread, Arguments args) {
549 return asInt(thread, args);
550}
551
552RawObject METH(int, __le__)(Thread* thread, Arguments args) {
553 return intBinaryOp(
554 thread, args,
555 [](Thread*, const Int& left, const Int& right) -> RawObject {
556 return Bool::fromBool(left.compare(*right) <= 0);
557 });
558}
559
560RawObject METH(int, __lt__)(Thread* thread, Arguments args) {
561 return intBinaryOp(
562 thread, args,
563 [](Thread*, const Int& left, const Int& right) -> RawObject {
564 return Bool::fromBool(left.compare(*right) < 0);
565 });
566}
567
568RawObject METH(int, __ge__)(Thread* thread, Arguments args) {
569 return intBinaryOp(
570 thread, args,
571 [](Thread*, const Int& left, const Int& right) -> RawObject {
572 return Bool::fromBool(left.compare(*right) >= 0);
573 });
574}
575
576RawObject METH(int, __gt__)(Thread* thread, Arguments args) {
577 return intBinaryOp(
578 thread, args,
579 [](Thread*, const Int& left, const Int& right) -> RawObject {
580 return Bool::fromBool(left.compare(*right) > 0);
581 });
582}
583
584RawObject METH(int, __mod__)(Thread* thread, Arguments args) {
585 return intBinaryOp(
586 thread, args, [](Thread* t, const Int& left, const Int& right) {
587 HandleScope scope(t);
588 Object remainder(&scope, NoneType::object());
589 if (!t->runtime()->intDivideModulo(t, left, right, nullptr,
590 &remainder)) {
591 return t->raiseWithFmt(LayoutId::kZeroDivisionError,
592 "integer division or modulo by zero");
593 }
594 return *remainder;
595 });
596}
597
598RawObject METH(int, __mul__)(Thread* thread, Arguments args) {
599 return intBinaryOp(thread, args,
600 [](Thread* t, const Int& left, const Int& right) {
601 return t->runtime()->intMultiply(t, left, right);
602 });
603}
604
605RawObject METH(int, __ne__)(Thread* thread, Arguments args) {
606 return intBinaryOp(
607 thread, args,
608 [](Thread*, const Int& left, const Int& right) -> RawObject {
609 return Bool::fromBool(left.compare(*right) != 0);
610 });
611}
612
613RawObject METH(int, __neg__)(Thread* thread, Arguments args) {
614 return intUnaryOp(thread, args, [](Thread* t, const Int& self) {
615 return t->runtime()->intNegate(t, self);
616 });
617}
618
619RawObject METH(int, __rshift__)(Thread* thread, Arguments args) {
620 return intBinaryOp(
621 thread, args, [](Thread* t, const Int& left, const Int& right) {
622 if (right.isNegative()) {
623 return t->raiseWithFmt(LayoutId::kValueError, "negative shift count");
624 }
625 return t->runtime()->intBinaryRshift(t, left, right);
626 });
627}
628
629RawObject METH(int, __str__)(Thread* thread, Arguments args) {
630 return asStr(thread, args);
631}
632
633RawObject METH(int, __sub__)(Thread* thread, Arguments args) {
634 return intBinaryOp(thread, args,
635 [](Thread* t, const Int& left, const Int& right) {
636 return t->runtime()->intSubtract(t, left, right);
637 });
638}
639
640RawObject METH(int, __truediv__)(Thread* thread, Arguments args) {
641 return intBinaryOp(
642 thread, args, [](Thread* t, const Int& left, const Int& right) {
643 if (right.isZero()) {
644 return t->raiseWithFmt(LayoutId::kZeroDivisionError,
645 "division by zero");
646 }
647 if (left.isLargeInt() || right.isLargeInt()) {
648 UNIMPLEMENTED("true division of LargeInts"); // TODO(T40072578)
649 }
650 return t->runtime()->newFloat(static_cast<double>(left.asWord()) /
651 right.asWord());
652 });
653}
654
655RawObject METH(int, __trunc__)(Thread* thread, Arguments args) {
656 return asInt(thread, args);
657}
658
659RawObject METH(int, __xor__)(Thread* thread, Arguments args) {
660 return intBinaryOp(thread, args,
661 [](Thread* t, const Int& left, const Int& right) {
662 return t->runtime()->intBinaryXor(t, left, right);
663 });
664}
665
666RawObject METH(int, __or__)(Thread* thread, Arguments args) {
667 return intBinaryOp(thread, args,
668 [](Thread* t, const Int& left, const Int& right) {
669 return t->runtime()->intBinaryOr(t, left, right);
670 });
671}
672
673RawObject METH(int, __lshift__)(Thread* thread, Arguments args) {
674 return intBinaryOp(
675 thread, args, [](Thread* t, const Int& left, const Int& right) {
676 if (right.isNegative()) {
677 return t->raiseWithFmt(LayoutId::kValueError, "negative shift count");
678 }
679 return t->runtime()->intBinaryLshift(t, left, right);
680 });
681}
682
683RawObject METH(int, __pos__)(Thread* thread, Arguments args) {
684 return asInt(thread, args);
685}
686
687RawObject METH(int, __repr__)(Thread* thread, Arguments args) {
688 return asStr(thread, args);
689}
690
691RawObject METH(int, __round__)(Thread* thread, Arguments args) {
692 return asInt(thread, args);
693}
694
695RawObject METH(int, as_integer_ratio)(Thread* thread, Arguments args) {
696 HandleScope scope(thread);
697 Object self_obj(&scope, args.get(0));
698 Runtime* runtime = thread->runtime();
699 if (!runtime->isInstanceOfInt(*self_obj)) {
700 return thread->raiseRequiresType(self_obj, ID(int));
701 }
702 Object numerator(&scope, intUnderlying(*self_obj));
703 Object denominator(&scope, SmallInt::fromWord(1));
704 return runtime->newTupleWith2(numerator, denominator);
705}
706
707RawObject METH(int, bit_length)(Thread* thread, Arguments args) {
708 return intUnaryOp(thread, args, [](Thread* t, const Int& self) {
709 return t->runtime()->newInt(self.bitLength());
710 });
711}
712
713RawObject METH(int, conjugate)(Thread* thread, Arguments args) {
714 return asInt(thread, args);
715}
716
717static RawObject toBytesImpl(Thread* thread, const Object& self_obj,
718 const Object& length_obj,
719 const Object& byteorder_obj, bool is_signed) {
720 HandleScope scope(thread);
721 Runtime* runtime = thread->runtime();
722 if (!runtime->isInstanceOfInt(*self_obj)) {
723 return thread->raiseRequiresType(self_obj, ID(int));
724 }
725 Int self(&scope, intUnderlying(*self_obj));
726 if (!runtime->isInstanceOfInt(*length_obj)) {
727 return thread->raiseWithFmt(
728 LayoutId::kTypeError,
729 "length argument cannot be interpreted as an integer");
730 }
731 Int length_int(&scope, intUnderlying(*length_obj));
732 OptInt<word> l = length_int.asInt<word>();
733 if (l.error != CastError::None) {
734 return thread->raiseWithFmt(LayoutId::kOverflowError,
735 "Python int too large to convert to C word");
736 }
737 word length = l.value;
738 if (length < 0) {
739 return thread->raiseWithFmt(LayoutId::kValueError,
740 "length argument must be non-negative");
741 }
742
743 if (!runtime->isInstanceOfStr(*byteorder_obj)) {
744 return thread->raiseWithFmt(LayoutId::kTypeError,
745 "to_bytes() argument 2 must be str, not int");
746 }
747 Str byteorder(&scope, *byteorder_obj);
748 endian endianness;
749 if (byteorder.equals(runtime->symbols()->at(ID(little)))) {
750 endianness = endian::little;
751 } else if (byteorder.equals(runtime->symbols()->at(ID(big)))) {
752 endianness = endian::big;
753 } else {
754 return thread->raiseWithFmt(LayoutId::kValueError,
755 "byteorder must be either 'little' or 'big'");
756 }
757
758 if (!is_signed && self.isNegative()) {
759 return thread->raiseWithFmt(LayoutId::kOverflowError,
760 "can't convert negative int to unsigned");
761 }
762
763 // Check for overflow.
764 word num_digits = self.numDigits();
765 uword high_digit = self.digitAt(num_digits - 1);
766 word bit_length =
767 num_digits * kBitsPerWord - Utils::numRedundantSignBits(high_digit);
768 if (bit_length > length * kBitsPerByte + !is_signed) {
769 return thread->raiseWithFmt(LayoutId::kOverflowError,
770 "int too big to convert");
771 }
772
773 return runtime->intToBytes(thread, self, length, endianness);
774}
775
776RawObject METH(int, to_bytes)(Thread* thread, Arguments args) {
777 HandleScope scope(thread);
778 Object self(&scope, args.get(0));
779 Object length(&scope, args.get(1));
780 Object byteorder(&scope, args.get(2));
781 if (!args.get(3).isBool()) {
782 return thread->raiseWithFmt(LayoutId::kTypeError, "signed must be bool");
783 }
784 return toBytesImpl(thread, self, length, byteorder,
785 Bool::cast(args.get(3)).value());
786}
787
788RawObject METH(bool, __new__)(Thread* thread, Arguments args) {
789 Runtime* runtime = thread->runtime();
790 HandleScope scope(thread);
791 Object type_obj(&scope, args.get(0));
792 if (!runtime->isInstanceOfType(*type_obj)) {
793 return thread->raiseWithFmt(LayoutId::kTypeError,
794 "bool.__new__(X): X is not a type object");
795 }
796 Type type(&scope, *type_obj);
797
798 // Since bool can't be subclassed, only need to check if the type is exactly
799 // bool.
800 if (type.instanceLayoutId() != LayoutId::kBool) {
801 return thread->raiseWithFmt(LayoutId::kTypeError,
802 "bool.__new__(X): X is not bool");
803 }
804
805 return Interpreter::isTrue(thread, args.get(1));
806}
807
808static RawObject boolOrImpl(Thread* thread, Arguments args) {
809 Runtime* runtime = thread->runtime();
810 HandleScope scope(thread);
811 Object self_obj(&scope, args.get(0));
812 if (!self_obj.isBool()) {
813 return thread->raiseRequiresType(self_obj, ID(bool));
814 }
815 Bool self(&scope, *self_obj);
816 Object other_obj(&scope, args.get(1));
817 if (other_obj.isBool()) {
818 return Bool::fromBool(self.value() || Bool::cast(*other_obj).value());
819 }
820 if (runtime->isInstanceOfInt(*other_obj)) {
821 return intBinaryOp(thread, args,
822 [](Thread* t, const Int& left, const Int& right) {
823 return t->runtime()->intBinaryOr(t, left, right);
824 });
825 }
826 return NotImplementedType::object();
827}
828
829static RawObject boolXorImpl(Thread* thread, Arguments args) {
830 Runtime* runtime = thread->runtime();
831 HandleScope scope(thread);
832 Object self_obj(&scope, args.get(0));
833 if (!self_obj.isBool()) {
834 return thread->raiseRequiresType(self_obj, ID(bool));
835 }
836 Bool self(&scope, *self_obj);
837 Object other_obj(&scope, args.get(1));
838 if (other_obj.isBool()) {
839 return Bool::fromBool(self.value() ^ Bool::cast(*other_obj).value());
840 }
841 if (runtime->isInstanceOfInt(*other_obj)) {
842 return intBinaryOp(thread, args,
843 [](Thread* t, const Int& left, const Int& right) {
844 return t->runtime()->intBinaryXor(t, left, right);
845 });
846 }
847 return NotImplementedType::object();
848}
849
850RawObject METH(bool, __or__)(Thread* thread, Arguments args) {
851 return boolOrImpl(thread, args);
852}
853
854RawObject METH(bool, __ror__)(Thread* thread, Arguments args) {
855 return boolOrImpl(thread, args);
856}
857
858RawObject METH(bool, __xor__)(Thread* thread, Arguments args) {
859 return boolXorImpl(thread, args);
860}
861
862RawObject METH(bool, __rxor__)(Thread* thread, Arguments args) {
863 return boolXorImpl(thread, args);
864}
865
866enum RoundingDirection {
867 RoundDown = -1,
868 NoRounding = 0,
869 RoundUp = 1,
870};
871
872// Convert a large int to double. Returns true and sets `result` if the
873// conversion was successful, false if the integer is too big to fit the
874// double range. If `rounding_direction` is not nullptr, it will be set to a
875// value indicating what rounding occured.
876static inline bool convertLargeIntToDouble(const LargeInt& large_int,
877 double* result,
878 RoundingDirection* rounding) {
879 // The following algorithm looks at the highest n bits of the integer and puts
880 // them into the mantissa of the floating point number. It extracts two
881 // extra bits to account for the highest bit not being explicitly encoded
882 // in floating point and the lowest bit to decide whether we should round
883 // up or down.
884
885 // Extract the highest two digits of the numbers magnitude.
886 word num_digits = large_int.numDigits();
887 DCHECK(num_digits > 1, "must have more than 1 digit");
888 uword high_digit = large_int.digitAt(num_digits - 1);
889 uword second_highest_digit = large_int.digitAt(num_digits - 2);
890 bool is_negative = large_int.isNegative();
891 uword carry_to_second_highest = 0;
892 if (is_negative) {
893 // The magnitude of a negative value is `~value + 1`. We compute the
894 // complement of the highest two digits and possibly add a carry.
895 carry_to_second_highest = 1;
896 for (word i = num_digits - 3; i >= 0; i--) {
897 // Any `digit != 0` will have a zero bit so we won't have a carry.
898 if (large_int.digitAt(i) != 0) {
899 carry_to_second_highest = 0;
900 break;
901 }
902 }
903 second_highest_digit = ~second_highest_digit + carry_to_second_highest;
904 uword carry_to_highest = second_highest_digit == 0 ? 1 : 0;
905 high_digit = ~high_digit + carry_to_highest;
906 // A negative number has the highest bit set so incrementing the complement
907 // cannot overflow.
908 DCHECK(carry_to_highest == 0 || high_digit != 0,
909 "highest digit cannot overflow");
910 }
911
912 // Determine the exponent bits.
913 int high_bit = Utils::highestBit(high_digit);
914 uword exponent_bits = kBitsPerDouble - kDoubleMantissaBits - 1;
915 uword exponent_bias = (1 << (exponent_bits - 1)) - 1;
916 uword exponent =
917 (num_digits - 1) * kBitsPerWord + high_bit - 1 + exponent_bias;
918
919 // Extract mantissa bits including the high bit which is implicit in the
920 // float representation and one extra bit to help determine if we need to
921 // round up.
922 // We also keep track if the bits shifted out on the right side are zero.
923 int shift = high_bit - (kDoubleMantissaBits + 2);
924 int shift_right = Utils::maximum(shift, 0);
925 int shift_left = -Utils::minimum(shift, 0);
926 uword value_as_word = (high_digit >> shift_right) << shift_left;
927 bool lesser_significant_bits_zero;
928 if (shift_left > 0) {
929 int lower_shift_right = kBitsPerWord - shift_left;
930 value_as_word |= second_highest_digit >> lower_shift_right;
931 lesser_significant_bits_zero = (second_highest_digit << shift_left) == 0;
932 } else {
933 lesser_significant_bits_zero =
934 second_highest_digit == 0 &&
935 (shift_right == 0 || (high_digit << (kBitsPerWord - shift_right)) == 0);
936 }
937
938 // Returns true if all digits (in the numbers magnitude) below the 2 highest
939 // digits are zero.
940 auto lower_bits_zero = [&]() -> bool {
941 if (!lesser_significant_bits_zero) return false;
942 // Already scanned the digits in the negative case and can look at carry.
943 if (is_negative) return carry_to_second_highest != 0;
944 for (word i = num_digits - 3; i >= 0; i--) {
945 if (large_int.digitAt(i) != 0) return false;
946 }
947 return true;
948 };
949
950 // We need to round down if the least significant bit is zero, we need to
951 // round up if the least significant and any other bit is one. If the
952 // least significant bit is one and all other bits are zero then we look at
953 // second least significant bit to round towards an even number.
954 if ((value_as_word & 0x3) == 0x3 ||
955 ((value_as_word & 1) && !lower_bits_zero())) {
956 value_as_word++;
957 // This may have triggered an overflow, so we need to add 1 to the exponent.
958 if (value_as_word == (uword{1} << (kDoubleMantissaBits + 2))) {
959 exponent++;
960 }
961 if (rounding != nullptr) {
962 *rounding = RoundUp;
963 }
964 } else if (rounding != nullptr) {
965 *rounding =
966 !(value_as_word & 1) && lower_bits_zero() ? NoRounding : RoundDown;
967 }
968 value_as_word >>= 1;
969
970 // Check for overflow.
971 // The biggest exponent is used to mark special numbers like NAN or INF.
972 uword max_exponent = (1 << exponent_bits) - 1;
973 if (exponent > max_exponent - 1) {
974 return false;
975 }
976
977 // Mask out implicit bit, combine mantissa, exponent and sign.
978 value_as_word &= (uword{1} << kDoubleMantissaBits) - 1;
979 value_as_word |= exponent << kDoubleMantissaBits;
980 value_as_word |= uword{is_negative} << (kDoubleMantissaBits + exponent_bits);
981 *result = bit_cast<double>(value_as_word);
982 return true;
983}
984
985RawObject convertIntToDouble(Thread* thread, const Int& value, double* result) {
986 if (value.numDigits() == 1) {
987 *result = static_cast<double>(value.asWord());
988 return NoneType::object();
989 }
990 HandleScope scope(thread);
991 LargeInt large_int(&scope, *value);
992 if (!convertLargeIntToDouble(large_int, result, nullptr)) {
993 return thread->raiseWithFmt(LayoutId::kOverflowError,
994 "int too large to convert to float");
995 }
996 return NoneType::object();
997}
998
999bool compareDoubleWithInt(Thread* thread, double left, const Int& right,
1000 CompareOp op) {
1001 DCHECK(op == GE || op == GT || op == LE || op == LT, "needs inequality op");
1002 bool compare_equal = op == LE || op == GE;
1003 bool compare_less = op == LT || op == LE;
1004 bool compare_greater = !compare_less;
1005 if (!std::isfinite(left)) {
1006 if (std::isnan(left)) return false;
1007 DCHECK(std::isinf(left), "remaining case must be infinity");
1008 return compare_less == (left < 0);
1009 }
1010
1011 word num_digits = right.numDigits();
1012 if (num_digits == 1) {
1013 word right_word = right.asWord();
1014 double right_double = static_cast<double>(right_word);
1015 if (left < right_double) return compare_less;
1016 if (left > right_double) return compare_greater;
1017 // TODO(matthiasb): We could also detect the rounding direction by
1018 // performing bit operations on `right_word` which is more complicated but
1019 // may be faster; benchmark.
1020 word right_double_word = static_cast<word>(right_double);
1021 if (right_double_word == right_word) return compare_equal;
1022 return compare_less == (right_double_word < right_word);
1023 }
1024
1025 // Shortcut for differing signs.
1026 if ((left < 0) != right.isNegative()) {
1027 DCHECK((compare_less == (left < 0)) == (compare_greater == (left > 0)),
1028 "conditions must be exclusive");
1029 return compare_less == (left < 0);
1030 }
1031
1032 double right_double;
1033 RoundingDirection rounding;
1034 HandleScope scope(thread);
1035 LargeInt large_int(&scope, *right);
1036 if (!convertLargeIntToDouble(large_int, &right_double, &rounding)) {
1037 return compare_less != (left < 0);
1038 }
1039 if (left < right_double) return compare_less;
1040 if (left > right_double) return compare_greater;
1041 if (rounding == NoRounding) return compare_equal;
1042 return compare_less == (rounding == RoundDown);
1043}
1044
1045bool doubleEqualsInt(Thread* thread, double left, const Int& right) {
1046 // This is basically the same code as `doubleCompareWithInt` but can take some
1047 // shortcuts because we don't care about the lesser/greater situations.
1048 word num_digits = right.numDigits();
1049 if (num_digits == 1) {
1050 word right_word = right.asWord();
1051 double right_double = static_cast<double>(right_word);
1052 if (left != right_double) return false;
1053 // Check whether any rounding occured when converting to floating-point.
1054 // TODO(matthiasb): We can also check this via bit operations on
1055 // `right_word` which is more complicated but may be faster; should run
1056 // some benchmarks.
1057 return static_cast<word>(right_double) == right_word;
1058 }
1059
1060 if (!std::isfinite(left)) {
1061 return false;
1062 }
1063 double right_double;
1064 RoundingDirection rounding;
1065 HandleScope scope(thread);
1066 LargeInt large_int(&scope, *right);
1067 if (!convertLargeIntToDouble(large_int, &right_double, &rounding)) {
1068 return false;
1069 }
1070 return rounding == NoRounding && left == right_double;
1071}
1072
1073RawObject intFromIndex(Thread* thread, const Object& obj) {
1074 Runtime* runtime = thread->runtime();
1075 if (runtime->isInstanceOfInt(*obj)) {
1076 return *obj;
1077 }
1078 HandleScope scope(thread);
1079 Object result(&scope, thread->invokeMethod1(obj, ID(__index__)));
1080 if (result.isError()) {
1081 if (result.isErrorNotFound()) {
1082 return thread->raiseWithFmt(
1083 LayoutId::kTypeError,
1084 "'%T' object cannot be interpreted as an integer", &obj);
1085 }
1086 return *result;
1087 }
1088 if (runtime->isInstanceOfInt(*result)) {
1089 return *result;
1090 }
1091 return thread->raiseWithFmt(LayoutId::kTypeError,
1092 "__index__ returned non-int (type %T)", &result);
1093}
1094
1095} // namespace py