]> wimlib.net Git - wimlib/blob - include/wimlib/endianness.h
bitops: rename bit scan functions
[wimlib] / include / wimlib / endianness.h
1 /*
2  * endianness.h - macros and inline functions for endianness conversion
3  *
4  * The following copying information applies to this specific source code file:
5  *
6  * Written in 2014-2015 by Eric Biggers <ebiggers3@gmail.com>
7  *
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").
12  *
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.
16  *
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/>.
19  */
20
21 #ifndef _WIMLIB_ENDIANNESS_H
22 #define _WIMLIB_ENDIANNESS_H
23
24 #include "wimlib/compiler.h"
25 #include "wimlib/types.h"
26
27 /* Watch out for conflict with ntfs-3g/endians.h ... */
28 #ifndef _NTFS_ENDIANS_H
29
30 #define bswap16_const(n)                        \
31         ((((u16)(n) & 0x00FF) << 8)     |       \
32          (((u16)(n) & 0xFF00) >> 8))
33
34 #define bswap32_const(n)                                \
35         ((((u32)(n) & 0x000000FF) << 24)        |       \
36          (((u32)(n) & 0x0000FF00) << 8)         |       \
37          (((u32)(n) & 0x00FF0000) >> 8)         |       \
38          (((u32)(n) & 0xFF000000) >> 24))
39
40 #define bswap64_const(n)                                        \
41         ((((u64)(n) & 0x00000000000000FF) << 56)        |       \
42          (((u64)(n) & 0x000000000000FF00) << 40)        |       \
43          (((u64)(n) & 0x0000000000FF0000) << 24)        |       \
44          (((u64)(n) & 0x00000000FF000000) << 8)         |       \
45          (((u64)(n) & 0x000000FF00000000) >> 8)         |       \
46          (((u64)(n) & 0x0000FF0000000000) >> 24)        |       \
47          (((u64)(n) & 0x00FF000000000000) >> 40)        |       \
48          (((u64)(n) & 0xFF00000000000000) >> 56))
49
50 static inline u16 do_bswap16(u16 n)
51 {
52 #ifdef compiler_bswap16
53         return compiler_bswap16(n);
54 #else
55         return bswap16_const(n);
56 #endif
57 }
58
59 static inline u32 do_bswap32(u32 n)
60 {
61 #ifdef compiler_bswap32
62         return compiler_bswap32(n);
63 #else
64         return bswap32_const(n);
65 #endif
66 }
67
68 static inline u64 do_bswap64(u64 n)
69 {
70 #ifdef compiler_bswap64
71         return compiler_bswap64(n);
72 #else
73         return bswap64_const(n);
74 #endif
75 }
76
77 #define bswap16(n) (__builtin_constant_p(n) ? bswap16_const(n) : do_bswap16(n))
78 #define bswap32(n) (__builtin_constant_p(n) ? bswap32_const(n) : do_bswap32(n))
79 #define bswap64(n) (__builtin_constant_p(n) ? bswap64_const(n) : do_bswap64(n))
80
81 #if CPU_IS_BIG_ENDIAN
82 #  define cpu_to_le16(n) ((_force_attr le16)bswap16(n))
83 #  define cpu_to_le32(n) ((_force_attr le32)bswap32(n))
84 #  define cpu_to_le64(n) ((_force_attr le64)bswap64(n))
85 #  define le16_to_cpu(n) bswap16((_force_attr u16)(le16)(n))
86 #  define le32_to_cpu(n) bswap32((_force_attr u32)(le32)(n))
87 #  define le64_to_cpu(n) bswap64((_force_attr u64)(le64)(n))
88 #  define cpu_to_be16(n) ((_force_attr be16)(u16)(n))
89 #  define cpu_to_be32(n) ((_force_attr be32)(u32)(n))
90 #  define cpu_to_be64(n) ((_force_attr be64)(u64)(n))
91 #  define be16_to_cpu(n) ((_force_attr u16)(be16)(n))
92 #  define be32_to_cpu(n) ((_force_attr u32)(be32)(n))
93 #  define be64_to_cpu(n) ((_force_attr u64)(be64)(n))
94 #else
95 #  define cpu_to_le16(n) ((_force_attr le16)(u16)(n))
96 #  define cpu_to_le32(n) ((_force_attr le32)(u32)(n))
97 #  define cpu_to_le64(n) ((_force_attr le64)(u64)(n))
98 #  define le16_to_cpu(n) ((_force_attr u16)(le16)(n))
99 #  define le32_to_cpu(n) ((_force_attr u32)(le32)(n))
100 #  define le64_to_cpu(n) ((_force_attr u64)(le64)(n))
101 #  define cpu_to_be16(n) ((_force_attr be16)bswap16(n))
102 #  define cpu_to_be32(n) ((_force_attr be32)bswap32(n))
103 #  define cpu_to_be64(n) ((_force_attr be64)bswap64(n))
104 #  define be16_to_cpu(n) bswap16((_force_attr u16)(be16)(n))
105 #  define be32_to_cpu(n) bswap32((_force_attr u32)(be32)(n))
106 #  define be64_to_cpu(n) bswap64((_force_attr u64)(be64)(n))
107 #endif
108
109 #endif /* _NTFS_ENDIANS_H */
110 #endif /* _WIMLIB_ENDIANNESS_H */