blob: fe41a6cf87ccc9598b4fc1566fe5e5167ef15336 [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
Deepa Dinamani15ef6632013-09-12 10:26:34 -070052#define SWAP_64(x) \
53 ((((x) & 0xff00000000000000ull) >> 56) \
54 | (((x) & 0x00ff000000000000ull) >> 40) \
55 | (((x) & 0x0000ff0000000000ull) >> 24) \
56 | (((x) & 0x000000ff00000000ull) >> 8) \
57 | (((x) & 0x00000000ff000000ull) << 8) \
58 | (((x) & 0x0000000000ff0000ull) << 24) \
59 | (((x) & 0x000000000000ff00ull) << 40) \
60 | (((x) & 0x00000000000000ffull) << 56))
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070061#define SWAP_32(x) \
62 (((uint32_t)(x) << 24) | (((uint32_t)(x) & 0xff00) << 8) |(((uint32_t)(x) & 0x00ff0000) >> 8) | ((uint32_t)(x) >> 24))
63#define SWAP_16(x) \
64 ((((uint16_t)(x) & 0xff) << 8) | ((uint16_t)(x) >> 8))
65
66// standard swap macros
67#if BYTE_ORDER == BIG_ENDIAN
Deepa Dinamani15ef6632013-09-12 10:26:34 -070068#define LE64(val) SWAP_64(val)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070069#define LE32(val) SWAP_32(val)
70#define LE16(val) SWAP_16(val)
Deepa Dinamani15ef6632013-09-12 10:26:34 -070071#define BE64(val) (val)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070072#define BE32(val) (val)
73#define BE16(val) (val)
74#else
Deepa Dinamani15ef6632013-09-12 10:26:34 -070075#define LE64(val) (val)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070076#define LE32(val) (val)
77#define LE16(val) (val)
Deepa Dinamani15ef6632013-09-12 10:26:34 -070078#define BE64(val) SWAP_64(val)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070079#define BE32(val) SWAP_32(val)
80#define BE16(val) SWAP_16(val)
81#endif
82
Deepa Dinamani15ef6632013-09-12 10:26:34 -070083#define LE64SWAP(var) (var) = LE64(var);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070084#define LE32SWAP(var) (var) = LE32(var);
85#define LE16SWAP(var) (var) = LE16(var);
Deepa Dinamani15ef6632013-09-12 10:26:34 -070086#define BE64SWAP(var) (var) = BE64(var);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070087#define BE32SWAP(var) (var) = BE32(var);
88#define BE16SWAP(var) (var) = BE16(var);
89
90/* classic network byte swap stuff */
91#define ntohs(n) BE16(n)
92#define htons(h) BE16(h)
93#define ntohl(n) BE32(n)
94#define htonl(h) BE32(h)
95
96// some memory access macros
97#if __POWERPC__
98#define READ_MEM_WORD(ptr) __lwbrx((word *)(ptr), 0)
99#define READ_MEM_HALFWORD(ptr) __lhbrx((halfword *)(ptr), 0)
100#define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
101#define WRITE_MEM_WORD(ptr, data) __stwbrx(data, (word *)(ptr), 0)
102#define WRITE_MEM_HALFWORD(ptr, data) __sthbrx(data, (halfword *)(ptr), 0)
103#define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
104#else
105#define READ_MEM_WORD(ptr) SWAPIT_32(*(word *)(ptr))
106#define READ_MEM_HALFWORD(ptr) SWAPIT_16(*(halfword *)(ptr))
107#define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
108#define WRITE_MEM_WORD(ptr, data) (*(word *)(ptr) = SWAPIT_32(data))
109#define WRITE_MEM_HALFWORD(ptr, data) (*(halfword *)(ptr) = SWAPIT_16(data))
110#define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
111#endif
112
113
114#endif