| // 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. | |
| #include "src/utils/saturating.h" | |
| #include <cstddef> | |
| #include <cstdint> | |
| #include <limits> | |
| #include <type_traits> | |
| #include "src/utils/gtest.h" | |
| namespace dawn { | |
| namespace { | |
| template <typename T> | |
| consteval bool CheckProperties() { | |
| using S = Saturating<T>; | |
| return ( | |
| // S behaves like T in layout and trivial copyability. | |
| sizeof(S) == sizeof(T) && alignof(S) == alignof(T) && std::is_trivially_copyable_v<S> && | |
| // T is implicitly convertible to S. | |
| std::is_convertible_v<T, S> && std::is_constructible_v<S, T> && | |
| // S is not implicitly convertible to T, but T{S} is constructible. | |
| !std::is_convertible_v<S, T> && std::is_constructible_v<T, S>); | |
| } | |
| static_assert(CheckProperties<uint32_t>()); | |
| static_assert(CheckProperties<uint64_t>()); | |
| static_assert(CheckProperties<size_t>()); | |
| static_assert(CheckProperties<uintptr_t>()); | |
| constexpr uint32_t SatConstexprAdd(uint32_t a, uint32_t b) { | |
| return uint32_t{Saturating<uint32_t>{a} + b}; | |
| } | |
| constexpr uint32_t SatConstexprSub(uint32_t a, uint32_t b) { | |
| return uint32_t{Saturating<uint32_t>{a} - b}; | |
| } | |
| constexpr uint32_t SatConstexprMul(uint32_t a, uint32_t b) { | |
| return uint32_t{Saturating<uint32_t>{a} * b}; | |
| } | |
| static_assert(SatConstexprAdd(10u, 20u) == 30u); | |
| static_assert(SatConstexprAdd(std::numeric_limits<uint32_t>::max(), 1u) == | |
| std::numeric_limits<uint32_t>::max()); | |
| static_assert(SatConstexprSub(10u, 3u) == 7u); | |
| static_assert(SatConstexprSub(3u, 10u) == 0u); | |
| static_assert(SatConstexprMul(0u, std::numeric_limits<uint32_t>::max()) == 0u); | |
| static_assert(SatConstexprMul(100000u, 100000u) == std::numeric_limits<uint32_t>::max()); | |
| template <std::unsigned_integral T> | |
| void CheckSaturatingCompoundAndExit() { | |
| constexpr T kMax = std::numeric_limits<T>::max(); | |
| volatile T rawMax = kMax; | |
| volatile T rawZero = T{0}; | |
| volatile T rawOne = T{1}; | |
| volatile T rawTwo = T{2}; | |
| volatile T rawTen = T{10}; | |
| Saturating<T> acc{rawMax}; | |
| ++acc; | |
| EXPECT_EQ(T{acc}, kMax); | |
| acc += rawTen; | |
| EXPECT_EQ(T{acc}, kMax); | |
| acc *= rawTwo; | |
| EXPECT_EQ(T{acc}, kMax); | |
| acc -= rawOne; | |
| EXPECT_EQ(T{acc}, kMax - T{1}); | |
| Saturating<T> low{rawZero}; | |
| --low; | |
| EXPECT_EQ(T{low}, T{0}); | |
| low -= rawTen; | |
| EXPECT_EQ(T{low}, T{0}); | |
| low += rawTwo; | |
| EXPECT_EQ(T{low}, T{2}); | |
| low *= rawTen; | |
| EXPECT_EQ(T{low}, T{20}); | |
| low -= Saturating<T>{rawMax}; | |
| EXPECT_EQ(T{low}, T{0}); | |
| } | |
| template <std::unsigned_integral T> | |
| void CheckSaturatingBinaryAndExit() { | |
| constexpr T kMax = std::numeric_limits<T>::max(); | |
| volatile T rawMax = kMax; | |
| volatile T rawZero = T{0}; | |
| volatile T rawOne = T{1}; | |
| volatile T rawTwo = T{2}; | |
| volatile T rawTen = T{10}; | |
| // Saturating<T> op T | |
| EXPECT_EQ(T{Saturating<T>{rawMax} + rawOne}, kMax); | |
| EXPECT_EQ(T{Saturating<T>{rawZero} - rawOne}, T{0}); | |
| EXPECT_EQ(T{Saturating<T>{rawMax} * rawTwo}, kMax); | |
| EXPECT_EQ(T{Saturating<T>{rawMax} * rawZero}, T{0}); | |
| // T op Saturating<T> | |
| EXPECT_EQ(T{rawOne + Saturating<T>{rawMax}}, kMax); | |
| EXPECT_EQ(T{rawZero - Saturating<T>{rawOne}}, T{0}); | |
| EXPECT_EQ(T{rawTwo * Saturating<T>{rawMax}}, kMax); | |
| EXPECT_EQ(T{rawZero * Saturating<T>{rawMax}}, T{0}); | |
| // Saturating<T> op Saturating<T> | |
| EXPECT_EQ(T{Saturating<T>{rawTen} + Saturating<T>{rawTwo}}, T{12}); | |
| EXPECT_EQ(T{Saturating<T>{rawMax} + Saturating<T>{rawMax}}, kMax); | |
| EXPECT_EQ(T{Saturating<T>{rawTen} - Saturating<T>{rawTwo}}, T{8}); | |
| EXPECT_EQ(T{Saturating<T>{rawTwo} - Saturating<T>{rawTen}}, T{0}); | |
| EXPECT_EQ(T{Saturating<T>{rawTen} * Saturating<T>{rawTwo}}, T{20}); | |
| EXPECT_EQ(T{Saturating<T>{rawMax} * Saturating<T>{rawMax}}, kMax); | |
| } | |
| TEST(SaturatingTest, SaturatingAndExitConversion) { | |
| CheckSaturatingCompoundAndExit<uint32_t>(); | |
| CheckSaturatingCompoundAndExit<uint64_t>(); | |
| CheckSaturatingCompoundAndExit<size_t>(); | |
| CheckSaturatingCompoundAndExit<uintptr_t>(); | |
| CheckSaturatingBinaryAndExit<uint32_t>(); | |
| CheckSaturatingBinaryAndExit<uint64_t>(); | |
| CheckSaturatingBinaryAndExit<size_t>(); | |
| CheckSaturatingBinaryAndExit<uintptr_t>(); | |
| EXPECT_TRUE(Saturating<uint32_t>{1u} < 2u); | |
| EXPECT_TRUE(2u > Saturating<uint32_t>{1u}); | |
| EXPECT_EQ(Saturating<uint32_t>{2u}, 2u); | |
| EXPECT_EQ(2u, Saturating<uint32_t>{2u}); | |
| // Chained expression stays inside Saturating<T>. | |
| EXPECT_EQ(uint32_t{Saturating<uint32_t>{100000u} * 100000u + 5u}, | |
| std::numeric_limits<uint32_t>::max()); | |
| } | |
| } // anonymous namespace | |
| } // namespace dawn |