From 82a16ccc9526df53f467dd5b6da26e08ad2c4784 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 20 Oct 2025 18:37:18 +0300 Subject: [PATCH] Add boost/core/static_assert.hpp; change bit.hpp and cmath.hpp to use it --- include/boost/core/bit.hpp | 44 ++++++++++++++-------------- include/boost/core/cmath.hpp | 6 ++-- include/boost/core/static_assert.hpp | 20 +++++++++++++ 3 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 include/boost/core/static_assert.hpp diff --git a/include/boost/core/bit.hpp b/include/boost/core/bit.hpp index dfc2ab2..92489c6 100644 --- a/include/boost/core/bit.hpp +++ b/include/boost/core/bit.hpp @@ -15,8 +15,8 @@ // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt +#include #include -#include #include #include #include @@ -80,7 +80,7 @@ BOOST_CONSTEXPR To bit_cast( From const & from ) BOOST_NOEXCEPT template To bit_cast( From const & from ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( sizeof(To) == sizeof(From) ); + BOOST_CORE_STATIC_ASSERT( sizeof(To) == sizeof(From) ); To to; std::memcpy( &to, &from, sizeof(To) ); @@ -126,7 +126,7 @@ BOOST_CONSTEXPR inline int countl_impl( boost::ulong_long_type x ) BOOST_NOEXCEP template BOOST_CONSTEXPR int countl_zero( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); return boost::core::detail::countl_impl( x ); } @@ -296,9 +296,9 @@ inline int countl_impl( boost::uint64_t x ) BOOST_NOEXCEPT template BOOST_CXX14_CONSTEXPR int countl_zero( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); - BOOST_STATIC_ASSERT( sizeof(T) == sizeof(boost::uint8_t) || sizeof(T) == sizeof(boost::uint16_t) || sizeof(T) == sizeof(boost::uint32_t) || sizeof(T) == sizeof(boost::uint64_t) ); + BOOST_CORE_STATIC_ASSERT( sizeof(T) == sizeof(boost::uint8_t) || sizeof(T) == sizeof(boost::uint16_t) || sizeof(T) == sizeof(boost::uint32_t) || sizeof(T) == sizeof(boost::uint64_t) ); BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint8_t) ) { @@ -323,7 +323,7 @@ BOOST_CXX14_CONSTEXPR int countl_zero( T x ) BOOST_NOEXCEPT template BOOST_CONSTEXPR int countl_one( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); return boost::core::countl_zero( static_cast( ~x ) ); } @@ -365,7 +365,7 @@ BOOST_CONSTEXPR inline int countr_impl( boost::ulong_long_type x ) BOOST_NOEXCEP template BOOST_CONSTEXPR int countr_zero( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); return boost::core::detail::countr_impl( x ); } @@ -521,9 +521,9 @@ inline int countr_impl( boost::uint64_t x ) BOOST_NOEXCEPT template BOOST_CXX14_CONSTEXPR int countr_zero( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); - BOOST_STATIC_ASSERT( sizeof(T) == sizeof(boost::uint8_t) || sizeof(T) == sizeof(boost::uint16_t) || sizeof(T) == sizeof(boost::uint32_t) || sizeof(T) == sizeof(boost::uint64_t) ); + BOOST_CORE_STATIC_ASSERT( sizeof(T) == sizeof(boost::uint8_t) || sizeof(T) == sizeof(boost::uint16_t) || sizeof(T) == sizeof(boost::uint32_t) || sizeof(T) == sizeof(boost::uint64_t) ); BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint8_t) ) { @@ -548,7 +548,7 @@ BOOST_CXX14_CONSTEXPR int countr_zero( T x ) BOOST_NOEXCEPT template BOOST_CONSTEXPR int countr_one( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); return boost::core::countr_zero( static_cast( ~x ) ); } @@ -598,7 +598,7 @@ BOOST_CORE_POPCOUNT_CONSTEXPR inline int popcount_impl( boost::ulong_long_type x template BOOST_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); return boost::core::detail::popcount_impl( x ); } @@ -631,9 +631,9 @@ BOOST_CXX14_CONSTEXPR inline int popcount_impl( boost::uint64_t x ) BOOST_NOEXCE template BOOST_CXX14_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); - BOOST_STATIC_ASSERT( sizeof(T) <= sizeof(boost::uint64_t) ); + BOOST_CORE_STATIC_ASSERT( sizeof(T) <= sizeof(boost::uint64_t) ); BOOST_IF_CONSTEXPR ( sizeof(T) <= sizeof(boost::uint32_t) ) { @@ -652,7 +652,7 @@ BOOST_CXX14_CONSTEXPR int popcount( T x ) BOOST_NOEXCEPT template BOOST_CXX14_CONSTEXPR T rotl( T x, int s ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); unsigned const mask = std::numeric_limits::digits - 1; return static_cast( x << (static_cast( s ) & mask) | x >> (static_cast( -s ) & mask) ); @@ -661,7 +661,7 @@ BOOST_CXX14_CONSTEXPR T rotl( T x, int s ) BOOST_NOEXCEPT template BOOST_CXX14_CONSTEXPR T rotr( T x, int s ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); unsigned const mask = std::numeric_limits::digits - 1; return static_cast( x >> (static_cast( s ) & mask) | x << (static_cast( -s ) & mask) ); @@ -672,7 +672,7 @@ BOOST_CXX14_CONSTEXPR T rotr( T x, int s ) BOOST_NOEXCEPT template BOOST_CONSTEXPR bool has_single_bit( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); return x != 0 && ( x & ( x - 1 ) ) == 0; } @@ -683,7 +683,7 @@ BOOST_CONSTEXPR bool has_single_bit( T x ) BOOST_NOEXCEPT template BOOST_CONSTEXPR int bit_width( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); return std::numeric_limits::digits - boost::core::countl_zero( x ); } @@ -691,7 +691,7 @@ BOOST_CONSTEXPR int bit_width( T x ) BOOST_NOEXCEPT template BOOST_CONSTEXPR T bit_floor( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); return x == 0? T(0): static_cast( T(1) << ( boost::core::bit_width( x ) - 1 ) ); } @@ -745,9 +745,9 @@ BOOST_CXX14_CONSTEXPR inline boost::uint64_t bit_ceil_impl( boost::uint64_t x ) template BOOST_CXX14_CONSTEXPR T bit_ceil( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer && !std::numeric_limits::is_signed ); - BOOST_STATIC_ASSERT( sizeof(T) <= sizeof(boost::uint64_t) ); + BOOST_CORE_STATIC_ASSERT( sizeof(T) <= sizeof(boost::uint64_t) ); BOOST_IF_CONSTEXPR ( sizeof(T) <= sizeof(boost::uint32_t) ) { @@ -922,9 +922,9 @@ BOOST_CXX14_CONSTEXPR inline boost::uint64_t byteswap_impl( boost::uint64_t x ) template BOOST_CXX14_CONSTEXPR T byteswap( T x ) BOOST_NOEXCEPT { - BOOST_STATIC_ASSERT( std::numeric_limits::is_integer ); + BOOST_CORE_STATIC_ASSERT( std::numeric_limits::is_integer ); - BOOST_STATIC_ASSERT( sizeof(T) == sizeof(boost::uint8_t) || sizeof(T) == sizeof(boost::uint16_t) || sizeof(T) == sizeof(boost::uint32_t) || sizeof(T) == sizeof(boost::uint64_t) ); + BOOST_CORE_STATIC_ASSERT( sizeof(T) == sizeof(boost::uint8_t) || sizeof(T) == sizeof(boost::uint16_t) || sizeof(T) == sizeof(boost::uint32_t) || sizeof(T) == sizeof(boost::uint64_t) ); BOOST_IF_CONSTEXPR ( sizeof(T) == sizeof(boost::uint8_t) ) { diff --git a/include/boost/core/cmath.hpp b/include/boost/core/cmath.hpp index a18c81b..b238bdf 100644 --- a/include/boost/core/cmath.hpp +++ b/include/boost/core/cmath.hpp @@ -20,8 +20,8 @@ #if defined(BOOST_CORE_USE_GENERIC_CMATH) || (!defined(_MSC_VER) && !defined(FP_SUBNORMAL)) +#include #include -#include #include #include @@ -81,7 +81,7 @@ inline bool signbit( float x ) { boost::int32_t y; - BOOST_STATIC_ASSERT( sizeof( x ) == sizeof( y ) ); + BOOST_CORE_STATIC_ASSERT( sizeof( x ) == sizeof( y ) ); std::memcpy( &y, &x, sizeof( y ) ); @@ -92,7 +92,7 @@ inline bool signbit( double x ) { boost::int64_t y; - BOOST_STATIC_ASSERT( sizeof( x ) == sizeof( y ) ); + BOOST_CORE_STATIC_ASSERT( sizeof( x ) == sizeof( y ) ); std::memcpy( &y, &x, sizeof( y ) ); diff --git a/include/boost/core/static_assert.hpp b/include/boost/core/static_assert.hpp new file mode 100644 index 0000000..9ace8aa --- /dev/null +++ b/include/boost/core/static_assert.hpp @@ -0,0 +1,20 @@ +#ifndef BOOST_CORE_STATIC_ASSERT_HPP_INCLUDED +#define BOOST_CORE_STATIC_ASSERT_HPP_INCLUDED + +// Copyright 2025 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#if defined(__cpp_static_assert) && __cpp_static_assert >= 200410L + +#define BOOST_CORE_STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +#else + +#include + +#define BOOST_CORE_STATIC_ASSERT(expr) BOOST_STATIC_ASSERT(expr) + +#endif + +#endif // #ifndef BOOST_CORE_STATIC_ASSERT_HPP_INCLUDED