-
Notifications
You must be signed in to change notification settings - Fork 9
/
endian.h
61 lines (54 loc) · 1.85 KB
/
endian.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
// Copyright (c) 2008 David Caldwell, All Rights Reserved.
#ifndef __ENDIAN_H__
#define __ENDIAN_H__
#ifdef __APPLE__
# include <machine/endian.h>
#endif
#ifdef __LINUX__
# error "LINUX"
# define __USE_BSD
# include <endian.h>
#endif
#if BYTE_ORDER == BIG_ENDIAN
# define to_le16(x) __swap16(x)
# define to_le32(x) __swap32(x)
# define to_le64(x) __swap64(x)
# define from_le16(x) __swap16(x)
# define from_le32(x) __swap32(x)
# define from_le64(x) __swap64(x)
# define to_be16(x) (x)
# define to_be32(x) (x)
# define to_be64(x) (x)
# define from_be16(x) (x)
# define from_be32(x) (x)
# define from_be64(x) (x)
#else
# define to_le16(x) (x)
# define to_le32(x) (x)
# define to_le64(x) (x)
# define from_le16(x) (x)
# define from_le32(x) (x)
# define from_le64(x) (x)
# define to_be16(x) __swap16(x)
# define to_be32(x) __swap32(x)
# define to_be64(x) __swap64(x)
# define from_be16(x) __swap16(x)
# define from_be32(x) __swap32(x)
# define from_be64(x) __swap64(x)
#endif
// Don't use these directly. The above macros are better for self-documentation
#define __swap16(x) ((x) << 8 & 0xff00 | \
(x) >> 8 & 0x00ff)
#define __swap32(x) ((x) << 24 & 0xff000000 | \
(x) << 8 & 0x00ff0000 | \
(x) >> 8 & 0x0000ff00 | \
(x) >> 24 & 0x000000ff)
#define __swap64(x) ((x) << 56 & 0xff00000000000000LL | \
(x) << 40 & 0x00ff000000000000LL | \
(x) << 24 & 0x0000ff0000000000LL | \
(x) << 8 & 0x000000ff00000000LL | \
(x) >> 8 & 0x00000000ff000000LL | \
(x) >> 24 & 0x0000000000ff0000LL | \
(x) >> 40 & 0x000000000000ff00LL | \
(x) >> 56 & 0x00000000000000ffLL)
#endif /* __ENDIAN_H__ */