]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/bitops.h
scriptreplay: cleanup usage()
[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(__OpenBSD__)
24 # include <sys/types.h>
25 # define be16toh(x) betoh16(x)
26 # define be32toh(x) betoh32(x)
27 # define be64toh(x) betoh64(x)
28 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
29 # define bswap_16(x) bswap16(x)
30 # define bswap_32(x) bswap32(x)
31 # define bswap_64(x) bswap64(x)
32 #elif defined(__APPLE__)
33 # include <libkern/OSByteOrder.h>
34 # define htobe16(x) OSSwapHostToBigInt16(x)
35 # define htole16(x) OSSwapHostToLittleInt16(x)
36 # define be16toh(x) OSSwapBigToHostInt16(x)
37 # define le16toh(x) OSSwapLittleToHostInt16(x)
38 # define htobe32(x) OSSwapHostToBigInt32(x)
39 # define htole32(x) OSSwapHostToLittleInt32(x)
40 # define be32toh(x) OSSwapBigToHostInt32(x)
41 # define le32toh(x) OSSwapLittleToHostInt32(x)
42 # define htobe64(x) OSSwapHostToBigInt64(x)
43 # define htole64(x) OSSwapHostToLittleInt64(x)
44 # define be64toh(x) OSSwapBigToHostInt64(x)
45 # define le64toh(x) OSSwapLittleToHostInt64(x)
46 # define bswap_16(x) OSSwapInt16(x)
47 # define bswap_32(x) OSSwapInt32(x)
48 # define bswap_64(x) OSSwapInt64(x)
49 #endif
50
51 /*
52 * Fallbacks
53 * casts are necessary for constants, because we never know how for sure
54 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
55 */
56 #ifndef bswap_16
57 # define bswap_16(x) ((uint16_t)( \
58 (((uint16_t)(x) & 0x00FF) << 8) | \
59 (((uint16_t)(x) & 0xFF00) >> 8)))
60 #endif
61
62 #ifndef bswap_32
63 # define bswap_32(x) ((uint32_t)( \
64 (((uint32_t)(x) & 0x000000FF) << 24) | \
65 (((uint32_t)(x) & 0x0000FF00) << 8) | \
66 (((uint32_t)(x) & 0x00FF0000) >> 8) | \
67 (((uint32_t)(x) & 0xFF000000) >> 24)))
68 #endif
69
70 #ifndef bswap_64
71 # define bswap_64(x) ((uint64_t)( \
72 (((uint64_t)(x) & 0x00000000000000FFULL) << 56) | \
73 (((uint64_t)(x) & 0x000000000000FF00ULL) << 40) | \
74 (((uint64_t)(x) & 0x0000000000FF0000ULL) << 24) | \
75 (((uint64_t)(x) & 0x00000000FF000000ULL) << 8) | \
76 (((uint64_t)(x) & 0x000000FF00000000ULL) >> 8) | \
77 (((uint64_t)(x) & 0x0000FF0000000000ULL) >> 24) | \
78 (((uint64_t)(x) & 0x00FF000000000000ULL) >> 40) | \
79 (((uint64_t)(x) & 0xFF00000000000000ULL) >> 56)))
80 #endif
81
82 #ifndef htobe16
83 # if !defined(WORDS_BIGENDIAN)
84 # define htobe16(x) bswap_16 (x)
85 # define htole16(x) (x)
86 # define be16toh(x) bswap_16 (x)
87 # define le16toh(x) (x)
88 # define htobe32(x) bswap_32 (x)
89 # define htole32(x) (x)
90 # define be32toh(x) bswap_32 (x)
91 # define le32toh(x) (x)
92 # define htobe64(x) bswap_64 (x)
93 # define htole64(x) (x)
94 # define be64toh(x) bswap_64 (x)
95 # define le64toh(x) (x)
96 # else
97 # define htobe16(x) (x)
98 # define htole16(x) bswap_16 (x)
99 # define be16toh(x) (x)
100 # define le16toh(x) bswap_16 (x)
101 # define htobe32(x) (x)
102 # define htole32(x) bswap_32 (x)
103 # define be32toh(x) (x)
104 # define le32toh(x) bswap_32 (x)
105 # define htobe64(x) (x)
106 # define htole64(x) bswap_64 (x)
107 # define be64toh(x) (x)
108 # define le64toh(x) bswap_64 (x)
109 # endif
110 #endif
111
112 /*
113 * Byte swab macros (based on linux/byteorder/swab.h)
114 */
115 #define swab16(x) bswap_16(x)
116 #define swab32(x) bswap_32(x)
117 #define swab64(x) bswap_64(x)
118
119 #define cpu_to_le16(x) ((uint16_t) htole16(x))
120 #define cpu_to_le32(x) ((uint32_t) htole32(x))
121 #define cpu_to_le64(x) ((uint64_t) htole64(x))
122
123 #define cpu_to_be16(x) ((uint16_t) htobe16(x))
124 #define cpu_to_be32(x) ((uint32_t) htobe32(x))
125 #define cpu_to_be64(x) ((uint64_t) htobe64(x))
126
127 #define le16_to_cpu(x) ((uint16_t) le16toh(x))
128 #define le32_to_cpu(x) ((uint32_t) le32toh(x))
129 #define le64_to_cpu(x) ((uint64_t) le64toh(x))
130
131 #define be16_to_cpu(x) ((uint16_t) be16toh(x))
132 #define be32_to_cpu(x) ((uint32_t) be32toh(x))
133 #define be64_to_cpu(x) ((uint64_t) be64toh(x))
134
135 /*
136 * Bit map related macros. Usually provided by libc.
137 */
138 #ifndef NBBY
139 # define NBBY CHAR_BIT
140 #endif
141
142 #ifndef setbit
143 # define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
144 # define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
145 # define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
146 # define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
147 #endif
148
149 #endif /* BITOPS_H */
150