No public description PiperOrigin-RevId: 559172498
diff --git a/sim/kelvin_vector_instructions.cc b/sim/kelvin_vector_instructions.cc index fce79e7..05f94f9 100644 --- a/sim/kelvin_vector_instructions.cc +++ b/sim/kelvin_vector_instructions.cc
@@ -736,58 +736,59 @@ T KelvinVShiftHelper(bool round, T vs1, T vs2) { using WT = typename mpact::sim::riscv::WideType<T>::type; if (std::is_signed<T>::value == true) { - constexpr int n = sizeof(T) * 8; + constexpr int kMaxShiftBit = sizeof(T) * 8; int shamt = vs2; - WT s = vs1; + WT shift = vs1; if (!vs1) { return 0; - } else if (vs1 < 0 && shamt >= n) { - s = -1 + round; - } else if (vs1 > 0 && shamt >= n) { - s = 0; + } else if (vs1 < 0 && shamt >= kMaxShiftBit) { + shift = -1 + round; + } else if (vs1 > 0 && shamt >= kMaxShiftBit) { + shift = 0; } else if (shamt > 0) { - s = (static_cast<WT>(vs1) + - (round ? static_cast<WT>(1ll << (shamt - 1)) : 0)) >> - shamt; + shift = (static_cast<WT>(vs1) + + (round ? static_cast<WT>(1ll << (shamt - 1)) : 0)) >> + shamt; } else { // shamt < 0 using UT = typename std::make_unsigned<T>::type; - UT ushamt = static_cast<UT>(-shamt <= n ? -shamt : n); - CHECK_LE(ushamt, n); + UT ushamt = + static_cast<UT>(-shamt <= kMaxShiftBit ? -shamt : kMaxShiftBit); + CHECK_LE(ushamt, kMaxShiftBit); CHECK_GE(ushamt, 0); // Use unsigned WideType to prevent undefined negative shift. using UWT = typename mpact::sim::riscv::WideType<UT>::type; - s = static_cast<WT>(static_cast<UWT>(vs1) << ushamt); + shift = static_cast<WT>(static_cast<UWT>(vs1) << ushamt); } T neg_max = std::numeric_limits<T>::min(); T pos_max = std::numeric_limits<T>::max(); - bool neg_sat = vs1 < 0 && (shamt <= -n || s < neg_max); - bool pos_sat = vs1 > 0 && (shamt <= -n || s > pos_max); + bool neg_sat = vs1 < 0 && (shamt <= -kMaxShiftBit || shift < neg_max); + bool pos_sat = vs1 > 0 && (shamt <= -kMaxShiftBit || shift > pos_max); if (neg_sat) return neg_max; if (pos_sat) return pos_max; - return s; - } else { - constexpr int n = sizeof(T) * 8; - // Shift can be positive/negative. - int shamt = static_cast<typename std::make_signed<T>::type>(vs2); - WT s = vs1; - if (!vs1) { - return 0; - } else if (shamt > n) { - s = 0; - } else if (shamt > 0) { - s = (static_cast<WT>(vs1) + - (round ? static_cast<WT>(1ull << (shamt - 1)) : 0)) >> - shamt; - } else { - using UT = typename std::make_unsigned<T>::type; - UT ushamt = static_cast<UT>(-shamt <= n ? -shamt : n); - s = static_cast<WT>(vs1) << (ushamt); - } - T pos_max = std::numeric_limits<T>::max(); - bool pos_sat = vs1 && (shamt < -n || s > pos_max); - if (pos_sat) return pos_max; - return s; + return shift; } + // unsigned. + constexpr int kMaxShiftBit = sizeof(T) * 8; + // Shift can be positive/negative. + int shamt = static_cast<typename std::make_signed<T>::type>(vs2); + WT shift = vs1; + if (!vs1) { + return 0; + } else if (shamt > kMaxShiftBit) { + shift = 0; + } else if (shamt > 0) { + shift = (static_cast<WT>(vs1) + + (round ? static_cast<WT>(1ull << (shamt - 1)) : 0)) >> + shamt; + } else { + using UT = typename std::make_unsigned<T>::type; + UT ushamt = static_cast<UT>(-shamt <= kMaxShiftBit ? -shamt : kMaxShiftBit); + shift = static_cast<WT>(vs1) << (ushamt); + } + T pos_max = std::numeric_limits<T>::max(); + bool pos_sat = vs1 && (shamt < -kMaxShiftBit || shift > pos_max); + if (pos_sat) return pos_max; + return shift; } template <typename T> @@ -939,11 +940,11 @@ static_cast<WT>(std::numeric_limits<T>::min()), std::min(static_cast<WT>(std::numeric_limits<T>::max()), result)); return result; - } else { - result = - std::min(static_cast<WT>(std::numeric_limits<T>::max()), result); - return result; } + + result = + std::min(static_cast<WT>(std::numeric_limits<T>::max()), result); + return result; })); } template void KelvinVMuls<int8_t>(bool, bool, Instruction *); @@ -1318,9 +1319,9 @@ if (dst_element_index & 1) { return GetInstructionSource<T>(inst, 1, scalar ? 0 : src_element_index); - } else { - return GetInstructionSource<T>(inst, 0, src_element_index); } + + return GetInstructionSource<T>(inst, 0, src_element_index); } template <typename T>
diff --git a/sim/test/kelvin_vector_instructions_test.cc b/sim/test/kelvin_vector_instructions_test.cc index 5c75baa..a465223 100644 --- a/sim/test/kelvin_vector_instructions_test.cc +++ b/sim/test/kelvin_vector_instructions_test.cc
@@ -745,10 +745,10 @@ if (std::is_signed<Vd>::value) { return static_cast<Vd>( (static_cast<int64_t>(vs1) + static_cast<int64_t>(vs2)) >> 1); - } else { - return static_cast<Vd>( - (static_cast<uint64_t>(vs1) + static_cast<uint64_t>(vs2)) >> 1); } + + return static_cast<Vd>( + (static_cast<uint64_t>(vs1) + static_cast<uint64_t>(vs2)) >> 1); } static void KelvinOp(bool scalar, bool strip_mine, Instruction *inst) { KelvinVHadd<Vd>(scalar, strip_mine, false /* round */, inst); @@ -770,10 +770,10 @@ if (std::is_signed<Vd>::value) { return static_cast<Vd>( (static_cast<int64_t>(vs1) + static_cast<int64_t>(vs2) + 1) >> 1); - } else { - return static_cast<Vd>( - (static_cast<uint64_t>(vs1) + static_cast<uint64_t>(vs2) + 1) >> 1); } + + return static_cast<Vd>( + (static_cast<uint64_t>(vs1) + static_cast<uint64_t>(vs2) + 1) >> 1); } static void KelvinOp(bool scalar, bool strip_mine, Instruction *inst) { KelvinVHadd<Vd>(scalar, strip_mine, true /* round */, inst); @@ -795,10 +795,10 @@ if (std::is_signed<Vd>::value) { return static_cast<Vd>( (static_cast<int64_t>(vs1) - static_cast<int64_t>(vs2)) >> 1); - } else { - return static_cast<Vd>( - (static_cast<uint64_t>(vs1) - static_cast<uint64_t>(vs2)) >> 1); } + + return static_cast<Vd>( + (static_cast<uint64_t>(vs1) - static_cast<uint64_t>(vs2)) >> 1); } static void KelvinOp(bool scalar, bool strip_mine, Instruction *inst) { KelvinVHsub<Vd>(scalar, strip_mine, false /* round */, inst); @@ -820,10 +820,10 @@ if (std::is_signed<Vd>::value) { return static_cast<Vd>( (static_cast<int64_t>(vs1) - static_cast<int64_t>(vs2) + 1) >> 1); - } else { - return static_cast<Vd>( - (static_cast<uint64_t>(vs1) - static_cast<uint64_t>(vs2) + 1) >> 1); } + + return static_cast<Vd>( + (static_cast<uint64_t>(vs1) - static_cast<uint64_t>(vs2) + 1) >> 1); } static void KelvinOp(bool scalar, bool strip_mine, Instruction *inst) { KelvinVHsub<Vd>(scalar, strip_mine, true /* round */, inst); @@ -1019,57 +1019,61 @@ struct VShiftOp { static Vd Op(bool round, Vs1 vs1, Vs2 vs2) { if (std::is_signed<Vd>::value == true) { - constexpr int n = sizeof(Vd) * 8; + constexpr int kMaxShiftBit = sizeof(Vd) * 8; int shamt = 0; if (sizeof(Vd) == 1) shamt = static_cast<int8_t>(vs2); if (sizeof(Vd) == 2) shamt = static_cast<int16_t>(vs2); if (sizeof(Vd) == 4) shamt = static_cast<int32_t>(vs2); - int64_t s = vs1; + int64_t shift = vs1; if (!vs1) { return 0; - } else if (vs1 < 0 && shamt >= n) { - s = -1 + round; - } else if (vs1 > 0 && shamt >= n) { - s = 0; + } else if (vs1 < 0 && shamt >= kMaxShiftBit) { + shift = -1 + round; + } else if (vs1 > 0 && shamt >= kMaxShiftBit) { + shift = 0; } else if (shamt > 0) { - s = (static_cast<int64_t>(vs1) + (round ? (1ll << (shamt - 1)) : 0)) >> + shift = + (static_cast<int64_t>(vs1) + (round ? (1ll << (shamt - 1)) : 0)) >> shamt; } else { // shamt < 0 - uint32_t ushamt = static_cast<uint32_t>(-shamt <= n ? -shamt : n); - s = static_cast<int64_t>(static_cast<uint64_t>(vs1) << ushamt); + uint32_t ushamt = static_cast<uint32_t>( + -shamt <= kMaxShiftBit ? -shamt : kMaxShiftBit); + shift = static_cast<int64_t>(static_cast<uint64_t>(vs1) << ushamt); } - int64_t neg_max = (-1ull) << (n - 1); - int64_t pos_max = (1ll << (n - 1)) - 1; - bool neg_sat = vs1 < 0 && (shamt <= -n || s < neg_max); - bool pos_sat = vs1 > 0 && (shamt <= -n || s > pos_max); + int64_t neg_max = (-1ull) << (kMaxShiftBit - 1); + int64_t pos_max = (1ll << (kMaxShiftBit - 1)) - 1; + bool neg_sat = vs1 < 0 && (shamt <= -kMaxShiftBit || shift < neg_max); + bool pos_sat = vs1 > 0 && (shamt <= -kMaxShiftBit || shift > pos_max); if (neg_sat) return neg_max; if (pos_sat) return pos_max; - return s; - } else { - constexpr int n = sizeof(Vd) * 8; - int shamt = 0; - if (sizeof(Vd) == 1) shamt = static_cast<int8_t>(vs2); - if (sizeof(Vd) == 2) shamt = static_cast<int16_t>(vs2); - if (sizeof(Vd) == 4) shamt = static_cast<int32_t>(vs2); - uint64_t s = vs1; - if (!vs1) { - return 0; - } else if (shamt > n) { - s = 0; - } else if (shamt > 0) { - s = (static_cast<uint64_t>(vs1) + - (round ? (1ull << (shamt - 1)) : 0)) >> - shamt; - } else { - using UT = typename std::make_unsigned<Vd>::type; - UT ushamt = static_cast<UT>(-shamt <= n ? -shamt : n); - s = static_cast<uint64_t>(vs1) << (ushamt); - } - uint64_t pos_max = (1ull << n) - 1; - bool pos_sat = vs1 && (shamt < -n || s >= (1ull << n)); - if (pos_sat) return pos_max; - return s; + return shift; } + + constexpr int kMaxShiftBit = sizeof(Vd) * 8; + int shamt = 0; + if (sizeof(Vd) == 1) shamt = static_cast<int8_t>(vs2); + if (sizeof(Vd) == 2) shamt = static_cast<int16_t>(vs2); + if (sizeof(Vd) == 4) shamt = static_cast<int32_t>(vs2); + uint64_t shift = vs1; + if (!vs1) { + return 0; + } else if (shamt > kMaxShiftBit) { + shift = 0; + } else if (shamt > 0) { + shift = + (static_cast<uint64_t>(vs1) + (round ? (1ull << (shamt - 1)) : 0)) >> + shamt; + } else { + using UT = typename std::make_unsigned<Vd>::type; + UT ushamt = + static_cast<UT>(-shamt <= kMaxShiftBit ? -shamt : kMaxShiftBit); + shift = static_cast<uint64_t>(vs1) << (ushamt); + } + uint64_t pos_max = (1ull << kMaxShiftBit) - 1; + bool pos_sat = + vs1 && (shamt < -kMaxShiftBit || shift >= (1ull << kMaxShiftBit)); + if (pos_sat) return pos_max; + return shift; } static void KelvinOp(bool round, bool scalar, bool strip_mine, @@ -1250,10 +1254,10 @@ if (std::is_signed<Vd>::value) { return static_cast<Vd>(static_cast<int64_t>(vs1) * static_cast<int64_t>(vs2)); - } else { - return static_cast<Vd>(static_cast<uint64_t>(vs1) * - static_cast<uint64_t>(vs2)); } + + return static_cast<Vd>(static_cast<uint64_t>(vs1) * + static_cast<uint64_t>(vs2)); } static void KelvinOp(bool scalar, bool strip_mine, Instruction *inst) { KelvinVMul<Vd>(scalar, strip_mine, inst); @@ -1273,11 +1277,11 @@ static_cast<int64_t>(std::numeric_limits<Vd>::min()), std::min(static_cast<int64_t>(std::numeric_limits<Vd>::max()), m)); return m; - } else { - uint64_t m = static_cast<uint64_t>(vs1) * static_cast<uint64_t>(vs2); - m = std::min(static_cast<uint64_t>(std::numeric_limits<Vd>::max()), m); - return m; } + + uint64_t m = static_cast<uint64_t>(vs1) * static_cast<uint64_t>(vs2); + m = std::min(static_cast<uint64_t>(std::numeric_limits<Vd>::max()), m); + return m; } static void KelvinOp(bool scalar, bool strip_mine, Instruction *inst) { KelvinVMuls<Vd>(scalar, strip_mine, inst); @@ -1321,10 +1325,10 @@ if (std::is_signed<Vs1>::value) { int64_t result = static_cast<int64_t>(vs1) * static_cast<int64_t>(vs2); return static_cast<uint64_t>(result) >> n; - } else { - uint64_t result = static_cast<uint64_t>(vs1) * static_cast<uint64_t>(vs2); - return result >> n; } + + uint64_t result = static_cast<uint64_t>(vs1) * static_cast<uint64_t>(vs2); + return result >> n; } static void KelvinOp(bool scalar, bool strip_mine, Instruction *inst) { KelvinVMulh<Vd>(scalar, strip_mine, false /* round */, inst); @@ -1349,11 +1353,11 @@ int64_t result = static_cast<int64_t>(vs1) * static_cast<int64_t>(vs2); result += 1ll << (n - 1); return static_cast<uint64_t>(result) >> n; - } else { - uint64_t result = static_cast<uint64_t>(vs1) * static_cast<uint64_t>(vs2); - result += 1ull << (n - 1); - return result >> n; } + + uint64_t result = static_cast<uint64_t>(vs1) * static_cast<uint64_t>(vs2); + result += 1ull << (n - 1); + return result >> n; } static void KelvinOp(bool scalar, bool strip_mine, Instruction *inst) { KelvinVMulh<Vd>(scalar, strip_mine, true /* round */, inst);