No public description

PiperOrigin-RevId: 999438218
Change-Id: I97ca5ebb32083cfbf75fd50812d85d8570e93f81
diff --git a/riscv/riscv_i_instructions.cc b/riscv/riscv_i_instructions.cc
index baf0afe..4be2ea5 100644
--- a/riscv/riscv_i_instructions.cc
+++ b/riscv/riscv_i_instructions.cc
@@ -199,10 +199,9 @@
 }
 
 void RiscVIAddw(const Instruction* instruction) {
-  RiscVBinaryOp<RegisterType, IntReg, NarrowIntReg>(
-      instruction, [](NarrowIntReg a, NarrowIntReg b) {
-        IntReg c = static_cast<IntReg>(a + b);
-        return c;
+  RiscVBinaryOp<RegisterType, IntReg, NarrowUIntReg>(
+      instruction, [](NarrowUIntReg a, NarrowUIntReg b) {
+        return static_cast<IntReg>(static_cast<NarrowIntReg>(a + b));
       });
 }
 
@@ -212,10 +211,9 @@
 }
 
 void RiscVISubw(const Instruction* instruction) {
-  RiscVBinaryOp<RegisterType, IntReg, NarrowIntReg>(
-      instruction, [](NarrowIntReg a, NarrowIntReg b) {
-        IntReg c = static_cast<IntReg>(a - b);
-        return c;
+  RiscVBinaryOp<RegisterType, IntReg, NarrowUIntReg>(
+      instruction, [](NarrowUIntReg a, NarrowUIntReg b) {
+        return static_cast<IntReg>(static_cast<NarrowIntReg>(a - b));
       });
 }
 
@@ -260,9 +258,10 @@
 }
 
 void RiscVISllw(const Instruction* instruction) {
-  RiscVBinaryOp<RegisterType, IntReg, NarrowIntReg>(
-      instruction, [](NarrowIntReg a, NarrowIntReg b) -> IntReg {
-        return static_cast<IntReg>(a << (b & 0x1f));
+  RiscVBinaryOp<RegisterType, IntReg, NarrowUIntReg>(
+      instruction, [](NarrowUIntReg a, NarrowUIntReg b) -> IntReg {
+        auto c = a << (b & 0x1f);
+        return static_cast<IntReg>(static_cast<NarrowIntReg>(c));
       });
 }
 
diff --git a/riscv/riscv_m_instructions.cc b/riscv/riscv_m_instructions.cc
index c7e231f..3584fe8 100644
--- a/riscv/riscv_m_instructions.cc
+++ b/riscv/riscv_m_instructions.cc
@@ -191,12 +191,11 @@
 }
 
 void MMulw(Instruction* instruction) {
-  BinaryOp<IntReg, NarrowIntReg>(instruction,
-                                 [](NarrowIntReg a, NarrowIntReg b) -> IntReg {
-                                   NarrowIntReg c = a * b;
-                                   IntReg c_wide = static_cast<IntReg>(c);
-                                   return c_wide;
-                                 });
+  BinaryOp<IntReg, NarrowUintReg>(
+      instruction, [](NarrowUintReg a, NarrowUintReg b) -> IntReg {
+        NarrowUintReg c = a * b;
+        return static_cast<IntReg>(static_cast<NarrowIntReg>(c));
+      });
 }
 
 void MDivw(Instruction* instruction) {
diff --git a/riscv/test/riscv64_i_instructions_test.cc b/riscv/test/riscv64_i_instructions_test.cc
index ad4bf9a..ae58e8c 100644
--- a/riscv/test/riscv64_i_instructions_test.cc
+++ b/riscv/test/riscv64_i_instructions_test.cc
@@ -25,6 +25,7 @@
 #include "mpact/sim/util/memory/flat_demand_memory.h"
 #include "riscv/riscv_csr.h"
 #include "riscv/riscv_i_instructions.h"
+#include "riscv/riscv_m_instructions.h"
 #include "riscv/riscv_register.h"
 #include "riscv/riscv_state.h"
 
@@ -40,6 +41,7 @@
 using ::mpact::sim::util::FlatDemandMemory;
 
 constexpr char kX1[] = "x1";
+constexpr char kX2[] = "x2";
 constexpr char kX3[] = "x3";
 
 constexpr uint32_t kInstAddress = 0x2468;
@@ -204,4 +206,72 @@
   EXPECT_EQ(trap_epc_, instruction_->address());
 }
 
+TEST_F(RV64IInstructionTest, RV64IAddwOverflow) {
+  AppendRegisterOperands({kX1, kX2}, {kX3});
+  SetSemanticFunction(&::mpact::sim::riscv::RV64::RiscVIAddw);
+
+  // 0x7fffffff + 1 = 0x80000000 -> sign-extended to 64-bit: 0xffffffff80000000
+  SetRegisterValues<uint64_t>({{kX1, 0x7fffffffULL}, {kX2, 1ULL}});
+  instruction_->Execute(nullptr);
+  EXPECT_EQ(GetRegisterValue<uint64_t>(kX3), 0xffffffff80000000ULL);
+
+  // 0x80000000 + (-1) = 0x7fffffff -> sign-extended: 0x000000007fffffff
+  SetRegisterValues<uint64_t>(
+      {{kX1, 0xffffffff80000000ULL}, {kX2, 0xffffffffffffffffULL}});
+  instruction_->Execute(nullptr);
+  EXPECT_EQ(GetRegisterValue<uint64_t>(kX3), 0x000000007fffffffULL);
+}
+
+TEST_F(RV64IInstructionTest, RV64ISubwOverflow) {
+  AppendRegisterOperands({kX1, kX2}, {kX3});
+  SetSemanticFunction(&::mpact::sim::riscv::RV64::RiscVISubw);
+
+  // 0x80000000 - 1 = 0x7fffffff -> sign-extended to 64-bit: 0x000000007fffffff
+  SetRegisterValues<uint64_t>({{kX1, 0xffffffff80000000ULL}, {kX2, 1ULL}});
+  instruction_->Execute(nullptr);
+  EXPECT_EQ(GetRegisterValue<uint64_t>(kX3), 0x000000007fffffffULL);
+
+  // 0x7fffffff - (-1) = 0x80000000 -> sign-extended: 0xffffffff80000000
+  SetRegisterValues<uint64_t>(
+      {{kX1, 0x7fffffffULL}, {kX2, 0xffffffffffffffffULL}});
+  instruction_->Execute(nullptr);
+  EXPECT_EQ(GetRegisterValue<uint64_t>(kX3), 0xffffffff80000000ULL);
+}
+
+TEST_F(RV64IInstructionTest, RV64ISllwOverflow) {
+  AppendRegisterOperands({kX1, kX2}, {kX3});
+  SetSemanticFunction(&::mpact::sim::riscv::RV64::RiscVISllw);
+
+  // 1 << 31 = 0x80000000 -> sign-extended to 64-bit: 0xffffffff80000000
+  SetRegisterValues<uint64_t>({{kX1, 1ULL}, {kX2, 31ULL}});
+  instruction_->Execute(nullptr);
+  EXPECT_EQ(GetRegisterValue<uint64_t>(kX3), 0xffffffff80000000ULL);
+
+  // (-1) << 1 = 0xfffffffe -> sign-extended to 64-bit: 0xfffffffffffffffe
+  SetRegisterValues<uint64_t>({{kX1, 0xffffffffffffffffULL}, {kX2, 1ULL}});
+  instruction_->Execute(nullptr);
+  EXPECT_EQ(GetRegisterValue<uint64_t>(kX3), 0xfffffffffffffffeULL);
+}
+
+TEST_F(RV64IInstructionTest, RV64MMulwOverflow) {
+  AppendRegisterOperands({kX1, kX2}, {kX3});
+  SetSemanticFunction(&::mpact::sim::riscv::RV64::MMulw);
+
+  // 0x40000000 * 2 = 0x80000000 -> sign-extended to 64-bit: 0xffffffff80000000
+  SetRegisterValues<uint64_t>({{kX1, 0x40000000ULL}, {kX2, 2ULL}});
+  instruction_->Execute(nullptr);
+  EXPECT_EQ(GetRegisterValue<uint64_t>(kX3), 0xffffffff80000000ULL);
+
+  // 0x7fffffff * 2 = 0xfffffffe -> sign-extended to 64-bit: 0xfffffffffffffffe
+  SetRegisterValues<uint64_t>({{kX1, 0x7fffffffULL}, {kX2, 2ULL}});
+  instruction_->Execute(nullptr);
+  EXPECT_EQ(GetRegisterValue<uint64_t>(kX3), 0xfffffffffffffffeULL);
+
+  // (-0x80000000) * (-1) = 0x80000000 -> sign-extended: 0xffffffff80000000
+  SetRegisterValues<uint64_t>(
+      {{kX1, 0xffffffff80000000ULL}, {kX2, 0xffffffffffffffffULL}});
+  instruction_->Execute(nullptr);
+  EXPECT_EQ(GetRegisterValue<uint64_t>(kX3), 0xffffffff80000000ULL);
+}
+
 }  // namespace
\ No newline at end of file