-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyteorder.h
108 lines (94 loc) · 2.33 KB
/
byteorder.h
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
/*
* byteorder.h
*
* LGPL 2
*/
#ifndef CEPH_BYTEORDER_H
#define CEPH_BYTEORDER_H
#include <sys/param.h>
#include "int_types.h"
#if defined(__APPLE__)
# if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
# define CEPH_LITTLE_ENDIAN
# elif __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN
# define CEPH_BIG_ENDIAN
# endif
#endif
#if defined(__FreeBSD__)
# if _BYTE_ORDER == _LITTLE_ENDIAN
# define CEPH_LITTLE_ENDIAN
# elif _BYTE_ORDER == _BIG_ENDIAN
# define CEPH_BIG_ENDIAN
# endif
#endif
#if defined(__linux__)
# if BYTE_ORDER == LITTLE_ENDIAN
# define CEPH_LITTLE_ENDIAN
# elif BYTE_ORDER == BIG_ENDIAN
# define CEPH_BIG_ENDIAN
# endif
#endif
static __inline__ __u16 swab16(__u16 val)
{
return (val >> 8) | (val << 8);
}
static __inline__ __u32 swab32(__u32 val)
{
return (( val >> 24) |
((val >> 8) & 0xff00) |
((val << 8) & 0xff0000) |
((val << 24)));
}
static __inline__ uint64_t swab64(uint64_t val)
{
return (( val >> 56) |
((val >> 40) & 0xff00ull) |
((val >> 24) & 0xff0000ull) |
((val >> 8) & 0xff000000ull) |
((val << 8) & 0xff00000000ull) |
((val << 24) & 0xff0000000000ull) |
((val << 40) & 0xff000000000000ull) |
((val << 56)));
}
// mswab == maybe swab (if not LE)
#ifdef CEPH_BIG_ENDIAN
# define mswab64(a) swab64(a)
# define mswab32(a) swab32(a)
# define mswab16(a) swab16(a)
#elif defined(CEPH_LITTLE_ENDIAN)
# define mswab64(a) (a)
# define mswab32(a) (a)
# define mswab16(a) (a)
#else
# error "Could not determine endianess"
#endif
#ifdef __cplusplus
#define MAKE_LE_CLASS(bits) \
struct ceph_le##bits { \
__u##bits v; \
ceph_le##bits &operator=(__u##bits nv) { \
v = mswab##bits(nv); \
return *this; \
} \
operator __u##bits() const { return mswab##bits(v); } \
} __attribute__ ((packed)); \
static inline bool operator==(ceph_le##bits a, ceph_le##bits b) { \
return a.v == b.v; \
}
MAKE_LE_CLASS(64)
MAKE_LE_CLASS(32)
MAKE_LE_CLASS(16)
#undef MAKE_LE_CLASS
#endif /* __cplusplus */
#define init_le64(x) { (__u64)mswab64(x) }
#define init_le32(x) { (__u32)mswab32(x) }
#define init_le16(x) { (__u16)mswab16(x) }
/*
#define cpu_to_le64(x) (x)
#define cpu_to_le32(x) (x)
#define cpu_to_le16(x) (x)
*/
#define le64_to_cpu(x) ((uint64_t)x)
#define le32_to_cpu(x) ((__u32)x)
#define le16_to_cpu(x) ((__u16)x)
#endif