-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathendian.h
More file actions
56 lines (42 loc) · 1.61 KB
/
endian.h
File metadata and controls
56 lines (42 loc) · 1.61 KB
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
#include <bits/types.h>
inline static uint16_t swabs(uint16_t n)
{
return ((n & 0xFF) << 8) | ((n >> 8) & 0xFF);
}
inline static uint32_t swabl(uint32_t n)
{
return (((n >> 0) & 0xFF) << 24)
| (((n >> 8) & 0xFF) << 16)
| (((n >> 16) & 0xFF) << 8)
| (((n >> 24) & 0xFF) << 0);
}
inline static uint64_t swabx(uint64_t n)
{
return (((n >> 0*8) & 0xFF) << 7*8)
| (((n >> 1*8) & 0xFF) << 6*8)
| (((n >> 2*8) & 0xFF) << 5*8)
| (((n >> 3*8) & 0xFF) << 4*8)
| (((n >> 4*8) & 0xFF) << 3*8)
| (((n >> 5*8) & 0xFF) << 2*8)
| (((n >> 6*8) & 0xFF) << 1*8)
| (((n >> 7*8) & 0xFF) << 0*8);
}
#ifdef BIGENDIAN
inline static uint16_t htons(uint16_t n) { return n; }
inline static uint32_t htonl(uint32_t n) { return n; }
inline static uint16_t ntohs(uint16_t n) { return n; }
inline static uint32_t ntohl(uint32_t n) { return n; }
inline static uint16_t itohs(uint16_t n) { return swabs(n); }
inline static uint32_t itohl(uint32_t n) { return swabl(n); }
inline static uint32_t htois(uint16_t n) { return swabs(n); }
inline static uint32_t htoil(uint32_t n) { return swabl(n); }
#else
inline static uint16_t htons(uint16_t n) { return swabs(n); }
inline static uint32_t htonl(uint32_t n) { return swabl(n); }
inline static uint16_t ntohs(uint16_t n) { return swabs(n); }
inline static uint32_t ntohl(uint32_t n) { return swabl(n); }
inline static uint16_t itohs(uint16_t n) { return n; }
inline static uint32_t itohl(uint32_t n) { return n; }
inline static uint32_t htois(uint16_t n) { return n; }
inline static uint32_t htoil(uint32_t n) { return n; }
#endif