File indexing completed on 2025-01-30 09:35:33
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_ENDIAN_CONVERSION_HPP
0009 #define BOOST_ENDIAN_CONVERSION_HPP
0010
0011 #include <boost/endian/detail/endian_reverse.hpp>
0012 #include <boost/endian/detail/endian_load.hpp>
0013 #include <boost/endian/detail/endian_store.hpp>
0014 #include <boost/endian/detail/order.hpp>
0015 #include <boost/endian/detail/static_assert.hpp>
0016 #include <boost/config.hpp>
0017 #include <type_traits>
0018 #include <cstdint>
0019
0020
0021
0022 namespace boost
0023 {
0024 namespace endian
0025 {
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 template <class EndianReversible >
0051 inline BOOST_CONSTEXPR EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT;
0052
0053 template <class EndianReversible >
0054 inline BOOST_CONSTEXPR EndianReversible native_to_big(EndianReversible x) BOOST_NOEXCEPT;
0055
0056
0057
0058 template <class EndianReversible >
0059 inline BOOST_CONSTEXPR EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT;
0060
0061 template <class EndianReversible >
0062 inline BOOST_CONSTEXPR EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT;
0063
0064
0065
0066 template <order From, order To,
0067 class EndianReversible>
0068 inline BOOST_CONSTEXPR EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT;
0069
0070
0071
0072
0073
0074
0075 template <class EndianReversible >
0076 inline BOOST_CONSTEXPR EndianReversible conditional_reverse(EndianReversible from,
0077 order from_order, order to_order)
0078 BOOST_NOEXCEPT;
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117 template <class EndianReversibleInplace>
0118 inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
0119
0120 template <class EndianReversibleInplace>
0121 inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
0122
0123
0124
0125 template <class EndianReversibleInplace>
0126 inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
0127
0128 template <class EndianReversibleInplace>
0129 inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
0130
0131
0132
0133 template <order From, order To,
0134 class EndianReversibleInplace>
0135 inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT;
0136
0137
0138 template <class EndianReversibleInplace>
0139 inline void conditional_reverse_inplace(EndianReversibleInplace& x,
0140 order from_order, order to_order)
0141 BOOST_NOEXCEPT;
0142
0143
0144
0145 template <class EndianReversible>
0146 inline BOOST_CONSTEXPR EndianReversible big_to_native( EndianReversible x ) BOOST_NOEXCEPT
0147 {
0148 return boost::endian::conditional_reverse<order::big, order::native>( x );
0149 }
0150
0151 template <class EndianReversible>
0152 inline BOOST_CONSTEXPR EndianReversible native_to_big( EndianReversible x ) BOOST_NOEXCEPT
0153 {
0154 return boost::endian::conditional_reverse<order::native, order::big>( x );
0155 }
0156
0157 template <class EndianReversible>
0158 inline BOOST_CONSTEXPR EndianReversible little_to_native( EndianReversible x ) BOOST_NOEXCEPT
0159 {
0160 return boost::endian::conditional_reverse<order::little, order::native>( x );
0161 }
0162
0163 template <class EndianReversible>
0164 inline BOOST_CONSTEXPR EndianReversible native_to_little( EndianReversible x ) BOOST_NOEXCEPT
0165 {
0166 return boost::endian::conditional_reverse<order::native, order::little>( x );
0167 }
0168
0169 namespace detail
0170 {
0171
0172 template<class EndianReversible>
0173 inline BOOST_CONSTEXPR EndianReversible conditional_reverse_impl( EndianReversible x, std::true_type ) BOOST_NOEXCEPT
0174 {
0175 return x;
0176 }
0177
0178 template<class EndianReversible>
0179 inline BOOST_CONSTEXPR EndianReversible conditional_reverse_impl( EndianReversible x, std::false_type ) BOOST_NOEXCEPT
0180 {
0181 return endian_reverse( x );
0182 }
0183
0184 }
0185
0186
0187 template <order From, order To, class EndianReversible>
0188 inline BOOST_CONSTEXPR EndianReversible conditional_reverse( EndianReversible x ) BOOST_NOEXCEPT
0189 {
0190 BOOST_ENDIAN_STATIC_ASSERT( std::is_class<EndianReversible>::value || detail::is_endian_reversible<EndianReversible>::value );
0191 return detail::conditional_reverse_impl( x, std::integral_constant<bool, From == To>() );
0192 }
0193
0194
0195 template <class EndianReversible>
0196 inline BOOST_CONSTEXPR EndianReversible conditional_reverse( EndianReversible x,
0197 order from_order, order to_order ) BOOST_NOEXCEPT
0198 {
0199 BOOST_ENDIAN_STATIC_ASSERT( std::is_class<EndianReversible>::value || detail::is_endian_reversible<EndianReversible>::value );
0200 return from_order == to_order? x: endian_reverse( x );
0201 }
0202
0203
0204
0205
0206
0207 template <class EndianReversibleInplace>
0208 inline void big_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
0209 {
0210 boost::endian::conditional_reverse_inplace<order::big, order::native>( x );
0211 }
0212
0213 template <class EndianReversibleInplace>
0214 inline void native_to_big_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
0215 {
0216 boost::endian::conditional_reverse_inplace<order::native, order::big>( x );
0217 }
0218
0219 template <class EndianReversibleInplace>
0220 inline void little_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
0221 {
0222 boost::endian::conditional_reverse_inplace<order::little, order::native>( x );
0223 }
0224
0225 template <class EndianReversibleInplace>
0226 inline void native_to_little_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
0227 {
0228 boost::endian::conditional_reverse_inplace<order::native, order::little>( x );
0229 }
0230
0231 namespace detail
0232 {
0233
0234 template<class EndianReversibleInplace>
0235 inline void conditional_reverse_inplace_impl( EndianReversibleInplace&, std::true_type ) BOOST_NOEXCEPT
0236 {
0237 }
0238
0239 template<class EndianReversibleInplace>
0240 inline void conditional_reverse_inplace_impl( EndianReversibleInplace& x, std::false_type ) BOOST_NOEXCEPT
0241 {
0242 endian_reverse_inplace( x );
0243 }
0244
0245 }
0246
0247
0248 template <order From, order To, class EndianReversibleInplace>
0249 inline void conditional_reverse_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
0250 {
0251 BOOST_ENDIAN_STATIC_ASSERT(
0252 std::is_class<EndianReversibleInplace>::value ||
0253 std::is_array<EndianReversibleInplace>::value ||
0254 detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
0255
0256 detail::conditional_reverse_inplace_impl( x, std::integral_constant<bool, From == To>() );
0257 }
0258
0259
0260 template <class EndianReversibleInplace>
0261 inline void conditional_reverse_inplace( EndianReversibleInplace& x,
0262 order from_order, order to_order ) BOOST_NOEXCEPT
0263 {
0264 BOOST_ENDIAN_STATIC_ASSERT(
0265 std::is_class<EndianReversibleInplace>::value ||
0266 std::is_array<EndianReversibleInplace>::value ||
0267 detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
0268
0269 if( from_order != to_order )
0270 {
0271 endian_reverse_inplace( x );
0272 }
0273 }
0274
0275
0276
0277
0278
0279 inline std::int16_t load_little_s16( unsigned char const * p ) BOOST_NOEXCEPT
0280 {
0281 return boost::endian::endian_load<std::int16_t, 2, order::little>( p );
0282 }
0283
0284 inline std::uint16_t load_little_u16( unsigned char const * p ) BOOST_NOEXCEPT
0285 {
0286 return boost::endian::endian_load<std::uint16_t, 2, order::little>( p );
0287 }
0288
0289 inline std::int16_t load_big_s16( unsigned char const * p ) BOOST_NOEXCEPT
0290 {
0291 return boost::endian::endian_load<std::int16_t, 2, order::big>( p );
0292 }
0293
0294 inline std::uint16_t load_big_u16( unsigned char const * p ) BOOST_NOEXCEPT
0295 {
0296 return boost::endian::endian_load<std::uint16_t, 2, order::big>( p );
0297 }
0298
0299
0300
0301 inline std::int32_t load_little_s24( unsigned char const * p ) BOOST_NOEXCEPT
0302 {
0303 return boost::endian::endian_load<std::int32_t, 3, order::little>( p );
0304 }
0305
0306 inline std::uint32_t load_little_u24( unsigned char const * p ) BOOST_NOEXCEPT
0307 {
0308 return boost::endian::endian_load<std::uint32_t, 3, order::little>( p );
0309 }
0310
0311 inline std::int32_t load_big_s24( unsigned char const * p ) BOOST_NOEXCEPT
0312 {
0313 return boost::endian::endian_load<std::int32_t, 3, order::big>( p );
0314 }
0315
0316 inline std::uint32_t load_big_u24( unsigned char const * p ) BOOST_NOEXCEPT
0317 {
0318 return boost::endian::endian_load<std::uint32_t, 3, order::big>( p );
0319 }
0320
0321
0322
0323 inline std::int32_t load_little_s32( unsigned char const * p ) BOOST_NOEXCEPT
0324 {
0325 return boost::endian::endian_load<std::int32_t, 4, order::little>( p );
0326 }
0327
0328 inline std::uint32_t load_little_u32( unsigned char const * p ) BOOST_NOEXCEPT
0329 {
0330 return boost::endian::endian_load<std::uint32_t, 4, order::little>( p );
0331 }
0332
0333 inline std::int32_t load_big_s32( unsigned char const * p ) BOOST_NOEXCEPT
0334 {
0335 return boost::endian::endian_load<std::int32_t, 4, order::big>( p );
0336 }
0337
0338 inline std::uint32_t load_big_u32( unsigned char const * p ) BOOST_NOEXCEPT
0339 {
0340 return boost::endian::endian_load<std::uint32_t, 4, order::big>( p );
0341 }
0342
0343
0344
0345 inline std::int64_t load_little_s40( unsigned char const * p ) BOOST_NOEXCEPT
0346 {
0347 return boost::endian::endian_load<std::int64_t, 5, order::little>( p );
0348 }
0349
0350 inline std::uint64_t load_little_u40( unsigned char const * p ) BOOST_NOEXCEPT
0351 {
0352 return boost::endian::endian_load<std::uint64_t, 5, order::little>( p );
0353 }
0354
0355 inline std::int64_t load_big_s40( unsigned char const * p ) BOOST_NOEXCEPT
0356 {
0357 return boost::endian::endian_load<std::int64_t, 5, order::big>( p );
0358 }
0359
0360 inline std::uint64_t load_big_u40( unsigned char const * p ) BOOST_NOEXCEPT
0361 {
0362 return boost::endian::endian_load<std::uint64_t, 5, order::big>( p );
0363 }
0364
0365
0366
0367 inline std::int64_t load_little_s48( unsigned char const * p ) BOOST_NOEXCEPT
0368 {
0369 return boost::endian::endian_load<std::int64_t, 6, order::little>( p );
0370 }
0371
0372 inline std::uint64_t load_little_u48( unsigned char const * p ) BOOST_NOEXCEPT
0373 {
0374 return boost::endian::endian_load<std::uint64_t, 6, order::little>( p );
0375 }
0376
0377 inline std::int64_t load_big_s48( unsigned char const * p ) BOOST_NOEXCEPT
0378 {
0379 return boost::endian::endian_load<std::int64_t, 6, order::big>( p );
0380 }
0381
0382 inline std::uint64_t load_big_u48( unsigned char const * p ) BOOST_NOEXCEPT
0383 {
0384 return boost::endian::endian_load<std::uint64_t, 6, order::big>( p );
0385 }
0386
0387
0388
0389 inline std::int64_t load_little_s56( unsigned char const * p ) BOOST_NOEXCEPT
0390 {
0391 return boost::endian::endian_load<std::int64_t, 7, order::little>( p );
0392 }
0393
0394 inline std::uint64_t load_little_u56( unsigned char const * p ) BOOST_NOEXCEPT
0395 {
0396 return boost::endian::endian_load<std::uint64_t, 7, order::little>( p );
0397 }
0398
0399 inline std::int64_t load_big_s56( unsigned char const * p ) BOOST_NOEXCEPT
0400 {
0401 return boost::endian::endian_load<std::int64_t, 7, order::big>( p );
0402 }
0403
0404 inline std::uint64_t load_big_u56( unsigned char const * p ) BOOST_NOEXCEPT
0405 {
0406 return boost::endian::endian_load<std::uint64_t, 7, order::big>( p );
0407 }
0408
0409
0410
0411 inline std::int64_t load_little_s64( unsigned char const * p ) BOOST_NOEXCEPT
0412 {
0413 return boost::endian::endian_load<std::int64_t, 8, order::little>( p );
0414 }
0415
0416 inline std::uint64_t load_little_u64( unsigned char const * p ) BOOST_NOEXCEPT
0417 {
0418 return boost::endian::endian_load<std::uint64_t, 8, order::little>( p );
0419 }
0420
0421 inline std::int64_t load_big_s64( unsigned char const * p ) BOOST_NOEXCEPT
0422 {
0423 return boost::endian::endian_load<std::int64_t, 8, order::big>( p );
0424 }
0425
0426 inline std::uint64_t load_big_u64( unsigned char const * p ) BOOST_NOEXCEPT
0427 {
0428 return boost::endian::endian_load<std::uint64_t, 8, order::big>( p );
0429 }
0430
0431
0432
0433 inline void store_little_s16( unsigned char * p, std::int16_t v )
0434 {
0435 boost::endian::endian_store<std::int16_t, 2, order::little>( p, v );
0436 }
0437
0438 inline void store_little_u16( unsigned char * p, std::uint16_t v )
0439 {
0440 boost::endian::endian_store<std::uint16_t, 2, order::little>( p, v );
0441 }
0442
0443 inline void store_big_s16( unsigned char * p, std::int16_t v )
0444 {
0445 boost::endian::endian_store<std::int16_t, 2, order::big>( p, v );
0446 }
0447
0448 inline void store_big_u16( unsigned char * p, std::uint16_t v )
0449 {
0450 boost::endian::endian_store<std::uint16_t, 2, order::big>( p, v );
0451 }
0452
0453
0454
0455 inline void store_little_s24( unsigned char * p, std::int32_t v )
0456 {
0457 boost::endian::endian_store<std::int32_t, 3, order::little>( p, v );
0458 }
0459
0460 inline void store_little_u24( unsigned char * p, std::uint32_t v )
0461 {
0462 boost::endian::endian_store<std::uint32_t, 3, order::little>( p, v );
0463 }
0464
0465 inline void store_big_s24( unsigned char * p, std::int32_t v )
0466 {
0467 boost::endian::endian_store<std::int32_t, 3, order::big>( p, v );
0468 }
0469
0470 inline void store_big_u24( unsigned char * p, std::uint32_t v )
0471 {
0472 boost::endian::endian_store<std::uint32_t, 3, order::big>( p, v );
0473 }
0474
0475
0476
0477 inline void store_little_s32( unsigned char * p, std::int32_t v )
0478 {
0479 boost::endian::endian_store<std::int32_t, 4, order::little>( p, v );
0480 }
0481
0482 inline void store_little_u32( unsigned char * p, std::uint32_t v )
0483 {
0484 boost::endian::endian_store<std::uint32_t, 4, order::little>( p, v );
0485 }
0486
0487 inline void store_big_s32( unsigned char * p, std::int32_t v )
0488 {
0489 boost::endian::endian_store<std::int32_t, 4, order::big>( p, v );
0490 }
0491
0492 inline void store_big_u32( unsigned char * p, std::uint32_t v )
0493 {
0494 boost::endian::endian_store<std::uint32_t, 4, order::big>( p, v );
0495 }
0496
0497
0498
0499 inline void store_little_s40( unsigned char * p, std::int64_t v )
0500 {
0501 boost::endian::endian_store<std::int64_t, 5, order::little>( p, v );
0502 }
0503
0504 inline void store_little_u40( unsigned char * p, std::uint64_t v )
0505 {
0506 boost::endian::endian_store<std::uint64_t, 5, order::little>( p, v );
0507 }
0508
0509 inline void store_big_s40( unsigned char * p, std::int64_t v )
0510 {
0511 boost::endian::endian_store<std::int64_t, 5, order::big>( p, v );
0512 }
0513
0514 inline void store_big_u40( unsigned char * p, std::uint64_t v )
0515 {
0516 boost::endian::endian_store<std::uint64_t, 5, order::big>( p, v );
0517 }
0518
0519
0520
0521 inline void store_little_s48( unsigned char * p, std::int64_t v )
0522 {
0523 boost::endian::endian_store<std::int64_t, 6, order::little>( p, v );
0524 }
0525
0526 inline void store_little_u48( unsigned char * p, std::uint64_t v )
0527 {
0528 boost::endian::endian_store<std::uint64_t, 6, order::little>( p, v );
0529 }
0530
0531 inline void store_big_s48( unsigned char * p, std::int64_t v )
0532 {
0533 boost::endian::endian_store<std::int64_t, 6, order::big>( p, v );
0534 }
0535
0536 inline void store_big_u48( unsigned char * p, std::uint64_t v )
0537 {
0538 boost::endian::endian_store<std::uint64_t, 6, order::big>( p, v );
0539 }
0540
0541
0542
0543 inline void store_little_s56( unsigned char * p, std::int64_t v )
0544 {
0545 boost::endian::endian_store<std::int64_t, 7, order::little>( p, v );
0546 }
0547
0548 inline void store_little_u56( unsigned char * p, std::uint64_t v )
0549 {
0550 boost::endian::endian_store<std::uint64_t, 7, order::little>( p, v );
0551 }
0552
0553 inline void store_big_s56( unsigned char * p, std::int64_t v )
0554 {
0555 boost::endian::endian_store<std::int64_t, 7, order::big>( p, v );
0556 }
0557
0558 inline void store_big_u56( unsigned char * p, std::uint64_t v )
0559 {
0560 boost::endian::endian_store<std::uint64_t, 7, order::big>( p, v );
0561 }
0562
0563
0564
0565 inline void store_little_s64( unsigned char * p, std::int64_t v )
0566 {
0567 boost::endian::endian_store<std::int64_t, 8, order::little>( p, v );
0568 }
0569
0570 inline void store_little_u64( unsigned char * p, std::uint64_t v )
0571 {
0572 boost::endian::endian_store<std::uint64_t, 8, order::little>( p, v );
0573 }
0574
0575 inline void store_big_s64( unsigned char * p, std::int64_t v )
0576 {
0577 boost::endian::endian_store<std::int64_t, 8, order::big>( p, v );
0578 }
0579
0580 inline void store_big_u64( unsigned char * p, std::uint64_t v )
0581 {
0582 boost::endian::endian_store<std::uint64_t, 8, order::big>( p, v );
0583 }
0584
0585 }
0586 }
0587
0588 #endif