Skip to content

Commit

Permalink
Remove endian functions from sus::assertions
Browse files Browse the repository at this point in the history
std::endian in C++20 will do fine. Note there's no equivalent interface
in Rust std to port over.
  • Loading branch information
danakj committed Sep 14, 2023
1 parent 90d529c commit 1613e76
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 202 deletions.
2 changes: 0 additions & 2 deletions sus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
49 changes: 0 additions & 49 deletions sus/assertions/endian.h

This file was deleted.

26 changes: 0 additions & 26 deletions sus/assertions/endian_unittest.cc

This file was deleted.

8 changes: 4 additions & 4 deletions sus/num/__private/signed_integer_methods.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions sus/num/__private/signed_integer_methods_impl.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(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;
Expand Down
8 changes: 4 additions & 4 deletions sus/num/__private/unsigned_integer_methods.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions sus/num/__private/unsigned_integer_methods_impl.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(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;
Expand Down
4 changes: 2 additions & 2 deletions sus/num/f32_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8, 4>(0x41_u8, 0x48_u8, 0x00_u8, 0x00_u8)
: sus::Array<u8, 4>(0x00_u8, 0x00_u8, 0x48_u8, 0x41_u8);
EXPECT_EQ(array, expected);
Expand All @@ -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<u8, 4>(0x41_u8, 0x48_u8, 0x00_u8, 0x00_u8));
EXPECT_EQ(value, 12.5_f32);
Expand Down
2 changes: 1 addition & 1 deletion sus/num/f64_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8, 8>(0x40_u8, 0x29_u8, 0x00_u8,
0x00_u8, 0x00_u8, 0x00_u8,
0x00_u8, 0x00_u8));
Expand Down
Loading

0 comments on commit 1613e76

Please sign in to comment.