blob: 34bef6e3fbe6f035dc07c7dbed621348aeeb0fcc [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 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 LITTLE_ENDIAN
29#define LITTLE_ENDIAN 1234
30#endif
31#ifndef BIG_ENDIAN
32#define BIG_ENDIAN 4321
33#endif
34
35#if __POWERPC__
36#include <ppc_intrinsics.h>
37#endif
38
39#if defined(ARCH_ARM)
40#define BYTE_ORDER LITTLE_ENDIAN
41#endif
42
Corey Tabaka84697242009-03-26 02:32:01 -040043#if defined(__i386__) || defined(_X86_)
44#define BYTE_ORDER LITTLE_ENDIAN
45#endif
46
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070047#ifndef BYTE_ORDER
48#error "need to get the BYTE_ORDER define from somewhere"
49#endif
50
51// define a macro that unconditionally swaps
52#define SWAP_32(x) \
53 (((uint32_t)(x) << 24) | (((uint32_t)(x) & 0xff00) << 8) |(((uint32_t)(x) & 0x00ff0000) >> 8) | ((uint32_t)(x) >> 24))
54#define SWAP_16(x) \
55 ((((uint16_t)(x) & 0xff) << 8) | ((uint16_t)(x) >> 8))
56
57// standard swap macros
58#if BYTE_ORDER == BIG_ENDIAN
59#define LE32(val) SWAP_32(val)
60#define LE16(val) SWAP_16(val)
61#define BE32(val) (val)
62#define BE16(val) (val)
63#else
64#define LE32(val) (val)
65#define LE16(val) (val)
66#define BE32(val) SWAP_32(val)
67#define BE16(val) SWAP_16(val)
68#endif
69
70#define LE32SWAP(var) (var) = LE32(var);
71#define LE16SWAP(var) (var) = LE16(var);
72#define BE32SWAP(var) (var) = BE32(var);
73#define BE16SWAP(var) (var) = BE16(var);
74
75/* classic network byte swap stuff */
76#define ntohs(n) BE16(n)
77#define htons(h) BE16(h)
78#define ntohl(n) BE32(n)
79#define htonl(h) BE32(h)
80
81// some memory access macros
82#if __POWERPC__
83#define READ_MEM_WORD(ptr) __lwbrx((word *)(ptr), 0)
84#define READ_MEM_HALFWORD(ptr) __lhbrx((halfword *)(ptr), 0)
85#define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
86#define WRITE_MEM_WORD(ptr, data) __stwbrx(data, (word *)(ptr), 0)
87#define WRITE_MEM_HALFWORD(ptr, data) __sthbrx(data, (halfword *)(ptr), 0)
88#define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
89#else
90#define READ_MEM_WORD(ptr) SWAPIT_32(*(word *)(ptr))
91#define READ_MEM_HALFWORD(ptr) SWAPIT_16(*(halfword *)(ptr))
92#define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
93#define WRITE_MEM_WORD(ptr, data) (*(word *)(ptr) = SWAPIT_32(data))
94#define WRITE_MEM_HALFWORD(ptr, data) (*(halfword *)(ptr) = SWAPIT_16(data))
95#define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
96#endif
97
98
99#endif