xref: /titanic_51/usr/src/uts/common/sys/byteorder.h (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #ifndef _SYS_BYTEORDER_H
41 #define	_SYS_BYTEORDER_H
42 
43 #pragma ident	"%Z%%M%	%I%	%E% SMI"
44 
45 #include <sys/isa_defs.h>
46 #include <sys/int_types.h>
47 
48 #if defined(__GNUC__) && defined(_ASM_INLINES)
49 #include <asm/byteorder.h>
50 #endif
51 
52 #ifdef	__cplusplus
53 extern "C" {
54 #endif
55 
56 /*
57  * macros for conversion between host and (internet) network byte order
58  */
59 
60 #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
61 /* big-endian */
62 #define	ntohl(x)	(x)
63 #define	ntohs(x)	(x)
64 #define	htonl(x)	(x)
65 #define	htons(x)	(x)
66 
67 #elif !defined(ntohl) /* little-endian */
68 
69 #ifndef	_IN_PORT_T
70 #define	_IN_PORT_T
71 typedef uint16_t in_port_t;
72 #endif
73 
74 #ifndef	_IN_ADDR_T
75 #define	_IN_ADDR_T
76 typedef uint32_t in_addr_t;
77 #endif
78 
79 #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
80 extern	uint32_t htonl(uint32_t);
81 extern	uint16_t htons(uint16_t);
82 extern 	uint32_t ntohl(uint32_t);
83 extern	uint16_t ntohs(uint16_t);
84 #else
85 extern	in_addr_t htonl(in_addr_t);
86 extern	in_port_t htons(in_port_t);
87 extern 	in_addr_t ntohl(in_addr_t);
88 extern	in_port_t ntohs(in_port_t);
89 #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */
90 #endif
91 
92 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
93 
94 /*
95  * Macros to reverse byte order
96  */
97 #define	BSWAP_8(x)	((x) & 0xff)
98 #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
99 #define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
100 #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
101 
102 #define	BMASK_8(x)	((x) & 0xff)
103 #define	BMASK_16(x)	((x) & 0xffff)
104 #define	BMASK_32(x)	((x) & 0xffffffff)
105 #define	BMASK_64(x)	(x)
106 
107 /*
108  * Macros to convert from a specific byte order to/from native byte order
109  */
110 #ifdef _BIG_ENDIAN
111 #define	BE_8(x)		BMASK_8(x)
112 #define	BE_16(x)	BMASK_16(x)
113 #define	BE_32(x)	BMASK_32(x)
114 #define	BE_64(x)	BMASK_64(x)
115 #define	LE_8(x)		BSWAP_8(x)
116 #define	LE_16(x)	BSWAP_16(x)
117 #define	LE_32(x)	BSWAP_32(x)
118 #define	LE_64(x)	BSWAP_64(x)
119 #else
120 #define	LE_8(x)		BMASK_8(x)
121 #define	LE_16(x)	BMASK_16(x)
122 #define	LE_32(x)	BMASK_32(x)
123 #define	LE_64(x)	BMASK_64(x)
124 #define	BE_8(x)		BSWAP_8(x)
125 #define	BE_16(x)	BSWAP_16(x)
126 #define	BE_32(x)	BSWAP_32(x)
127 #define	BE_64(x)	BSWAP_64(x)
128 #endif
129 
130 #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
131 
132 #ifdef	__cplusplus
133 }
134 #endif
135 
136 #endif /* _SYS_BYTEORDER_H */
137