4 * Macros and inline functions for endianness conversion.
9 * The author dedicates this file to the public domain.
10 * You can do whatever you want with this file.
13 #ifndef _WIMLIB_ENDIANNESS_H
14 #define _WIMLIB_ENDIANNESS_H
16 #include "wimlib/compiler.h"
17 #include "wimlib/types.h"
19 /* Watch out for conflict with ntfs-3g/endians.h ... */
20 #ifndef _NTFS_ENDIANS_H
22 #define bswap16_const(n) \
23 ((((u16)(n) & 0x00FF) << 8) | \
24 (((u16)(n) & 0xFF00) >> 8))
26 #define bswap32_const(n) \
27 ((((u32)(n) & 0x000000FF) << 24) | \
28 (((u32)(n) & 0x0000FF00) << 8) | \
29 (((u32)(n) & 0x00FF0000) >> 8) | \
30 (((u32)(n) & 0xFF000000) >> 24))
32 #define bswap64_const(n) \
33 ((((u64)(n) & 0x00000000000000FF) << 56) | \
34 (((u64)(n) & 0x000000000000FF00) << 40) | \
35 (((u64)(n) & 0x0000000000FF0000) << 24) | \
36 (((u64)(n) & 0x00000000FF000000) << 8) | \
37 (((u64)(n) & 0x000000FF00000000) >> 8) | \
38 (((u64)(n) & 0x0000FF0000000000) >> 24) | \
39 (((u64)(n) & 0x00FF000000000000) >> 40) | \
40 (((u64)(n) & 0xFF00000000000000) >> 56))
42 static inline u16 do_bswap16(u16 n)
44 #ifdef compiler_bswap16
45 return compiler_bswap16(n);
47 return bswap16_const(n);
51 static inline u32 do_bswap32(u32 n)
53 #ifdef compiler_bswap32
54 return compiler_bswap32(n);
56 return bswap32_const(n);
60 static inline u64 do_bswap64(u64 n)
62 #ifdef compiler_bswap64
63 return compiler_bswap64(n);
65 return bswap64_const(n);
69 #define bswap16(n) (__builtin_constant_p(n) ? bswap16_const(n) : do_bswap16(n))
70 #define bswap32(n) (__builtin_constant_p(n) ? bswap32_const(n) : do_bswap32(n))
71 #define bswap64(n) (__builtin_constant_p(n) ? bswap64_const(n) : do_bswap64(n))
74 # define cpu_to_le16(n) ((_force_attr le16)bswap16(n))
75 # define cpu_to_le32(n) ((_force_attr le32)bswap32(n))
76 # define cpu_to_le64(n) ((_force_attr le64)bswap64(n))
77 # define le16_to_cpu(n) bswap16((_force_attr u16)(le16)(n))
78 # define le32_to_cpu(n) bswap32((_force_attr u32)(le32)(n))
79 # define le64_to_cpu(n) bswap64((_force_attr u64)(le64)(n))
80 # define cpu_to_be16(n) ((_force_attr be16)(u16)(n))
81 # define cpu_to_be32(n) ((_force_attr be32)(u32)(n))
82 # define cpu_to_be64(n) ((_force_attr be64)(u64)(n))
83 # define be16_to_cpu(n) ((_force_attr u16)(be16)(n))
84 # define be32_to_cpu(n) ((_force_attr u32)(be32)(n))
85 # define be64_to_cpu(n) ((_force_attr u64)(be64)(n))
87 # define cpu_to_le16(n) ((_force_attr le16)(u16)(n))
88 # define cpu_to_le32(n) ((_force_attr le32)(u32)(n))
89 # define cpu_to_le64(n) ((_force_attr le64)(u64)(n))
90 # define le16_to_cpu(n) ((_force_attr u16)(le16)(n))
91 # define le32_to_cpu(n) ((_force_attr u32)(le32)(n))
92 # define le64_to_cpu(n) ((_force_attr u64)(le64)(n))
93 # define cpu_to_be16(n) ((_force_attr be16)bswap16(n))
94 # define cpu_to_be32(n) ((_force_attr be32)bswap32(n))
95 # define cpu_to_be64(n) ((_force_attr be64)bswap64(n))
96 # define be16_to_cpu(n) bswap16((_force_attr u16)(be16)(n))
97 # define be32_to_cpu(n) bswap32((_force_attr u32)(be32)(n))
98 # define be64_to_cpu(n) bswap64((_force_attr u64)(be64)(n))
101 #endif /* _NTFS_ENDIANS_H */
102 #endif /* _WIMLIB_ENDIANNESS_H */