blob: 30623b208a9eaec696b8dc9f909af30e97b15838 [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
43#ifndef BYTE_ORDER
44#error "need to get the BYTE_ORDER define from somewhere"
45#endif
46
47// define a macro that unconditionally swaps
48#define SWAP_32(x) \
49 (((uint32_t)(x) << 24) | (((uint32_t)(x) & 0xff00) << 8) |(((uint32_t)(x) & 0x00ff0000) >> 8) | ((uint32_t)(x) >> 24))
50#define SWAP_16(x) \
51 ((((uint16_t)(x) & 0xff) << 8) | ((uint16_t)(x) >> 8))
52
53// standard swap macros
54#if BYTE_ORDER == BIG_ENDIAN
55#define LE32(val) SWAP_32(val)
56#define LE16(val) SWAP_16(val)
57#define BE32(val) (val)
58#define BE16(val) (val)
59#else
60#define LE32(val) (val)
61#define LE16(val) (val)
62#define BE32(val) SWAP_32(val)
63#define BE16(val) SWAP_16(val)
64#endif
65
66#define LE32SWAP(var) (var) = LE32(var);
67#define LE16SWAP(var) (var) = LE16(var);
68#define BE32SWAP(var) (var) = BE32(var);
69#define BE16SWAP(var) (var) = BE16(var);
70
71/* classic network byte swap stuff */
72#define ntohs(n) BE16(n)
73#define htons(h) BE16(h)
74#define ntohl(n) BE32(n)
75#define htonl(h) BE32(h)
76
77// some memory access macros
78#if __POWERPC__
79#define READ_MEM_WORD(ptr) __lwbrx((word *)(ptr), 0)
80#define READ_MEM_HALFWORD(ptr) __lhbrx((halfword *)(ptr), 0)
81#define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
82#define WRITE_MEM_WORD(ptr, data) __stwbrx(data, (word *)(ptr), 0)
83#define WRITE_MEM_HALFWORD(ptr, data) __sthbrx(data, (halfword *)(ptr), 0)
84#define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
85#else
86#define READ_MEM_WORD(ptr) SWAPIT_32(*(word *)(ptr))
87#define READ_MEM_HALFWORD(ptr) SWAPIT_16(*(halfword *)(ptr))
88#define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
89#define WRITE_MEM_WORD(ptr, data) (*(word *)(ptr) = SWAPIT_32(data))
90#define WRITE_MEM_HALFWORD(ptr, data) (*(halfword *)(ptr) = SWAPIT_16(data))
91#define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
92#endif
93
94
95#endif