Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions onnxruntime/core/providers/cpu/math/element_wise_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,19 @@ BitShift<T>::BitShift(const OpKernelInfo& info) : OpKernel(info) {
ORT_THROW("Invalid direction value of '", direction, "'. Valid values are 'LEFT' or 'RIGHT'.");
}

// Shifting by >= the bit width of an unsigned type is undefined behavior in C++.
// On x86, 64-bit shifts mask the shift amount to 6 bits, so shift by 64 acts like shift by 0.
// Guard against this by returning 0 when the shift amount >= the bit width.
template <typename T>
inline T SafeShiftLeft(T value, T shift) {
return shift >= sizeof(T) * 8 ? T{0} : value << shift;
}

template <typename T>
inline T SafeShiftRight(T value, T shift) {
return shift >= sizeof(T) * 8 ? T{0} : value >> shift;
}

template <typename T>
Status BitShift<T>::Compute(OpKernelContext* context) const {
ProcessBroadcastSpanFuncs funcs{
Expand All @@ -1345,11 +1358,11 @@ Status BitShift<T>::Compute(OpKernelContext* context) const {
ptrdiff_t i = 0;
if (shift_left) {
for (const auto& input : input1.array()) {
output[i++] = input0 << input;
output[i++] = SafeShiftLeft(input0, input);
}
} else {
for (const auto& input : input1.array()) {
output[i++] = input0 >> input;
output[i++] = SafeShiftRight(input0, input);
}
}
},
Expand All @@ -1361,11 +1374,11 @@ Status BitShift<T>::Compute(OpKernelContext* context) const {
ptrdiff_t i = 0;
if (shift_left) {
for (const auto& input : input0.array()) {
output[i++] = input << input1;
output[i++] = SafeShiftLeft(input, input1);
}
} else {
for (const auto& input : input0.array()) {
output[i++] = input >> input1;
output[i++] = SafeShiftRight(input, input1);
}
}
},
Expand All @@ -1380,11 +1393,11 @@ Status BitShift<T>::Compute(OpKernelContext* context) const {
auto cur_out = output.begin(), end_out = output.end();
if (shift_left) {
for (; cur0 != end0; ++cur0, ++cur1, ++cur_out) {
*cur_out = *cur0 << *cur1;
*cur_out = SafeShiftLeft(*cur0, *cur1);
}
} else {
for (; cur0 != end0; ++cur0, ++cur1, ++cur_out) {
*cur_out = *cur0 >> *cur1;
*cur_out = SafeShiftRight(*cur0, *cur1);
}
}

Expand Down
56 changes: 56 additions & 0 deletions onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4420,6 +4420,62 @@ TEST(BitShiftOpTest, BroadcastXRight_Uint8) {
test.Run();
}

// Test that shift amounts >= bit width produce 0 (not undefined behavior).
// DirectML EP has the same hardware-level shift masking behavior, so skip these tests for DML.
TEST(BitShiftOpTest, RightShiftByBitWidth_Uint64) {
OpTester test("BitShift", 11);
test.AddAttribute("direction", "RIGHT");
test.AddInput<uint64_t>("X", {4}, {1000, 255, 1, 42});
test.AddInput<uint64_t>("Y", {4}, {64, 64, 64, 64});
test.AddOutput<uint64_t>("Z", {4}, {0, 0, 0, 0});
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
}

TEST(BitShiftOpTest, LeftShiftByBitWidth_Uint64) {
OpTester test("BitShift", 11);
test.AddAttribute("direction", "LEFT");
test.AddInput<uint64_t>("X", {4}, {1000, 255, 1, 42});
test.AddInput<uint64_t>("Y", {4}, {64, 64, 64, 64});
test.AddOutput<uint64_t>("Z", {4}, {0, 0, 0, 0});
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
}

TEST(BitShiftOpTest, RightShiftByBitWidth_Uint32) {
OpTester test("BitShift", 11);
test.AddAttribute("direction", "RIGHT");
test.AddInput<uint32_t>("X", {3}, {16, 4, 1});
test.AddInput<uint32_t>("Y", {3}, {32, 32, 32});
test.AddOutput<uint32_t>("Z", {3}, {0, 0, 0});
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
}

TEST(BitShiftOpTest, RightShiftByMoreThanBitWidth_Uint64) {
OpTester test("BitShift", 11);
test.AddAttribute("direction", "RIGHT");
test.AddInput<uint64_t>("X", {2}, {1000, 42});
test.AddInput<uint64_t>("Y", {2}, {65, 128});
test.AddOutput<uint64_t>("Z", {2}, {0, 0});
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
}

TEST(BitShiftOpTest, ScalarRightShiftByBitWidth_Uint64) {
OpTester test("BitShift", 11);
test.AddAttribute("direction", "RIGHT");
test.AddInput<uint64_t>("X", {1}, {1000});
test.AddInput<uint64_t>("Y", {3}, {64, 65, 128});
test.AddOutput<uint64_t>("Z", {3}, {0, 0, 0});
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
}

TEST(BitShiftOpTest, ScalarLeftShiftByBitWidth_Uint64) {
OpTester test("BitShift", 11);
test.AddAttribute("direction", "LEFT");
test.AddInput<uint64_t>("X", {3}, {1000, 255, 42});
test.AddInput<uint64_t>("Y", {1}, {64});
test.AddOutput<uint64_t>("Z", {3}, {0, 0, 0});
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kDmlExecutionProvider});
}

TEST(MathOpTest, BitwiseAnd) {
OpTester test("BitwiseAnd", 18);
std::vector<int64_t> dims{3};
Expand Down
Loading