-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.h
132 lines (100 loc) · 2.23 KB
/
common.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef __COMMON_H__
#define __COMMON_H__
#include <stdint.h>
#include <intrin.h>
#define USE_INTRINSICS
#ifdef __BIG_ENDIAN__
static inline uint16_t BE16(uint16_t x)
{
return x;
}
static inline uint32_t BE32(uint32_t x)
{
return x;
}
static inline uint64_t BE64(uint64_t x)
{
return x;
}
static inline uint16_t LE16(uint16_t x)
{
uint16_t ret = (x<<8)&0xFF00;
ret |= ((x>>8)&0xFF);
return ret;
}
static inline uint32_t LE32(uint32_t x)
{
uint32_t ret = (((x) & 0xff) << 24);
ret |= (((x) & 0xff00) << 8);
ret |= (((x) & 0xff0000) >> 8);
ret |= (((x) >> 24) & 0xff);
return ret;
}
static inline uint64_t LE64(uint64_t x)
{
uint64_t ret = (x << 56) & 0xff00000000000000ULL;
ret |= ((x << 40) & 0x00ff000000000000ULL);
ret |= ((x << 24) & 0x0000ff0000000000ULL);
ret |= ((x << 8) & 0x000000ff00000000ULL);
ret |= ((x >> 8) & 0x00000000ff000000ULL);
ret |= ((x >> 24) & 0x0000000000ff0000ULL);
ret |= ((x >> 40) & 0x000000000000ff00ULL);
ret |= ((x >> 56) & 0x00000000000000ffULL);
return ret;
}
#else
#ifdef USE_INTRINSICS
static inline uint16_t BE16(uint16_t x)
{
return _byteswap_ushort(x);
}
static inline uint32_t BE32(uint32_t x)
{
return _byteswap_ulong(x);
}
static inline uint64_t BE64(uint64_t x)
{
return _byteswap_uint64(x);
}
#else
static inline uint16_t BE16(uint16_t x)
{
uint16_t ret = (x<<8)&0xFF00;
ret |= ((x>>8)&0xFF);
return ret;
}
static inline uint32_t BE32(uint32_t x)
{
uint32_t ret = (((x) & 0xff) << 24);
ret |= (((x) & 0xff00) << 8);
ret |= (((x) & 0xff0000) >> 8);
ret |= (((x) >> 24) & 0xff);
return ret;
}
static inline uint64_t BE64(uint64_t x)
{
uint64_t ret = (x << 56) & 0xff00000000000000ULL;
ret |= ((x << 40) & 0x00ff000000000000ULL);
ret |= ((x << 24) & 0x0000ff0000000000ULL);
ret |= ((x << 8) & 0x000000ff00000000ULL);
ret |= ((x >> 8) & 0x00000000ff000000ULL);
ret |= ((x >> 24) & 0x0000000000ff0000ULL);
ret |= ((x >> 40) & 0x000000000000ff00ULL);
ret |= ((x >> 56) & 0x00000000000000ffULL);
return ret;
}
#endif /* USE_INTRINSICS */
static inline uint16_t LE16(uint16_t x)
{
return x;
}
static inline uint32_t LE32(uint32_t x)
{
return x;
}
static inline uint64_t LE64(uint64_t x)
{
return x;
}
#endif /* __BIG_ENDIAN__ */
#endif /* __COMMON_H__ */