2 * endianness.h - macros and inline functions for endianness conversion
4 * The following copying information applies to this specific source code file:
6 * Written in 2014-2015 by Eric Biggers <ebiggers3@gmail.com>
8 * To the extent possible under law, the author(s) have dedicated all copyright
9 * and related and neighboring rights to this software to the public domain
10 * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
11 * Dedication (the "CC0").
13 * This software is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
17 * You should have received a copy of the CC0 along with this software; if not
18 * see <http://creativecommons.org/publicdomain/zero/1.0/>.
21 #ifndef _WIMLIB_ENDIANNESS_H
22 #define _WIMLIB_ENDIANNESS_H
24 #include "wimlib/compiler.h"
25 #include "wimlib/types.h"
27 #ifdef HAVE_SYS_ENDIAN_H
28 /* Needed on NetBSD to stop system bswap macros from messing things up */
29 # include <sys/endian.h>
35 /* Watch out for conflict with ntfs-3g/endians.h ... */
36 #ifndef _NTFS_ENDIANS_H
38 #define bswap16_const(n) \
39 ((((u16)(n) & 0x00FF) << 8) | \
40 (((u16)(n) & 0xFF00) >> 8))
42 #define bswap32_const(n) \
43 ((((u32)(n) & 0x000000FF) << 24) | \
44 (((u32)(n) & 0x0000FF00) << 8) | \
45 (((u32)(n) & 0x00FF0000) >> 8) | \
46 (((u32)(n) & 0xFF000000) >> 24))
48 #define bswap64_const(n) \
49 ((((u64)(n) & 0x00000000000000FF) << 56) | \
50 (((u64)(n) & 0x000000000000FF00) << 40) | \
51 (((u64)(n) & 0x0000000000FF0000) << 24) | \
52 (((u64)(n) & 0x00000000FF000000) << 8) | \
53 (((u64)(n) & 0x000000FF00000000) >> 8) | \
54 (((u64)(n) & 0x0000FF0000000000) >> 24) | \
55 (((u64)(n) & 0x00FF000000000000) >> 40) | \
56 (((u64)(n) & 0xFF00000000000000) >> 56))
58 static forceinline u16 do_bswap16(u16 n)
60 #ifdef compiler_bswap16
61 return compiler_bswap16(n);
63 return bswap16_const(n);
67 static forceinline u32 do_bswap32(u32 n)
69 #ifdef compiler_bswap32
70 return compiler_bswap32(n);
72 return bswap32_const(n);
76 static forceinline u64 do_bswap64(u64 n)
78 #ifdef compiler_bswap64
79 return compiler_bswap64(n);
81 return bswap64_const(n);
85 #define bswap16(n) (__builtin_constant_p(n) ? bswap16_const(n) : do_bswap16(n))
86 #define bswap32(n) (__builtin_constant_p(n) ? bswap32_const(n) : do_bswap32(n))
87 #define bswap64(n) (__builtin_constant_p(n) ? bswap64_const(n) : do_bswap64(n))
90 # define cpu_to_le16(n) ((_force_attr le16)bswap16(n))
91 # define cpu_to_le32(n) ((_force_attr le32)bswap32(n))
92 # define cpu_to_le64(n) ((_force_attr le64)bswap64(n))
93 # define le16_to_cpu(n) bswap16((_force_attr u16)(le16)(n))
94 # define le32_to_cpu(n) bswap32((_force_attr u32)(le32)(n))
95 # define le64_to_cpu(n) bswap64((_force_attr u64)(le64)(n))
96 # define cpu_to_be16(n) ((_force_attr be16)(u16)(n))
97 # define cpu_to_be32(n) ((_force_attr be32)(u32)(n))
98 # define cpu_to_be64(n) ((_force_attr be64)(u64)(n))
99 # define be16_to_cpu(n) ((_force_attr u16)(be16)(n))
100 # define be32_to_cpu(n) ((_force_attr u32)(be32)(n))
101 # define be64_to_cpu(n) ((_force_attr u64)(be64)(n))
103 # define cpu_to_le16(n) ((_force_attr le16)(u16)(n))
104 # define cpu_to_le32(n) ((_force_attr le32)(u32)(n))
105 # define cpu_to_le64(n) ((_force_attr le64)(u64)(n))
106 # define le16_to_cpu(n) ((_force_attr u16)(le16)(n))
107 # define le32_to_cpu(n) ((_force_attr u32)(le32)(n))
108 # define le64_to_cpu(n) ((_force_attr u64)(le64)(n))
109 # define cpu_to_be16(n) ((_force_attr be16)bswap16(n))
110 # define cpu_to_be32(n) ((_force_attr be32)bswap32(n))
111 # define cpu_to_be64(n) ((_force_attr be64)bswap64(n))
112 # define be16_to_cpu(n) bswap16((_force_attr u16)(be16)(n))
113 # define be32_to_cpu(n) bswap32((_force_attr u32)(be32)(n))
114 # define be64_to_cpu(n) bswap64((_force_attr u64)(be64)(n))
117 #endif /* _NTFS_ENDIANS_H */
118 #endif /* _WIMLIB_ENDIANNESS_H */