blob: 7439ece91cabfb68ef275a97ffde4ee1eecc66f8 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
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#define HAVE_SYS_ENDIAN_H
18#if defined(HAVE_ENDIAN_H)
19# include <endian.h>
20#elif defined(HAVE_SYS_ENDIAN_H) /* BSDs have them here */
21# include <sys/endian.h>
22#endif
23
24#if defined(__OpenBSD__)
25# include <sys/types.h>
26# define be16toh(x) betoh16(x)
27# define be32toh(x) betoh32(x)
28# define be64toh(x) betoh64(x)
29#endif
30
31/*
32 * Fallbacks
33 */
34#ifndef bswap_16
35# define bswap_16(x) ((((x) & 0x00FF) << 8) | \
36 (((x) & 0xFF00) >> 8))
37#endif
38
39#ifndef bswap_32
40# define bswap_32(x) ((((x) & 0x000000FF) << 24) | \
41 (((x) & 0x0000FF00) << 8) | \
42 (((x) & 0x00FF0000) >> 8) | \
43 (((x) & 0xFF000000) >> 24))
44#endif
45
46#ifndef bswap_64
47# define bswap_64(x) ((((x) & 0x00000000000000FFULL) << 56) | \
48 (((x) & 0x000000000000FF00ULL) << 40) | \
49 (((x) & 0x0000000000FF0000ULL) << 24) | \
50 (((x) & 0x00000000FF000000ULL) << 8) | \
51 (((x) & 0x000000FF00000000ULL) >> 8) | \
52 (((x) & 0x0000FF0000000000ULL) >> 24) | \
53 (((x) & 0x00FF000000000000ULL) >> 40) | \
54 (((x) & 0xFF00000000000000ULL) >> 56))
55#endif
56
57//#ifndef htobe16
58//# if !defined(WORDS_BIGENDIAN)
Vojtech Bocek7cc278b2013-02-24 01:40:19 +010059//# define htobe16(x) bswap_16 (x)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050060# define htole16(x) (x)
61# define be16toh(x) bswap_16 (x)
62# define le16toh(x) (x)
Vojtech Bocek7cc278b2013-02-24 01:40:19 +010063//# define htobe32(x) bswap_32 (x)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050064# define htole32(x) (x)
65# define be32toh(x) bswap_32 (x)
66# define le32toh(x) (x)
Vojtech Bocek7cc278b2013-02-24 01:40:19 +010067//# define htobe64(x) bswap_64 (x)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050068# define htole64(x) (x)
69# define be64toh(x) bswap_64 (x)
70# define le64toh(x) (x)
71/*
72# else
73# define htobe16(x) (x)
74# define htole16(x) bswap_16 (x)
75# define be16toh(x) (x)
76# define le16toh(x) bswap_16 (x)
77# define htobe32(x) (x)
78# define htole32(x) bswap_32 (x)
79# define be32toh(x) (x)
80# define le32toh(x) bswap_32 (x)
81# define htobe64(x) (x)
82# define htole64(x) bswap_64 (x)
83# define be64toh(x) (x)
84# define le64toh(x) bswap_64 (x)
85# endif
86#endif
87*/
88/*
89 * Byte swab macros (based on linux/byteorder/swab.h)
90 */
91#define swab16(x) bswap_16(x)
92#define swab32(x) bswap_32(x)
93#define swab64(x) bswap_64(x)
94
95#define cpu_to_le16(x) ((uint16_t) htole16(x))
96#define cpu_to_le32(x) ((uint32_t) htole32(x))
97#define cpu_to_le64(x) ((uint64_t) htole64(x))
98
99#define cpu_to_be16(x) ((uint16_t) htobe16(x))
100#define cpu_to_be32(x) ((uint32_t) htobe32(x))
101#define cpu_to_be64(x) ((uint64_t) htobe64(x))
102
103#define le16_to_cpu(x) ((uint16_t) le16toh(x))
104#define le32_to_cpu(x) ((uint32_t) le32toh(x))
105#define le64_to_cpu(x) ((uint64_t) le64toh(x))
106
107#define be16_to_cpu(x) ((uint16_t) be16toh(x))
108#define be32_to_cpu(x) ((uint32_t) be32toh(x))
109#define be64_to_cpu(x) ((uint64_t) be64toh(x))
110
111/*
112 * Bit map related macros. Usually provided by libc.
113 */
114#ifndef NBBY
115# define NBBY CHAR_BIT
116#endif
117
118#ifndef setbit
119# define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
120# define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
121# define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
122# define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
123#endif
124
125#endif /* BITOPS_H */
126