rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rendianness.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2015-2016 Haakon Sporsheim <haakon.sporsheim@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library.
16 * See the COPYING file at the root of the source repository.
17 */
18#ifndef __R_ENDIANNESS_H__
19#define __R_ENDIANNESS_H__
20
21#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
22#error "#include <rlib.h> only please."
23#endif
24
25/* This header is included by rtypes.h (@ bottom of file) */
26
56
58#define R_LITTLE_ENDIAN 1234
60#define R_BIG_ENDIAN 4321
61
62#if R_GNUC_PREREQ(4, 2)
63/* Apparently 16bit byteswap is missing from some versions of GCC on x86?? */
64#if !defined(__clang__) && !R_GNUC_PREREQ(4, 8)
66#define RUINT16_BSWAP(val) ((ruint16) (((ruint16)(val) >> 8) | ((ruint16)(val) << 8)))
67#else
69#define RUINT16_BSWAP(val) ((ruint16) __builtin_bswap16 ((rint16) (val)))
70#endif
72#define RUINT32_BSWAP(val) ((ruint32) __builtin_bswap32 ((rint32) (val)))
74#define RUINT64_BSWAP(val) ((ruint64) __builtin_bswap64 ((rint64) (val)))
75#elif defined(_MSC_VER)
76#define RUINT16_BSWAP(val) (_byteswap_ushort ((unsigned short)(val)))
77#define RUINT32_BSWAP(val) (_byteswap_ulong ((unsigned long)(val)))
78#define RUINT64_BSWAP(val) (_byteswap_uint64 ((unsigned __int64)(val)))
79#else
80#define RUINT16_BSWAP(val) ((ruint16) (((ruint16)(val) >> 8) | ((ruint16)(val) << 8)))
81#define RUINT32_BSWAP(val) ((ruint32) ( \
82 ((((ruint32)(val)) ) >> 24) | \
83 ((((ruint32)(val)) & 0x00FF0000) >> 8) | \
84 ((((ruint32)(val)) & 0x0000FF00) << 8) | \
85 ((((ruint32)(val)) ) << 24)))
86#define RUINT64_BSWAP(val) ((ruint64) ( \
87 (((ruint64)(val) & RUINT64_CONSTANT (0x00000000000000FF)) << 56) | \
88 (((ruint64)(val) & RUINT64_CONSTANT (0x000000000000FF00)) << 40) | \
89 (((ruint64)(val) & RUINT64_CONSTANT (0x0000000000FF0000)) << 24) | \
90 (((ruint64)(val) & RUINT64_CONSTANT (0x00000000FF000000)) << 8) | \
91 (((ruint64)(val) & RUINT64_CONSTANT (0x000000FF00000000)) >> 8) | \
92 (((ruint64)(val) & RUINT64_CONSTANT (0x0000FF0000000000)) >> 24) | \
93 (((ruint64)(val) & RUINT64_CONSTANT (0x00FF000000000000)) >> 40) | \
94 (((ruint64)(val) & RUINT64_CONSTANT (0xFF00000000000000)) >> 56)))
95#endif
96
98#if defined(_MSC_VER)
99#define R_BYTE_ORDER R_LITTLE_ENDIAN
100#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
101#define R_BYTE_ORDER R_LITTLE_ENDIAN
102#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
103#define R_BYTE_ORDER R_BIG_ENDIAN
104#else
105/* If your compiler doesn't support __BYTE_ORDER__ this doesn't work! */
106#warning "__BYTE_ORDER__ not defined"
107#endif
108
109#if R_BYTE_ORDER == R_BIG_ENDIAN
110#define RINT16_TO_BE(val) ((rint16) (val))
111#define RUINT16_TO_BE(val) ((ruint16) (val))
112#define RINT16_TO_LE(val) ((rint16) RUINT16_BSWAP (val))
113#define RUINT16_TO_LE(val) (RUINT16_BSWAP (val))
114#define RINT32_TO_BE(val) ((rint32) (val))
115#define RUINT32_TO_BE(val) ((ruint32) (val))
116#define RINT32_TO_LE(val) ((rint32) RUINT32_BSWAP (val))
117#define RUINT32_TO_LE(val) (RUINT32_BSWAP (val))
118#define RINT64_TO_BE(val) ((rint64) (val))
119#define RUINT64_TO_BE(val) ((ruint64) (val))
120#define RINT64_TO_LE(val) ((rint64) RUINT64_BSWAP (val))
121#define RUINT64_TO_LE(val) (RUINT64_BSWAP (val))
122#elif R_BYTE_ORDER == R_LITTLE_ENDIAN
123#define RINT16_TO_LE(val) ((rint16) (val))
124#define RUINT16_TO_LE(val) ((ruint16) (val))
125#define RINT16_TO_BE(val) ((rint16) RUINT16_BSWAP (val))
126#define RUINT16_TO_BE(val) (RUINT16_BSWAP (val))
127#define RINT32_TO_LE(val) ((rint32) (val))
128#define RUINT32_TO_LE(val) ((ruint32) (val))
129#define RINT32_TO_BE(val) ((rint32) RUINT32_BSWAP (val))
130#define RUINT32_TO_BE(val) (RUINT32_BSWAP (val))
131#define RINT64_TO_LE(val) ((rint64) (val))
132#define RUINT64_TO_LE(val) ((ruint64) (val))
133#define RINT64_TO_BE(val) ((rint64) RUINT64_BSWAP (val))
134#define RUINT64_TO_BE(val) (RUINT64_BSWAP (val))
135#else
136#error "__BYTE_ORDER__ is not supported"
137#endif
138
139#if RLIB_SIZEOF_SIZE_T == 8
140#define RSIZE_TO_LE(val) ((rsize) RUINT64_TO_LE (val))
141#define RSSIZE_TO_LE(val) ((rssize) RINT64_TO_LE (val))
142#define RSIZE_TO_BE(val) ((rsize) RUINT64_TO_BE (val))
143#define RSSIZE_TO_BE(val) ((rssize) RINT64_TO_BE (val))
144#elif RLIB_SIZEOF_SIZE_T == 4
145#define RSIZE_TO_LE(val) ((rsize) RUINT32_TO_LE (val))
146#define RSSIZE_TO_LE(val) ((rssize) RINT32_TO_LE (val))
147#define RSIZE_TO_BE(val) ((rsize) RUINT32_TO_BE (val))
148#define RSSIZE_TO_BE(val) ((rssize) RINT32_TO_BE (val))
149#elif RLIB_SIZEOF_SIZE_T == 2
150#define RSIZE_TO_LE(val) ((rsize) RUINT16_TO_LE (val))
151#define RSSIZE_TO_LE(val) ((rssize) RINT16_TO_LE (val))
152#define RSIZE_TO_BE(val) ((rsize) RUINT16_TO_BE (val))
153#define RSSIZE_TO_BE(val) ((rssize) RINT16_TO_BE (val))
154#endif
155
156#if RLIB_SIZEOF_LONG == 8
157#define RULONG_TO_LE(val) ((rulong) RUINT64_TO_LE (val))
158#define RLONG_TO_LE(val) ((rlong) RINT64_TO_LE (val))
159#define RULONG_TO_BE(val) ((rulong) RUINT64_TO_BE (val))
160#define RLONG_TO_BE(val) ((rlong) RINT64_TO_BE (val))
161#elif RLIB_SIZEOF_LONG == 4
162#define RULONG_TO_LE(val) ((rulong) RUINT32_TO_LE (val))
163#define RLONG_TO_LE(val) ((rlong) RINT32_TO_LE (val))
164#define RULONG_TO_BE(val) ((rulong) RUINT32_TO_BE (val))
165#define RLONG_TO_BE(val) ((rlong) RINT32_TO_BE (val))
166#elif RLIB_SIZEOF_LONG == 2
167#define RULONG_TO_LE(val) ((rulong) RUINT16_TO_LE (val))
168#define RLONG_TO_LE(val) ((rlong) RINT16_TO_LE (val))
169#define RULONG_TO_BE(val) ((rulong) RUINT16_TO_BE (val))
170#define RLONG_TO_BE(val) ((rlong) RINT16_TO_BE (val))
171#endif
172
173#if RLIB_SIZEOF_INTMAX == 8
174#define RUINTMAX_TO_LE(val) ((rulong) RUINT64_TO_LE (val))
175#define RINTMAX_TO_LE(val) ((rlong) RINT64_TO_LE (val))
176#define RUINTMAX_TO_BE(val) ((rulong) RUINT64_TO_BE (val))
177#define RINTMAX_TO_BE(val) ((rlong) RINT64_TO_BE (val))
178#elif RLIB_SIZEOF_INTMAX == 4
179#define RUINTMAX_TO_LE(val) ((rulong) RUINT32_TO_LE (val))
180#define RINTMAX_TO_LE(val) ((rlong) RINT32_TO_LE (val))
181#define RUINTMAX_TO_BE(val) ((rulong) RUINT32_TO_BE (val))
182#define RINTMAX_TO_BE(val) ((rlong) RINT32_TO_BE (val))
183#elif RLIB_SIZEOF_INTMAX == 2
184#define RUINTMAX_TO_LE(val) ((rulong) RUINT16_TO_LE (val))
185#define RINTMAX_TO_LE(val) ((rlong) RINT16_TO_LE (val))
186#define RUINTMAX_TO_BE(val) ((rulong) RUINT16_TO_BE (val))
187#define RINTMAX_TO_BE(val) ((rlong) RINT16_TO_BE (val))
188#endif
189
190#if RLIB_SIZEOF_INT == 8
191#define RUINT_TO_LE(val) ((rulong) RUINT64_TO_LE (val))
192#define RINT_TO_LE(val) ((rlong) RINT64_TO_LE (val))
193#define RUINT_TO_BE(val) ((rulong) RUINT64_TO_BE (val))
194#define RINT_TO_BE(val) ((rlong) RINT64_TO_BE (val))
195#elif RLIB_SIZEOF_INT == 4
196#define RUINT_TO_LE(val) ((rulong) RUINT32_TO_LE (val))
197#define RINT_TO_LE(val) ((rlong) RINT32_TO_LE (val))
198#define RUINT_TO_BE(val) ((rulong) RUINT32_TO_BE (val))
199#define RINT_TO_BE(val) ((rlong) RINT32_TO_BE (val))
200#elif RLIB_SIZEOF_INT == 2
201#define RUINT_TO_LE(val) ((rulong) RUINT16_TO_LE (val))
202#define RINT_TO_LE(val) ((rlong) RINT16_TO_LE (val))
203#define RUINT_TO_BE(val) ((rulong) RUINT16_TO_BE (val))
204#define RINT_TO_BE(val) ((rlong) RINT16_TO_BE (val))
205#endif
206
207#define RINT16_FROM_LE(val) (RINT16_TO_LE (val))
208#define RUINT16_FROM_LE(val) (RUINT16_TO_LE (val))
209#define RINT16_FROM_BE(val) (RINT16_TO_BE (val))
210#define RUINT16_FROM_BE(val) (RUINT16_TO_BE (val))
211#define RINT32_FROM_LE(val) (RINT32_TO_LE (val))
212#define RUINT32_FROM_LE(val) (RUINT32_TO_LE (val))
213#define RINT32_FROM_BE(val) (RINT32_TO_BE (val))
214#define RUINT32_FROM_BE(val) (RUINT32_TO_BE (val))
215#define RINT64_FROM_LE(val) (RINT64_TO_LE (val))
216#define RUINT64_FROM_LE(val) (RUINT64_TO_LE (val))
217#define RINT64_FROM_BE(val) (RINT64_TO_BE (val))
218#define RUINT64_FROM_BE(val) (RUINT64_TO_BE (val))
219#define RLONG_FROM_LE(val) (RLONG_TO_LE (val))
220#define RULONG_FROM_LE(val) (RULONG_TO_LE (val))
221#define RLONG_FROM_BE(val) (RLONG_TO_BE (val))
222#define RULONG_FROM_BE(val) (RULONG_TO_BE (val))
223#define RINT_FROM_LE(val) (RINT_TO_LE (val))
224#define RUINT_FROM_LE(val) (RUINT_TO_LE (val))
225#define RINT_FROM_BE(val) (RINT_TO_BE (val))
226#define RUINT_FROM_BE(val) (RUINT_TO_BE (val))
227#define RSIZE_FROM_LE(val) (RSIZE_TO_LE (val))
228#define RSSIZE_FROM_LE(val) (RSSIZE_TO_LE (val))
229#define RSIZE_FROM_BE(val) (RSIZE_TO_BE (val))
230#define RSSIZE_FROM_BE(val) (RSSIZE_TO_BE (val))
231
232
236#define r_ntohl(val) (RUINT32_FROM_BE (val))
238#define r_ntohs(val) (RUINT16_FROM_BE (val))
240#define r_htonl(val) (RUINT32_TO_BE (val))
242#define r_htons(val) (RUINT16_TO_BE (val))
254static inline ruint16 r_load_be16 (const void * p)
255{
256 ruint16 v;
257 memcpy (&v, p, sizeof (v));
258 return RUINT16_FROM_BE (v);
259}
261static inline ruint32 r_load_be32 (const void * p)
262{
263 ruint32 v;
264 memcpy (&v, p, sizeof (v));
265 return RUINT32_FROM_BE (v);
266}
268static inline ruint64 r_load_be64 (const void * p)
269{
270 ruint64 v;
271 memcpy (&v, p, sizeof (v));
272 return RUINT64_FROM_BE (v);
273}
275static inline ruint16 r_load_le16 (const void * p)
276{
277 ruint16 v;
278 memcpy (&v, p, sizeof (v));
279 return RUINT16_FROM_LE (v);
280}
282static inline ruint32 r_load_le32 (const void * p)
283{
284 ruint32 v;
285 memcpy (&v, p, sizeof (v));
286 return RUINT32_FROM_LE (v);
287}
289static inline ruint64 r_load_le64 (const void * p)
290{
291 ruint64 v;
292 memcpy (&v, p, sizeof (v));
293 return RUINT64_FROM_LE (v);
294}
296static inline void r_store_be16 (void * p, ruint16 v)
297{
298 ruint16 be = RUINT16_TO_BE (v);
299 memcpy (p, &be, sizeof (be));
300}
302static inline void r_store_be32 (void * p, ruint32 v)
303{
304 ruint32 be = RUINT32_TO_BE (v);
305 memcpy (p, &be, sizeof (be));
306}
308static inline void r_store_be64 (void * p, ruint64 v)
309{
310 ruint64 be = RUINT64_TO_BE (v);
311 memcpy (p, &be, sizeof (be));
312}
314static inline void r_store_le16 (void * p, ruint16 v)
315{
316 ruint16 le = RUINT16_TO_LE (v);
317 memcpy (p, &le, sizeof (le));
318}
320static inline void r_store_le32 (void * p, ruint32 v)
321{
322 ruint32 le = RUINT32_TO_LE (v);
323 memcpy (p, &le, sizeof (le));
324}
326static inline void r_store_le64 (void * p, ruint64 v)
327{
328 ruint64 le = RUINT64_TO_LE (v);
329 memcpy (p, &le, sizeof (le));
330}
333R_END_DECLS
334
/* r_endian */
336
337#endif /* __R_ENDIANNESS_H__ */
static ruint32 r_load_be32(const void *p)
Load a big-endian 32-bit integer from p.
Definition rendianness.h:261
static void r_store_le16(void *p, ruint16 v)
Store v as a little-endian 16-bit integer at p.
Definition rendianness.h:314
static void r_store_le32(void *p, ruint32 v)
Store v as a little-endian 32-bit integer at p.
Definition rendianness.h:320
static ruint64 r_load_be64(const void *p)
Load a big-endian 64-bit integer from p.
Definition rendianness.h:268
static ruint64 r_load_le64(const void *p)
Load a little-endian 64-bit integer from p.
Definition rendianness.h:289
static void r_store_le64(void *p, ruint64 v)
Store v as a little-endian 64-bit integer at p.
Definition rendianness.h:326
static ruint16 r_load_be16(const void *p)
Load a big-endian 16-bit integer from p.
Definition rendianness.h:254
static ruint32 r_load_le32(const void *p)
Load a little-endian 32-bit integer from p.
Definition rendianness.h:282
static ruint16 r_load_le16(const void *p)
Load a little-endian 16-bit integer from p.
Definition rendianness.h:275
static void r_store_be16(void *p, ruint16 v)
Store v as a big-endian 16-bit integer at p.
Definition rendianness.h:296
static void r_store_be32(void *p, ruint32 v)
Store v as a big-endian 32-bit integer at p.
Definition rendianness.h:302
static void r_store_be64(void *p, ruint64 v)
Store v as a big-endian 64-bit integer at p.
Definition rendianness.h:308
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
unsigned int ruint32
Unsigned 32-bit integer.
Definition rtypes.h:192
unsigned long ruint64
Unsigned 64-bit integer.
Definition rtypes.h:193
unsigned short ruint16
Unsigned 16-bit integer.
Definition rtypes.h:191