agssim: implement call/ret instructions
[rofl0r-agsutils.git] / endianness.h
blob074b8a477074ce73a797a9d1bd7507b7c27cfec3
1 /*
2 * Endianness.h
4 * Created on: 29.11.2010
5 *
6 * author: rofl0r
7 *
8 * License: Public Domain
9 *
12 #ifndef ENDIANNESS_H_
13 #define ENDIANNESS_H_
15 #ifdef __linux
16 #include <endian.h>
17 #else
18 #include <machine/endian.h>
19 #endif
21 #if (!defined(__BYTE_ORDER)) && (!defined(BYTE_ORDER))
22 #error "no __BYTE_ORDER macro found!"
23 #endif
25 #ifndef __BYTE_ORDER
26 #define __BYTE_ORDER BYTE_ORDER
27 #endif
29 #if (!defined(__LITTLE_ENDIAN)) && (!defined(LITTLE_ENDIAN))
30 #error "no __LITTLE_ENDIAN macro found!"
31 #endif
33 #ifndef __LITTLE_ENDIAN
34 #define __LITTLE_ENDIAN LITTLE_ENDIAN
35 #define __BIG_ENDIAN BIG_ENDIAN
36 #endif
39 #if __BYTE_ORDER == __LITTLE_ENDIAN
40 #define IS_LITTLE_ENDIAN
41 #endif
43 #define byteswap16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
45 #define byteswap32(x) \
46 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
47 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
49 #include <stdint.h>
50 static inline uint64_t byteswap64(uint64_t x) {
51 union {
52 uint64_t ll;
53 uint32_t l[2];
54 } w, r;
55 w.ll = x;
56 r.l[0] = byteswap32 (w.l[1]);
57 r.l[1] = byteswap32 (w.l[0]);
58 return r.ll;
61 #ifdef IS_LITTLE_ENDIAN
62 #define le32(X) (X)
63 #define le16(X) (X)
64 #define be32(X) byteswap32(X)
65 #define be16(X) byteswap16(X)
66 #define end_htole32(X) (X)
67 #define end_htole16(X) (X)
68 #else
69 #define le32(X) byteswap32(X)
70 #define le16(X) byteswap16(X)
71 #define be32(X) (X)
72 #define be16(X) (X)
73 #define end_htole32(X) byteswap32(X)
74 #define end_htole16(X) byteswap16(X)
75 #endif
77 #endif /* ENDIANNESS_H_ */