2 * endianness.h - macros and inline functions for endianness conversion
4 * Copyright 2022 Eric Biggers
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 #ifndef _WIMLIB_ENDIANNESS_H
29 #define _WIMLIB_ENDIANNESS_H
31 #include "wimlib/compiler.h"
32 #include "wimlib/types.h"
34 #ifdef HAVE_SYS_ENDIAN_H
35 /* Needed on NetBSD to stop system bswap macros from messing things up */
36 # include <sys/endian.h>
42 /* Watch out for conflict with ntfs-3g/endians.h ... */
43 #ifndef _NTFS_ENDIANS_H
45 #define bswap16_const(n) \
46 ((((u16)(n) & 0x00FF) << 8) | \
47 (((u16)(n) & 0xFF00) >> 8))
49 #define bswap32_const(n) \
50 ((((u32)(n) & 0x000000FF) << 24) | \
51 (((u32)(n) & 0x0000FF00) << 8) | \
52 (((u32)(n) & 0x00FF0000) >> 8) | \
53 (((u32)(n) & 0xFF000000) >> 24))
55 #define bswap64_const(n) \
56 ((((u64)(n) & 0x00000000000000FF) << 56) | \
57 (((u64)(n) & 0x000000000000FF00) << 40) | \
58 (((u64)(n) & 0x0000000000FF0000) << 24) | \
59 (((u64)(n) & 0x00000000FF000000) << 8) | \
60 (((u64)(n) & 0x000000FF00000000) >> 8) | \
61 (((u64)(n) & 0x0000FF0000000000) >> 24) | \
62 (((u64)(n) & 0x00FF000000000000) >> 40) | \
63 (((u64)(n) & 0xFF00000000000000) >> 56))
65 static forceinline u16 do_bswap16(u16 n)
67 #ifdef compiler_bswap16
68 return compiler_bswap16(n);
70 return bswap16_const(n);
74 static forceinline u32 do_bswap32(u32 n)
76 #ifdef compiler_bswap32
77 return compiler_bswap32(n);
79 return bswap32_const(n);
83 static forceinline u64 do_bswap64(u64 n)
85 #ifdef compiler_bswap64
86 return compiler_bswap64(n);
88 return bswap64_const(n);
92 #define bswap16(n) (__builtin_constant_p(n) ? bswap16_const(n) : do_bswap16(n))
93 #define bswap32(n) (__builtin_constant_p(n) ? bswap32_const(n) : do_bswap32(n))
94 #define bswap64(n) (__builtin_constant_p(n) ? bswap64_const(n) : do_bswap64(n))
96 #if CPU_IS_BIG_ENDIAN()
97 # define cpu_to_le16(n) ((_force_attr le16)bswap16(n))
98 # define cpu_to_le32(n) ((_force_attr le32)bswap32(n))
99 # define cpu_to_le64(n) ((_force_attr le64)bswap64(n))
100 # define le16_to_cpu(n) bswap16((_force_attr u16)(le16)(n))
101 # define le32_to_cpu(n) bswap32((_force_attr u32)(le32)(n))
102 # define le64_to_cpu(n) bswap64((_force_attr u64)(le64)(n))
103 # define cpu_to_be16(n) ((_force_attr be16)(u16)(n))
104 # define cpu_to_be32(n) ((_force_attr be32)(u32)(n))
105 # define cpu_to_be64(n) ((_force_attr be64)(u64)(n))
106 # define be16_to_cpu(n) ((_force_attr u16)(be16)(n))
107 # define be32_to_cpu(n) ((_force_attr u32)(be32)(n))
108 # define be64_to_cpu(n) ((_force_attr u64)(be64)(n))
110 # define cpu_to_le16(n) ((_force_attr le16)(u16)(n))
111 # define cpu_to_le32(n) ((_force_attr le32)(u32)(n))
112 # define cpu_to_le64(n) ((_force_attr le64)(u64)(n))
113 # define le16_to_cpu(n) ((_force_attr u16)(le16)(n))
114 # define le32_to_cpu(n) ((_force_attr u32)(le32)(n))
115 # define le64_to_cpu(n) ((_force_attr u64)(le64)(n))
116 # define cpu_to_be16(n) ((_force_attr be16)bswap16(n))
117 # define cpu_to_be32(n) ((_force_attr be32)bswap32(n))
118 # define cpu_to_be64(n) ((_force_attr be64)bswap64(n))
119 # define be16_to_cpu(n) bswap16((_force_attr u16)(be16)(n))
120 # define be32_to_cpu(n) bswap32((_force_attr u32)(be32)(n))
121 # define be64_to_cpu(n) bswap64((_force_attr u64)(be64)(n))
124 #endif /* _NTFS_ENDIANS_H */
125 #endif /* _WIMLIB_ENDIANNESS_H */