8#ifndef INCLUDED_BSLS_BYTEORDERUTIL_IMPL
9#define INCLUDED_BSLS_BYTEORDERUTIL_IMPL
75#include <bsls_assert.h>
79#ifdef BSLS_PLATFORM_CMP_MSVC
90template <
class TYPE, Types::
size_type WIDTH = sizeof(TYPE)>
102 static TYPE swapBytes(TYPE x);
114 static TYPE swapBytes(TYPE x);
126 static TYPE swapBytes(TYPE x);
138 static TYPE swapBytes(TYPE x);
166#if (defined(BSLS_ASSERT_SAFE_IS_ACTIVE) || \
167 defined(BSLS_ASSERT_IS_ACTIVE))
169# define BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(expr) \
170 { enum { k_NOT_INFINITY = 1 / static_cast<int>(expr) }; \
171 (void) k_NOT_INFINITY; }
175# define BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(expr)
179#ifndef BSLS_BYTEORDERUTIL_IMPL_ENABLE_COUNTERPRODUCTIVE_MACROS
180#define BSLS_BYTEORDERUTIL_IMPL_DISABLE_COUNTERPRODUCTIVE_MACROS 1
183#if (defined(BSLS_PLATFORM_CMP_GNU) && BSLS_PLATFORM_CMP_VERSION >= 40300) \
184 || defined(BSLS_PLATFORM_CMP_CLANG)
193#if !defined(BSLS_BYTEORDERUTIL_IMPL_DISABLE_COUNTERPRODUCTIVE_MACROS) || \
194 !defined(BSLS_PLATFORM_OS_SOLARIS)
196#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32(dstType, x) \
197 return static_cast<dstType>(__builtin_bswap32(static_cast<int>(x)))
201#if !defined(BSLS_BYTEORDERUTIL_IMPL_DISABLE_COUNTERPRODUCTIVE_MACROS) || \
202 !defined(BSLS_PLATFORM_OS_SOLARIS) || \
203 !defined(BSLS_PLATFORM_CPU_64_BIT) || !defined(BDE_BUILD_TARGET_OPT)
205#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64(dstType, x) \
206 return static_cast<dstType>(__builtin_bswap64( \
207 static_cast<BloombergLP::bsls::Types::Int64>(x)))
211#elif defined(BSLS_PLATFORM_CMP_MSVC)
216#if !defined(BSLS_BYTEORDERUTIL_IMPL_DISABLE_COUNTERPRODUCTIVE_MACROS)
218#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16(dstType, x) \
219 return static_cast<dstType>(_byteswap_ushort( \
220 static_cast<unsigned short>(x)))
224#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32(dstType, x) \
225 return static_cast<dstType>(_byteswap_ulong(static_cast<unsigned int>(x)))
227#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64(dstType, x) \
228 return static_cast<dstType>(_byteswap_uint64( \
229 static_cast<BloombergLP::bsls::Types::Uint64>(x)))
231#elif defined(BSLS_PLATFORM_CPU_POWERPC) && defined(BSLS_PLATFORM_CMP_IBM) \
232 && !defined(BSLS_BYTEORDERUTIL_IMPL_DISABLE_COUNTERPRODUCTIVE_MACROS)
238bsls_byteOrderUtil_Impl_powerpc_swap_p16(
const unsigned short *x);
247#pragma mc_func bsls_byteOrderUtil_Impl_powerpc_swap_p16 { "7c601e2c" }
248#pragma reg_killed_by bsls_byteOrderUtil_Impl_powerpc_swap_p16 gr3
250unsigned int bsls_byteOrderUtil_Impl_powerpc_swap_p32(
const unsigned int *x);
261#pragma mc_func bsls_byteOrderUtil_Impl_powerpc_swap_p32 { "7c601c2c" }
262#pragma reg_killed_by bsls_byteOrderUtil_Impl_powerpc_swap_p32 gr3
264unsigned long long bsls_byteOrderUtil_Impl_powerpc_swap_p64(
265 const unsigned long long *x);
268#ifdef BSLS_PLATFORM_CPU_32_BIT
275#pragma mc_func bsls_byteOrderUtil_Impl_powerpc_swap_p64 \
276 { "7c801c2c" "38630004" "7c601c2c"}
277#pragma reg_killed_by bsls_byteOrderUtil_Impl_powerpc_swap_p64 gr3,gr4
287#pragma mc_func bsls_byteOrderUtil_Impl_powerpc_swap_p64 \
288 { "38830004" "7c601c2c" "7c80242c" "7883000e" }
289#pragma reg_killed_by bsls_byteOrderUtil_Impl_powerpc_swap_p64 gr3,gr4,cr0
293#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(dstType, x) \
295 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(2 == sizeof *x); \
296 return static_cast<dstType>(bsls_byteOrderUtil_Impl_powerpc_swap_p16( \
297 reinterpret_cast<const unsigned short *>(x))); \
300#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(dstType, x) \
302 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(4 == sizeof *x); \
303 return static_cast<dstType>(bsls_byteOrderUtil_Impl_powerpc_swap_p32( \
304 reinterpret_cast<const unsigned int *>(x))); \
307#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(dstType, x) \
309 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(8 == sizeof *x); \
310 return static_cast<dstType>(bsls_byteOrderUtil_Impl_powerpc_swap_p64( \
311 reinterpret_cast<const BloombergLP::bsls::Types::Uint64 *>(x))); \
316#if defined(BSLS_PLATFORM_CPU_SPARC)
321#if defined(BSLS_PLATFORM_CMP_GNU) || defined(BSLS_PLATFORM_CMP_CLANG)
323#if !defined(BSLS_BYTEORDERUTIL_IMPL_DISABLE_COUNTERPRODUCTIVE_MACROS) || \
324 !defined(BDE_BUILD_TARGET_OPT)
332#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(dstType, x) \
334 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(2 == sizeof *x); \
336 asm("lduha [%1] %2, %0" \
338 : "r" (x), "i"(0x88), "m" ( \
339 *reinterpret_cast<const unsigned short *>(x))); \
341 return static_cast<dstType>(y); \
346#if !defined(BSLS_BYTEORDERUTIL_IMPL_DISABLE_COUNTERPRODUCTIVE_MACROS) && \
347 !defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32) && \
348 !defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32)
352#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(dstType, x) \
354 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(4 == sizeof *x); \
356 asm("lduwa [%1] %2, %0" \
358 : "r" (x), "i"(0x88), "m" ( \
359 *reinterpret_cast<const unsigned int *>(x))); \
361 return static_cast<dstType>(y); \
366#if !defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64) \
367 && !defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64)
369#if defined(BSLS_PLATFORM_CPU_64_BIT)
377#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(dstType, x) \
379 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(8 == sizeof *x); \
380 BloombergLP::bsls::Types::Uint64 y; \
382 asm("ldxa [%1] %2, %0" \
384 : "r" (x), "i"(0x88), "m" ( \
385 *reinterpret_cast<const BloombergLP::bsls::Types::Uint64 *>(x))); \
387 return static_cast<dstType>(y); \
407#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(dstType, x) \
409 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(8 == sizeof *x); \
410 BloombergLP::bsls::Types::Uint64 y; \
411 asm("ldxa [%1] %2, %0\n\t" \
412 "srl %0, 0, %R0\n\t" \
415 : "r" (x), "i"(0x88), "m" ( \
416 *reinterpret_cast<BloombergLP::bsls::Types::Uint64 *>(x))); \
418 return static_cast<dstType>(y); \
427#if !defined(BSLS_BYTEORDERUTIL_IMPL_DISABLE_COUNTERPRODUCTIVE_MACROS) || \
428 !defined(BDE_BUILD_TARGET_OPT)
435unsigned long long bsls_byteOrderUtil_Impl_sparc_CC_swap_p64(
436 const unsigned long long *x);
441#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(dstType, x) \
443 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(8 == sizeof *x); \
444 return static_cast<dstType>( \
445 bsls_byteOrderUtil_Impl_sparc_CC_swap_p64( \
446 reinterpret_cast<const unsigned long long *>(x))); \
453#elif (defined(BSLS_PLATFORM_CPU_X86) || defined(BSLS_PLATFORM_CPU_X86_64)) \
454 && (defined(BSLS_PLATFORM_CMP_GNU) || defined(BSLS_PLATFORM_CMP_CLANG))
456#if !defined(BSLS_BYTEORDERUTIL_IMPL_DISABLE_COUNTERPRODUCTIVE_MACROS)
463#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16(dstType, x) \
466 __asm__ ("xchg %b0, %h0" : "=Q" (y) : "0" ( \
467 static_cast<unsigned short>(x))); \
469 return static_cast<dstType>(y); \
474#if !defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32) \
475 && !defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64) \
476 && !defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32) \
477 && !defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64)
481#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32(dstType, x) \
484 __asm__ ("bswap %0" : "=r" (y) : "0" (static_cast<unsigned int>(x))); \
486 return static_cast<dstType>(y); \
489#if BSLS_PLATFORM_CPU_32_BIT
493#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64(dstType, x) \
495 typedef BloombergLP::bsls::Types::Uint64 Uint64; \
497 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(8 == sizeof x); \
499 unsigned int res, tmp; \
500 __asm__ ("bswap %0\n\t" \
502 : "=r" (res), "=r" (tmp) \
503 : "0" ((unsigned)x), "1" ((unsigned)(x >> 32))); \
505 return static_cast<dstType>(((Uint64)res << 32) | (Uint64)tmp); \
512#define BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64(dstType, x) \
514 typedef BloombergLP::bsls::Types::Uint64 Uint64; \
516 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(8 == sizeof x); \
519 __asm__ ("bswap %0" : "=r" (y) : "0" (x)); \
521 return static_cast<dstType>(y); \
528#define BSLS_BYTEORDERUTIL_IMPL_GENERICSWAP_16(dstType, x) \
530 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(2 == sizeof x); \
531 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(2 == sizeof(dstType)); \
533 return static_cast<dstType>((((x) & 0xff00u) >> 8) | \
534 (((x) & 0x00ffu) << 8)); \
537#define BSLS_BYTEORDERUTIL_IMPL_GENERICSWAP_32(dstType, x) \
539 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(4 == sizeof x); \
540 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(4 == sizeof(dstType)); \
542 return static_cast<dstType>((((x) & 0x000000ffU) << 24) \
543 | (((x) & 0x0000ff00U) << 8) \
544 | (((x) & 0x00ff0000U) >> 8) \
545 | (((x) & 0xff000000U) >> 24)); \
548#define BSLS_BYTEORDERUTIL_IMPL_GENERICSWAP_64(dstType, x) \
550 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(8 == sizeof x); \
551 BSLS_BYTEORDERUTIL_IMPL_COMPILE_TIME_ASSERT(8 == sizeof(dstType)); \
553 return static_cast<dstType>((((x) & 0x00000000000000ffULL) << 56) \
554 | (((x) & 0x000000000000ff00ULL) << 40) \
555 | (((x) & 0x0000000000ff0000ULL) << 24) \
556 | (((x) & 0x00000000ff000000ULL) << 8) \
557 | (((x) & 0x000000ff00000000ULL) >> 8) \
558 | (((x) & 0x0000ff0000000000ULL) >> 24) \
559 | (((x) & 0x00ff000000000000ULL) >> 40) \
560 | (((x) & 0xff00000000000000ULL) >> 56)); \
587#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16)
588 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16( TYPE, x);
589#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16)
590 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(TYPE, &x);
602#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32)
603 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32( TYPE, x);
604#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32)
605 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(TYPE, &x);
617#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64)
618 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64( TYPE, x);
619#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64)
620# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
621# pragma GCC diagnostic push
622# pragma GCC diagnostic ignored "-Wstrict-aliasing"
625 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(TYPE, &x);
627# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
628# pragma GCC diagnostic pop
#define BSLS_BYTEORDERUTIL_IMPL_GENERICSWAP_32(dstType, x)
Definition bsls_byteorderutil_impl.h:537
#define BSLS_BYTEORDERUTIL_IMPL_GENERICSWAP_16(dstType, x)
Definition bsls_byteorderutil_impl.h:528
#define BSLS_BYTEORDERUTIL_IMPL_GENERICSWAP_64(dstType, x)
Definition bsls_byteorderutil_impl.h:548
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlt_iso8601util.h:691
Definition bsls_byteorderutil_impl.h:91