blob: b32ff1ba699c0d317083846385738c19cc90d228 [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.
#include "src/utils/wrapping.h"
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include "src/utils/gtest.h"
namespace dawn {
namespace {
template <typename T>
consteval bool CheckProperties() {
// SAFETY: Test.
using W = Wrapping<T>;
return (
// W behaves like T.
sizeof(W) == sizeof(T) && alignof(W) == alignof(T) && std::is_trivially_copyable_v<W> &&
// T is implicitly convertible to W.
std::is_convertible_v<T, W> && std::is_constructible_v<W, T> &&
// W is not implicitly convertible to T, but T{W} is constructible.
!std::is_convertible_v<W, T> && std::is_constructible_v<T, W>);
}
static_assert(CheckProperties<uint32_t>());
static_assert(CheckProperties<uint64_t>());
static_assert(CheckProperties<size_t>());
static_assert(CheckProperties<uintptr_t>());
constexpr uint32_t WrapConstexprAdd(uint32_t a, uint32_t b) {
// SAFETY: Test.
return uint32_t{Wrapping<uint32_t>{a} + b};
}
static_assert(WrapConstexprAdd(std::numeric_limits<uint32_t>::max(), 2u) == 1u);
template <std::unsigned_integral T>
void CheckWrappingCompoundAndExit() {
volatile T rawMax = std::numeric_limits<T>::max();
volatile T rawOne = T{1};
volatile T rawTwo = T{2};
// SAFETY: Test.
Wrapping<T> acc{rawMax};
++acc;
EXPECT_EQ(T{acc}, T{0});
--acc;
EXPECT_EQ(T{acc}, std::numeric_limits<T>::max());
acc += rawTwo;
EXPECT_EQ(T{acc}, T{1});
acc -= rawTwo;
EXPECT_EQ(T{acc}, std::numeric_limits<T>::max());
acc *= rawTwo;
EXPECT_EQ(T{acc}, T(std::numeric_limits<T>::max() - T{1}));
acc += rawOne;
EXPECT_EQ(T{acc}, std::numeric_limits<T>::max());
}
template <std::unsigned_integral T>
void CheckWrappingBinaryAndExit() {
volatile T rawMax = std::numeric_limits<T>::max();
volatile T rawZero = T{0};
volatile T rawOne = T{1};
volatile T rawTwo = T{2};
// Wrapping<T> + T
// SAFETY: Test.
T addResult = T{Wrapping<T>{rawMax} + rawOne};
EXPECT_EQ(addResult, T{0});
// T + Wrapping<T>
// SAFETY: Test.
T addResult2 = T{rawOne + Wrapping<T>{rawMax}};
EXPECT_EQ(T{0}, addResult2);
// Wrapping<T> + Wrapping<T>
// SAFETY: Test.
T addResult3 = T{Wrapping<T>{rawMax} + Wrapping<T>{rawOne}};
EXPECT_EQ(T{0}, addResult3);
// SAFETY: Test.
T subResult = T{Wrapping<T>{rawZero} - rawOne};
EXPECT_EQ(subResult, std::numeric_limits<T>::max());
// SAFETY: Test.
T mulResult = T{Wrapping<T>{rawMax} * rawTwo};
EXPECT_EQ(mulResult, std::numeric_limits<T>::max() - T{1});
}
TEST(WrappingTest, WrappingAndExitConversion) {
CheckWrappingCompoundAndExit<uint32_t>();
CheckWrappingCompoundAndExit<uint64_t>();
CheckWrappingCompoundAndExit<size_t>();
CheckWrappingCompoundAndExit<uintptr_t>();
CheckWrappingBinaryAndExit<uint32_t>();
CheckWrappingBinaryAndExit<uint64_t>();
CheckWrappingBinaryAndExit<size_t>();
CheckWrappingBinaryAndExit<uintptr_t>();
// SAFETY: Test.
Wrapping<uint32_t> h{0x12345678u};
h ^= h >> 16;
h *= 0x85ebca6bU;
h <<= 1;
h = ~h;
EXPECT_EQ(uint32_t{h}, 0xf0fef877u);
// SAFETY: Test.
EXPECT_EQ(uint32_t{-Wrapping<uint32_t>{1u}}, std::numeric_limits<uint32_t>::max());
// SAFETY: Test.
EXPECT_EQ(uint32_t{1u + Wrapping<uint32_t>{std::numeric_limits<uint32_t>::max()}}, 0u);
// SAFETY: Test.
EXPECT_TRUE(Wrapping<uint32_t>{1u} < 2u);
// SAFETY: Test.
EXPECT_TRUE(2u > Wrapping<uint32_t>{1u});
// SAFETY: Test.
EXPECT_EQ(Wrapping<uint32_t>{2u}, 2u);
// SAFETY: Test.
EXPECT_EQ(2u, Wrapping<uint32_t>{2u});
uint32_t unwrappedExpr =
// SAFETY: Test.
uint32_t{Wrapping<uint32_t>{std::numeric_limits<uint32_t>::max()} + 2u};
EXPECT_EQ(unwrappedExpr, 1u);
}
} // anonymous namespace
} // namespace dawn