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