]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/bitops.h
Merge branch 'symver' of https://github.com/nekopsykose/util-linux
[thirdparty/util-linux.git] / include / bitops.h
1 /*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 *
5 * Written by Karel Zak <kzak@redhat.com>
6 */
7 #ifndef BITOPS_H
8 #define BITOPS_H
9
10 #include <stdint.h>
11 #include <sys/param.h>
12
13 #if defined(HAVE_BYTESWAP_H)
14 # include <byteswap.h>
15 #endif
16
17 #if defined(HAVE_ENDIAN_H)
18 # include <endian.h>
19 #elif defined(HAVE_SYS_ENDIAN_H) /* BSDs have them here */
20 # include <sys/endian.h>
21 #endif
22
23 #if !(defined(HAVE_BYTESWAP_H) && defined(HAVE_ENDIAN_H))
24 /*
25 * When both byteswap.h and endian.h are preseent, the proper macros are defined
26 * as those files are glibc compatible. Otherwise, compensate for the slightly
27 * different interfaces between the different BSDs.
28 */
29 #if defined(__OpenBSD__)
30 # include <sys/types.h>
31 # define be16toh(x) betoh16(x)
32 # define be32toh(x) betoh32(x)
33 # define be64toh(x) betoh64(x)
34 #elif defined(__NetBSD__) || defined(__DragonFly__)
35 # define bswap_16(x) bswap16(x)
36 # define bswap_32(x) bswap32(x)
37 # define bswap_64(x) bswap64(x)
38 #elif defined(__APPLE__)
39 # include <libkern/OSByteOrder.h>
40 # define htobe16(x) OSSwapHostToBigInt16(x)
41 # define htole16(x) OSSwapHostToLittleInt16(x)
42 # define be16toh(x) OSSwapBigToHostInt16(x)
43 # define le16toh(x) OSSwapLittleToHostInt16(x)
44 # define htobe32(x) OSSwapHostToBigInt32(x)
45 # define htole32(x) OSSwapHostToLittleInt32(x)
46 # define be32toh(x) OSSwapBigToHostInt32(x)
47 # define le32toh(x) OSSwapLittleToHostInt32(x)
48 # define htobe64(x) OSSwapHostToBigInt64(x)
49 # define htole64(x) OSSwapHostToLittleInt64(x)
50 # define be64toh(x) OSSwapBigToHostInt64(x)
51 # define le64toh(x) OSSwapLittleToHostInt64(x)
52 # define bswap_16(x) OSSwapInt16(x)
53 # define bswap_32(x) OSSwapInt32(x)
54 # define bswap_64(x) OSSwapInt64(x)
55 #endif
56 #endif
57
58 /*
59 * Fallbacks
60 * casts are necessary for constants, because we never know how for sure
61 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
62 */
63 #ifndef bswap_16
64 # define bswap_16(x) ((uint16_t)( \
65 (((uint16_t)(x) & 0x00FF) << 8) | \
66 (((uint16_t)(x) & 0xFF00) >> 8)))
67 #endif
68
69 #ifndef bswap_32
70 # define bswap_32(x) ((uint32_t)( \
71 (((uint32_t)(x) & 0x000000FF) << 24) | \
72 (((uint32_t)(x) & 0x0000FF00) << 8) | \
73 (((uint32_t)(x) & 0x00FF0000) >> 8) | \
74 (((uint32_t)(x) & 0xFF000000) >> 24)))
75 #endif
76
77 #ifndef bswap_64
78 # define bswap_64(x) ((uint64_t)( \
79 (((uint64_t)(x) & 0x00000000000000FFULL) << 56) | \
80 (((uint64_t)(x) & 0x000000000000FF00ULL) << 40) | \
81 (((uint64_t)(x) & 0x0000000000FF0000ULL) << 24) | \
82 (((uint64_t)(x) & 0x00000000FF000000ULL) << 8) | \
83 (((uint64_t)(x) & 0x000000FF00000000ULL) >> 8) | \
84 (((uint64_t)(x) & 0x0000FF0000000000ULL) >> 24) | \
85 (((uint64_t)(x) & 0x00FF000000000000ULL) >> 40) | \
86 (((uint64_t)(x) & 0xFF00000000000000ULL) >> 56)))
87 #endif
88
89 #ifndef htobe16
90 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
91 # define htobe16(x) bswap_16 (x)
92 # define htole16(x) (x)
93 # define be16toh(x) bswap_16 (x)
94 # define le16toh(x) (x)
95 # define htobe32(x) bswap_32 (x)
96 # define htole32(x) (x)
97 # define be32toh(x) bswap_32 (x)
98 # define le32toh(x) (x)
99 # define htobe64(x) bswap_64 (x)
100 # define htole64(x) (x)
101 # define be64toh(x) bswap_64 (x)
102 # define le64toh(x) (x)
103 # else
104 # define htobe16(x) (x)
105 # define htole16(x) bswap_16 (x)
106 # define be16toh(x) (x)
107 # define le16toh(x) bswap_16 (x)
108 # define htobe32(x) (x)
109 # define htole32(x) bswap_32 (x)
110 # define be32toh(x) (x)
111 # define le32toh(x) bswap_32 (x)
112 # define htobe64(x) (x)
113 # define htole64(x) bswap_64 (x)
114 # define be64toh(x) (x)
115 # define le64toh(x) bswap_64 (x)
116 # endif
117 #endif
118
119 /*
120 * Byte swab macros (based on linux/byteorder/swab.h)
121 */
122 #define swab16(x) bswap_16(x)
123 #define swab32(x) bswap_32(x)
124 #define swab64(x) bswap_64(x)
125
126 #define cpu_to_le16(x) ((uint16_t) htole16(x))
127 #define cpu_to_le32(x) ((uint32_t) htole32(x))
128 #define cpu_to_le64(x) ((uint64_t) htole64(x))
129
130 #define cpu_to_be16(x) ((uint16_t) htobe16(x))
131 #define cpu_to_be32(x) ((uint32_t) htobe32(x))
132 #define cpu_to_be64(x) ((uint64_t) htobe64(x))
133
134 #define le16_to_cpu(x) ((uint16_t) le16toh(x))
135 #define le32_to_cpu(x) ((uint32_t) le32toh(x))
136 #define le64_to_cpu(x) ((uint64_t) le64toh(x))
137
138 #define be16_to_cpu(x) ((uint16_t) be16toh(x))
139 #define be32_to_cpu(x) ((uint32_t) be32toh(x))
140 #define be64_to_cpu(x) ((uint64_t) be64toh(x))
141
142 /*
143 * Bit map related macros. Usually provided by libc.
144 */
145 #ifndef NBBY
146 # define NBBY CHAR_BIT
147 #endif
148
149 #ifndef setbit
150 # define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
151 # define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
152 # define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
153 # define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
154 #endif
155
156 #endif /* BITOPS_H */
157