blob: 21241ede8a6caebcf231db326af300b629494461 [file]
// Copyright 2026 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef SRC_UTILS_WRAPPING_H_
#define SRC_UTILS_WRAPPING_H_
#include <concepts>
#include <type_traits>
#include "src/utils/compiler.h"
namespace dawn {
// Wrapper class for unsigned integer arithmetic where two's-complement wraparound is semantically
// intended. Similar to Rust's std::num::Wrapping. In standard C++ this does nothing, because
// unsigned integer overflow is always defined to wrap, but -fsanitize=unsigned-integer-overflow
// makes it illegal. This wrapper is simply an ergonomic way of disabling that sanitizer without
// applying it overbroadly.
//
// Restricted to types at least as wide as `unsigned int`, as smaller types confusingly upcast to
// signed `int` for all operations. We could make this wrapper support smaller types by upcasting to
// uint32_t instead, but then this class wouldn't have the same semantics as standard-C++ integers.
//
// Conversions into Wrapping are implicit, while conversions out are explicit.
// Mixed operations like `Wrapping<T> + T` implicitly cast into `Wrapping<T> + Wrapping<T>`.
template <typename T>
requires(std::unsigned_integral<T> && !std::same_as<T, bool> &&
sizeof(T) >= sizeof(unsigned int))
class DAWN_TRIVIAL_ABI Wrapping {
T mValue{};
public:
constexpr Wrapping() = default;
// std::type_identity_t<T> here prevents CTAD (`Wrapping{x}`), so the arithmetic domain T is
// always spelled out explicitly (`Wrapping<T>{x}`)..
explicit(false) constexpr Wrapping(std::type_identity_t<T> value) : mValue(value) {}
explicit constexpr operator T() const { return mValue; }
friend constexpr bool operator==(Wrapping lhs, Wrapping rhs) {
return lhs.mValue == rhs.mValue;
}
friend constexpr auto operator<=>(Wrapping lhs, Wrapping rhs) {
return lhs.mValue <=> rhs.mValue;
}
constexpr Wrapping operator-() const { return Wrapping{T{0}} - *this; }
constexpr Wrapping operator~() const { return Wrapping(~mValue); }
constexpr Wrapping& operator++() { return *this += T{1}; }
constexpr Wrapping operator++(int) {
Wrapping prev = *this;
++(*this);
return prev;
}
constexpr Wrapping& operator--() { return *this -= T{1}; }
constexpr Wrapping operator--(int) {
Wrapping prev = *this;
--(*this);
return prev;
}
// Arithmetic and bitwise operators (`Wrapping op Wrapping`, allows `Wrapping op T` and
// `T op Wrapping` via implicit construction).
DAWN_NO_SANITIZE("unsigned-integer-overflow")
friend constexpr Wrapping operator+(Wrapping lhs, Wrapping rhs) {
return Wrapping(lhs.mValue + rhs.mValue);
}
DAWN_NO_SANITIZE("unsigned-integer-overflow")
friend constexpr Wrapping operator-(Wrapping lhs, Wrapping rhs) {
return Wrapping(lhs.mValue - rhs.mValue);
}
DAWN_NO_SANITIZE("unsigned-integer-overflow")
friend constexpr Wrapping operator*(Wrapping lhs, Wrapping rhs) {
return Wrapping(lhs.mValue * rhs.mValue);
}
friend constexpr Wrapping operator&(Wrapping lhs, Wrapping rhs) {
return Wrapping(lhs.mValue & rhs.mValue);
}
friend constexpr Wrapping operator|(Wrapping lhs, Wrapping rhs) {
return Wrapping(lhs.mValue | rhs.mValue);
}
friend constexpr Wrapping operator^(Wrapping lhs, Wrapping rhs) {
return Wrapping(lhs.mValue ^ rhs.mValue);
}
template <std::integral Shift>
constexpr Wrapping operator<<(Shift rhs) const {
return Wrapping(mValue << rhs);
}
template <std::integral Shift>
constexpr Wrapping operator>>(Shift rhs) const {
return Wrapping(mValue >> rhs);
}
// Compound assignment operators (`op= Wrapping`, allows `op= T` via implicit construction).
constexpr Wrapping& operator+=(Wrapping rhs) { return *this = *this + rhs; }
constexpr Wrapping& operator-=(Wrapping rhs) { return *this = *this - rhs; }
constexpr Wrapping& operator*=(Wrapping rhs) { return *this = *this * rhs; }
constexpr Wrapping& operator&=(Wrapping rhs) { return *this = *this & rhs; }
constexpr Wrapping& operator|=(Wrapping rhs) { return *this = *this | rhs; }
constexpr Wrapping& operator^=(Wrapping rhs) { return *this = *this ^ rhs; }
template <std::integral Shift>
constexpr Wrapping& operator<<=(Shift rhs) {
return *this = *this << rhs;
}
template <std::integral Shift>
constexpr Wrapping& operator>>=(Shift rhs) {
return *this = *this >> rhs;
}
};
} // namespace dawn
#endif // SRC_UTILS_WRAPPING_H_