blueloveTH 1 سال پیش
والد
کامیت
50cde5ff03
2فایلهای تغییر یافته به همراه9 افزوده شده و 3 حذف شده
  1. 7 2
      include/pocketpy/common.h
  2. 2 1
      src/expr.cpp

+ 7 - 2
include/pocketpy/common.h

@@ -71,13 +71,18 @@ namespace std = ::std;
 template <size_t T>
 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;
 };
 

+ 2 - 1
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{