This commit is contained in:
blueloveTH 2024-03-24 20:07:28 +08:00
parent 3f5e3c3968
commit 50cde5ff03
2 changed files with 9 additions and 3 deletions

View File

@ -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;
};

View File

@ -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{