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