From 1613e766298f8f2f4d0d808c141d47f736269487 Mon Sep 17 00:00:00 2001 From: danakj Date: Wed, 13 Sep 2023 18:48:47 -0400 Subject: [PATCH] Remove endian functions from sus::assertions std::endian in C++20 will do fine. Note there's no equivalent interface in Rust std to port over. --- sus/CMakeLists.txt | 2 - sus/assertions/endian.h | 49 ---------- sus/assertions/endian_unittest.cc | 26 ----- sus/num/__private/signed_integer_methods.inc | 8 +- .../__private/signed_integer_methods_impl.inc | 6 +- .../__private/unsigned_integer_methods.inc | 8 +- .../unsigned_integer_methods_impl.inc | 4 +- sus/num/f32_unittest.cc | 4 +- sus/num/f64_unittest.cc | 2 +- sus/num/i32_unittest.cc | 94 +++++++------------ sus/num/signed_integer.h | 1 - sus/num/u32_unittest.cc | 68 +++++--------- sus/num/unsigned_integer.h | 16 +++- sus/option/option_unittest.cc | 2 +- 14 files changed, 88 insertions(+), 202 deletions(-) delete mode 100644 sus/assertions/endian.h delete mode 100644 sus/assertions/endian_unittest.cc diff --git a/sus/CMakeLists.txt b/sus/CMakeLists.txt index ac6e5b27d..61571c384 100644 --- a/sus/CMakeLists.txt +++ b/sus/CMakeLists.txt @@ -20,7 +20,6 @@ target_link_libraries(subspace target_sources(subspace PUBLIC "assertions/check.h" "assertions/debug_check.h" - "assertions/endian.h" "assertions/panic.h" "assertions/panic.cc" "assertions/unreachable.h" @@ -242,7 +241,6 @@ if(${SUBSPACE_BUILD_TESTS}) add_executable(subspace_unittests "assertions/check_unittest.cc" - "assertions/endian_unittest.cc" "assertions/panic_unittest.cc" "assertions/unreachable_unittest.cc" "boxed/box_unittest.cc" diff --git a/sus/assertions/endian.h b/sus/assertions/endian.h deleted file mode 100644 index 358672c4e..000000000 --- a/sus/assertions/endian.h +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "sus/macros/inline.h" - -namespace sus::assertions { - -constexpr sus_always_inline bool is_big_endian() { -#if _MSC_VER - -#if _M_PPC - return true; -#else - return false; -#endif - -#elif defined(__BYTE_ORDER__) - -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - return true; -#else - return false; -#endif - -#else - -#error "Compiler doesn't specify __BYTE_ORDER__." - -#endif -} - -constexpr sus_always_inline bool is_little_endian() noexcept { - return !is_big_endian(); -} - -} // namespace sus::assertions diff --git a/sus/assertions/endian_unittest.cc b/sus/assertions/endian_unittest.cc deleted file mode 100644 index 4cbb79a1e..000000000 --- a/sus/assertions/endian_unittest.cc +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "sus/assertions/endian.h" - -#include - -namespace { - -static_assert(sus::assertions::is_big_endian() == - (std::endian::native == std::endian::big)); -static_assert(sus::assertions::is_little_endian() == - (std::endian::native == std::endian::little)); - -} // namespace diff --git a/sus/num/__private/signed_integer_methods.inc b/sus/num/__private/signed_integer_methods.inc index 6295e3b8a..51c7ec1ae 100644 --- a/sus/num/__private/signed_integer_methods.inc +++ b/sus/num/__private/signed_integer_methods.inc @@ -1881,7 +1881,7 @@ sus_pure constexpr u32 log(_self base) const& noexcept { /// /// On big endian this is a no-op. On little endian the bytes are swapped. sus_pure static constexpr _self from_be(_self x) noexcept { - if constexpr (::sus::assertions::is_big_endian()) + if constexpr (std::endian::native == std::endian::big) return x; else return x.swap_bytes(); @@ -1891,7 +1891,7 @@ sus_pure static constexpr _self from_be(_self x) noexcept { /// /// On little endian this is a no-op. On big endian the bytes are swapped. sus_pure static constexpr _self from_le(_self x) noexcept { - if constexpr (::sus::assertions::is_little_endian()) + if constexpr (std::endian::native == std::endian::little) return x; else return x.swap_bytes(); @@ -1901,7 +1901,7 @@ sus_pure static constexpr _self from_le(_self x) noexcept { /// /// On big endian this is a no-op. On little endian the bytes are swapped. sus_pure constexpr _self to_be() const& noexcept { - if constexpr (::sus::assertions::is_big_endian()) + if constexpr (std::endian::native == std::endian::big) return *this; else return swap_bytes(); @@ -1911,7 +1911,7 @@ sus_pure constexpr _self to_be() const& noexcept { /// /// On little endian this is a no-op. On big endian the bytes are swapped. sus_pure constexpr _self to_le() const& noexcept { - if constexpr (::sus::assertions::is_little_endian()) + if constexpr (std::endian::native == std::endian::little) return *this; else return swap_bytes(); diff --git a/sus/num/__private/signed_integer_methods_impl.inc b/sus/num/__private/signed_integer_methods_impl.inc index 3de8eadf8..559247114 100644 --- a/sus/num/__private/signed_integer_methods_impl.inc +++ b/sus/num/__private/signed_integer_methods_impl.inc @@ -51,12 +51,12 @@ _self::to_ne_bytes() const& noexcept { auto uval = __private::into_unsigned(primitive_value); for (usize i; i < ::sus::mem::size_of<_primitive>(); i += 1u) { const auto last_byte = static_cast(uval & 0xff); - if (sus::assertions::is_little_endian()) + if (std::endian::native == std::endian::little) bytes[i] = last_byte; else bytes[::sus::mem::size_of<_primitive>() - 1u - i] = last_byte; - /* If _self is one byte, this shift would be UB. But it's also not needed - since the loop will not run again. */ + // If _self is one byte, this shift would be UB. But it's also not needed + // since the loop will not run again. if constexpr (::sus::mem::size_of<_primitive>() > 1u) uval >>= 8u; } return bytes; diff --git a/sus/num/__private/unsigned_integer_methods.inc b/sus/num/__private/unsigned_integer_methods.inc index 07ad268c7..0ccbd9f6c 100644 --- a/sus/num/__private/unsigned_integer_methods.inc +++ b/sus/num/__private/unsigned_integer_methods.inc @@ -1655,7 +1655,7 @@ sus_pure constexpr _self wrapping_next_power_of_two() const& noexcept { /// /// On big endian this is a no-op. On little endian the bytes are swapped. [[nodiscard]] sus_pure static constexpr _self from_be(const _self& x) noexcept { - if constexpr (::sus::assertions::is_big_endian()) + if constexpr (std::endian::native == std::endian::big) return x; else return x.swap_bytes(); @@ -1665,7 +1665,7 @@ sus_pure constexpr _self wrapping_next_power_of_two() const& noexcept { /// /// On little endian this is a no-op. On big endian the bytes are swapped. [[nodiscard]] sus_pure static constexpr _self from_le(const _self& x) noexcept { - if constexpr (::sus::assertions::is_little_endian()) + if constexpr (std::endian::native == std::endian::little) return x; else return x.swap_bytes(); @@ -1675,7 +1675,7 @@ sus_pure constexpr _self wrapping_next_power_of_two() const& noexcept { /// /// On big endian this is a no-op. On little endian the bytes are swapped. sus_pure constexpr _self to_be() const& noexcept { - if constexpr (::sus::assertions::is_big_endian()) + if constexpr (std::endian::native == std::endian::big) return *this; else return swap_bytes(); @@ -1685,7 +1685,7 @@ sus_pure constexpr _self to_be() const& noexcept { /// /// On little endian this is a no-op. On big endian the bytes are swapped. sus_pure constexpr _self to_le() const& noexcept { - if constexpr (::sus::assertions::is_little_endian()) + if constexpr (std::endian::native == std::endian::little) return *this; else return swap_bytes(); diff --git a/sus/num/__private/unsigned_integer_methods_impl.inc b/sus/num/__private/unsigned_integer_methods_impl.inc index dc943b239..ada0dfa8c 100644 --- a/sus/num/__private/unsigned_integer_methods_impl.inc +++ b/sus/num/__private/unsigned_integer_methods_impl.inc @@ -477,12 +477,12 @@ _self::to_ne_bytes() const& noexcept { auto uval = primitive_value; for (usize i; i < ::sus::mem::size_of<_primitive>(); i += 1u) { const auto last_byte = static_cast(uval & 0xff); - if (sus::assertions::is_little_endian()) + if (std::endian::native == std::endian::little) bytes[i] = last_byte; else bytes[::sus::mem::size_of<_primitive>() - 1u - i] = last_byte; // If _self is one byte, this shift would be UB. But it's also not needed - // since the loop will not run again. + // since the loop will not run again. if constexpr (::sus::mem::size_of<_primitive>() > 1u) uval >>= 8u; } return bytes; diff --git a/sus/num/f32_unittest.cc b/sus/num/f32_unittest.cc index 4bc623762..3df9b7428 100644 --- a/sus/num/f32_unittest.cc +++ b/sus/num/f32_unittest.cc @@ -1065,7 +1065,7 @@ TEST(f32, ToLeBytes) { TEST(f32, ToNeBytes) { auto array = (12.5_f32).to_ne_bytes(); - auto expected = sus::assertions::is_big_endian() + auto expected = std::endian::native == std::endian::big ? sus::Array(0x41_u8, 0x48_u8, 0x00_u8, 0x00_u8) : sus::Array(0x00_u8, 0x00_u8, 0x48_u8, 0x41_u8); EXPECT_EQ(array, expected); @@ -1084,7 +1084,7 @@ TEST(f32, FromLeBytes) { } TEST(f32, FromNeBytes) { - if constexpr (sus::assertions::is_big_endian()) { + if constexpr (std::endian::native == std::endian::big) { auto value = f32::from_ne_bytes( sus::Array(0x41_u8, 0x48_u8, 0x00_u8, 0x00_u8)); EXPECT_EQ(value, 12.5_f32); diff --git a/sus/num/f64_unittest.cc b/sus/num/f64_unittest.cc index 5be787296..d79e945a8 100644 --- a/sus/num/f64_unittest.cc +++ b/sus/num/f64_unittest.cc @@ -1059,7 +1059,7 @@ TEST(f64, FromLeBytes) { } TEST(f64, FromNeBytes) { - if constexpr (sus::assertions::is_big_endian()) { + if constexpr (std::endian::native == std::endian::big) { auto value = f64::from_ne_bytes(sus::Array(0x40_u8, 0x29_u8, 0x00_u8, 0x00_u8, 0x00_u8, 0x00_u8, 0x00_u8, 0x00_u8)); diff --git a/sus/num/i32_unittest.cc b/sus/num/i32_unittest.cc index 795f61557..9f56b6874 100644 --- a/sus/num/i32_unittest.cc +++ b/sus/num/i32_unittest.cc @@ -700,12 +700,10 @@ TEST(i32, OverflowingAbs) { (Tuple(1234567_i32, false))); EXPECT_EQ(i32(-1234567).overflowing_abs(), (Tuple(1234567_i32, false))); - EXPECT_EQ(i32::MAX.overflowing_abs(), - (Tuple(i32::MAX, false))); + EXPECT_EQ(i32::MAX.overflowing_abs(), (Tuple(i32::MAX, false))); EXPECT_EQ((i32::MIN + 1_i32).overflowing_abs(), (Tuple(i32::MAX, false))); - EXPECT_EQ((i32::MIN).overflowing_abs(), - (Tuple(i32::MIN, true))); + EXPECT_EQ((i32::MIN).overflowing_abs(), (Tuple(i32::MIN, true))); // lvalue. auto i = -9000_i32; @@ -881,8 +879,7 @@ TEST(i32, OverflowingAdd) { constexpr auto a = (1_i32).overflowing_add(3_i32); EXPECT_EQ(a, (Tuple(4_i32, false))); - EXPECT_EQ((0_i32).overflowing_add(0_i32), - (Tuple(0_i32, false))); + EXPECT_EQ((0_i32).overflowing_add(0_i32), (Tuple(0_i32, false))); EXPECT_EQ(i32::MAX.overflowing_add(1_i32), (Tuple(i32::MIN, true))); @@ -1096,8 +1093,7 @@ TEST(i32, CheckedDiv) { TEST(i32, OverflowingDiv) { [[maybe_unused]] constexpr auto a = (-1_i32).overflowing_div(3_i32); - EXPECT_EQ((0_i32).overflowing_div(123_i32), - (Tuple(0_i32, false))); + EXPECT_EQ((0_i32).overflowing_div(123_i32), (Tuple(0_i32, false))); // ** Signed only. EXPECT_EQ((-2345_i32).overflowing_div(1_i32), @@ -1454,12 +1450,10 @@ TEST(i32, OverflowingNeg) { // static_cast(-123), true))); // ** Signed only. - EXPECT_EQ(i32::MIN.overflowing_neg(), - (Tuple(i32::MIN, true))); + EXPECT_EQ(i32::MIN.overflowing_neg(), (Tuple(i32::MIN, true))); EXPECT_EQ(i32::MAX.overflowing_neg(), (Tuple(i32::MIN + i32(1), false))); - EXPECT_EQ((20_i32).overflowing_neg(), - (Tuple(-20_i32, false))); + EXPECT_EQ((20_i32).overflowing_neg(), (Tuple(-20_i32, false))); } // ** Signed only. @@ -1608,16 +1602,14 @@ TEST(i32, OverflowingRem) { constexpr auto a = (5_i32).overflowing_rem(3_i32); EXPECT_EQ(a, (Tuple(2_i32, false))); - EXPECT_EQ((0_i32).overflowing_rem(123_i32), - (Tuple(0_i32, false))); + EXPECT_EQ((0_i32).overflowing_rem(123_i32), (Tuple(0_i32, false))); EXPECT_EQ((2345_i32).overflowing_rem(4_i32), (Tuple(1_i32, false))); // ** Signed only. EXPECT_EQ((-2345_i32).overflowing_rem(4_i32), (Tuple(-1_i32, false))); - EXPECT_EQ(i32::MIN.overflowing_rem(-1_i32), - (Tuple(0_i32, true))); + EXPECT_EQ(i32::MIN.overflowing_rem(-1_i32), (Tuple(0_i32, true))); } TEST(i32DeathTest, OverflowingRemByZero) { @@ -1753,15 +1745,12 @@ TEST(i32DeathTest, ShlOverflow) { TEST(i32, OverflowingShl) { [[maybe_unused]] constexpr auto a = (5_i32).overflowing_shl(1_u32); - EXPECT_EQ((2_i32).overflowing_shl(1_u32), - (Tuple(4_i32, false))); + EXPECT_EQ((2_i32).overflowing_shl(1_u32), (Tuple(4_i32, false))); // Masks out everything. - EXPECT_EQ((2_i32).overflowing_shl(32_u32), - (Tuple(2_i32, true))); + EXPECT_EQ((2_i32).overflowing_shl(32_u32), (Tuple(2_i32, true))); // Masks out everything but the 1. - EXPECT_EQ((2_i32).overflowing_shl(33_u32), - (Tuple(4_i32, true))); + EXPECT_EQ((2_i32).overflowing_shl(33_u32), (Tuple(4_i32, true))); // ** Signed only. EXPECT_EQ( @@ -1781,9 +1770,9 @@ TEST(i32, CheckedShl) { EXPECT_EQ((2_i32).checked_shl(64_u32), None); // ** Signed only. - EXPECT_EQ((-2_i32).checked_shl(1_u32), - Option( - i32(static_cast(static_cast(-2) << 1)))); + EXPECT_EQ( + (-2_i32).checked_shl(1_u32), + Option(i32(static_cast(static_cast(-2) << 1)))); } TEST(i32, WrappingShl) { @@ -1856,24 +1845,21 @@ TEST(i32, CheckedShr) { EXPECT_EQ((1_i32).checked_shr(64_u32), None); // ** Signed only. - EXPECT_EQ((-2_i32).checked_shr(1_u32), - Option( - i32(static_cast(static_cast(-2) >> 1)))); + EXPECT_EQ( + (-2_i32).checked_shr(1_u32), + Option(i32(static_cast(static_cast(-2) >> 1)))); EXPECT_EQ((-1_i32).checked_shr(32_u32), None); } TEST(i32, OverflowingShr) { [[maybe_unused]] constexpr auto a = (5_i32).overflowing_shr(1_u32); - EXPECT_EQ((4_i32).overflowing_shr(1_u32), - (Tuple(2_i32, false))); + EXPECT_EQ((4_i32).overflowing_shr(1_u32), (Tuple(2_i32, false))); // Masks out everything. - EXPECT_EQ((4_i32).overflowing_shr(32_u32), - (Tuple(4_i32, true))); + EXPECT_EQ((4_i32).overflowing_shr(32_u32), (Tuple(4_i32, true))); // Masks out everything but the 1. - EXPECT_EQ((4_i32).overflowing_shr(33_u32), - (Tuple(2_i32, true))); + EXPECT_EQ((4_i32).overflowing_shr(33_u32), (Tuple(2_i32, true))); // ** Signed only. EXPECT_EQ( @@ -1980,8 +1966,7 @@ TEST(i32, OverflowingSub) { constexpr auto a = (5_i32).overflowing_sub(3_i32); EXPECT_EQ(a, (Tuple(2_i32, false))); - EXPECT_EQ((0_i32).overflowing_sub(0_i32), - (Tuple(0_i32, false))); + EXPECT_EQ((0_i32).overflowing_sub(0_i32), (Tuple(0_i32, false))); EXPECT_EQ((12345_i32).overflowing_sub(12345_i32), (Tuple(0_i32, false))); @@ -2268,14 +2253,11 @@ TEST(i32DeathTest, PowOverflow) { TEST(i32, OverflowingPow) { [[maybe_unused]] constexpr auto a = (2_i32).overflowing_pow(5_u32); - EXPECT_EQ((2_i32).overflowing_pow(5_u32), - (Tuple(32_i32, false))); - EXPECT_EQ((2_i32).overflowing_pow(0_u32), - (Tuple(1_i32, false))); + EXPECT_EQ((2_i32).overflowing_pow(5_u32), (Tuple(32_i32, false))); + EXPECT_EQ((2_i32).overflowing_pow(0_u32), (Tuple(1_i32, false))); EXPECT_EQ((i32::MAX).overflowing_pow(1_u32), (Tuple(i32::MAX, false))); - EXPECT_EQ((i32::MAX).overflowing_pow(2_u32), - (Tuple(1_i32, true))); + EXPECT_EQ((i32::MAX).overflowing_pow(2_u32), (Tuple(1_i32, true))); } TEST(i32, CheckedPow) { @@ -2528,7 +2510,7 @@ TEST(i32, CheckedLog) { } TEST(i32, ToBe) { - if constexpr (sus::assertions::is_little_endian()) { + if constexpr (std::endian::native == std::endian::little) { constexpr auto a = (0x12345678_i32).to_be(); EXPECT_EQ(a, 0x78563412_i32); @@ -2552,7 +2534,7 @@ TEST(i32, ToBe) { } TEST(i32, FromBe) { - if constexpr (sus::assertions::is_little_endian()) { + if constexpr (std::endian::native == std::endian::little) { constexpr auto a = i32::from_be(0x12345678_i32); EXPECT_EQ(a, 0x78563412_i32); @@ -2576,7 +2558,7 @@ TEST(i32, FromBe) { } TEST(i32, ToLe) { - if constexpr (sus::assertions::is_big_endian()) { + if constexpr (std::endian::native == std::endian::big) { constexpr auto a = (0x12345678_i32).to_le(); EXPECT_EQ(a, 0x78563412_i32); @@ -2600,7 +2582,7 @@ TEST(i32, ToLe) { } TEST(i32, FromLe) { - if constexpr (sus::assertions::is_big_endian()) { + if constexpr (std::endian::native == std::endian::big) { constexpr auto a = i32::from_le(0x12345678_i32); EXPECT_EQ(a, 0x78563412_i32); @@ -2646,27 +2628,23 @@ TEST(i32, ToLeBytes) { } TEST(i32, ToNeBytes) { - if constexpr (sus::assertions::is_big_endian()) { + if constexpr (std::endian::native == std::endian::big) { { constexpr auto a = (0x12345678_i32).to_ne_bytes(); - EXPECT_EQ(a, - (sus::Array(0x12_u8, 0x34_u8, 0x56_u8, 0x78_u8))); + EXPECT_EQ(a, (sus::Array(0x12_u8, 0x34_u8, 0x56_u8, 0x78_u8))); } { auto a = (0x12345678_i32).to_ne_bytes(); - EXPECT_EQ(a, - (sus::Array(0x12_u8, 0x34_u8, 0x56_u8, 0x78_u8))); + EXPECT_EQ(a, (sus::Array(0x12_u8, 0x34_u8, 0x56_u8, 0x78_u8))); } } else { { constexpr auto a = (0x12345678_i32).to_ne_bytes(); - EXPECT_EQ(a, - (sus::Array(0x78_u8, 0x56_u8, 0x34_u8, 0x12_u8))); + EXPECT_EQ(a, (sus::Array(0x78_u8, 0x56_u8, 0x34_u8, 0x12_u8))); } { auto a = (0x12345678_i32).to_ne_bytes(); - EXPECT_EQ(a, - (sus::Array(0x78_u8, 0x56_u8, 0x34_u8, 0x12_u8))); + EXPECT_EQ(a, (sus::Array(0x78_u8, 0x56_u8, 0x34_u8, 0x12_u8))); } } } @@ -2677,8 +2655,7 @@ TEST(i32, CheckedAddUnsigned) { EXPECT_EQ(a, Option(4_i32)); EXPECT_EQ((-1_i32).checked_add_unsigned(2u), Option(1_i32)); - EXPECT_EQ((i32::MIN).checked_add_unsigned(u32::MAX), - Option(i32::MAX)); + EXPECT_EQ((i32::MIN).checked_add_unsigned(u32::MAX), Option(i32::MAX)); EXPECT_EQ((i32::MIN + 1_i32).checked_add_unsigned(u32::MAX), None); EXPECT_EQ((i32::MIN + 1_i32).checked_add_unsigned(u32::MAX - 1_u32), Option(i32::MAX)); @@ -2734,8 +2711,7 @@ TEST(i32, CheckedSubUnsigned) { EXPECT_EQ(a, Option(-2_i32)); EXPECT_EQ((-1_i32).checked_sub_unsigned(2u), Option(-3_i32)); - EXPECT_EQ((i32::MAX).checked_sub_unsigned(u32::MAX), - Option(i32::MIN)); + EXPECT_EQ((i32::MAX).checked_sub_unsigned(u32::MAX), Option(i32::MIN)); EXPECT_EQ((i32::MAX - 1_i32).checked_sub_unsigned(u32::MAX), None); EXPECT_EQ((i32::MAX - 1_i32).checked_sub_unsigned(u32::MAX - 1_u32), Option(i32::MIN)); diff --git a/sus/num/signed_integer.h b/sus/num/signed_integer.h index f059c19fa..9e2ddb318 100644 --- a/sus/num/signed_integer.h +++ b/sus/num/signed_integer.h @@ -24,7 +24,6 @@ #include "fmt/format.h" #include "sus/assertions/check.h" -#include "sus/assertions/endian.h" #include "sus/iter/iterator_concept.h" #include "sus/macros/pure.h" #include "sus/marker/unsafe.h" diff --git a/sus/num/u32_unittest.cc b/sus/num/u32_unittest.cc index 8fedf8a53..823f26217 100644 --- a/sus/num/u32_unittest.cc +++ b/sus/num/u32_unittest.cc @@ -729,8 +729,7 @@ TEST(u32, OverflowingAdd) { constexpr auto a = (1_u32).overflowing_add(3_u32); EXPECT_EQ(a, (Tuple(4_u32, false))); - EXPECT_EQ((0_u32).overflowing_add(0_u32), - (Tuple(0_u32, false))); + EXPECT_EQ((0_u32).overflowing_add(0_u32), (Tuple(0_u32, false))); EXPECT_EQ(u32::MAX.overflowing_add(1_u32), (Tuple(u32::MIN, true))); @@ -857,8 +856,7 @@ TEST(u32, OverflowingDiv) { constexpr auto a = (4_u32).overflowing_div(2_u32); EXPECT_EQ(a, (Tuple(2_u32, false))); - EXPECT_EQ((0_u32).overflowing_div(123_u32), - (Tuple(0_u32, false))); + EXPECT_EQ((0_u32).overflowing_div(123_u32), (Tuple(0_u32, false))); } TEST(u32DeathTest, OverflowingDivByZero) { @@ -1068,8 +1066,8 @@ TEST(u32, OverflowingNeg) { // ** Unsigned only. EXPECT_EQ((123_u32).overflowing_neg(), - (Tuple( - static_cast(-123), true))); + (Tuple(static_cast(-123), + true))); } TEST(u32, WrappingNeg) { @@ -1155,8 +1153,7 @@ TEST(u32, OverflowingRem) { constexpr auto a = (5_u32).overflowing_rem(3_u32); EXPECT_EQ(a, (Tuple(2_u32, false))); - EXPECT_EQ((0_u32).overflowing_rem(123_u32), - (Tuple(0_u32, false))); + EXPECT_EQ((0_u32).overflowing_rem(123_u32), (Tuple(0_u32, false))); EXPECT_EQ((2345_u32).overflowing_rem(4_u32), (Tuple(1_u32, false))); } @@ -1260,15 +1257,12 @@ TEST(u32DeathTest, ShlOverflow) { TEST(u32, OverflowingShl) { [[maybe_unused]] constexpr auto a = (5_u32).overflowing_shl(1_u32); - EXPECT_EQ((2_u32).overflowing_shl(1_u32), - (Tuple(4_u32, false))); + EXPECT_EQ((2_u32).overflowing_shl(1_u32), (Tuple(4_u32, false))); // Masks out everything. - EXPECT_EQ((2_u32).overflowing_shl(32_u32), - (Tuple(2_u32, true))); + EXPECT_EQ((2_u32).overflowing_shl(32_u32), (Tuple(2_u32, true))); // Masks out everything but the 1. - EXPECT_EQ((2_u32).overflowing_shl(33_u32), - (Tuple(4_u32, true))); + EXPECT_EQ((2_u32).overflowing_shl(33_u32), (Tuple(4_u32, true))); } TEST(u32, CheckedShl) { @@ -1334,15 +1328,12 @@ TEST(u32, OverflowingShr) { constexpr auto a = (5_u32).overflowing_shr(1_u32); EXPECT_EQ(a, (Tuple(2_u32, false))); - EXPECT_EQ((4_u32).overflowing_shr(1_u32), - (Tuple(2_u32, false))); + EXPECT_EQ((4_u32).overflowing_shr(1_u32), (Tuple(2_u32, false))); // Masks out everything. - EXPECT_EQ((4_u32).overflowing_shr(32_u32), - (Tuple(4_u32, true))); + EXPECT_EQ((4_u32).overflowing_shr(32_u32), (Tuple(4_u32, true))); // Masks out everything but the 1. - EXPECT_EQ((4_u32).overflowing_shr(33_u32), - (Tuple(2_u32, true))); + EXPECT_EQ((4_u32).overflowing_shr(33_u32), (Tuple(2_u32, true))); } TEST(u32, WrappingShr) { @@ -1407,8 +1398,7 @@ TEST(u32, OverflowingSub) { constexpr auto a = (5_u32).overflowing_sub(3_u32); EXPECT_EQ(a, (Tuple(2_u32, false))); - EXPECT_EQ((0_u32).overflowing_sub(0_u32), - (Tuple(0_u32, false))); + EXPECT_EQ((0_u32).overflowing_sub(0_u32), (Tuple(0_u32, false))); EXPECT_EQ((12345_u32).overflowing_sub(12345_u32), (Tuple(0_u32, false))); @@ -1587,14 +1577,11 @@ TEST(u32DeathTest, PowOverflow) { TEST(u32, OverflowingPow) { [[maybe_unused]] constexpr auto a = (2_u32).overflowing_pow(5_u32); - EXPECT_EQ((2_u32).overflowing_pow(5_u32), - (Tuple(32_u32, false))); - EXPECT_EQ((2_u32).overflowing_pow(0_u32), - (Tuple(1_u32, false))); + EXPECT_EQ((2_u32).overflowing_pow(5_u32), (Tuple(32_u32, false))); + EXPECT_EQ((2_u32).overflowing_pow(0_u32), (Tuple(1_u32, false))); EXPECT_EQ((u32::MAX).overflowing_pow(1_u32), (Tuple(u32::MAX, false))); - EXPECT_EQ((u32::MAX).overflowing_pow(2_u32), - (Tuple(1_u32, true))); + EXPECT_EQ((u32::MAX).overflowing_pow(2_u32), (Tuple(1_u32, true))); } TEST(u32, CheckedPow) { @@ -1785,7 +1772,7 @@ TEST(u32, CheckedLog) { } TEST(u32, ToBe) { - if constexpr (sus::assertions::is_little_endian()) { + if constexpr (std::endian::native == std::endian::little) { constexpr auto a = (0x12345678_u32).to_be(); EXPECT_EQ(a, 0x78563412_u32); @@ -1803,7 +1790,7 @@ TEST(u32, ToBe) { } TEST(u32, FromBe) { - if constexpr (sus::assertions::is_little_endian()) { + if constexpr (std::endian::native == std::endian::little) { constexpr auto a = u32::from_be(0x12345678_u32); EXPECT_EQ(a, 0x78563412_u32); @@ -1821,7 +1808,7 @@ TEST(u32, FromBe) { } TEST(u32, ToLe) { - if constexpr (sus::assertions::is_big_endian()) { + if constexpr (std::endian::native == std::endian::big) { constexpr auto a = (0x12345678_u32).to_le(); EXPECT_EQ(a, 0x78563412_u32); @@ -1839,7 +1826,7 @@ TEST(u32, ToLe) { } TEST(u32, FromLe) { - if constexpr (sus::assertions::is_big_endian()) { + if constexpr (std::endian::native == std::endian::big) { constexpr auto a = u32::from_le(0x12345678_u32); EXPECT_EQ(a, 0x78563412_u32); @@ -1879,27 +1866,23 @@ TEST(u32, ToLeBytes) { } TEST(u32, ToNeBytes) { - if constexpr (sus::assertions::is_big_endian()) { + if constexpr (std::endian::native == std::endian::big) { { constexpr auto a = (0x12345678_u32).to_ne_bytes(); - EXPECT_EQ(a, - (sus::Array(0x12_u8, 0x34_u8, 0x56_u8, 0x78_u8))); + EXPECT_EQ(a, (sus::Array(0x12_u8, 0x34_u8, 0x56_u8, 0x78_u8))); } { auto a = (0x12345678_u32).to_ne_bytes(); - EXPECT_EQ(a, - (sus::Array(0x12_u8, 0x34_u8, 0x56_u8, 0x78_u8))); + EXPECT_EQ(a, (sus::Array(0x12_u8, 0x34_u8, 0x56_u8, 0x78_u8))); } } else { { constexpr auto a = (0x12345678_u32).to_ne_bytes(); - EXPECT_EQ(a, - (sus::Array(0x78_u8, 0x56_u8, 0x34_u8, 0x12_u8))); + EXPECT_EQ(a, (sus::Array(0x78_u8, 0x56_u8, 0x34_u8, 0x12_u8))); } { auto a = (0x12345678_u32).to_ne_bytes(); - EXPECT_EQ(a, - (sus::Array(0x78_u8, 0x56_u8, 0x34_u8, 0x12_u8))); + EXPECT_EQ(a, (sus::Array(0x78_u8, 0x56_u8, 0x34_u8, 0x12_u8))); } } } @@ -1996,8 +1979,7 @@ TEST(u32, CheckedNextPowerOfTwo) { EXPECT_EQ((2_u32).checked_next_power_of_two(), Option(2_u32)); EXPECT_EQ((3_u32).checked_next_power_of_two(), Option(4_u32)); EXPECT_EQ((4_u32).checked_next_power_of_two(), Option(4_u32)); - EXPECT_EQ((1000_u32).checked_next_power_of_two(), - Option(1024_u32)); + EXPECT_EQ((1000_u32).checked_next_power_of_two(), Option(1024_u32)); EXPECT_EQ(u32::MAX.checked_next_power_of_two(), None); } diff --git a/sus/num/unsigned_integer.h b/sus/num/unsigned_integer.h index 7de4b698a..4a71aecfb 100644 --- a/sus/num/unsigned_integer.h +++ b/sus/num/unsigned_integer.h @@ -19,12 +19,12 @@ #include #include +#include #include #include // TODO: remove this but we need to hash things > size_t. #include "fmt/format.h" #include "sus/assertions/check.h" -#include "sus/assertions/endian.h" #include "sus/iter/iterator_concept.h" #include "sus/lib/__private/forward_decl.h" #include "sus/macros/__private/compiler_bugs.h" @@ -130,13 +130,15 @@ struct [[sus_trivial_abi]] usize final { #define _self usize #define _pointer false #define _pointer_sized -#define _primitive ::sus::num::__private::ptr_type<::sus::mem::size_of()>::unsigned_type +#define _primitive \ + ::sus::num::__private::ptr_type<::sus::mem::size_of()>::unsigned_type #define _signed isize #include "sus/num/__private/unsigned_integer_methods.inc" }; #define _self usize #define _pointer false -#define _primitive ::sus::num::__private::ptr_type<::sus::mem::size_of()>::unsigned_type +#define _primitive \ + ::sus::num::__private::ptr_type<::sus::mem::size_of()>::unsigned_type #include "sus/num/__private/unsigned_integer_consts.inc" /// A pointer-sized unsigned integer. @@ -167,12 +169,16 @@ struct [[sus_trivial_abi]] uptr final { #define _self uptr #define _pointer true #define _pointer_sized ::sus::num::__private::ptr_type<>::pointer_sized_type -#define _primitive ::sus::num::__private::ptr_type<::sus::mem::size_of()>::unsigned_type +#define _primitive \ + ::sus::num::__private::ptr_type< \ + ::sus::mem::size_of()>::unsigned_type #include "sus/num/__private/unsigned_integer_methods.inc" }; #define _self uptr #define _pointer true -#define _primitive ::sus::num::__private::ptr_type<::sus::mem::size_of()>::unsigned_type +#define _primitive \ + ::sus::num::__private::ptr_type< \ + ::sus::mem::size_of()>::unsigned_type #include "sus/num/__private/unsigned_integer_consts.inc" /// Satisfies the [`Add`]($sus::num::Add) concept for pointers diff --git a/sus/option/option_unittest.cc b/sus/option/option_unittest.cc index 8c4e529fe..43a6d737a 100644 --- a/sus/option/option_unittest.cc +++ b/sus/option/option_unittest.cc @@ -15,10 +15,10 @@ #include "sus/option/option.h" #include +#include #include "fmt/std.h" #include "googletest/include/gtest/gtest.h" -#include "sus/assertions/endian.h" #include "sus/collections/array.h" #include "sus/iter/from_iterator.h" #include "sus/iter/iterator.h"