xref: /freebsd/sys/powerpc/include/endian.h (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
1fd8e4ebcSMike Barcroft /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
4fa561e1eSDavid E. O'Brien  * Copyright (c) 1987, 1991, 1993
5fa561e1eSDavid E. O'Brien  *	The Regents of the University of California.  All rights reserved.
6fa561e1eSDavid E. O'Brien  *
7fa561e1eSDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
8fa561e1eSDavid E. O'Brien  * modification, are permitted provided that the following conditions
9fa561e1eSDavid E. O'Brien  * are met:
10fa561e1eSDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
11fa561e1eSDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
12fa561e1eSDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
13fa561e1eSDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
14fa561e1eSDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16fa561e1eSDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
17fa561e1eSDavid E. O'Brien  *    without specific prior written permission.
18fa561e1eSDavid E. O'Brien  *
19fa561e1eSDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20fa561e1eSDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21fa561e1eSDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22fa561e1eSDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23fa561e1eSDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24fa561e1eSDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25fa561e1eSDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26fa561e1eSDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27fa561e1eSDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28fa561e1eSDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29fa561e1eSDavid E. O'Brien  * SUCH DAMAGE.
30fa561e1eSDavid E. O'Brien  *
31fa561e1eSDavid E. O'Brien  *	@(#)endian.h	8.1 (Berkeley) 6/10/93
32fa561e1eSDavid E. O'Brien  * $FreeBSD$
33fa561e1eSDavid E. O'Brien  */
34fa561e1eSDavid E. O'Brien 
35fd8e4ebcSMike Barcroft #ifndef _MACHINE_ENDIAN_H_
36fd8e4ebcSMike Barcroft #define	_MACHINE_ENDIAN_H_
37fd8e4ebcSMike Barcroft 
38795aff0eSMike Barcroft #include <sys/cdefs.h>
39abbd8902SMike Barcroft #include <sys/_types.h>
40fa561e1eSDavid E. O'Brien 
41fa561e1eSDavid E. O'Brien /*
42fa561e1eSDavid E. O'Brien  * Define the order of 32-bit words in 64-bit words.
43fa561e1eSDavid E. O'Brien  */
440558e4bbSNathan Whitehorn #ifdef __LITTLE_ENDIAN__
450558e4bbSNathan Whitehorn #define	_QUAD_HIGHWORD 1
460558e4bbSNathan Whitehorn #define	_QUAD_LOWWORD 0
470558e4bbSNathan Whitehorn #else
48fa561e1eSDavid E. O'Brien #define	_QUAD_HIGHWORD 0
49fa561e1eSDavid E. O'Brien #define	_QUAD_LOWWORD 1
500558e4bbSNathan Whitehorn #endif
51fa561e1eSDavid E. O'Brien 
52fa561e1eSDavid E. O'Brien /*
53636f2ebfSMarcel Moolenaar  * GCC defines _BIG_ENDIAN and _LITTLE_ENDIAN equal to __BIG_ENDIAN__
54636f2ebfSMarcel Moolenaar  * and __LITTLE_ENDIAN__ (resp).
55636f2ebfSMarcel Moolenaar  */
56636f2ebfSMarcel Moolenaar #ifdef _BIG_ENDIAN
57636f2ebfSMarcel Moolenaar #undef _BIG_ENDIAN
58636f2ebfSMarcel Moolenaar #endif
59636f2ebfSMarcel Moolenaar #ifdef _LITTLE_ENDIAN
60636f2ebfSMarcel Moolenaar #undef _LITTLE_ENDIAN
61636f2ebfSMarcel Moolenaar #endif
62636f2ebfSMarcel Moolenaar 
63636f2ebfSMarcel Moolenaar /*
64fa561e1eSDavid E. O'Brien  * Definitions for byte order, according to byte significance from low
65fa561e1eSDavid E. O'Brien  * address to high.
66fa561e1eSDavid E. O'Brien  */
677f0f1cfdSMike Barcroft #define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
687f0f1cfdSMike Barcroft #define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
697f0f1cfdSMike Barcroft #define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
70fa561e1eSDavid E. O'Brien 
71636f2ebfSMarcel Moolenaar #ifdef __LITTLE_ENDIAN__
72636f2ebfSMarcel Moolenaar #define	_BYTE_ORDER	_LITTLE_ENDIAN
73636f2ebfSMarcel Moolenaar #else
747f0f1cfdSMike Barcroft #define	_BYTE_ORDER	_BIG_ENDIAN
75636f2ebfSMarcel Moolenaar #endif
767f0f1cfdSMike Barcroft 
777f0f1cfdSMike Barcroft /*
787f0f1cfdSMike Barcroft  * Deprecated variants that don't have enough underscores to be useful in more
797f0f1cfdSMike Barcroft  * strict namespaces.
807f0f1cfdSMike Barcroft  */
817f0f1cfdSMike Barcroft #if __BSD_VISIBLE
827f0f1cfdSMike Barcroft #define	LITTLE_ENDIAN	_LITTLE_ENDIAN
837f0f1cfdSMike Barcroft #define	BIG_ENDIAN	_BIG_ENDIAN
847f0f1cfdSMike Barcroft #define	PDP_ENDIAN	_PDP_ENDIAN
857f0f1cfdSMike Barcroft #define	BYTE_ORDER	_BYTE_ORDER
867f0f1cfdSMike Barcroft #endif
87fa561e1eSDavid E. O'Brien 
88e2c3e32aSNathan Whitehorn #if defined(__GNUCLIKE_BUILTIN_CONSTANT_P)
89e2c3e32aSNathan Whitehorn #define	__is_constant(x)	__builtin_constant_p(x)
90e2c3e32aSNathan Whitehorn #else
91e2c3e32aSNathan Whitehorn #define	__is_constant(x)	0
92e2c3e32aSNathan Whitehorn #endif
93e2c3e32aSNathan Whitehorn 
94e2c3e32aSNathan Whitehorn #define	__bswap16_const(x)	((((__uint16_t)(x) >> 8) & 0xff) |	\
95e2c3e32aSNathan Whitehorn 	(((__uint16_t)(x) << 8) & 0xff00))
96e2c3e32aSNathan Whitehorn #define	__bswap32_const(x)	((((__uint32_t)(x) >> 24) & 0xff) |	\
97e2c3e32aSNathan Whitehorn 	(((__uint32_t)(x) >> 8) & 0xff00) |				\
98e2c3e32aSNathan Whitehorn 	(((__uint32_t)(x)<< 8) & 0xff0000) |				\
99e2c3e32aSNathan Whitehorn 	(((__uint32_t)(x) << 24) & 0xff000000))
100e2c3e32aSNathan Whitehorn #define	__bswap64_const(x)	((((__uint64_t)(x) >> 56) & 0xff) |	\
101e2c3e32aSNathan Whitehorn 	(((__uint64_t)(x) >> 40) & 0xff00) |				\
102e2c3e32aSNathan Whitehorn 	(((__uint64_t)(x) >> 24) & 0xff0000) |				\
103e2c3e32aSNathan Whitehorn 	(((__uint64_t)(x) >> 8) & 0xff000000) |				\
104e2c3e32aSNathan Whitehorn 	(((__uint64_t)(x) << 8) & ((__uint64_t)0xff << 32)) |		\
105e2c3e32aSNathan Whitehorn 	(((__uint64_t)(x) << 24) & ((__uint64_t)0xff << 40)) |		\
106e2c3e32aSNathan Whitehorn 	(((__uint64_t)(x) << 40) & ((__uint64_t)0xff << 48)) |		\
107e2c3e32aSNathan Whitehorn 	(((__uint64_t)(x) << 56) & ((__uint64_t)0xff << 56)))
108d846855dSMike Barcroft 
109d846855dSMike Barcroft static __inline __uint16_t
110e2c3e32aSNathan Whitehorn __bswap16_var(__uint16_t _x)
111d846855dSMike Barcroft {
112d846855dSMike Barcroft 
113d846855dSMike Barcroft 	return ((_x >> 8) | ((_x << 8) & 0xff00));
114d846855dSMike Barcroft }
115d846855dSMike Barcroft 
116d846855dSMike Barcroft static __inline __uint32_t
117e2c3e32aSNathan Whitehorn __bswap32_var(__uint32_t _x)
118d846855dSMike Barcroft {
119d846855dSMike Barcroft 
120d846855dSMike Barcroft 	return ((_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) |
121d846855dSMike Barcroft 	    ((_x << 24) & 0xff000000));
122d846855dSMike Barcroft }
123d846855dSMike Barcroft 
124d846855dSMike Barcroft static __inline __uint64_t
125e2c3e32aSNathan Whitehorn __bswap64_var(__uint64_t _x)
126d846855dSMike Barcroft {
127d846855dSMike Barcroft 
128d846855dSMike Barcroft 	return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
129d846855dSMike Barcroft 	    ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
130d846855dSMike Barcroft 	    ((_x << 24) & ((__uint64_t)0xff << 40)) |
131d846855dSMike Barcroft 	    ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
132d846855dSMike Barcroft }
133d846855dSMike Barcroft 
13463d094a7SDimitry Andric #define	__bswap16(x)	((__uint16_t)(__is_constant(x) ? __bswap16_const(x) : \
13563d094a7SDimitry Andric 	__bswap16_var(x)))
136e2c3e32aSNathan Whitehorn #define	__bswap32(x)	(__is_constant(x) ? __bswap32_const(x) : \
137e2c3e32aSNathan Whitehorn 	__bswap32_var(x))
138e2c3e32aSNathan Whitehorn #define	__bswap64(x)	(__is_constant(x) ? __bswap64_const(x) : \
139e2c3e32aSNathan Whitehorn 	__bswap64_var(x))
140e2c3e32aSNathan Whitehorn 
1410558e4bbSNathan Whitehorn #ifdef __LITTLE_ENDIAN__
1420558e4bbSNathan Whitehorn #define	__htonl(x)	(__bswap32((__uint32_t)(x)))
1430558e4bbSNathan Whitehorn #define	__htons(x)	(__bswap16((__uint16_t)(x)))
1440558e4bbSNathan Whitehorn #define	__ntohl(x)	(__bswap32((__uint32_t)(x)))
1450558e4bbSNathan Whitehorn #define	__ntohs(x)	(__bswap16((__uint16_t)(x)))
1460558e4bbSNathan Whitehorn #else
147d846855dSMike Barcroft #define	__htonl(x)	((__uint32_t)(x))
148d846855dSMike Barcroft #define	__htons(x)	((__uint16_t)(x))
149d846855dSMike Barcroft #define	__ntohl(x)	((__uint32_t)(x))
150d846855dSMike Barcroft #define	__ntohs(x)	((__uint16_t)(x))
1510558e4bbSNathan Whitehorn #endif
152fa561e1eSDavid E. O'Brien 
153fd8e4ebcSMike Barcroft #endif /* !_MACHINE_ENDIAN_H_ */
154