Removes undefined behaviors in case of rotate amount of 0. PiperOrigin-RevId: 673014095 Change-Id: I4c7a1f453320cd57c043c253758132c77e549ead
diff --git a/riscv/riscv_bitmanip_instructions.cc b/riscv/riscv_bitmanip_instructions.cc index a217259..f7c8468 100644 --- a/riscv/riscv_bitmanip_instructions.cc +++ b/riscv/riscv_bitmanip_instructions.cc
@@ -129,6 +129,7 @@ RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( instruction, [](UIntReg a, UIntReg b) { int shamt = b & 0x1f; + if (shamt == 0) return a; return (a << shamt) | (a >> (kXlen - shamt)); }); } @@ -138,6 +139,7 @@ RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( instruction, [](UIntReg a, UIntReg b) { int shamt = b & 0x1f; + if (shamt == 0) return a; return (a >> shamt) | (a << (kXlen - shamt)); }); } @@ -369,6 +371,7 @@ RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( instruction, [](UIntReg a, UIntReg b) { int shamt = b & 0x3f; + if (shamt == 0) return a; return (a << shamt) | (a >> (kXlen - shamt)); }); } @@ -378,7 +381,7 @@ instruction, [](uint32_t a, uint32_t b) { int shamt = b & 0x1f; // Perform rotation. - auto c = (a << shamt) | (a >> (32 - shamt)); + auto c = shamt == 0 ? a : (a << shamt) | (a >> (32 - shamt)); // Sign extend the result. auto d = absl::bit_cast<int32_t>(c); return static_cast<IntReg>(d); @@ -390,6 +393,7 @@ RiscVBinaryOp<RegisterType, UIntReg, UIntReg>( instruction, [](UIntReg a, UIntReg b) { int shamt = b & 0x3f; + if (shamt == 0) return a; return (a >> shamt) | (a << (kXlen - shamt)); }); } @@ -399,7 +403,7 @@ instruction, [](uint32_t a, uint32_t b) { int shamt = b & 0x1f; // Perform rotation. - auto c = (a >> shamt) | (a << (32 - shamt)); + auto c = shamt == 0 ? a : (a >> shamt) | (a << (32 - shamt)); // Sign extend the result. auto d = absl::bit_cast<int32_t>(c); return static_cast<IntReg>(d);