From 50cde5ff03dc7a2fe403abfcea9954e8e0b35f42 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 24 Mar 2024 20:07:28 +0800 Subject: [PATCH] some fix --- include/pocketpy/common.h | 9 +++++++-- src/expr.cpp | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index 610483f2..187bc2b8 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -71,13 +71,18 @@ namespace std = ::std; template struct NumberTraits; +inline constexpr bool is_negative_shift_well_defined(){ + // rshift does not affect the sign bit + return ((int)-1) >> 1 == -1 && ((int64_t)-1) >> 1 == -1; +} + template <> struct NumberTraits<4> { using int_t = int32_t; using float_t = float; static constexpr int_t kMaxSmallInt = (1 << 28) - 1; - static constexpr int_t kMinSmallInt = - (1 << 28); + static constexpr int_t kMinSmallInt = is_negative_shift_well_defined() ? -(1 << 28) : 0; static constexpr float_t kEpsilon = (float_t)1e-4; }; @@ -87,7 +92,7 @@ struct NumberTraits<8> { using float_t = double; static constexpr int_t kMaxSmallInt = (1ll << 60) - 1; - static constexpr int_t kMinSmallInt = - (1ll << 60); + static constexpr int_t kMinSmallInt = is_negative_shift_well_defined() ? -(1ll << 60) : 0; static constexpr float_t kEpsilon = (float_t)1e-8; }; diff --git a/src/expr.cpp b/src/expr.cpp index 6b5dd112..76d5981d 100644 --- a/src/expr.cpp +++ b/src/expr.cpp @@ -61,7 +61,8 @@ namespace pkpy{ } int CodeEmitContext::emit_int(i64 value, int line){ - if(value >= -5 && value <= 16){ + bool allow_neg_int = is_negative_shift_well_defined() || value >= 0; + if(allow_neg_int && value >= -5 && value <= 16){ uint8_t op = OP_LOAD_INT_NEG_5 + (uint8_t)value + 5; return emit_((Opcode)op, BC_NOARG, line); }else{