Commit 69c011ef by Shahbaz Youssefi Committed by Commit Bot

Assert no undefined behavior with left shift in angle::Bit

(uintN_t)1 << M has undefined behavior when M >= N. For example, the following: shift = 64; value = 1 << shift; Gives a value of 1 (instead of the arithmetically expected 0) on (some?) Intel CPUs. Bug: None Change-Id: I5fbb01eff812a62eb778474cec25a25b80052bac Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2269857 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: 's avatarTim Van Patten <timvp@google.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org>
parent e69298d1
...@@ -24,6 +24,9 @@ namespace angle ...@@ -24,6 +24,9 @@ namespace angle
template <typename BitsT, typename ParamT> template <typename BitsT, typename ParamT>
constexpr static BitsT Bit(ParamT x) constexpr static BitsT Bit(ParamT x)
{ {
// It's undefined behavior if the shift size is equal to or larger than the width of the type.
ASSERT(static_cast<size_t>(x) < sizeof(BitsT) * 8);
return (static_cast<BitsT>(1) << static_cast<size_t>(x)); return (static_cast<BitsT>(1) << static_cast<size_t>(x));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment