diff --git a/src/ceval.h b/src/ceval.h index 6ddb57b3..073d8a24 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -112,8 +112,8 @@ PyVar VM::run_frame(Frame* frame){ case OP_POP_TOP: frame->_pop(); continue; case OP_BINARY_OP: { pkpy::Args args(2); - args[1] = frame->pop(); - args[0] = frame->top(); + args[1] = frame->pop_value(this); + args[0] = frame->top_value(this); frame->top() = fast_call(BINARY_SPECIAL_METHODS[byte.arg], std::move(args)); } continue; case OP_BITWISE_OP: { diff --git a/src/common.h b/src/common.h index 2b07652d..3d5488a1 100644 --- a/src/common.h +++ b/src/common.h @@ -39,13 +39,9 @@ #if defined(__EMSCRIPTEN__) || defined(__arm__) || defined(__i386__) typedef int32_t i64; typedef float f64; -const i64 kMinSafeInt = -((i64)1 << 30); -const i64 kMaxSafeInt = ((i64)1 << 30) - 1; #else typedef int64_t i64; typedef double f64; -const i64 kMinSafeInt = -((i64)1 << 62); -const i64 kMaxSafeInt = ((i64)1 << 62) - 1; #endif struct Dummy { char _; }; diff --git a/src/pocketpy.h b/src/pocketpy.h index e4713c6e..e164dbca 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -86,7 +86,7 @@ void init_builtins(VM* _vm) { _vm->bind_builtin_func<1>("hash", [](VM* vm, pkpy::Args& args){ i64 value = vm->hash(args[0]); - if(value < kMinSafeInt || value > kMaxSafeInt) value >>= 2; + if(((value << 2) >> 2) != value) value >>= 2; return vm->PyInt(value); }); diff --git a/src/vm.h b/src/vm.h index 2349b322..dcd9d059 100644 --- a/src/vm.h +++ b/src/vm.h @@ -551,7 +551,7 @@ public: } inline PyVar PyInt(i64 value) { - if(value < kMinSafeInt || value > kMaxSafeInt){ + if(((value << 2) >> 2) != value){ _error("OverflowError", std::to_string(value) + " is out of range"); } value = (value << 2) | 0b01; diff --git a/tests/_basic.py b/tests/_basic.py index 9bf207c3..98e556a5 100644 --- a/tests/_basic.py +++ b/tests/_basic.py @@ -120,9 +120,8 @@ assert round(-23.8) == -24 assert 7**21 == 558545864083284007 -assert 7**22 == 3909821048582988049 -assert 2**61 == 2305843009213693952 -assert -2**61 == -2305843009213693952 +assert 2**60 == 1152921504606846976 +assert -2**60 == -1152921504606846976 assert eq(2**-2, 0.25) assert 0**0 == 1 assert 0**1 == 0