Skip to content

Commit

Permalink
Added tag dispatching to check if type is only one byte wide
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-Iroh committed Oct 11, 2024
1 parent c05c2df commit e9c100c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion include/preview/__bit/byteswap.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ constexpr T byteswap_impl(T n, std::false_type /* is signed */) {
return preview::bit_cast<T>(preview::detail::byteswap_impl<UnsignedT>(preview::bit_cast<UnsignedT>(n), std::true_type{}));
}

template<typename T, std::size_t Size>
constexpr inline T byteswap_check_size(T n, std::integral_constant<std::size_t, Size>) {
return preview::detail::byteswap_impl<T>(n, std::is_unsigned<T>{});
}

template<typename T>
constexpr inline T byteswap_check_size(T n, std::integral_constant<std::size_t, 1>) {
return n;
}

} // namespace detail

template<typename T, std::enable_if_t<std::is_integral<T>::value, int> = 0>
constexpr inline T byteswap(T n) noexcept {
#if PREVIEW_CXX_VERSION >= 23
return std::byteswap<T>(n);
#else
return sizeof(T) == 1 ? n : preview::detail::byteswap_impl<T>(n, std::is_unsigned<T>{});
return preview::detail::byteswap_check_size<T>(n, std::integral_constant<std::size_t, sizeof(T)>{});
#endif
}

Expand Down

0 comments on commit e9c100c

Please sign in to comment.