summaryrefslogtreecommitdiffstats
path: root/include/bitops.h
blob: 72274de0b539c4c1055e5c873c2ebcdda6833055 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * No copyright is claimed.  This code is in the public domain; do with
 * it what you wish.
 *
 * Written by Karel Zak <kzak@redhat.com>
 */
#ifndef BITOPS_H
#define BITOPS_H

#include <stdint.h>
#include <sys/param.h>

#if defined(HAVE_BYTESWAP_H)
# include <byteswap.h>
#endif

#if defined(HAVE_ENDIAN_H)
#  include <endian.h>
#elif defined(HAVE_SYS_ENDIAN_H)	/* BSDs have them here */
#  include <sys/endian.h>
#endif

#if !(defined(HAVE_BYTESWAP_H) && defined(HAVE_ENDIAN_H))
/*
 * When both byteswap.h and endian.h are preseent, the proper macros are defined
 * as those files are glibc compatible.  Otherwise, compensate for the slightly
 * different interfaces between the different BSDs.
 */
#if defined(__OpenBSD__)
# include <sys/types.h>
# define be16toh(x) betoh16(x)
# define be32toh(x) betoh32(x)
# define be64toh(x) betoh64(x)
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
# define bswap_16(x) bswap16(x)
# define bswap_32(x) bswap32(x)
# define bswap_64(x) bswap64(x)
#elif defined(__APPLE__)
# include <libkern/OSByteOrder.h>
# define htobe16(x) OSSwapHostToBigInt16(x)
# define htole16(x) OSSwapHostToLittleInt16(x)
# define be16toh(x) OSSwapBigToHostInt16(x)
# define le16toh(x) OSSwapLittleToHostInt16(x)
# define htobe32(x) OSSwapHostToBigInt32(x)
# define htole32(x) OSSwapHostToLittleInt32(x)
# define be32toh(x) OSSwapBigToHostInt32(x)
# define le32toh(x) OSSwapLittleToHostInt32(x)
# define htobe64(x) OSSwapHostToBigInt64(x)
# define htole64(x) OSSwapHostToLittleInt64(x)
# define be64toh(x) OSSwapBigToHostInt64(x)
# define le64toh(x) OSSwapLittleToHostInt64(x)
# define bswap_16(x) OSSwapInt16(x)
# define bswap_32(x) OSSwapInt32(x)
# define bswap_64(x) OSSwapInt64(x)
#endif
#endif

/*
 * Fallbacks
 * casts are necessary for constants, because we never know how for sure
 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
 */
#ifndef bswap_16
# define bswap_16(x) ((uint16_t)( \
	(((uint16_t)(x) & 0x00FF) << 8) | \
	(((uint16_t)(x) & 0xFF00) >> 8)))
#endif

#ifndef bswap_32
# define bswap_32(x) ((uint32_t)( \
	(((uint32_t)(x) & 0x000000FF) << 24) | \
	(((uint32_t)(x) & 0x0000FF00) << 8)  | \
	(((uint32_t)(x) & 0x00FF0000) >> 8)  | \
	(((uint32_t)(x) & 0xFF000000) >> 24)))
#endif

#ifndef bswap_64
# define bswap_64(x) ((uint64_t)( \
	(((uint64_t)(x) & 0x00000000000000FFULL) << 56) | \
	(((uint64_t)(x) & 0x000000000000FF00ULL) << 40) | \
	(((uint64_t)(x) & 0x0000000000FF0000ULL) << 24) | \
	(((uint64_t)(x) & 0x00000000FF000000ULL) << 8)  | \
	(((uint64_t)(x) & 0x000000FF00000000ULL) >> 8)  | \
	(((uint64_t)(x) & 0x0000FF0000000000ULL) >> 24) | \
	(((uint64_t)(x) & 0x00FF000000000000ULL) >> 40) | \
	(((uint64_t)(x) & 0xFF00000000000000ULL) >> 56)))
#endif

#ifndef htobe16
# if !defined(WORDS_BIGENDIAN)
#  define htobe16(x) bswap_16 (x)
#  define htole16(x) (x)
#  define be16toh(x) bswap_16 (x)
#  define le16toh(x) (x)
#  define htobe32(x) bswap_32 (x)
#  define htole32(x) (x)
#  define be32toh(x) bswap_32 (x)
#  define le32toh(x) (x)
#  define htobe64(x) bswap_64 (x)
#  define htole64(x) (x)
#  define be64toh(x) bswap_64 (x)
#  define le64toh(x) (x)
# else
#  define htobe16(x) (x)
#  define htole16(x) bswap_16 (x)
#  define be16toh(x) (x)
#  define le16toh(x) bswap_16 (x)
#  define htobe32(x) (x)
#  define htole32(x) bswap_32 (x)
#  define be32toh(x) (x)
#  define le32toh(x) bswap_32 (x)
#  define htobe64(x) (x)
#  define htole64(x) bswap_64 (x)
#  define be64toh(x) (x)
#  define le64toh(x) bswap_64 (x)
# endif
#endif

/*
 * Byte swab macros (based on linux/byteorder/swab.h)
 */
#define swab16(x) bswap_16(x)
#define swab32(x) bswap_32(x)
#define swab64(x) bswap_64(x)

#define cpu_to_le16(x) ((uint16_t) htole16(x))
#define cpu_to_le32(x) ((uint32_t) htole32(x))
#define cpu_to_le64(x) ((uint64_t) htole64(x))

#define cpu_to_be16(x) ((uint16_t) htobe16(x))
#define cpu_to_be32(x) ((uint32_t) htobe32(x))
#define cpu_to_be64(x) ((uint64_t) htobe64(x))

#define le16_to_cpu(x) ((uint16_t) le16toh(x))
#define le32_to_cpu(x) ((uint32_t) le32toh(x))
#define le64_to_cpu(x) ((uint64_t) le64toh(x))

#define be16_to_cpu(x) ((uint16_t) be16toh(x))
#define be32_to_cpu(x) ((uint32_t) be32toh(x))
#define be64_to_cpu(x) ((uint64_t) be64toh(x))

/*
 * Bit map related macros. Usually provided by libc.
 */
#ifndef NBBY
# define NBBY            CHAR_BIT
#endif

#ifndef setbit
# define setbit(a,i)	((a)[(i)/NBBY] |= 1<<((i)%NBBY))
# define clrbit(a,i)	((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
# define isset(a,i)	((a)[(i)/NBBY] & (1<<((i)%NBBY)))
# define isclr(a,i)	(((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
#endif

#endif /* BITOPS_H */