Skip to content

Commit

Permalink
Add constexprs buffers - refs boostorg#54
Browse files Browse the repository at this point in the history
* allows several operations on buffer types to be constexpr
* ensure old execution path is still used at runtime, to avoid any performance
  regression

Signed-off-by: Julien Blanc <[email protected]>
  • Loading branch information
Julien-Blanc-tgcm committed Sep 9, 2024
1 parent be21291 commit 04333a6
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 100 deletions.
20 changes: 10 additions & 10 deletions include/boost/endian/arithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,53 +184,53 @@ class endian_arithmetic

#ifndef BOOST_ENDIAN_NO_CTORS

endian_arithmetic() = default;
BOOST_ENDIAN_CONSTEXPR endian_arithmetic() = default;

BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic( T val ) BOOST_NOEXCEPT: buf_( val )
BOOST_ENDIAN_EXPLICIT_OPT constexpr endian_arithmetic( T val ) BOOST_NOEXCEPT: buf_( val )
{
}

#endif

endian_arithmetic& operator=( T val ) BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR endian_arithmetic& operator=( T val ) BOOST_NOEXCEPT
{
buf_ = val;
return *this;
}

value_type value() const BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR value_type value() const BOOST_NOEXCEPT
{
return buf_.value();
}

unsigned char const * data() const BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR unsigned char const * data() const BOOST_NOEXCEPT
{
return buf_.data();
}

unsigned char * data() BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR unsigned char * data() BOOST_NOEXCEPT
{
return buf_.data();
}

operator value_type() const BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR operator value_type() const BOOST_NOEXCEPT
{
return this->value();
}

operator buffer_type& () BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR operator buffer_type& () BOOST_NOEXCEPT
{
return buf_;
}

operator buffer_type const& () BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR operator buffer_type const& () BOOST_NOEXCEPT
{
return buf_;
}

// operators

T operator+() const BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR T operator+() const BOOST_NOEXCEPT
{
return this->value();
}
Expand Down
36 changes: 18 additions & 18 deletions include/boost/endian/buffers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,32 +219,32 @@ class endian_buffer<Order, T, n_bits, align::no>

#ifndef BOOST_ENDIAN_NO_CTORS

endian_buffer() = default;
BOOST_ENDIAN_CONSTEXPR endian_buffer() = default;

explicit endian_buffer( T val ) BOOST_NOEXCEPT
explicit BOOST_ENDIAN_CONSTEXPR endian_buffer( T val ) BOOST_NOEXCEPT
{
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
}

#endif

endian_buffer& operator=( T val ) BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR endian_buffer& operator=( T val ) BOOST_NOEXCEPT
{
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
return *this;
}

value_type value() const BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR value_type value() const BOOST_NOEXCEPT
{
return boost::endian::endian_load<T, n_bits / 8, Order>( value_ );
}

unsigned char const * data() const BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR unsigned char const * data() const BOOST_NOEXCEPT
{
return value_;
}

unsigned char * data() BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR unsigned char * data() BOOST_NOEXCEPT
{
return value_;
}
Expand Down Expand Up @@ -274,32 +274,32 @@ class endian_buffer<Order, T, n_bits, align::yes>

#ifndef BOOST_ENDIAN_NO_CTORS

endian_buffer() = default;
BOOST_ENDIAN_CONSTEXPR endian_buffer() = default;

explicit endian_buffer( T val ) BOOST_NOEXCEPT
explicit BOOST_ENDIAN_CONSTEXPR endian_buffer( T val ) BOOST_NOEXCEPT
{
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
}

#endif

endian_buffer& operator=( T val ) BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR endian_buffer& operator=( T val ) BOOST_NOEXCEPT
{
boost::endian::endian_store<T, n_bits / 8, Order>( value_, val );
return *this;
}

value_type value() const BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR value_type value() const BOOST_NOEXCEPT
{
return boost::endian::endian_load<T, n_bits / 8, Order>( value_ );
}

unsigned char const * data() const BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR unsigned char const * data() const BOOST_NOEXCEPT
{
return value_;
}

unsigned char * data() BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR unsigned char * data() BOOST_NOEXCEPT
{
return value_;
}
Expand All @@ -323,31 +323,31 @@ class endian_buffer<order::native, T, n_bits, align::yes>

#ifndef BOOST_ENDIAN_NO_CTORS

endian_buffer() = default;
BOOST_ENDIAN_CONSTEXPR endian_buffer() = default;

explicit endian_buffer( T val ) BOOST_NOEXCEPT: value_( val )
explicit BOOST_ENDIAN_CONSTEXPR endian_buffer( T val ) BOOST_NOEXCEPT: value_( val )
{
}

#endif

endian_buffer& operator=( T val ) BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR endian_buffer& operator=( T val ) BOOST_NOEXCEPT
{
value_ = val;
return *this;
}

value_type value() const BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR value_type value() const BOOST_NOEXCEPT
{
return value_;
}

unsigned char const * data() const BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR unsigned char const * data() const BOOST_NOEXCEPT
{
return reinterpret_cast< unsigned char const* >( &value_ );
}

unsigned char * data() BOOST_NOEXCEPT
BOOST_ENDIAN_CONSTEXPR unsigned char * data() BOOST_NOEXCEPT
{
return reinterpret_cast< unsigned char* >( &value_ );
}
Expand Down
16 changes: 16 additions & 0 deletions include/boost/endian/detail/constexpr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef BOOST_ENDIAN_DETAIL_CONSTEXPR_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_CONSTEXPR_HPP_INCLUDED

// Copyright 2024 Julien Blanc
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#if __cpp_lib_is_constant_evaluated >= 201802L && __cpp_lib_bit_cast >= 201806L
#define BOOST_ENDIAN_CXX20_CONSTEXPR constexpr
#define BOOST_ENDIAN_HAS_CXX20_CONSTEXPR 1
#else
#define BOOST_ENDIAN_CXX20_CONSTEXPR inline
#define BOOST_ENDIAN_HAS_CXX20_CONSTEXPR 0
#endif

#endif // BOOST_ENDIAN_DETAIL_CONSTEXPR_HPP_INCLUDED
Loading

0 comments on commit 04333a6

Please sign in to comment.