/src/cyclonedds/install/include/dds/ddsrt/bswap.h
Line | Count | Source |
1 | | // Copyright(c) 2006 to 2021 ZettaScale Technology and others |
2 | | // |
3 | | // This program and the accompanying materials are made available under the |
4 | | // terms of the Eclipse Public License v. 2.0 which is available at |
5 | | // http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License |
6 | | // v. 1.0 which is available at |
7 | | // http://www.eclipse.org/org/documents/edl-v10.php. |
8 | | // |
9 | | // SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
10 | | |
11 | | #ifndef DDSRT_BSWAP_H |
12 | | #define DDSRT_BSWAP_H |
13 | | |
14 | | /** @file bswap.h |
15 | | This header implements byteswapping (between big endian and little endian) for different integer types. |
16 | | */ |
17 | | |
18 | | #include <stdint.h> |
19 | | #include <stdlib.h> |
20 | | |
21 | | #include "dds/export.h" |
22 | | #include "dds/ddsrt/endian.h" |
23 | | |
24 | | #if defined (__cplusplus) |
25 | | extern "C" { |
26 | | #endif |
27 | | |
28 | | #if DDSRT_ENDIAN == DDSRT_LITTLE_ENDIAN |
29 | | typedef struct { uint64_t l, h; } ddsrt_uint128_t; |
30 | | typedef struct { uint64_t l; int64_t h; } ddsrt_int128_t; |
31 | | #else |
32 | | typedef struct { uint64_t h, l; } ddsrt_uint128_t; |
33 | | typedef struct { int64_t h; uint64_t l; } ddsrt_int128_t; |
34 | | #endif |
35 | | |
36 | | /** |
37 | | * @brief Defines the byte order options: native, big endian, and little endian |
38 | | */ |
39 | | enum ddsrt_byte_order_selector { |
40 | | DDSRT_BOSEL_NATIVE, |
41 | | DDSRT_BOSEL_BE, |
42 | | DDSRT_BOSEL_LE, |
43 | | }; |
44 | | |
45 | | /** |
46 | | * @brief Byteswap uint16_t |
47 | | * |
48 | | * @param[in] x the uint16_t to byteswap |
49 | | * @return the byteswapped uint16_t |
50 | | */ |
51 | | DDS_INLINE_EXPORT inline uint16_t ddsrt_bswap2u (uint16_t x) |
52 | | { |
53 | | return (uint16_t) ((x >> 8) | (x << 8)); |
54 | | } |
55 | | |
56 | | /** |
57 | | * @brief Byteswap int16_t |
58 | | * |
59 | | * @param[in] x the int16_t to byteswap |
60 | | * @return the byteswapped int16_t |
61 | | */ |
62 | | DDS_INLINE_EXPORT inline int16_t ddsrt_bswap2 (int16_t x) |
63 | | { |
64 | | return (int16_t) ddsrt_bswap2u ((uint16_t) x); |
65 | | } |
66 | | |
67 | | /** |
68 | | * @brief Byteswap uint32_t |
69 | | * |
70 | | * @param[in] x the uint32_t to byteswap |
71 | | * @return the byteswapped uint32_t |
72 | | */ |
73 | | DDS_INLINE_EXPORT inline uint32_t ddsrt_bswap4u (uint32_t x) |
74 | | { |
75 | | return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24); |
76 | | } |
77 | | |
78 | | /** |
79 | | * @brief Byteswap int32_t |
80 | | * |
81 | | * @param[in] x the int32_t to byteswap |
82 | | * @return the byteswapped int32_t |
83 | | */ |
84 | | DDS_INLINE_EXPORT inline int32_t ddsrt_bswap4 (int32_t x) |
85 | | { |
86 | | return (int32_t) ddsrt_bswap4u ((uint32_t) x); |
87 | | } |
88 | | |
89 | | /** |
90 | | * @brief Byteswap uint64_t |
91 | | * |
92 | | * @param[in] x the uint64_t to byteswap |
93 | | * @return the byteswapped uint64_t |
94 | | */ |
95 | | DDS_INLINE_EXPORT inline uint64_t ddsrt_bswap8u (uint64_t x) |
96 | | { |
97 | | const uint32_t newhi = ddsrt_bswap4u ((uint32_t) x); |
98 | | const uint32_t newlo = ddsrt_bswap4u ((uint32_t) (x >> 32)); |
99 | | return ((uint64_t) newhi << 32) | (uint64_t) newlo; |
100 | | } |
101 | | |
102 | | /** |
103 | | * @brief Byteswap int64_t |
104 | | * |
105 | | * @param[in] x the int64_t to byteswap |
106 | | * @return the byteswapped int64_t |
107 | | */ |
108 | | DDS_INLINE_EXPORT inline int64_t ddsrt_bswap8 (int64_t x) |
109 | | { |
110 | | return (int64_t) ddsrt_bswap8u ((uint64_t) x); |
111 | | } |
112 | | |
113 | | /** |
114 | | * @brief Byteswap uint64_t |
115 | | * |
116 | | * @param[in] x the uint64_t to byteswap |
117 | | * @return the byteswapped uint64_t |
118 | | */ |
119 | | DDS_INLINE_EXPORT inline ddsrt_uint128_t ddsrt_bswap16u (ddsrt_uint128_t x) |
120 | | { |
121 | | ddsrt_uint128_t tmp; |
122 | | tmp.h = ddsrt_bswap8u (x.l); |
123 | | tmp.l = ddsrt_bswap8u (x.h); |
124 | | return tmp; |
125 | | } |
126 | | |
127 | | /** |
128 | | * @brief Byteswap int64_t |
129 | | * |
130 | | * @param[in] x the int64_t to byteswap |
131 | | * @return the byteswapped int64_t |
132 | | */ |
133 | | DDS_INLINE_EXPORT inline ddsrt_int128_t ddsrt_bswap16 (ddsrt_int128_t x) |
134 | 0 | { |
135 | 0 | ddsrt_int128_t tmp; |
136 | 0 | tmp.h = (int64_t) ddsrt_bswap8u (x.l); |
137 | 0 | tmp.l = ddsrt_bswap8u ((uint64_t) x.h); |
138 | 0 | return tmp; |
139 | 0 | } |
140 | | |
141 | | /** |
142 | | * @brief Macros for byteswapping |
143 | | * |
144 | | * These macros are for converting integer types from and to big endian or little endian. |
145 | | * They abstract from the endianness of your system by providing a different implementation based on the value of DDSRT_ENDIAN |
146 | | * (for example if you want to convert to big endian, it converts if your system is little endian, |
147 | | * but if your system is already big endian, the conversion is skipped). |
148 | | * Postfix numbers indicate the number of bytes of the integer (2,4,8 for 16-bit, 32-bit, 64-bit respectively), |
149 | | * whilst an 'u' postfix indicates an unsigned integer type. |
150 | | * |
151 | | * - ddsrt_toBE: convert from native to big endian |
152 | | * - ddsrt_toLE: convert from native to little endian |
153 | | * - ddsrt_toBO: convert from native to byte order 'bo', @see ddsrt_byte_order_selector |
154 | | * - ddsrt_fromBE: convert from big endian to native endianness (DDSRT_ENDIAN) |
155 | | */ |
156 | | #if DDSRT_ENDIAN == DDSRT_LITTLE_ENDIAN |
157 | | #define ddsrt_toBE2(x) ddsrt_bswap2 (x) |
158 | | #define ddsrt_toBE2u(x) ddsrt_bswap2u (x) |
159 | | #define ddsrt_toBE4(x) ddsrt_bswap4 (x) |
160 | | #define ddsrt_toBE4u(x) ddsrt_bswap4u (x) |
161 | | #define ddsrt_toBE8(x) ddsrt_bswap8 (x) |
162 | | #define ddsrt_toBE8u(x) ddsrt_bswap8u (x) |
163 | | #define ddsrt_toBE16(x) ddsrt_bswap16 (x) |
164 | | #define ddsrt_toBE16u(x) ddsrt_bswap16u (x) |
165 | | #define ddsrt_toLE2(x) (x) |
166 | | #define ddsrt_toLE2u(x) (x) |
167 | | #define ddsrt_toLE4(x) (x) |
168 | | #define ddsrt_toLE4u(x) (x) |
169 | | #define ddsrt_toLE8(x) (x) |
170 | | #define ddsrt_toLE8u(x) (x) |
171 | | #define ddsrt_toLE16(x) (x) |
172 | | #define ddsrt_toLE16u(x) (x) |
173 | | #define ddsrt_toBO2(bo, x) ((bo) == DDSRT_BOSEL_BE ? ddsrt_bswap2 (x) : (x)) |
174 | | #define ddsrt_toBO2u(bo, x) ((bo) == DDSRT_BOSEL_BE ? ddsrt_bswap2u (x) : (x)) |
175 | | #define ddsrt_toBO4(bo, x) ((bo) == DDSRT_BOSEL_BE ? ddsrt_bswap4 (x) : (x)) |
176 | | #define ddsrt_toBO4u(bo, x) ((bo) == DDSRT_BOSEL_BE ? ddsrt_bswap4u (x) : (x)) |
177 | | #define ddsrt_toBO8(bo, x) ((bo) == DDSRT_BOSEL_BE ? ddsrt_bswap8 (x) : (x)) |
178 | | #define ddsrt_toBO8u(bo, x) ((bo) == DDSRT_BOSEL_BE ? ddsrt_bswap8u (x) : (x)) |
179 | | #define ddsrt_toBO16(bo, x) ((bo) == DDSRT_BOSEL_BE ? ddsrt_bswap16 (x) : (x)) |
180 | | #define ddsrt_toBO16u(bo, x) ((bo) == DDSRT_BOSEL_BE ? ddsrt_bswap16u (x) : (x)) |
181 | | #define ddsrt_fromBE2(x) ddsrt_bswap2 (x) |
182 | | #define ddsrt_fromBE2u(x) ddsrt_bswap2u (x) |
183 | | #define ddsrt_fromBE4(x) ddsrt_bswap4 (x) |
184 | | #define ddsrt_fromBE4u(x) ddsrt_bswap4u (x) |
185 | | #define ddsrt_fromBE8(x) ddsrt_bswap8 (x) |
186 | | #define ddsrt_fromBE8u(x) ddsrt_bswap8u (x) |
187 | | #define ddsrt_fromBE16(x) ddsrt_bswap16 (x) |
188 | | #define ddsrt_fromBE16u(x) ddsrt_bswap16u (x) |
189 | | #else |
190 | | #define ddsrt_toBE2(x) (x) |
191 | | #define ddsrt_toBE2u(x) (x) |
192 | | #define ddsrt_toBE4(x) (x) |
193 | | #define ddsrt_toBE4u(x) (x) |
194 | | #define ddsrt_toBE8(x) (x) |
195 | | #define ddsrt_toBE8u(x) (x) |
196 | | #define ddsrt_toBE16(x) (x) |
197 | | #define ddsrt_toBE16u(x) (x) |
198 | | #define ddsrt_toLE2(x) ddsrt_bswap2 (x) |
199 | | #define ddsrt_toLE2u(x) ddsrt_bswap2u (x) |
200 | | #define ddsrt_toLE4(x) ddsrt_bswap4 (x) |
201 | | #define ddsrt_toLE4u(x) ddsrt_bswap4u (x) |
202 | | #define ddsrt_toLE8(x) ddsrt_bswap8 (x) |
203 | | #define ddsrt_toLE8u(x) ddsrt_bswap8u (x) |
204 | | #define ddsrt_toLE16(x) ddsrt_bswap16 (x) |
205 | | #define ddsrt_toLE16u(x) ddsrt_bswap16u (x) |
206 | | #define ddsrt_toBO2(bo, x) ((bo) == DDSRT_BOSEL_LE ? ddsrt_bswap2 (x) : (x)) |
207 | | #define ddsrt_toBO2u(bo, x) ((bo) == DDSRT_BOSEL_LE ? ddsrt_bswap2u (x) : (x)) |
208 | | #define ddsrt_toBO4(bo, x) ((bo) == DDSRT_BOSEL_LE ? ddsrt_bswap4 (x) : (x)) |
209 | | #define ddsrt_toBO4u(bo, x) ((bo) == DDSRT_BOSEL_LE ? ddsrt_bswap4u (x) : (x)) |
210 | | #define ddsrt_toBO8(bo, x) ((bo) == DDSRT_BOSEL_LE ? ddsrt_bswap8 (x) : (x)) |
211 | | #define ddsrt_toBO8u(bo, x) ((bo) == DDSRT_BOSEL_LE ? ddsrt_bswap8u (x) : (x)) |
212 | | #define ddsrt_toBO16(bo, x) ((bo) == DDSRT_BOSEL_LE ? ddsrt_bswap16 (x) : (x)) |
213 | | #define ddsrt_toBO16u(bo, x) ((bo) == DDSRT_BOSEL_LE ? ddsrt_bswap16u (x) : (x)) |
214 | | #define ddsrt_fromBE2(x) (x) |
215 | | #define ddsrt_fromBE2u(x) (x) |
216 | | #define ddsrt_fromBE4(x) (x) |
217 | | #define ddsrt_fromBE4u(x) (x) |
218 | | #define ddsrt_fromBE8(x) (x) |
219 | | #define ddsrt_fromBE8u(x) (x) |
220 | | #define ddsrt_fromBE16(x) (x) |
221 | | #define ddsrt_fromBE16u(x) (x) |
222 | | #endif |
223 | | |
224 | | #if defined (__cplusplus) |
225 | | } |
226 | | #endif |
227 | | |
228 | | #endif /* DDSRT_BSWAP_H */ |