blob: 90da5eabb3b63681e41d7fb92bf7ce66bcf99b1e [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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/* classic network byte swap stuff */
67#define ntohs(n) BE16(n)
68#define htons(h) BE16(h)
69#define ntohl(n) BE32(n)
70#define htonl(h) BE32(h)
71
72/* 64-bit network byte swap stuff */
73#define htobe64(h) BE64(h)
74#define be64toh(b) BE64(b)
75
76// some memory access macros
77#if __POWERPC__
78#include <ppc_intrinsics.h>
79
80#define READ_MEM_WORD(ptr) __lwbrx((word *)(ptr), 0)
81#define READ_MEM_HALFWORD(ptr) __lhbrx((halfword *)(ptr), 0)
82#define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
83#define WRITE_MEM_WORD(ptr, data) __stwbrx(data, (word *)(ptr), 0)
84#define WRITE_MEM_HALFWORD(ptr, data) __sthbrx(data, (halfword *)(ptr), 0)
85#define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
86#else
87#define READ_MEM_WORD(ptr) SWAPIT_32(*(word *)(ptr))
88#define READ_MEM_HALFWORD(ptr) SWAPIT_16(*(halfword *)(ptr))
89#define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
90#define WRITE_MEM_WORD(ptr, data) (*(word *)(ptr) = SWAPIT_32(data))
91#define WRITE_MEM_HALFWORD(ptr, data) (*(halfword *)(ptr) = SWAPIT_16(data))
92#define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
93#endif
94
95
96#endif