Skip to content

Commit 435dbb6

Browse files
authored
Execution Tests: Update datasets for rhs values for shift operations (microsoft#8179)
Assisted by gh copilot. Fix BitShiftRhs test input sets to avoid shift amounts >= bit width, which is undefined behavior in C++ and can cause test failures as expected values are computed via C++.
1 parent 02686ef commit 435dbb6

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

tools/clang/unittests/HLSLExec/LongVectors.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,17 @@ template <typename T> uint32_t FirstBitLow(T A) {
755755
DEFAULT_OP_2(OpType::And, (A & B));
756756
DEFAULT_OP_2(OpType::Or, (A | B));
757757
DEFAULT_OP_2(OpType::Xor, (A ^ B));
758-
DEFAULT_OP_2(OpType::LeftShift, (A << B));
759-
DEFAULT_OP_2(OpType::RightShift, (A >> B));
758+
759+
// HLSL/DXIL masks shift amounts to the low bits (4 bits for 16-bit, 5 bits for
760+
// 32-bit, 6 bits for 64-bit). We must do the same in C++ to avoid undefined
761+
// behavior when shift amount >= bit width, and to match GPU results.
762+
template <typename T> T MaskShiftAmount(T ShiftAmount) {
763+
constexpr T ShiftMask = static_cast<T>(sizeof(T) * 8 - 1);
764+
return ShiftAmount & ShiftMask;
765+
}
766+
767+
DEFAULT_OP_2(OpType::LeftShift, (A << MaskShiftAmount(B)));
768+
DEFAULT_OP_2(OpType::RightShift, (A >> MaskShiftAmount(B)));
760769
DEFAULT_OP_1(OpType::Saturate, (Saturate(A)));
761770
DEFAULT_OP_1(OpType::ReverseBits, (ReverseBits(A)));
762771

0 commit comments

Comments
 (0)