1 /*
2  * Copyright (c) 2008-2014 Travis Geiselbrecht
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #ifndef __ENDIAN_H
24 #define __ENDIAN_H
25 
26 #include <sys/types.h>
27 
28 #ifndef __BYTE_ORDER__
29 #error Compiler does not provide __BYTE_ORDER__
30 #endif
31 
32 /* the compiler provides it, use what it says */
33 #define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
34 #define BIG_ENDIAN __ORDER_BIG_ENDIAN__
35 #define BYTE_ORDER __BYTE_ORDER__
36 
37 // define a macro that unconditionally swaps
38 #define SWAP_32(x) \
39     (((uint32_t)(x) << 24) | (((uint32_t)(x) & 0xff00) << 8) |(((uint32_t)(x) & 0x00ff0000) >> 8) | ((uint32_t)(x) >> 24))
40 #define SWAP_16(x) \
41     ((((uint16_t)(x) & 0xff) << 8) | ((uint16_t)(x) >> 8))
42 #define SWAP_64(x) ((((uint64_t)(SWAP_32((uint64_t)(x)))) << 32) | (SWAP_32(((uint64_t)(x)) >> 32)))
43 
44 // standard swap macros
45 #if BYTE_ORDER == BIG_ENDIAN
46 #define LE64(val) SWAP_64(val)
47 #define LE32(val) SWAP_32(val)
48 #define LE16(val) SWAP_16(val)
49 #define BE64(val) (val)
50 #define BE32(val) (val)
51 #define BE16(val) (val)
52 #else
53 #define LE64(val) (val)
54 #define LE32(val) (val)
55 #define LE16(val) (val)
56 #define BE64(val) SWAP_64(val)
57 #define BE32(val) SWAP_32(val)
58 #define BE16(val) SWAP_16(val)
59 #endif
60 
61 #define LE32SWAP(var) (var) = LE32(var);
62 #define LE16SWAP(var) (var) = LE16(var);
63 #define BE32SWAP(var) (var) = BE32(var);
64 #define BE16SWAP(var) (var) = BE16(var);
65 
66 /* standard endian.h stuff */
67 #define htobe16(n) BE16(n)
68 #define be16toh(n) BE16(n)
69 #define htobe32(n) BE32(n)
70 #define be32toh(n) BE32(n)
71 
72 /* classic network byte swap stuff */
73 #define ntohs(n) BE16(n)
74 #define htons(h) BE16(h)
75 #define ntohl(n) BE32(n)
76 #define htonl(h) BE32(h)
77 
78 /* 64-bit network byte swap stuff */
79 #define htobe64(h) BE64(h)
80 #define be64toh(b) BE64(b)
81 
82 // some memory access macros
83 #if __POWERPC__
84 #include <ppc_intrinsics.h>
85 
86 #define READ_MEM_WORD(ptr)      __lwbrx((word *)(ptr), 0)
87 #define READ_MEM_HALFWORD(ptr)  __lhbrx((halfword *)(ptr), 0)
88 #define READ_MEM_BYTE(ptr)      (*(byte *)(ptr))
89 #define WRITE_MEM_WORD(ptr, data)   __stwbrx(data, (word *)(ptr), 0)
90 #define WRITE_MEM_HALFWORD(ptr, data)   __sthbrx(data, (halfword *)(ptr), 0)
91 #define WRITE_MEM_BYTE(ptr, data)   (*(byte *)(ptr) = (data))
92 #else
93 #define READ_MEM_WORD(ptr)      SWAPIT_32(*(word *)(ptr))
94 #define READ_MEM_HALFWORD(ptr)  SWAPIT_16(*(halfword *)(ptr))
95 #define READ_MEM_BYTE(ptr)      (*(byte *)(ptr))
96 #define WRITE_MEM_WORD(ptr, data)   (*(word *)(ptr) = SWAPIT_32(data))
97 #define WRITE_MEM_HALFWORD(ptr, data)   (*(halfword *)(ptr) = SWAPIT_16(data))
98 #define WRITE_MEM_BYTE(ptr, data)   (*(byte *)(ptr) = (data))
99 #endif
100 
101 
102 #endif
103