Import Tint changes from Dawn
Changes:
- 875d116a873624816f02c3fdfc1b6b60d82efcd1 tint: fix signed overflow in const eval modulo by Antonio Maiorano <amaiorano@google.com>
GitOrigin-RevId: 875d116a873624816f02c3fdfc1b6b60d82efcd1
Change-Id: I7d48687fa7471c3203c4c709ee3e6457caaf4cf0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/112862
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/number.h b/src/tint/number.h
index 9b7ce43..975ce79 100644
--- a/src/tint/number.h
+++ b/src/tint/number.h
@@ -547,7 +547,12 @@
/// @returns the remainder of e1 / e2
template <typename T>
inline T Mod(T e1, T e2) {
- return e1 - e2 * static_cast<T>(std::trunc(e1 / e2));
+ if constexpr (IsIntegral<T>) {
+ return e1 % e2;
+
+ } else {
+ return e1 - e2 * std::trunc(e1 / e2);
+ }
}
} // namespace detail
diff --git a/src/tint/number_test.cc b/src/tint/number_test.cc
index 040fd76..a795840 100644
--- a/src/tint/number_test.cc
+++ b/src/tint/number_test.cc
@@ -704,6 +704,15 @@
{AInt(2), AInt(10), AInt(4)},
{AInt(0), AInt::Highest(), AInt::Highest()},
{AInt(0), AInt::Lowest(), AInt::Lowest()},
+ {AInt(2), AInt::Highest(), AInt(5)},
+ {AInt(1), AInt::Highest(), AInt(6)},
+ {AInt(0), AInt::Highest(), AInt(7)},
+ {-AInt{1}, -AInt{10}, AInt{3}},
+ {-AInt{2}, -AInt{10}, AInt{4}},
+ {AInt{1}, AInt{10}, -AInt{3}},
+ {AInt{2}, AInt{10}, -AInt{4}},
+ {-AInt{1}, -AInt{10}, -AInt{3}},
+ {-AInt{2}, -AInt{10}, -AInt{4}},
{OVERFLOW, AInt::Highest(), AInt(0)},
{OVERFLOW, AInt::Lowest(), AInt(0)},
////////////////////////////////////////////////////////////////////////
@@ -725,11 +734,22 @@
template <typename T>
std::vector<BinaryCheckedCase_Float> CheckedModTest_FloatCases() {
return {
- {T(0.5), T(10.5), T(1)}, {T(0.5), T(10.5), T(2)},
- {T(1.5), T(10.5), T(3)}, {T(2.5), T(10.5), T(4)},
- {T(0.5), T(10.5), T(5)}, {T(0), T::Highest(), T::Highest()},
- {T(0), T::Lowest(), T::Lowest()}, {Overflow<T>, T(123), T(0)},
- {Overflow<T>, T(123), T(-0)}, {Overflow<T>, T(-123), T(0)},
+ {T(0.5), T(10.5), T(1)}, //
+ {T(0.5), T(10.5), T(2)}, //
+ {T(1.5), T(10.5), T(3)}, //
+ {T(2.5), T(10.5), T(4)}, //
+ {T(0.5), T(10.5), T(5)}, //
+ {T(0), T::Highest(), T::Highest()}, //
+ {T(0), T::Lowest(), T::Lowest()}, //
+ {-T{1}, -T{10}, T{3}}, //
+ {-T{2}, -T{10}, T{4}}, //
+ {T{1}, T{10}, -T{3}}, //
+ {T{2}, T{10}, -T{4}}, //
+ {-T{1}, -T{10}, -T{3}}, //
+ {-T{2}, -T{10}, -T{4}}, //
+ {Overflow<T>, T(123), T(0)}, //
+ {Overflow<T>, T(123), T(-0)}, //
+ {Overflow<T>, T(-123), T(0)}, //
{Overflow<T>, T(-123), T(-0)},
};
}