| // 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_SATURATING_H_ | |
| #define SRC_UTILS_SATURATING_H_ | |
| #include <concepts> | |
| #include <limits> | |
| #include <type_traits> | |
| #include "src/utils/compiler.h" | |
| namespace dawn { | |
| // Wrapper class for unsigned integer arithmetic that saturates at the lower and upper bounds of T | |
| // instead of overflowing or underflowing. Similar to Rust's std::num::Saturating. | |
| // | |
| // Restricted to types at least as wide as `unsigned int`, as smaller types | |
| // confusingly upcast to signed `int` for all operations. | |
| // | |
| // Conversions into Saturating are implicit, while conversions out are explicit. | |
| // Mixed operations like `Saturating<T> + T` implicitly cast into `Saturating<T> + Saturating<T>`. | |
| template <typename T> | |
| requires(std::unsigned_integral<T> && !std::same_as<T, bool> && | |
| sizeof(T) >= sizeof(unsigned int)) | |
| class DAWN_TRIVIAL_ABI Saturating { | |
| static constexpr T kMax = std::numeric_limits<T>::max(); | |
| T mValue{}; | |
| public: | |
| constexpr Saturating() = default; | |
| // std::type_identity_t<T> here prevents CTAD (`Saturating{x}`), so the arithmetic domain T is | |
| // always spelled out explicitly (`Saturating<T>{x}`). | |
| explicit(false) constexpr Saturating(std::type_identity_t<T> value) : mValue(value) {} | |
| explicit constexpr operator T() const { return mValue; } | |
| friend constexpr bool operator==(Saturating lhs, Saturating rhs) { | |
| return lhs.mValue == rhs.mValue; | |
| } | |
| friend constexpr auto operator<=>(Saturating lhs, Saturating rhs) { | |
| return lhs.mValue <=> rhs.mValue; | |
| } | |
| constexpr Saturating& operator++() { return *this += T{1}; } | |
| constexpr Saturating operator++(int) { | |
| Saturating prev = *this; | |
| ++(*this); | |
| return prev; | |
| } | |
| constexpr Saturating& operator--() { return *this -= T{1}; } | |
| constexpr Saturating operator--(int) { | |
| Saturating prev = *this; | |
| --(*this); | |
| return prev; | |
| } | |
| // Arithmetic operators (`Saturating op Saturating`, allows `Saturating op T` and | |
| // `T op Saturating` via implicit construction). | |
| friend constexpr Saturating operator+(Saturating lhs, Saturating rhs) { | |
| return Saturating(lhs.mValue > kMax - rhs.mValue ? kMax : lhs.mValue + rhs.mValue); | |
| } | |
| friend constexpr Saturating operator-(Saturating lhs, Saturating rhs) { | |
| return Saturating(lhs.mValue <= rhs.mValue ? T{0} : lhs.mValue - rhs.mValue); | |
| } | |
| friend constexpr Saturating operator*(Saturating lhs, Saturating rhs) { | |
| return Saturating( | |
| lhs.mValue != 0 && rhs.mValue > kMax / lhs.mValue ? kMax : lhs.mValue * rhs.mValue); | |
| } | |
| // Compound assignment operators (`op= Saturating`, allows `op= T` via implicit construction). | |
| constexpr Saturating& operator+=(Saturating rhs) { return *this = *this + rhs; } | |
| constexpr Saturating& operator-=(Saturating rhs) { return *this = *this - rhs; } | |
| constexpr Saturating& operator*=(Saturating rhs) { return *this = *this * rhs; } | |
| }; | |
| } // namespace dawn | |
| #endif // SRC_UTILS_SATURATING_H_ |