Thrill  0.1
bswap.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/math/bswap.hpp
3  *
4  * bswap16(), bswap32(), and bswap64() to swap bytes - mainly for portability.
5  *
6  * Part of tlx - http://panthema.net/tlx
7  *
8  * Copyright (C) 2018 Timo Bingmann <[email protected]>
9  *
10  * All rights reserved. Published under the Boost Software License, Version 1.0
11  ******************************************************************************/
12 
13 #ifndef TLX_MATH_BSWAP_HEADER
14 #define TLX_MATH_BSWAP_HEADER
15 
16 #include <cstdint>
17 
18 #ifdef _MSC_VER
19 #include <cstdlib>
20 #endif
21 
22 namespace tlx {
23 
24 //! \addtogroup tlx_math
25 //! \{
26 
27 /******************************************************************************/
28 // bswap16() - swap 16-bit integers
29 
30 //! bswap16 - generic implementation
31 static inline uint16_t bswap16_generic(const uint16_t& x) {
32  return ((x >> 8) & 0x00FFUL) | ((x << 8) & 0xFF00UL);
33 }
34 
35 #if defined(__GNUC__) || defined(__clang__)
36 
37 //! bswap16 - gcc/clang intrinsic
38 static inline uint16_t bswap16(const uint16_t& v) {
39  return __builtin_bswap16(v);
40 }
41 
42 #elif defined(_MSC_VER)
43 
44 //! bswap16 - MSVC intrinsic
45 static inline uint16_t bswap16(const uint16_t& v) {
46  return _byteswap_ushort(v);
47 }
48 
49 #else
50 
51 //! bswap16 - generic
52 static inline uint16_t bswap16(const uint16_t& v) {
53  return bswap16_generic(v);
54 }
55 
56 #endif
57 
58 /******************************************************************************/
59 // bswap32() - swap 32-bit integers
60 
61 //! bswap32 - generic implementation
62 static inline uint32_t bswap32_generic(const uint32_t& x) {
63  return ((x >> 24) & 0x000000FFUL) | ((x << 24) & 0xFF000000UL) |
64  ((x >> 8) & 0x0000FF00UL) | ((x << 8) & 0x00FF0000UL);
65 }
66 
67 #if defined(__GNUC__) || defined(__clang__)
68 
69 //! bswap32 - gcc/clang intrinsic
70 static inline uint32_t bswap32(const uint32_t& v) {
71  return __builtin_bswap32(v);
72 }
73 
74 #elif defined(_MSC_VER)
75 
76 //! bswap32 - MSVC intrinsic
77 static inline uint32_t bswap32(const uint32_t& v) {
78  return _byteswap_ulong(v);
79 }
80 
81 #else
82 
83 //! bswap32 - generic
84 static inline uint32_t bswap32(const uint32_t& v) {
85  return bswap32_generic(v);
86 }
87 
88 #endif
89 
90 /******************************************************************************/
91 // bswap64() - swap 64-bit integers
92 
93 //! bswap64 - generic implementation
94 static inline uint64_t bswap64_generic(const uint64_t& x) {
95  return ((x >> 56) & 0x00000000000000FFull) |
96  ((x >> 40) & 0x000000000000FF00ull) |
97  ((x >> 24) & 0x0000000000FF0000ull) |
98  ((x >> 8) & 0x00000000FF000000ull) |
99  ((x << 8) & 0x000000FF00000000ull) |
100  ((x << 24) & 0x0000FF0000000000ull) |
101  ((x << 40) & 0x00FF000000000000ull) |
102  ((x << 56) & 0xFF00000000000000ull);
103 }
104 
105 #if defined(__GNUC__) || defined(__clang__)
106 
107 //! bswap64 - gcc/clang intrinsic
108 static inline uint64_t bswap64(const uint64_t& v) {
109  return __builtin_bswap64(v);
110 }
111 
112 #elif defined(_MSC_VER)
113 
114 //! bswap64 - MSVC intrinsic
115 static inline uint64_t bswap64(const uint64_t& v) {
116  return _byteswap_uint64(v);
117 }
118 
119 #else
120 
121 //! bswap64 - generic
122 static inline uint64_t bswap64(const uint64_t& v) {
123  return bswap64_generic(v);
124 }
125 
126 #endif
127 
128 /******************************************************************************/
129 
130 //! \}
131 
132 } // namespace tlx
133 
134 #endif // !TLX_MATH_BSWAP_HEADER
135 
136 /******************************************************************************/
static uint32_t bswap32(const uint32_t &v)
bswap32 - generic
Definition: bswap.hpp:84
static uint64_t bswap64(const uint64_t &v)
bswap64 - generic
Definition: bswap.hpp:122
list x
Definition: gen_data.py:39
static uint16_t bswap16(const uint16_t &v)
bswap16 - generic
Definition: bswap.hpp:52
static uint32_t bswap32_generic(const uint32_t &x)
bswap32 - generic implementation
Definition: bswap.hpp:62
static uint16_t bswap16_generic(const uint16_t &x)
bswap16 - generic implementation
Definition: bswap.hpp:31
static uint64_t bswap64_generic(const uint64_t &x)
bswap64 - generic implementation
Definition: bswap.hpp:94