xref: /freebsd/sys/powerpc/include/endian.h (revision d846855da8c8142a2f647f7a4c6949b341261297)
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.
13fa561e1eSDavid E. O'Brien  * 3. All advertising materials mentioning features or use of this software
14fa561e1eSDavid E. O'Brien  *    must display the following acknowledgement:
15fa561e1eSDavid E. O'Brien  *	This product includes software developed by the University of
16fa561e1eSDavid E. O'Brien  *	California, Berkeley and its contributors.
17fa561e1eSDavid E. O'Brien  * 4. Neither the name of the University nor the names of its contributors
18fa561e1eSDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
19fa561e1eSDavid E. O'Brien  *    without specific prior written permission.
20fa561e1eSDavid E. O'Brien  *
21fa561e1eSDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22fa561e1eSDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23fa561e1eSDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24fa561e1eSDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25fa561e1eSDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26fa561e1eSDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27fa561e1eSDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28fa561e1eSDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29fa561e1eSDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30fa561e1eSDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31fa561e1eSDavid E. O'Brien  * SUCH DAMAGE.
32fa561e1eSDavid E. O'Brien  *
33fa561e1eSDavid E. O'Brien  *	@(#)endian.h	8.1 (Berkeley) 6/10/93
34fa561e1eSDavid E. O'Brien  *	$NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $
35fa561e1eSDavid E. O'Brien  * $FreeBSD$
36fa561e1eSDavid E. O'Brien  */
37fa561e1eSDavid E. O'Brien 
38fd8e4ebcSMike Barcroft #ifndef _MACHINE_ENDIAN_H_
39fd8e4ebcSMike Barcroft #define	_MACHINE_ENDIAN_H_
40fd8e4ebcSMike Barcroft 
41fd8e4ebcSMike Barcroft #include <machine/ansi.h>
42fa561e1eSDavid E. O'Brien 
43fa561e1eSDavid E. O'Brien /*
44fa561e1eSDavid E. O'Brien  * Define the order of 32-bit words in 64-bit words.
45fa561e1eSDavid E. O'Brien  */
46fa561e1eSDavid E. O'Brien #define	_QUAD_HIGHWORD 0
47fa561e1eSDavid E. O'Brien #define	_QUAD_LOWWORD 1
48fa561e1eSDavid E. O'Brien 
49fa561e1eSDavid E. O'Brien /*
50fa561e1eSDavid E. O'Brien  * Definitions for byte order, according to byte significance from low
51fa561e1eSDavid E. O'Brien  * address to high.
52fa561e1eSDavid E. O'Brien  */
53fd8e4ebcSMike Barcroft #ifndef _POSIX_SOURCE
54fa561e1eSDavid E. O'Brien #define	LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
55fa561e1eSDavid E. O'Brien #define	BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
56fa561e1eSDavid E. O'Brien #define	PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
57fa561e1eSDavid E. O'Brien 
58fa561e1eSDavid E. O'Brien #define	BYTE_ORDER	BIG_ENDIAN
59fd8e4ebcSMike Barcroft #endif /* !_POSIX_SOURCE */
60fa561e1eSDavid E. O'Brien 
61d846855dSMike Barcroft #ifdef __GNUC__
62d846855dSMike Barcroft 
63d846855dSMike Barcroft static __inline __uint16_t
64d846855dSMike Barcroft __bswap16(__uint16_t _x)
65d846855dSMike Barcroft {
66d846855dSMike Barcroft 
67d846855dSMike Barcroft 	return ((_x >> 8) | ((_x << 8) & 0xff00));
68d846855dSMike Barcroft }
69d846855dSMike Barcroft 
70d846855dSMike Barcroft static __inline __uint32_t
71d846855dSMike Barcroft __bswap32(__uint32_t _x)
72d846855dSMike Barcroft {
73d846855dSMike Barcroft 
74d846855dSMike Barcroft 	return ((_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) |
75d846855dSMike Barcroft 	    ((_x << 24) & 0xff000000));
76d846855dSMike Barcroft }
77d846855dSMike Barcroft 
78d846855dSMike Barcroft static __inline __uint64_t
79d846855dSMike Barcroft __bswap64(__uint64_t _x)
80d846855dSMike Barcroft {
81d846855dSMike Barcroft 
82d846855dSMike Barcroft 	return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
83d846855dSMike Barcroft 	    ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
84d846855dSMike Barcroft 	    ((_x << 24) & ((__uint64_t)0xff << 40)) |
85d846855dSMike Barcroft 	    ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
86d846855dSMike Barcroft }
87d846855dSMike Barcroft 
88d846855dSMike Barcroft #endif /* __GNUC__ */
89d846855dSMike Barcroft 
90d846855dSMike Barcroft #define	__htonl(x)	((__uint32_t)(x))
91d846855dSMike Barcroft #define	__htons(x)	((__uint16_t)(x))
92d846855dSMike Barcroft #define	__ntohl(x)	((__uint32_t)(x))
93d846855dSMike Barcroft #define	__ntohs(x)	((__uint16_t)(x))
94fa561e1eSDavid E. O'Brien 
95fd8e4ebcSMike Barcroft #endif /* !_MACHINE_ENDIAN_H_ */
96