bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 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 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 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 | #endif |
| 29 | |
| 30 | /* |
| 31 | * Fallbacks |
| 32 | */ |
| 33 | #ifndef bswap_16 |
| 34 | # define bswap_16(x) ((((x) & 0x00FF) << 8) | \ |
| 35 | (((x) & 0xFF00) >> 8)) |
| 36 | #endif |
| 37 | |
| 38 | #ifndef bswap_32 |
| 39 | # define bswap_32(x) ((((x) & 0x000000FF) << 24) | \ |
| 40 | (((x) & 0x0000FF00) << 8) | \ |
| 41 | (((x) & 0x00FF0000) >> 8) | \ |
| 42 | (((x) & 0xFF000000) >> 24)) |
| 43 | #endif |
| 44 | |
| 45 | #ifndef bswap_64 |
| 46 | # define bswap_64(x) ((((x) & 0x00000000000000FFULL) << 56) | \ |
| 47 | (((x) & 0x000000000000FF00ULL) << 40) | \ |
| 48 | (((x) & 0x0000000000FF0000ULL) << 24) | \ |
| 49 | (((x) & 0x00000000FF000000ULL) << 8) | \ |
| 50 | (((x) & 0x000000FF00000000ULL) >> 8) | \ |
| 51 | (((x) & 0x0000FF0000000000ULL) >> 24) | \ |
| 52 | (((x) & 0x00FF000000000000ULL) >> 40) | \ |
| 53 | (((x) & 0xFF00000000000000ULL) >> 56)) |
| 54 | #endif |
| 55 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 56 | #ifndef htobe16 |
| 57 | # if !defined(WORDS_BIGENDIAN) |
| 58 | # define htobe16(x) bswap_16 (x) |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 59 | # define htole16(x) (x) |
| 60 | # define be16toh(x) bswap_16 (x) |
| 61 | # define le16toh(x) (x) |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 62 | # define htobe32(x) bswap_32 (x) |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 63 | # define htole32(x) (x) |
| 64 | # define be32toh(x) bswap_32 (x) |
| 65 | # define le32toh(x) (x) |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 66 | # define htobe64(x) bswap_64 (x) |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 67 | # define htole64(x) (x) |
| 68 | # define be64toh(x) bswap_64 (x) |
| 69 | # define le64toh(x) (x) |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 70 | # else |
| 71 | # define htobe16(x) (x) |
| 72 | # define htole16(x) bswap_16 (x) |
| 73 | # define be16toh(x) (x) |
| 74 | # define le16toh(x) bswap_16 (x) |
| 75 | # define htobe32(x) (x) |
| 76 | # define htole32(x) bswap_32 (x) |
| 77 | # define be32toh(x) (x) |
| 78 | # define le32toh(x) bswap_32 (x) |
| 79 | # define htobe64(x) (x) |
| 80 | # define htole64(x) bswap_64 (x) |
| 81 | # define be64toh(x) (x) |
| 82 | # define le64toh(x) bswap_64 (x) |
| 83 | # endif |
| 84 | #endif |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 85 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 86 | /* |
| 87 | * Byte swab macros (based on linux/byteorder/swab.h) |
| 88 | */ |
| 89 | #define swab16(x) bswap_16(x) |
| 90 | #define swab32(x) bswap_32(x) |
| 91 | #define swab64(x) bswap_64(x) |
| 92 | |
| 93 | #define cpu_to_le16(x) ((uint16_t) htole16(x)) |
| 94 | #define cpu_to_le32(x) ((uint32_t) htole32(x)) |
| 95 | #define cpu_to_le64(x) ((uint64_t) htole64(x)) |
| 96 | |
| 97 | #define cpu_to_be16(x) ((uint16_t) htobe16(x)) |
| 98 | #define cpu_to_be32(x) ((uint32_t) htobe32(x)) |
| 99 | #define cpu_to_be64(x) ((uint64_t) htobe64(x)) |
| 100 | |
| 101 | #define le16_to_cpu(x) ((uint16_t) le16toh(x)) |
| 102 | #define le32_to_cpu(x) ((uint32_t) le32toh(x)) |
| 103 | #define le64_to_cpu(x) ((uint64_t) le64toh(x)) |
| 104 | |
| 105 | #define be16_to_cpu(x) ((uint16_t) be16toh(x)) |
| 106 | #define be32_to_cpu(x) ((uint32_t) be32toh(x)) |
| 107 | #define be64_to_cpu(x) ((uint64_t) be64toh(x)) |
| 108 | |
| 109 | /* |
| 110 | * Bit map related macros. Usually provided by libc. |
| 111 | */ |
| 112 | #ifndef NBBY |
| 113 | # define NBBY CHAR_BIT |
| 114 | #endif |
| 115 | |
| 116 | #ifndef setbit |
| 117 | # define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY)) |
| 118 | # define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY))) |
| 119 | # define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY))) |
| 120 | # define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0) |
| 121 | #endif |
| 122 | |
| 123 | #endif /* BITOPS_H */ |
| 124 | |