Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:46

0001 //  boost/endian/arithmetic.hpp  -------------------------------------------------------//
0002 
0003 //  (C) Copyright Darin Adler 2000
0004 //  (C) Copyright Beman Dawes 2006, 2009, 2014
0005 //  (C) Copyright Peter Dimov 2019
0006 
0007 //  Distributed under the Boost Software License, Version 1.0.
0008 //  See http://www.boost.org/LICENSE_1_0.txt
0009 
0010 //  See library home page at http://www.boost.org/libs/endian
0011 
0012 //--------------------------------------------------------------------------------------//
0013 
0014 //  Original design developed by Darin Adler based on classes developed by Mark
0015 //  Borgerding. Four original class templates were combined into a single endian
0016 //  class template by Beman Dawes, who also added the unrolled_byte_loops sign
0017 //  partial specialization to correctly extend the sign when cover integer size
0018 //  differs from endian representation size.
0019 
0020 // TODO: When a compiler supporting constexpr becomes available, try possible uses.
0021 
0022 #ifndef BOOST_ENDIAN_ARITHMETIC_HPP
0023 #define BOOST_ENDIAN_ARITHMETIC_HPP
0024 
0025 #if defined(_MSC_VER)
0026 # pragma warning(push)
0027 # pragma warning(disable:4365)  // conversion ... signed/unsigned mismatch
0028 #endif
0029 
0030 #include <boost/endian/buffers.hpp>
0031 #include <boost/endian/detail/static_assert.hpp>
0032 #include <boost/config.hpp>
0033 #include <cstdint>
0034 #include <iosfwd>
0035 #include <climits>
0036 
0037 #if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
0038 # pragma pack(push, 1)
0039 #endif
0040 
0041 # if CHAR_BIT != 8
0042 #   error Platforms with CHAR_BIT != 8 are not supported
0043 # endif
0044 
0045 # ifndef BOOST_ENDIAN_EXPLICIT_CTORS
0046 #   define BOOST_ENDIAN_EXPLICIT_OPT
0047 # else
0048 #   define BOOST_ENDIAN_EXPLICIT_OPT explicit
0049 # endif
0050 
0051 //----------------------------------  synopsis  ----------------------------------------//
0052 
0053 namespace boost
0054 {
0055 namespace endian
0056 {
0057 
0058   template <order Order, class T, std::size_t n_bits,
0059     align Align = align::no>
0060       class endian_arithmetic;
0061 
0062   // big endian signed integer aligned types
0063   typedef endian_arithmetic<order::big, std::int8_t, 8, align::yes>        big_int8_at;
0064   typedef endian_arithmetic<order::big, std::int16_t, 16, align::yes>      big_int16_at;
0065   typedef endian_arithmetic<order::big, std::int32_t, 32, align::yes>      big_int32_at;
0066   typedef endian_arithmetic<order::big, std::int64_t, 64, align::yes>      big_int64_at;
0067 
0068   // big endian unsigned integer aligned types
0069   typedef endian_arithmetic<order::big, std::uint8_t, 8, align::yes>       big_uint8_at;
0070   typedef endian_arithmetic<order::big, std::uint16_t, 16, align::yes>     big_uint16_at;
0071   typedef endian_arithmetic<order::big, std::uint32_t, 32, align::yes>     big_uint32_at;
0072   typedef endian_arithmetic<order::big, std::uint64_t, 64, align::yes>     big_uint64_at;
0073 
0074   // little endian signed integer aligned types
0075   typedef endian_arithmetic<order::little, std::int8_t, 8, align::yes>     little_int8_at;
0076   typedef endian_arithmetic<order::little, std::int16_t, 16, align::yes>   little_int16_at;
0077   typedef endian_arithmetic<order::little, std::int32_t, 32, align::yes>   little_int32_at;
0078   typedef endian_arithmetic<order::little, std::int64_t, 64, align::yes>   little_int64_at;
0079 
0080   // little endian unsigned integer aligned types
0081   typedef endian_arithmetic<order::little, std::uint8_t, 8, align::yes>    little_uint8_at;
0082   typedef endian_arithmetic<order::little, std::uint16_t, 16, align::yes>  little_uint16_at;
0083   typedef endian_arithmetic<order::little, std::uint32_t, 32, align::yes>  little_uint32_at;
0084   typedef endian_arithmetic<order::little, std::uint64_t, 64, align::yes>  little_uint64_at;
0085 
0086   // aligned floating point types
0087   typedef endian_arithmetic<order::big, float, 32, align::yes>        big_float32_at;
0088   typedef endian_arithmetic<order::big, double, 64, align::yes>       big_float64_at;
0089   typedef endian_arithmetic<order::little, float, 32, align::yes>     little_float32_at;
0090   typedef endian_arithmetic<order::little, double, 64, align::yes>    little_float64_at;
0091 
0092   // aligned native endian typedefs are not provided because
0093   // <cstdint> types are superior for this use case
0094 
0095   // big endian signed integer unaligned types
0096   typedef endian_arithmetic<order::big, std::int_least8_t, 8>        big_int8_t;
0097   typedef endian_arithmetic<order::big, std::int_least16_t, 16>      big_int16_t;
0098   typedef endian_arithmetic<order::big, std::int_least32_t, 24>      big_int24_t;
0099   typedef endian_arithmetic<order::big, std::int_least32_t, 32>      big_int32_t;
0100   typedef endian_arithmetic<order::big, std::int_least64_t, 40>      big_int40_t;
0101   typedef endian_arithmetic<order::big, std::int_least64_t, 48>      big_int48_t;
0102   typedef endian_arithmetic<order::big, std::int_least64_t, 56>      big_int56_t;
0103   typedef endian_arithmetic<order::big, std::int_least64_t, 64>      big_int64_t;
0104 
0105   // big endian unsigned integer unaligned types
0106   typedef endian_arithmetic<order::big, std::uint_least8_t, 8>       big_uint8_t;
0107   typedef endian_arithmetic<order::big, std::uint_least16_t, 16>     big_uint16_t;
0108   typedef endian_arithmetic<order::big, std::uint_least32_t, 24>     big_uint24_t;
0109   typedef endian_arithmetic<order::big, std::uint_least32_t, 32>     big_uint32_t;
0110   typedef endian_arithmetic<order::big, std::uint_least64_t, 40>     big_uint40_t;
0111   typedef endian_arithmetic<order::big, std::uint_least64_t, 48>     big_uint48_t;
0112   typedef endian_arithmetic<order::big, std::uint_least64_t, 56>     big_uint56_t;
0113   typedef endian_arithmetic<order::big, std::uint_least64_t, 64>     big_uint64_t;
0114 
0115   // little endian signed integer unaligned types
0116   typedef endian_arithmetic<order::little, std::int_least8_t, 8>     little_int8_t;
0117   typedef endian_arithmetic<order::little, std::int_least16_t, 16>   little_int16_t;
0118   typedef endian_arithmetic<order::little, std::int_least32_t, 24>   little_int24_t;
0119   typedef endian_arithmetic<order::little, std::int_least32_t, 32>   little_int32_t;
0120   typedef endian_arithmetic<order::little, std::int_least64_t, 40>   little_int40_t;
0121   typedef endian_arithmetic<order::little, std::int_least64_t, 48>   little_int48_t;
0122   typedef endian_arithmetic<order::little, std::int_least64_t, 56>   little_int56_t;
0123   typedef endian_arithmetic<order::little, std::int_least64_t, 64>   little_int64_t;
0124 
0125   // little endian unsigned integer unaligned types
0126   typedef endian_arithmetic<order::little, std::uint_least8_t, 8>    little_uint8_t;
0127   typedef endian_arithmetic<order::little, std::uint_least16_t, 16>  little_uint16_t;
0128   typedef endian_arithmetic<order::little, std::uint_least32_t, 24>  little_uint24_t;
0129   typedef endian_arithmetic<order::little, std::uint_least32_t, 32>  little_uint32_t;
0130   typedef endian_arithmetic<order::little, std::uint_least64_t, 40>  little_uint40_t;
0131   typedef endian_arithmetic<order::little, std::uint_least64_t, 48>  little_uint48_t;
0132   typedef endian_arithmetic<order::little, std::uint_least64_t, 56>  little_uint56_t;
0133   typedef endian_arithmetic<order::little, std::uint_least64_t, 64>  little_uint64_t;
0134 
0135   // native endian signed integer unaligned types
0136   typedef endian_arithmetic<order::native, std::int_least8_t, 8>     native_int8_t;
0137   typedef endian_arithmetic<order::native, std::int_least16_t, 16>   native_int16_t;
0138   typedef endian_arithmetic<order::native, std::int_least32_t, 24>   native_int24_t;
0139   typedef endian_arithmetic<order::native, std::int_least32_t, 32>   native_int32_t;
0140   typedef endian_arithmetic<order::native, std::int_least64_t, 40>   native_int40_t;
0141   typedef endian_arithmetic<order::native, std::int_least64_t, 48>   native_int48_t;
0142   typedef endian_arithmetic<order::native, std::int_least64_t, 56>   native_int56_t;
0143   typedef endian_arithmetic<order::native, std::int_least64_t, 64>   native_int64_t;
0144 
0145   // native endian unsigned integer unaligned types
0146   typedef endian_arithmetic<order::native, std::uint_least8_t, 8>    native_uint8_t;
0147   typedef endian_arithmetic<order::native, std::uint_least16_t, 16>  native_uint16_t;
0148   typedef endian_arithmetic<order::native, std::uint_least32_t, 24>  native_uint24_t;
0149   typedef endian_arithmetic<order::native, std::uint_least32_t, 32>  native_uint32_t;
0150   typedef endian_arithmetic<order::native, std::uint_least64_t, 40>  native_uint40_t;
0151   typedef endian_arithmetic<order::native, std::uint_least64_t, 48>  native_uint48_t;
0152   typedef endian_arithmetic<order::native, std::uint_least64_t, 56>  native_uint56_t;
0153   typedef endian_arithmetic<order::native, std::uint_least64_t, 64>  native_uint64_t;
0154 
0155   // unaligned floating point types
0156   typedef endian_arithmetic<order::big, float, 32, align::no>        big_float32_t;
0157   typedef endian_arithmetic<order::big, double, 64, align::no>       big_float64_t;
0158   typedef endian_arithmetic<order::little, float, 32, align::no>     little_float32_t;
0159   typedef endian_arithmetic<order::little, double, 64, align::no>    little_float64_t;
0160   typedef endian_arithmetic<order::native, float, 32, align::no>     native_float32_t;
0161   typedef endian_arithmetic<order::native, double, 64, align::no>    native_float64_t;
0162 
0163 //----------------------------------  end synopsis  ------------------------------------//
0164 
0165 template <order Order, class T, std::size_t n_bits,
0166     align Align>
0167 class endian_arithmetic
0168 {
0169 private:
0170 
0171     typedef endian_buffer<Order, T, n_bits, Align> buffer_type;
0172 
0173 #ifdef BOOST_ENDIAN_NO_CTORS
0174 public:
0175 #else
0176 private:
0177 #endif
0178 
0179     buffer_type buf_;
0180 
0181 public:
0182 
0183     typedef T value_type;
0184 
0185 #ifndef BOOST_ENDIAN_NO_CTORS
0186 
0187     endian_arithmetic() = default;
0188 
0189     BOOST_ENDIAN_EXPLICIT_OPT endian_arithmetic( T val ) BOOST_NOEXCEPT: buf_( val )
0190     {
0191     }
0192 
0193 #endif
0194 
0195     endian_arithmetic& operator=( T val ) BOOST_NOEXCEPT
0196     {
0197         buf_ = val;
0198         return *this;
0199     }
0200 
0201     value_type value() const BOOST_NOEXCEPT
0202     {
0203         return buf_.value();
0204     }
0205 
0206     unsigned char const * data() const BOOST_NOEXCEPT
0207     {
0208         return buf_.data();
0209     }
0210 
0211     unsigned char * data() BOOST_NOEXCEPT
0212     {
0213         return buf_.data();
0214     }
0215 
0216     operator value_type() const BOOST_NOEXCEPT
0217     {
0218         return this->value();
0219     }
0220 
0221     operator buffer_type& () BOOST_NOEXCEPT
0222     {
0223         return buf_;
0224     }
0225 
0226     operator buffer_type const& () BOOST_NOEXCEPT
0227     {
0228         return buf_;
0229     }
0230 
0231     // operators
0232 
0233     T operator+() const BOOST_NOEXCEPT
0234     {
0235         return this->value();
0236     }
0237 
0238     endian_arithmetic& operator+=( T y ) BOOST_NOEXCEPT
0239     {
0240         *this = static_cast<T>( this->value() + y );
0241         return *this;
0242     }
0243 
0244     endian_arithmetic& operator-=( T y ) BOOST_NOEXCEPT
0245     {
0246         *this = static_cast<T>( this->value() - y );
0247         return *this;
0248     }
0249 
0250     endian_arithmetic& operator*=( T y ) BOOST_NOEXCEPT
0251     {
0252         *this = static_cast<T>( this->value() * y );
0253         return *this;
0254     }
0255 
0256     endian_arithmetic& operator/=( T y ) BOOST_NOEXCEPT
0257     {
0258         *this = static_cast<T>( this->value() / y );
0259         return *this;
0260     }
0261 
0262     endian_arithmetic& operator%=( T y ) BOOST_NOEXCEPT
0263     {
0264         *this = static_cast<T>( this->value() % y );
0265         return *this;
0266     }
0267 
0268     endian_arithmetic& operator&=( T y ) BOOST_NOEXCEPT
0269     {
0270         *this = static_cast<T>( this->value() & y );
0271         return *this;
0272     }
0273 
0274     endian_arithmetic& operator|=( T y ) BOOST_NOEXCEPT
0275     {
0276         *this = static_cast<T>( this->value() | y );
0277         return *this;
0278     }
0279 
0280     endian_arithmetic& operator^=( T y ) BOOST_NOEXCEPT
0281     {
0282         *this = static_cast<T>( this->value() ^ y );
0283         return *this;
0284     }
0285 
0286     endian_arithmetic& operator<<=( T y ) BOOST_NOEXCEPT
0287     {
0288         *this = static_cast<T>( this->value() << y );
0289         return *this;
0290     }
0291 
0292     endian_arithmetic& operator>>=( T y ) BOOST_NOEXCEPT
0293     {
0294         *this = static_cast<T>( this->value() >> y );
0295         return *this;
0296     }
0297 
0298     endian_arithmetic& operator++() BOOST_NOEXCEPT
0299     {
0300         *this += 1;
0301         return *this;
0302     }
0303 
0304     endian_arithmetic& operator--() BOOST_NOEXCEPT
0305     {
0306         *this -= 1;
0307         return *this;
0308     }
0309 
0310     endian_arithmetic operator++(int) BOOST_NOEXCEPT
0311     {
0312         endian_arithmetic tmp( *this );
0313         *this += 1;
0314         return tmp;
0315     }
0316 
0317     endian_arithmetic operator--(int) BOOST_NOEXCEPT
0318     {
0319         endian_arithmetic tmp( *this );
0320         *this -= 1;
0321         return tmp;
0322     }
0323 
0324     template<class Ch, class Tr>
0325     friend std::basic_ostream<Ch, Tr>&
0326     operator<<( std::basic_ostream<Ch, Tr>& os, endian_arithmetic const& x )
0327     {
0328         return os << x.value();
0329     }
0330 
0331     template<class Ch, class Tr>
0332     friend std::basic_istream<Ch, Tr>&
0333     operator>>( std::basic_istream<Ch, Tr>& is, endian_arithmetic& x )
0334     {
0335         T i;
0336 
0337         if( is >> i )
0338         {
0339             x = i;
0340         }
0341 
0342         return is;
0343     }
0344 };
0345 
0346 } // namespace endian
0347 } // namespace boost
0348 
0349 #if defined(BOOST_BORLANDC) || defined(BOOST_CODEGEARC)
0350 # pragma pack(pop)
0351 #endif
0352 
0353 #if defined(_MSC_VER)
0354 # pragma warning(pop)
0355 #endif
0356 
0357 #endif // BOOST_ENDIAN_ARITHMETIC_HPP