block: stage extroot mounts in /tmp/extroot since /tmp/overlay might be in use by...
[project/ubox.git] / libblkid-tiny / bitops.h
1 #ifndef BITOPS_H
2 #define BITOPS_H
3
4 #include <stdint.h>
5
6 /*
7  * Bit map related macros. Usually provided by libc.
8  */
9 #include <sys/param.h>
10
11 #ifndef NBBY
12 # define NBBY            CHAR_BIT
13 #endif
14
15 #ifndef setbit
16 # define setbit(a,i)    ((a)[(i)/NBBY] |= 1<<((i)%NBBY))
17 # define clrbit(a,i)    ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
18 # define isset(a,i)     ((a)[(i)/NBBY] & (1<<((i)%NBBY)))
19 # define isclr(a,i)     (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
20 #endif
21
22 /*
23  * Byte swab macros (based on linux/byteorder/swab.h)
24  */
25 #define swab16(x) \
26         ((uint16_t)( \
27                 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
28                 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ))
29
30 #define swab32(x) \
31         ((uint32_t)( \
32                 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
33                 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) <<  8) | \
34                 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >>  8) | \
35                 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) ))
36
37 #define swab64(x) \
38         ((uint64_t)( \
39                 (uint64_t)(((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
40                 (uint64_t)(((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
41                 (uint64_t)(((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
42                 (uint64_t)(((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) <<  8) | \
43                 (uint64_t)(((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >>  8) | \
44                 (uint64_t)(((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
45                 (uint64_t)(((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
46                 (uint64_t)(((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) ))
47
48
49 #if __BYTE_ORDER == __BIG_ENDIAN
50
51 #define cpu_to_le16(x) swab16(x)
52 #define cpu_to_le32(x) swab32(x)
53 #define cpu_to_le64(x) swab64(x)
54 #define cpu_to_be16(x) ((uint16_t)(x))
55 #define cpu_to_be32(x) ((uint32_t)(x))
56 #define cpu_to_be64(x) ((uint64_t)(x))
57
58 #define le16_to_cpu(x) swab16(x)
59 #define le32_to_cpu(x) swab32(x)
60 #define le64_to_cpu(x) swab64(x)
61 #define be16_to_cpu(x) ((uint16_t)(x))
62 #define be32_to_cpu(x) ((uint32_t)(x))
63 #define be64_to_cpu(x) ((uint64_t)(x))
64
65 #else /* !WORDS_BIGENDIAN */
66
67 #define cpu_to_le16(x) ((uint16_t)(x))
68 #define cpu_to_le32(x) ((uint32_t)(x))
69 #define cpu_to_le64(x) ((uint64_t)(x))
70 #define cpu_to_be16(x) swab16(x)
71 #define cpu_to_be32(x) swab32(x)
72 #define cpu_to_be64(x) swab64(x)
73
74 #define le16_to_cpu(x) ((uint16_t)(x))
75 #define le32_to_cpu(x) ((uint32_t)(x))
76 #define le64_to_cpu(x) ((uint64_t)(x))
77 #define be16_to_cpu(x) swab16(x)
78 #define be32_to_cpu(x) swab32(x)
79 #define be64_to_cpu(x) swab64(x)
80
81 #endif /* WORDS_BIGENDIAN */
82
83 #endif /* BITOPS_H */
84