Adding semantic functions for bit manipulation instructions. PiperOrigin-RevId: 668929425 Change-Id: I1f550f37ee1689df13e5b403475ab1adb882f9c4
diff --git a/riscv/BUILD b/riscv/BUILD index 90d5cf2..8eb8fea 100644 --- a/riscv/BUILD +++ b/riscv/BUILD
@@ -170,6 +170,26 @@ ) cc_library( + name = "riscv_bitmanip_instructions", + srcs = [ + "riscv_bitmanip_instructions.cc", + ], + hdrs = [ + "riscv_bitmanip_instructions.h", + ], + copts = [ + "-O3", + "-ffp-model=strict", + ], + deps = [ + ":riscv_g", + ":riscv_state", + "@com_google_absl//absl/numeric:bits", + "@com_google_mpact-sim//mpact/sim/generic:instruction", + ], +) + +cc_library( name = "riscv_v", srcs = [ "riscv_vector_fp_compare_instructions.cc", @@ -343,9 +363,7 @@ "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/log", - "@com_google_absl//absl/memory", "@com_google_absl//absl/strings", - "@com_google_mpact-sim//mpact/sim/generic:arch_state", "@com_google_mpact-sim//mpact/sim/generic:core", "@com_google_mpact-sim//mpact/sim/generic:instruction", "@com_google_mpact-sim//mpact/sim/generic:program_error", @@ -401,7 +419,6 @@ "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/log", "@com_google_absl//absl/strings", - "@com_google_mpact-sim//mpact/sim/generic:arch_state", "@com_google_mpact-sim//mpact/sim/generic:core", "@com_google_mpact-sim//mpact/sim/generic:instruction", "@com_google_mpact-sim//mpact/sim/generic:program_error", @@ -567,7 +584,6 @@ "@com_google_mpact-sim//mpact/sim/generic:core_debug_interface", "@com_google_mpact-sim//mpact/sim/generic:debug_command_shell_interface", "@com_google_mpact-sim//mpact/sim/generic:type_helpers", - "@com_google_mpact-sim//mpact/sim/util/program_loader:elf_loader", "@com_googlesource_code_re2//:re2", ], ) @@ -750,7 +766,7 @@ "riscv_clint.h", ], deps = [ - "//riscv:riscv_state", + ":riscv_state", "@com_google_absl//absl/log", "@com_google_mpact-sim//mpact/sim/generic:core", "@com_google_mpact-sim//mpact/sim/generic:counters", @@ -774,7 +790,6 @@ "@com_google_absl//absl/flags:parse", "@com_google_absl//absl/log", "@com_google_absl//absl/strings", - "@com_google_absl//absl/strings:str_format", "@com_google_mpact-sim//mpact/sim/generic:core_debug_interface", "@com_google_mpact-sim//mpact/sim/generic:type_helpers", "@com_google_mpact-sim//mpact/sim/util/memory", @@ -790,7 +805,6 @@ copts = ["-O3"], deps = [ ":riscv64g_decoder", - ":riscv_fp_state", ":riscv_state", ":riscv_test_mem_watcher", ":riscv_top", @@ -846,14 +860,13 @@ ":instrumentation", ":riscv32g_decoder", ":riscv64g_decoder", + ":riscv_arm_semihost", + ":riscv_clint", ":riscv_debug_info", ":riscv_debug_interface", + ":riscv_state", ":riscv_top", - "//riscv:riscv_arm_semihost", - "//riscv:riscv_clint", - "//riscv:riscv_state", - "//riscv:stoull_wrapper", - "@com_google_absl//absl/container:flat_hash_map", + ":stoull_wrapper", "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/functional:bind_front", "@com_google_absl//absl/log", @@ -868,7 +881,6 @@ "@com_google_mpact-sim//mpact/sim/util/memory", "@com_google_mpact-sim//mpact/sim/util/other:instruction_profiler", "@com_google_mpact-sim//mpact/sim/util/program_loader:elf_loader", - "@com_google_mpact-sim//mpact/sim/util/renode", "@com_google_mpact-sim//mpact/sim/util/renode:renode_debug_interface", "@com_google_mpact-sim//mpact/sim/util/renode:socket_cli", "@com_google_protobuf//:protobuf",
diff --git a/riscv/riscv_bitmanip_instructions.cc b/riscv/riscv_bitmanip_instructions.cc new file mode 100644 index 0000000..f721266 --- /dev/null +++ b/riscv/riscv_bitmanip_instructions.cc
@@ -0,0 +1,232 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "riscv/riscv_bitmanip_instructions.h" + +#include <algorithm> +#include <cstdint> + +#include "absl/numeric/bits.h" +#include "mpact/sim/generic/instruction.h" +#include "riscv/riscv_instruction_helpers.h" +#include "riscv/riscv_register.h" +#include "riscv/riscv_state.h" + +// This file contains the semantic function definitions for the bit manipulation +// instructions in RiscV. + +namespace mpact::sim::riscv { + +using ::mpact::sim::generic::Instruction; + +namespace RV32 { + +using RegisterType = RV32Register; +using UIntReg = uint32_t; +using IntReg = int32_t; +constexpr int kXlen = sizeof(UIntReg) * 8; + +// Add with shift. +void RiscVShAdd(const Instruction *instruction) { + auto shift = generic::GetInstructionSource<UIntReg>(instruction, 2); + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [shift](UIntReg a, UIntReg b) { return b + (a << shift); }); +} + +// And not. +void RiscVAndn(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a, UIntReg b) { return a & ~b; }); +} + +// Or not. +void RiscVOrn(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a, UIntReg b) { return a | ~b; }); +} + +// Xnor. +void RiscVXnor(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a, UIntReg b) { return ~(a ^ b); }); +} + +// Count leading zeros. +void RiscVClz(const Instruction *instruction) { + RiscVUnaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a) -> UIntReg { return absl::countl_zero(a); }); +} + +// Count trailing zeros. +void RiscVCtz(const Instruction *instruction) { + RiscVUnaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a) -> UIntReg { return absl::countr_zero(a); }); +} + +// Bit population count. +void RiscVCpop(const Instruction *instruction) { + RiscVUnaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a) -> UIntReg { return absl::popcount(a); }); +} + +// Signed max. +void RiscVMax(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, IntReg, IntReg>( + instruction, [](IntReg a, IntReg b) { return std::max(a, b); }); +} + +// Unsigned max. +void RiscVMaxu(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a, UIntReg b) { return std::max(a, b); }); +} + +// Signed min. +void RiscVMin(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, IntReg, IntReg>( + instruction, [](IntReg a, IntReg b) { return std::min(a, b); }); +} + +// Unsigned min. +void RiscVMinu(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a, UIntReg b) { return std::min(a, b); }); +} + +// Sign extend byte. +void RiscVSextB(const Instruction *instruction) { + RiscVUnaryOp<RegisterType, IntReg, int8_t>( + instruction, [](int8_t a) -> IntReg { return static_cast<IntReg>(a); }); +} + +// Sign extend half word. +void RiscVSextH(const Instruction *instruction) { + RiscVUnaryOp<RegisterType, IntReg, int16_t>( + instruction, [](int16_t a) -> IntReg { return static_cast<IntReg>(a); }); +} + +// Zero extend half word. +void RiscVZextH(const Instruction *instruction) { + RiscVUnaryOp<RegisterType, UIntReg, uint16_t>( + instruction, + [](uint16_t a) -> UIntReg { return static_cast<UIntReg>(a); }); +} + +// Rotate left. +void RiscVrol(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, + [](UIntReg a, UIntReg b) { return (a << b) | (a >> (32 - b)); }); +} + +// Rotate right. +void RiscVror(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, + [](UIntReg a, UIntReg b) { return (a >> b) | (a << (32 - b)); }); +} + +// Or combine (byte wise). +void RiscVOrcb(const Instruction *instruction) { + RiscVUnaryOp<RegisterType, UIntReg, UIntReg>(instruction, [](UIntReg a) { + UIntReg mask = 0xff; + UIntReg result = 0; + for (int i = 0; i < 4; i++) { + if (a & mask) { + result |= mask; + } + mask <<= 8; + } + return result; + }); +} + +// Byte reverse. +void RiscVRev8(const Instruction *instruction) { + RiscVUnaryOp<RegisterType, UIntReg, UIntReg>(instruction, [](UIntReg a) { + UIntReg result = 0; + for (int i = 0; i < sizeof(UIntReg); i++) { + result <<= 8; + result |= a & 0xff; + a >>= 8; + } + return result; + }); +} + +// Carry-less multiplication (using xor) - low 32 bits. +void RiscVClmul(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a, UIntReg b) { + UIntReg result = 0; + for (int i = 0; i < kXlen; i++) { + result = ((b >> i) & 1) ? result ^ (a << i) : result; + } + return result; + }); +} + +// Carry-less multiplication (using xor) - high 32 bits. +void RiscVClmulh(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a, UIntReg b) { + UIntReg result = 0; + for (int i = 1; i < kXlen; i++) { + result = ((b >> i) & 1) ? result ^ (a >> (kXlen - i)) : result; + } + return result; + }); +} + +// Reverse carry-less multiplication (using xor). +void RiscVClmulr(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a, UIntReg b) { + UIntReg result = 0; + for (int i = 0; i < kXlen - 1; i++) { + result = ((b >> i) & 1) ? result ^ (a >> (kXlen - i - 1)) : result; + } + return result; + }); +} + +// Clear bit. +void RiscVBclr(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, + [](UIntReg a, UIntReg b) { return a & ~(1U << (b & 0x1f)); }); +} + +// Extract bit. +void RiscVBext(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, + [](UIntReg a, UIntReg b) { return (a >> (b & 0x1f)) & 0x1; }); +} + +// Invert bit. +void RiscVBinv(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a, UIntReg b) { return a ^ (1U << (b & 0x1f)); }); +} + +// Set bit. +void RiscVBset(const Instruction *instruction) { + RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( + instruction, [](UIntReg a, UIntReg b) { return a | (1U << (b & 0x1f)); }); +} + +} // namespace RV32 + +} // namespace mpact::sim::riscv
diff --git a/riscv/riscv_bitmanip_instructions.h b/riscv/riscv_bitmanip_instructions.h new file mode 100644 index 0000000..83ea8f9 --- /dev/null +++ b/riscv/riscv_bitmanip_instructions.h
@@ -0,0 +1,95 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_MPACT_RISCV_RISCV_BITMANIP_INSTRUCTIONS_H_ +#define THIRD_PARTY_MPACT_RISCV_RISCV_BITMANIP_INSTRUCTIONS_H_ + +#include "mpact/sim/generic/instruction.h" + +// This file contains the declarations of the semantic functions for the +// bit manipulation instructions in RiscV. + +namespace mpact::sim::riscv { + +using ::mpact::sim::generic::Instruction; + +namespace RV32 { + +// This function takes 3 source operands, rs1, rs2, and a shift amount. It takes +// one destination operand rd. +void RiscVShAdd(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVAndn(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVOrn(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVXnor(const Instruction *instruction); +// This function takes 1 source operands, rs1, and one destination operand rd. +void RiscVClz(const Instruction *instruction); +// This function takes 1 source operands, rs1, and one destination operand rd. +void RiscVCtz(const Instruction *instruction); +// This function takes 1 source operands, rs1, and one destination operand rd. +void RiscVCpop(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVMax(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVMaxu(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVMin(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVMinu(const Instruction *instruction); +// This function takes 1 source operands, rs1, and one destination operand rd. +void RiscVSextB(const Instruction *instruction); +// This function takes 1 source operands, rs1, and one destination operand rd. +void RiscVSextH(const Instruction *instruction); +// This function takes 1 source operands, rs1, and one destination operand rd. +void RiscVZextH(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVrol(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVror(const Instruction *instruction); +// This function takes 1 source operands, rs1, and one destination operand rd. +void RiscVOrcb(const Instruction *instruction); +// This function takes 1 source operands, rs1, and one destination operand rd. +void RiscVRev8(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVClmul(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVClmulh(const Instruction *instruction); +// This function takes 2 source operands, rs1, rs2, and one destination operand +// rd. +void RiscVClmulr(const Instruction *instruction); +// The following functions take 2 source operands, rs1, rs2/imm, and one +// destination operand rd. +void RiscVBclr(const Instruction *instruction); +void RiscVBext(const Instruction *instruction); +void RiscVBinv(const Instruction *instruction); +void RiscVBset(const Instruction *instruction); + +} // namespace RV32 + +} // namespace mpact::sim::riscv + +#endif // THIRD_PARTY_MPACT_RISCV_RISCV_BITMANIP_INSTRUCTIONS_H_
diff --git a/riscv/test/BUILD b/riscv/test/BUILD index d4b9a46..1b968cd 100644 --- a/riscv/test/BUILD +++ b/riscv/test/BUILD
@@ -697,3 +697,19 @@ "@com_google_mpact-sim//mpact/sim/generic:type_helpers", ], ) + +cc_test( + name = "riscv_bitmanip_instructions_test", + size = "small", + srcs = ["riscv_bitmanip_instructions_test.cc"], + deps = [ + "//riscv:riscv_bitmanip_instructions", + "//riscv:riscv_state", + "@com_google_absl//absl/random", + "@com_google_absl//absl/strings", + "@com_google_googletest//:gtest_main", + "@com_google_mpact-sim//mpact/sim/generic:core", + "@com_google_mpact-sim//mpact/sim/generic:instruction", + "@com_google_mpact-sim//mpact/sim/generic:type_helpers", + ], +)
diff --git a/riscv/test/riscv_bitmanip_instructions_test.cc b/riscv/test/riscv_bitmanip_instructions_test.cc new file mode 100644 index 0000000..0490538 --- /dev/null +++ b/riscv/test/riscv_bitmanip_instructions_test.cc
@@ -0,0 +1,550 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "riscv/riscv_bitmanip_instructions.h" + +#include <algorithm> +#include <cstdint> +#include <ios> +#include <limits> +#include <new> +#include <string> +#include <tuple> +#include <vector> + +#include "absl/random/random.h" +#include "absl/strings/string_view.h" +#include "googlemock/include/gmock/gmock.h" +#include "mpact/sim/generic/data_buffer.h" +#include "mpact/sim/generic/immediate_operand.h" +#include "mpact/sim/generic/instruction.h" +#include "riscv/riscv_register.h" +#include "riscv/riscv_state.h" + +// This file contains tests for individual RiscV32 bit manipulation +// instructions. + +namespace { + +using ::mpact::sim::riscv::RV32::RiscVAndn; +using ::mpact::sim::riscv::RV32::RiscVBclr; +using ::mpact::sim::riscv::RV32::RiscVBext; +using ::mpact::sim::riscv::RV32::RiscVBinv; +using ::mpact::sim::riscv::RV32::RiscVBset; +using ::mpact::sim::riscv::RV32::RiscVClmul; +using ::mpact::sim::riscv::RV32::RiscVClmulh; +using ::mpact::sim::riscv::RV32::RiscVClmulr; +using ::mpact::sim::riscv::RV32::RiscVClz; +using ::mpact::sim::riscv::RV32::RiscVCpop; +using ::mpact::sim::riscv::RV32::RiscVCtz; +using ::mpact::sim::riscv::RV32::RiscVMax; +using ::mpact::sim::riscv::RV32::RiscVMaxu; +using ::mpact::sim::riscv::RV32::RiscVMin; +using ::mpact::sim::riscv::RV32::RiscVMinu; +using ::mpact::sim::riscv::RV32::RiscVOrcb; +using ::mpact::sim::riscv::RV32::RiscVOrn; +using ::mpact::sim::riscv::RV32::RiscVRev8; +using ::mpact::sim::riscv::RV32::RiscVrol; +using ::mpact::sim::riscv::RV32::RiscVror; +using ::mpact::sim::riscv::RV32::RiscVSextB; +using ::mpact::sim::riscv::RV32::RiscVSextH; +using ::mpact::sim::riscv::RV32::RiscVShAdd; +using ::mpact::sim::riscv::RV32::RiscVXnor; +using ::mpact::sim::riscv::RV32::RiscVZextH; + +using ::mpact::sim::generic::ImmediateOperand; +using ::mpact::sim::generic::Instruction; +using ::mpact::sim::riscv::RiscVState; +using ::mpact::sim::riscv::RiscVXlen; +using ::mpact::sim::riscv::RV32Register; + +constexpr char kX1[] = "x1"; +constexpr char kX2[] = "x2"; +constexpr char kX3[] = "x3"; + +constexpr uint32_t kInstAddress = 0x2468; +constexpr int32_t kVal1 = 0x1234; +constexpr int32_t kVal2 = -0x5678; + +// The test fixture allocates a machine state object and an instruction object. +// It also contains convenience methods for interacting with the instruction +// object in a more short hand form. +class RV32BitmanipInstructionTest : public testing::Test { + public: + RV32BitmanipInstructionTest() { + state_ = new RiscVState("test", RiscVXlen::RV32, nullptr); + instruction_ = new Instruction(kInstAddress, state_); + instruction_->set_size(4); + } + ~RV32BitmanipInstructionTest() override { + delete state_; + delete instruction_; + } + + // Appends the source and destination operands for the register names + // given in the two vectors. + void AppendRegisterOperands(Instruction *inst, + const std::vector<std::string> &sources, + const std::vector<std::string> &destinations) { + for (auto ®_name : sources) { + auto *reg = state_->GetRegister<RV32Register>(reg_name).first; + inst->AppendSource(reg->CreateSourceOperand()); + } + for (auto ®_name : destinations) { + auto *reg = state_->GetRegister<RV32Register>(reg_name).first; + inst->AppendDestination(reg->CreateDestinationOperand(0)); + } + } + + void AppendRegisterOperands(const std::vector<std::string> &sources, + const std::vector<std::string> &destinations) { + AppendRegisterOperands(instruction_, sources, destinations); + } + + // Appends immediate source operands with the given values. + template <typename T> + void AppendImmediateOperands(const std::vector<T> &values) { + for (auto value : values) { + auto *src = new ImmediateOperand<T>(value); + instruction_->AppendSource(src); + } + } + + // Takes a vector of tuples of register names and values. Fetches each + // named register and sets it to the corresponding value. + template <typename T> + void SetRegisterValues(const std::vector<std::tuple<std::string, T>> values) { + for (auto &[reg_name, value] : values) { + auto *reg = state_->GetRegister<RV32Register>(reg_name).first; + auto *db = state_->db_factory()->Allocate<RV32Register::ValueType>(1); + db->Set<T>(0, value); + reg->SetDataBuffer(db); + db->DecRef(); + } + } + + // Initializes the semantic function of the instruction object. + void SetSemanticFunction(Instruction::SemanticFunction fcn) { + instruction_->set_semantic_function(fcn); + } + + // Returns the value of the named register. + template <typename T> + T GetRegisterValue(absl::string_view reg_name) { + auto *reg = state_->GetRegister<RV32Register>(reg_name).first; + return reg->data_buffer()->Get<T>(0); + } + + RiscVState *state_; + Instruction *instruction_; + absl::BitGen bitgen_; +}; + +TEST_F(RV32BitmanipInstructionTest, RV32Sh1Add) { + AppendRegisterOperands({kX1, kX2}, {kX3}); + AppendImmediateOperands<uint32_t>({1}); + SetSemanticFunction(&RiscVShAdd); + SetRegisterValues<int32_t>({{kX1, kVal1}, {kX2, kVal2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<int32_t>(kX3), kVal2 + (kVal1 << 1)); +} + +TEST_F(RV32BitmanipInstructionTest, RV32Sh2Add) { + AppendRegisterOperands({kX1, kX2}, {kX3}); + AppendImmediateOperands<uint32_t>({2}); + SetSemanticFunction(&RiscVShAdd); + SetRegisterValues<int32_t>({{kX1, kVal1}, {kX2, kVal2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<int32_t>(kX3), kVal2 + (kVal1 << 2)); +} + +TEST_F(RV32BitmanipInstructionTest, RV32Sh3Add) { + AppendRegisterOperands({kX1, kX2}, {kX3}); + AppendImmediateOperands<uint32_t>({3}); + SetSemanticFunction(&RiscVShAdd); + SetRegisterValues<int32_t>({{kX1, kVal1}, {kX2, kVal2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<int32_t>(kX3), kVal2 + (kVal1 << 3)); +} + +TEST_F(RV32BitmanipInstructionTest, RV32Andn) { + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVAndn); + SetRegisterValues<int32_t>({{kX1, kVal1}, {kX2, kVal2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<int32_t>(kX3), kVal1 & ~kVal2); +} + +TEST_F(RV32BitmanipInstructionTest, RV32Orn) { + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVOrn); + SetRegisterValues<int32_t>({{kX1, kVal1}, {kX2, kVal2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<int32_t>(kX3), kVal1 | ~kVal2); +} + +TEST_F(RV32BitmanipInstructionTest, RV32Xnor) { + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVXnor); + SetRegisterValues<int32_t>({{kX1, kVal1}, {kX2, kVal2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<int32_t>(kX3), ~(kVal1 ^ kVal2)); +} + +TEST_F(RV32BitmanipInstructionTest, RV32Clz) { + AppendRegisterOperands({kX1}, {kX3}); + SetSemanticFunction(&RiscVClz); + uint32_t value = 0; + for (int i = 0; i < 33; ++i) { + SetRegisterValues<int32_t>({{kX1, value}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<int32_t>(kX3), 32 - i); + value = (value << 1) | 1; + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Ctz) { + AppendRegisterOperands({kX1}, {kX3}); + SetSemanticFunction(&RiscVCtz); + uint32_t value = 0; + for (int i = 0; i < 33; ++i) { + SetRegisterValues<int32_t>({{kX1, value}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<int32_t>(kX3), 32 - i); + value = (value >> 1) | 0x8000'0000; + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32CPop) { + AppendRegisterOperands({kX1}, {kX3}); + SetSemanticFunction(&RiscVCpop); + uint32_t value = 0; + for (int i = 0; i < 33; ++i) { + SetRegisterValues<int32_t>({{kX1, value}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<int32_t>(kX3), i); + value = (value >> 1) | 0x8000'0000; + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Max) { + using T = int32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVMax); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), std::max(val1, val2)); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Maxu) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVMaxu); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), std::max(val1, val2)); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Min) { + using T = int32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVMin); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), std::min(val1, val2)); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Minu) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVMinu); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), std::min(val1, val2)); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32SextB) { + using T = int32_t; + AppendRegisterOperands({kX1}, {kX3}); + SetSemanticFunction(&RiscVSextB); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), (val1 << 24) >> 24); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32SextH) { + using T = int32_t; + AppendRegisterOperands({kX1}, {kX3}); + SetSemanticFunction(&RiscVSextH); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), (val1 << 16) >> 16); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32ZextH) { + using T = uint32_t; + AppendRegisterOperands({kX1}, {kX3}); + SetSemanticFunction(&RiscVZextH); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), val1 & 0xffff); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Rol) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVrol); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, 0, 32); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), + (val1 << val2) | (val1 >> (32 - val2))); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Ror) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVror); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, 0, 32); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), + (val1 >> val2) | (val1 << (32 - val2))); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Orcb) { + using T = uint32_t; + AppendRegisterOperands({kX1}, {kX3}); + SetSemanticFunction(&RiscVOrcb); + for (int i = 0; i < 100; i++) { + T val1 = 0; + // Randomly set bytes of val1 to random values. + for (int i = 0; i < sizeof(T); ++i) { + bool bit = absl::Bernoulli(bitgen_, 0.5); + if (bit) { + val1 |= (absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<uint8_t>::min(), + std::numeric_limits<uint8_t>::max()) + << (8 * i)); + } + } + SetRegisterValues<uint32_t>({{kX1, val1}}); + instruction_->Execute(nullptr); + T result = 0; + for (int i = 0; i < sizeof(T); ++i) { + result |= (val1 & (0xff << (8 * i))) ? (0xff << (8 * i)) : 0; + } + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), result) + << std::hex << GetRegisterValue<uint32_t>(kX3) << " " << result; + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Rev8) { + using T = uint32_t; + AppendRegisterOperands({kX1}, {kX3}); + SetSemanticFunction(&RiscVRev8); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), + ((val1 & 0xff000000) >> 24) | ((val1 & 0x00ff0000) >> 8) | + ((val1 & 0x0000ff00) << 8) | ((val1 & 0x000000ff) << 24)); + } +} + +// TODO(torerik): clmul, clmulh, clmulr +TEST_F(RV32BitmanipInstructionTest, RV32Clmul) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVClmul); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + uint64_t result = 0; + for (int i = 0; i < sizeof(T) * 8; ++i) { + if (val2 & (1 << i)) result ^= static_cast<uint64_t>(val1 << i); + } + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), result & 0xffff'ffff); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Clmulh) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVClmulh); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + uint64_t result = 0; + for (int i = 0; i < sizeof(T) * 8; ++i) { + if (val2 & (1 << i)) result ^= (static_cast<uint64_t>(val1) << i); + } + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), result >> 32); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Clmulr) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVClmulr); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + uint32_t result = 0; + for (int i = 0; i < sizeof(T) * 8 - 1; ++i) { + if ((val2 >> i) & 1) result ^= (val1 >> (sizeof(T) * 8 - 1 - i)); + } + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), result); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Bclr) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVBclr); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, 0, 31); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), val1 & ~(1 << val2)); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Bext) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVBext); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, 0, 31); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), (val1 >> val2) & 0x1); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Binv) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVBinv); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, 0, 31); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), val1 ^ (1 << val2)); + } +} + +TEST_F(RV32BitmanipInstructionTest, RV32Bset) { + using T = uint32_t; + AppendRegisterOperands({kX1, kX2}, {kX3}); + SetSemanticFunction(&RiscVBset); + for (int i = 0; i < 100; i++) { + T val1 = absl::Uniform(absl::IntervalClosed, bitgen_, + std::numeric_limits<T>::min(), + std::numeric_limits<T>::max()); + T val2 = absl::Uniform(absl::IntervalClosed, bitgen_, 0, 32); + SetRegisterValues<uint32_t>({{kX1, val1}, {kX2, val2}}); + instruction_->Execute(nullptr); + EXPECT_EQ(GetRegisterValue<uint32_t>(kX3), val1 | (1 << val2)); + } +} + +} // namespace