1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  *
21eda14cbcSMatt Macy  * $FreeBSD$
22eda14cbcSMatt Macy  */
23eda14cbcSMatt Macy 
24eda14cbcSMatt Macy /*
25eda14cbcSMatt Macy  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26eda14cbcSMatt Macy  * Use is subject to license terms.
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
30eda14cbcSMatt Macy /*	  All Rights Reserved  	*/
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy /*
33eda14cbcSMatt Macy  * University Copyright- Copyright (c) 1982, 1986, 1988
34eda14cbcSMatt Macy  * The Regents of the University of California
35eda14cbcSMatt Macy  * All Rights Reserved
36eda14cbcSMatt Macy  *
37eda14cbcSMatt Macy  * University Acknowledgment- Portions of this document are derived from
38eda14cbcSMatt Macy  * software developed by the University of California, Berkeley, and its
39eda14cbcSMatt Macy  * contributors.
40eda14cbcSMatt Macy  */
41eda14cbcSMatt Macy 
42eda14cbcSMatt Macy #ifndef _OPENSOLARIS_SYS_BYTEORDER_H_
43eda14cbcSMatt Macy #define	_OPENSOLARIS_SYS_BYTEORDER_H_
44eda14cbcSMatt Macy 
45eda14cbcSMatt Macy #include <sys/endian.h>
46eda14cbcSMatt Macy 
47eda14cbcSMatt Macy /*
48eda14cbcSMatt Macy  * Macros to reverse byte order
49eda14cbcSMatt Macy  */
50eda14cbcSMatt Macy #define	BSWAP_8(x)	((x) & 0xff)
51eda14cbcSMatt Macy #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
52eda14cbcSMatt Macy #define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
53eda14cbcSMatt Macy #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
54eda14cbcSMatt Macy 
55eda14cbcSMatt Macy #define	BMASK_8(x)	((x) & 0xff)
56eda14cbcSMatt Macy #define	BMASK_16(x)	((x) & 0xffff)
57eda14cbcSMatt Macy #define	BMASK_32(x)	((x) & 0xffffffff)
58eda14cbcSMatt Macy #define	BMASK_64(x)	(x)
59eda14cbcSMatt Macy 
60eda14cbcSMatt Macy /*
61eda14cbcSMatt Macy  * Macros to convert from a specific byte order to/from native byte order
62eda14cbcSMatt Macy  */
63eda14cbcSMatt Macy #if BYTE_ORDER == _BIG_ENDIAN
64eda14cbcSMatt Macy #define	BE_8(x)		BMASK_8(x)
65eda14cbcSMatt Macy #define	BE_16(x)	BMASK_16(x)
66eda14cbcSMatt Macy #define	BE_32(x)	BMASK_32(x)
67eda14cbcSMatt Macy #define	BE_64(x)	BMASK_64(x)
68eda14cbcSMatt Macy #define	LE_8(x)		BSWAP_8(x)
69eda14cbcSMatt Macy #define	LE_16(x)	BSWAP_16(x)
70eda14cbcSMatt Macy #define	LE_32(x)	BSWAP_32(x)
71eda14cbcSMatt Macy #define	LE_64(x)	BSWAP_64(x)
72eda14cbcSMatt Macy #else
73eda14cbcSMatt Macy #define	LE_8(x)		BMASK_8(x)
74eda14cbcSMatt Macy #define	LE_16(x)	BMASK_16(x)
75eda14cbcSMatt Macy #define	LE_32(x)	BMASK_32(x)
76eda14cbcSMatt Macy #define	LE_64(x)	BMASK_64(x)
77eda14cbcSMatt Macy #define	BE_8(x)		BSWAP_8(x)
78eda14cbcSMatt Macy #define	BE_16(x)	BSWAP_16(x)
79eda14cbcSMatt Macy #define	BE_32(x)	BSWAP_32(x)
80eda14cbcSMatt Macy #define	BE_64(x)	BSWAP_64(x)
81eda14cbcSMatt Macy #endif
82eda14cbcSMatt Macy 
832fec3ae8SWarner Losh #if !defined(_STANDALONE)
84eda14cbcSMatt Macy #if BYTE_ORDER == _BIG_ENDIAN
85eda14cbcSMatt Macy #define	htonll(x)	BMASK_64(x)
86eda14cbcSMatt Macy #define	ntohll(x)	BMASK_64(x)
872fec3ae8SWarner Losh #else /* BYTE_ORDER == _LITTLE_ENDIAN */
88eda14cbcSMatt Macy #ifndef __LP64__
89eda14cbcSMatt Macy static __inline__ uint64_t
90eda14cbcSMatt Macy htonll(uint64_t n)
91eda14cbcSMatt Macy {
92eda14cbcSMatt Macy 	return ((((uint64_t)htonl(n)) << 32) + htonl(n >> 32));
93eda14cbcSMatt Macy }
94eda14cbcSMatt Macy 
95eda14cbcSMatt Macy static __inline__ uint64_t
96eda14cbcSMatt Macy ntohll(uint64_t n)
97eda14cbcSMatt Macy {
98eda14cbcSMatt Macy 	return ((((uint64_t)ntohl(n)) << 32) + ntohl(n >> 32));
99eda14cbcSMatt Macy }
1002fec3ae8SWarner Losh #else	/* !__LP64__ */
101eda14cbcSMatt Macy #define	htonll(x)	BSWAP_64(x)
102eda14cbcSMatt Macy #define	ntohll(x)	BSWAP_64(x)
1032fec3ae8SWarner Losh #endif	/* __LP64__ */
1042fec3ae8SWarner Losh #endif	/* BYTE_ORDER */
1052fec3ae8SWarner Losh #endif	/* _STANDALONE */
106eda14cbcSMatt Macy 
107eda14cbcSMatt Macy #define	BE_IN32(xa)	htonl(*((uint32_t *)(void *)(xa)))
108eda14cbcSMatt Macy 
109eda14cbcSMatt Macy #endif /* _OPENSOLARIS_SYS_BYTEORDER_H_ */
110