4 * Macros and inline functions for endianness conversion.
6 * The author dedicates this file to the public domain.
7 * You can do whatever you want with this file.
10 #ifndef _WIMLIB_ENDIANNESS_H
11 #define _WIMLIB_ENDIANNESS_H
13 #include "wimlib/compiler.h"
14 #include "wimlib/types.h"
16 /* Watch out for conflict with ntfs-3g/endians.h ... */
17 #ifndef _NTFS_ENDIANS_H
19 static inline u16 bswap16(u16 n)
21 #ifdef compiler_bswap16
22 return compiler_bswap16(n);
24 return (n << 8) | (n >> 8);
28 static inline u32 bswap32(u32 n)
30 #ifdef compiler_bswap32
31 return compiler_bswap32(n);
35 ((n & 0xFF0000) >> 8) |
40 static inline u64 bswap64(u64 n)
42 #ifdef compiler_bswap64
43 return compiler_bswap64(n);
46 ((n & 0xFF00) << 40) |
47 ((n & 0xFF0000) << 24) |
48 ((n & 0xFF000000) << 8) |
49 ((n & 0xFF00000000) >> 8) |
50 ((n & 0xFF0000000000) >> 24) |
51 ((n & 0xFF000000000000) >> 40) |
57 # define cpu_to_le16(n) ((_force_attr le16)bswap16(n))
58 # define cpu_to_le32(n) ((_force_attr le32)bswap32(n))
59 # define cpu_to_le64(n) ((_force_attr le64)bswap64(n))
60 # define le16_to_cpu(n) bswap16((_force_attr u16)(le16)(n))
61 # define le32_to_cpu(n) bswap32((_force_attr u32)(le32)(n))
62 # define le64_to_cpu(n) bswap64((_force_attr u64)(le64)(n))
63 # define cpu_to_be16(n) ((_force_attr be16)(u16)(n))
64 # define cpu_to_be32(n) ((_force_attr be32)(u32)(n))
65 # define cpu_to_be64(n) ((_force_attr be64)(u64)(n))
66 # define be16_to_cpu(n) ((_force_attr u16)(be16)(n))
67 # define be32_to_cpu(n) ((_force_attr u32)(be32)(n))
68 # define be64_to_cpu(n) ((_force_attr u64)(be64)(n))
70 # define cpu_to_le16(n) ((_force_attr le16)(u16)(n))
71 # define cpu_to_le32(n) ((_force_attr le32)(u32)(n))
72 # define cpu_to_le64(n) ((_force_attr le64)(u64)(n))
73 # define le16_to_cpu(n) ((_force_attr u16)(le16)(n))
74 # define le32_to_cpu(n) ((_force_attr u32)(le32)(n))
75 # define le64_to_cpu(n) ((_force_attr u64)(le64)(n))
76 # define cpu_to_be16(n) ((_force_attr be16)bswap16(n))
77 # define cpu_to_be32(n) ((_force_attr be32)bswap32(n))
78 # define cpu_to_be64(n) ((_force_attr be64)bswap64(n))
79 # define be16_to_cpu(n) bswap16((_force_attr u16)(be16)(n))
80 # define be32_to_cpu(n) bswap32((_force_attr u32)(be32)(n))
81 # define be64_to_cpu(n) bswap64((_force_attr u64)(be64)(n))
84 #endif /* _NTFS_ENDIANS_H */
85 #endif /* _WIMLIB_ENDIANNESS_H */