File indexing completed on 2025-12-13 10:07:20
0001 #ifndef BOOST_UUID_DETAIL_ENDIAN_INCLUDED
0002 #define BOOST_UUID_DETAIL_ENDIAN_INCLUDED
0003
0004
0005
0006
0007
0008 #include <cstring>
0009 #include <cstdint>
0010
0011 #if defined(_MSC_VER) && !defined(__clang__)
0012 # include <intrin.h>
0013 #endif
0014
0015 namespace boost {
0016 namespace uuids {
0017 namespace detail {
0018
0019
0020
0021 #if defined(__BYTE_ORDER__)
0022
0023 #define BOOST_UUID_BYTE_ORDER __BYTE_ORDER__
0024 #define BOOST_UUID_ORDER_LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
0025 #define BOOST_UUID_ORDER_BIG_ENDIAN __ORDER_BIG_ENDIAN__
0026
0027 #elif defined(__LITTLE_ENDIAN__) || defined(_MSC_VER) || defined(__i386__) || defined(__x86_64__)
0028
0029 #define BOOST_UUID_BYTE_ORDER 1234
0030 #define BOOST_UUID_ORDER_LITTLE_ENDIAN 1234
0031 #define BOOST_UUID_ORDER_BIG_ENDIAN 4321
0032
0033 #elif defined(__BIG_ENDIAN__)
0034
0035 #define BOOST_UUID_BYTE_ORDER 4321
0036 #define BOOST_UUID_ORDER_LITTLE_ENDIAN 1234
0037 #define BOOST_UUID_ORDER_BIG_ENDIAN 4321
0038
0039 #else
0040
0041 # error Unrecognized platform
0042
0043 #endif
0044
0045
0046
0047 #if defined(__GNUC__) || defined(__clang__)
0048
0049 inline std::uint16_t byteswap( std::uint16_t x ) noexcept
0050 {
0051 return __builtin_bswap16( x );
0052 }
0053
0054 inline std::uint32_t byteswap( std::uint32_t x ) noexcept
0055 {
0056 return __builtin_bswap32( x );
0057 }
0058
0059 inline std::uint64_t byteswap( std::uint64_t x ) noexcept
0060 {
0061 return __builtin_bswap64( x );
0062 }
0063
0064 #elif defined(_MSC_VER)
0065
0066 inline std::uint16_t byteswap( std::uint16_t x ) noexcept
0067 {
0068 return _byteswap_ushort( x );
0069 }
0070
0071 inline std::uint32_t byteswap( std::uint32_t x ) noexcept
0072 {
0073 return _byteswap_ulong( x );
0074 }
0075
0076 inline std::uint64_t byteswap( std::uint64_t x ) noexcept
0077 {
0078 return _byteswap_uint64( x );
0079 }
0080
0081 #else
0082
0083 inline std::uint16_t byteswap( std::uint16_t x ) noexcept
0084 {
0085 return static_cast<std::uint16_t>( x << 8 | x >> 8 );
0086 }
0087
0088 inline std::uint32_t byteswap( std::uint32_t x ) noexcept
0089 {
0090 std::uint32_t step16 = x << 16 | x >> 16;
0091 return ((step16 << 8) & 0xff00ff00) | ((step16 >> 8) & 0x00ff00ff);
0092 }
0093
0094 inline std::uint64_t byteswap( std::uint64_t x ) noexcept
0095 {
0096 std::uint64_t step32 = x << 32 | x >> 32;
0097 std::uint64_t step16 = (step32 & 0x0000FFFF0000FFFFULL) << 16 | (step32 & 0xFFFF0000FFFF0000ULL) >> 16;
0098 return (step16 & 0x00FF00FF00FF00FFULL) << 8 | (step16 & 0xFF00FF00FF00FF00ULL) >> 8;
0099 }
0100
0101 #endif
0102
0103 #if defined(__SIZEOF_INT128__)
0104
0105 inline __uint128_t byteswap( __uint128_t x ) noexcept
0106 {
0107 return ( static_cast<__uint128_t>( detail::byteswap( static_cast<std::uint64_t>( x ) ) ) << 64 ) | detail::byteswap( static_cast<std::uint64_t>( x >> 64 ) );
0108 }
0109
0110 #endif
0111
0112
0113
0114 inline std::uint16_t load_native_u16( void const* p ) noexcept
0115 {
0116 std::uint16_t tmp;
0117 std::memcpy( &tmp, p, sizeof( tmp ) );
0118 return tmp;
0119 }
0120
0121 inline std::uint16_t load_little_u16( void const* p ) noexcept
0122 {
0123 std::uint16_t tmp;
0124 std::memcpy( &tmp, p, sizeof( tmp ) );
0125
0126 #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_LITTLE_ENDIAN
0127
0128 return tmp;
0129
0130 #else
0131
0132 return detail::byteswap( tmp );
0133
0134 #endif
0135 }
0136
0137 inline std::uint16_t load_big_u16( void const* p ) noexcept
0138 {
0139 std::uint16_t tmp;
0140 std::memcpy( &tmp, p, sizeof( tmp ) );
0141
0142 #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_BIG_ENDIAN
0143
0144 return tmp;
0145
0146 #else
0147
0148 return detail::byteswap( tmp );
0149
0150 #endif
0151 }
0152
0153
0154
0155 inline std::uint32_t load_native_u32( void const* p ) noexcept
0156 {
0157 std::uint32_t tmp;
0158 std::memcpy( &tmp, p, sizeof( tmp ) );
0159 return tmp;
0160 }
0161
0162 inline std::uint32_t load_little_u32( void const* p ) noexcept
0163 {
0164 std::uint32_t tmp;
0165 std::memcpy( &tmp, p, sizeof( tmp ) );
0166
0167 #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_LITTLE_ENDIAN
0168
0169 return tmp;
0170
0171 #else
0172
0173 return detail::byteswap( tmp );
0174
0175 #endif
0176 }
0177
0178 inline std::uint32_t load_big_u32( void const* p ) noexcept
0179 {
0180 std::uint32_t tmp;
0181 std::memcpy( &tmp, p, sizeof( tmp ) );
0182
0183 #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_BIG_ENDIAN
0184
0185 return tmp;
0186
0187 #else
0188
0189 return detail::byteswap( tmp );
0190
0191 #endif
0192 }
0193
0194
0195
0196 inline std::uint64_t load_native_u64( void const* p ) noexcept
0197 {
0198 std::uint64_t tmp;
0199 std::memcpy( &tmp, p, sizeof( tmp ) );
0200 return tmp;
0201 }
0202
0203 inline std::uint64_t load_little_u64( void const* p ) noexcept
0204 {
0205 std::uint64_t tmp;
0206 std::memcpy( &tmp, p, sizeof( tmp ) );
0207
0208 #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_LITTLE_ENDIAN
0209
0210 return tmp;
0211
0212 #else
0213
0214 return detail::byteswap( tmp );
0215
0216 #endif
0217 }
0218
0219 inline std::uint64_t load_big_u64( void const* p ) noexcept
0220 {
0221 std::uint64_t tmp;
0222 std::memcpy( &tmp, p, sizeof( tmp ) );
0223
0224 #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_BIG_ENDIAN
0225
0226 return tmp;
0227
0228 #else
0229
0230 return detail::byteswap( tmp );
0231
0232 #endif
0233 }
0234
0235
0236
0237 #if defined(__SIZEOF_INT128__)
0238
0239 inline __uint128_t load_native_u128( void const* p ) noexcept
0240 {
0241 __uint128_t tmp;
0242 std::memcpy( &tmp, p, sizeof( tmp ) );
0243 return tmp;
0244 }
0245
0246 inline __uint128_t load_little_u128( void const* p ) noexcept
0247 {
0248 __uint128_t tmp;
0249 std::memcpy( &tmp, p, sizeof( tmp ) );
0250
0251 #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_LITTLE_ENDIAN
0252
0253 return tmp;
0254
0255 #else
0256
0257 return detail::byteswap( tmp );
0258
0259 #endif
0260 }
0261
0262 inline __uint128_t load_big_u128( void const* p ) noexcept
0263 {
0264 __uint128_t tmp;
0265 std::memcpy( &tmp, p, sizeof( tmp ) );
0266
0267 #if BOOST_UUID_BYTE_ORDER == BOOST_UUID_ORDER_BIG_ENDIAN
0268
0269 return tmp;
0270
0271 #else
0272
0273 return detail::byteswap( tmp );
0274
0275 #endif
0276 }
0277
0278 #endif
0279
0280
0281
0282 inline void store_native_u16( void* p, std::uint16_t v ) noexcept
0283 {
0284 std::memcpy( p, &v, sizeof( v ) );
0285 }
0286
0287 inline void store_little_u16( void* p, std::uint16_t v ) noexcept
0288 {
0289 #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_LITTLE_ENDIAN
0290
0291 v = detail::byteswap( v );
0292
0293 #endif
0294
0295 std::memcpy( p, &v, sizeof( v ) );
0296 }
0297
0298 inline void store_big_u16( void* p, std::uint16_t v ) noexcept
0299 {
0300 #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_BIG_ENDIAN
0301
0302 v = detail::byteswap( v );
0303
0304 #endif
0305
0306 std::memcpy( p, &v, sizeof( v ) );
0307 }
0308
0309
0310
0311 inline void store_native_u32( void* p, std::uint32_t v ) noexcept
0312 {
0313 std::memcpy( p, &v, sizeof( v ) );
0314 }
0315
0316 inline void store_little_u32( void* p, std::uint32_t v ) noexcept
0317 {
0318 #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_LITTLE_ENDIAN
0319
0320 v = detail::byteswap( v );
0321
0322 #endif
0323
0324 std::memcpy( p, &v, sizeof( v ) );
0325 }
0326
0327 inline void store_big_u32( void* p, std::uint32_t v ) noexcept
0328 {
0329 #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_BIG_ENDIAN
0330
0331 v = detail::byteswap( v );
0332
0333 #endif
0334
0335 std::memcpy( p, &v, sizeof( v ) );
0336 }
0337
0338
0339
0340 inline void store_native_u64( void* p, std::uint64_t v ) noexcept
0341 {
0342 std::memcpy( p, &v, sizeof( v ) );
0343 }
0344
0345 inline void store_little_u64( void* p, std::uint64_t v ) noexcept
0346 {
0347 #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_LITTLE_ENDIAN
0348
0349 v = detail::byteswap( v );
0350
0351 #endif
0352
0353 std::memcpy( p, &v, sizeof( v ) );
0354 }
0355
0356 inline void store_big_u64( void* p, std::uint64_t v ) noexcept
0357 {
0358 #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_BIG_ENDIAN
0359
0360 v = detail::byteswap( v );
0361
0362 #endif
0363
0364 std::memcpy( p, &v, sizeof( v ) );
0365 }
0366
0367
0368
0369 #if defined(__SIZEOF_INT128__)
0370
0371 inline void store_native_u128( void* p, __uint128_t v ) noexcept
0372 {
0373 std::memcpy( p, &v, sizeof( v ) );
0374 }
0375
0376 inline void store_little_u128( void* p, __uint128_t v ) noexcept
0377 {
0378 #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_LITTLE_ENDIAN
0379
0380 v = detail::byteswap( v );
0381
0382 #endif
0383
0384 std::memcpy( p, &v, sizeof( v ) );
0385 }
0386
0387 inline void store_big_u128( void* p, __uint128_t v ) noexcept
0388 {
0389 #if BOOST_UUID_BYTE_ORDER != BOOST_UUID_ORDER_BIG_ENDIAN
0390
0391 v = detail::byteswap( v );
0392
0393 #endif
0394
0395 std::memcpy( p, &v, sizeof( v ) );
0396 }
0397
0398 #endif
0399
0400 }
0401 }
0402 }
0403
0404 #endif