xref: /titanic_51/usr/src/uts/common/sys/byteorder.h (revision 94e7edb1e6b194fffd0e7901c0b32e9fc836bc5b)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 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 #include <sys/isa_defs.h>
44 #include <sys/int_types.h>
45 
46 #if defined(__GNUC__) && defined(_ASM_INLINES) && \
47 	(defined(__i386) || defined(__amd64))
48 #include <asm/byteorder.h>
49 #endif
50 
51 #ifdef	__cplusplus
52 extern "C" {
53 #endif
54 
55 /*
56  * macros for conversion between host and (internet) network byte order
57  */
58 
59 #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
60 /* big-endian */
61 #define	ntohl(x)	(x)
62 #define	ntohs(x)	(x)
63 #define	htonl(x)	(x)
64 #define	htons(x)	(x)
65 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
66 #define	ntohll(x)	(x)
67 #define	htonll(x)	(x)
68 #endif	/* !_XPG4_2 || __EXTENSIONS__ */
69 
70 #elif !defined(ntohl) /* little-endian */
71 
72 #ifndef	_IN_PORT_T
73 #define	_IN_PORT_T
74 typedef uint16_t in_port_t;
75 #endif
76 
77 #ifndef	_IN_ADDR_T
78 #define	_IN_ADDR_T
79 typedef uint32_t in_addr_t;
80 #endif
81 
82 #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
83 extern	uint32_t htonl(uint32_t);
84 extern	uint16_t htons(uint16_t);
85 extern	uint32_t ntohl(uint32_t);
86 extern	uint16_t ntohs(uint16_t);
87 #else
88 extern	in_addr_t htonl(in_addr_t);
89 extern	in_port_t htons(in_port_t);
90 extern	in_addr_t ntohl(in_addr_t);
91 extern	in_port_t ntohs(in_port_t);
92 #endif	/* !_XPG4_2 || __EXTENSIONS__ || _XPG5 */
93 
94 #if defined(_LP64) || defined(_LONGLONG_TYPE)
95 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
96 extern	uint64_t htonll(uint64_t);
97 extern	uint64_t ntohll(uint64_t);
98 #endif	/* !_XPG4_2 || __EXTENSIONS__ */
99 #endif	/* _LP64 || _LONGLONG_TYPE  */
100 #endif
101 
102 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
103 
104 /*
105  * Macros to reverse byte order
106  */
107 #define	BSWAP_8(x)	((x) & 0xff)
108 #if !defined(__i386) && !defined(__amd64)
109 #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
110 #define	BSWAP_32(x)	(((uint32_t)(x) << 24) | \
111 			(((uint32_t)(x) << 8) & 0xff0000) | \
112 			(((uint32_t)(x) >> 8) & 0xff00) | \
113 			((uint32_t)(x)  >> 24))
114 #else /* x86 */
115 #define	BSWAP_16(x)	htons(x)
116 #define	BSWAP_32(x)	htonl(x)
117 #endif	/* !__i386 && !__amd64 */
118 
119 #if defined(_LP64) || defined(_LONGLONG_TYPE)
120 #if (!defined(__i386) && !defined(__amd64))
121 #define	BSWAP_64(x)	(((uint64_t)(x) << 56) | \
122 			(((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
123 			(((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
124 			(((uint64_t)(x) << 8)  & 0xff00000000ULL) | \
125 			(((uint64_t)(x) >> 8)  & 0xff000000ULL) | \
126 			(((uint64_t)(x) >> 24) & 0xff0000ULL) | \
127 			(((uint64_t)(x) >> 40) & 0xff00ULL) | \
128 			((uint64_t)(x)  >> 56))
129 #else /* x86 */
130 #define	BSWAP_64(x)	htonll(x)
131 #endif	/* !__i386 && !__amd64 */
132 #else /* no uint64_t */
133 #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
134 #endif	/* _LP64 || _LONGLONG_TYPE  */
135 
136 #define	BMASK_8(x)	((x) & 0xff)
137 #define	BMASK_16(x)	((x) & 0xffff)
138 #define	BMASK_32(x)	((x) & 0xffffffff)
139 #define	BMASK_64(x)	(x)
140 
141 /*
142  * Macros to convert from a specific byte order to/from native byte order
143  */
144 #ifdef _BIG_ENDIAN
145 #define	BE_8(x)		BMASK_8(x)
146 #define	BE_16(x)	BMASK_16(x)
147 #define	BE_32(x)	BMASK_32(x)
148 #define	BE_64(x)	BMASK_64(x)
149 #define	LE_8(x)		BSWAP_8(x)
150 #define	LE_16(x)	BSWAP_16(x)
151 #define	LE_32(x)	BSWAP_32(x)
152 #define	LE_64(x)	BSWAP_64(x)
153 #else
154 #define	LE_8(x)		BMASK_8(x)
155 #define	LE_16(x)	BMASK_16(x)
156 #define	LE_32(x)	BMASK_32(x)
157 #define	LE_64(x)	BMASK_64(x)
158 #define	BE_8(x)		BSWAP_8(x)
159 #define	BE_16(x)	BSWAP_16(x)
160 #define	BE_32(x)	BSWAP_32(x)
161 #define	BE_64(x)	BSWAP_64(x)
162 #endif
163 
164 /*
165  * Macros to read unaligned values from a specific byte order to
166  * native byte order
167  */
168 
169 #define	BE_IN8(xa) \
170 	*((uint8_t *)(xa))
171 
172 #if !defined(__i386) && !defined(__amd64)
173 #define	BE_IN16(xa) \
174 	(((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa) + 1))
175 
176 #define	BE_IN32(xa) \
177 	(((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa) + 2))
178 
179 #else /* x86 */
180 #define	BE_IN16(xa) htons(*((uint16_t *)(void *)(xa)))
181 #define	BE_IN32(xa) htonl(*((uint32_t *)(void *)(xa)))
182 #endif	/* !__i386 && !__amd64 */
183 
184 #if (!defined(__i386) && !defined(__amd64)) || \
185 	(!defined(_LP64) && !defined(_LONGLONG_TYPE))
186 #define	BE_IN64(xa) \
187 	(((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa) + 4))
188 #else /* x86 */
189 #define	BE_IN64(xa) htonll(*((uint64_t *)(void *)(xa)))
190 #endif /* (!__i386 && !__amd64) || (!_LP64 && !_LONGLONG_TYPE) */
191 
192 #define	LE_IN8(xa) \
193 	*((uint8_t *)(xa))
194 
195 #define	LE_IN16(xa) \
196 	(((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa))
197 
198 #define	LE_IN32(xa) \
199 	(((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa))
200 
201 #define	LE_IN64(xa) \
202 	(((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa))
203 
204 /*
205  * Macros to write unaligned values from native byte order to a specific byte
206  * order.
207  */
208 
209 #define	BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
210 
211 #define	BE_OUT16(xa, yv) \
212 	BE_OUT8((uint8_t *)(xa) + 1, yv); \
213 	BE_OUT8((uint8_t *)(xa), (yv) >> 8);
214 
215 #define	BE_OUT32(xa, yv) \
216 	BE_OUT16((uint8_t *)(xa) + 2, yv); \
217 	BE_OUT16((uint8_t *)(xa), (yv) >> 16);
218 
219 #if (!defined(__i386) && !defined(__amd64)) || \
220 	(!defined(_LP64) && !defined(_LONGLONG_TYPE))
221 #define	BE_OUT64(xa, yv) \
222 	BE_OUT32((uint8_t *)(xa) + 4, yv); \
223 	BE_OUT32((uint8_t *)(xa), (yv) >> 32);
224 #else /* x86 with uint64_t */
225 #define	BE_OUT64(xa, yv) *((uint64_t *)(void *)(xa)) = htonll((uint64_t)(yv));
226 #endif	/* (!__i386 && !__amd64) || (!_LP64 && !_LONGLONG_TYPE) */
227 
228 #define	LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
229 
230 #define	LE_OUT16(xa, yv) \
231 	LE_OUT8((uint8_t *)(xa), yv); \
232 	LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8);
233 
234 #define	LE_OUT32(xa, yv) \
235 	LE_OUT16((uint8_t *)(xa), yv); \
236 	LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16);
237 
238 #define	LE_OUT64(xa, yv) \
239 	LE_OUT32((uint8_t *)(xa), yv); \
240 	LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32);
241 
242 #endif	/* !_XPG4_2 || __EXTENSIONS__ */
243 
244 #ifdef	__cplusplus
245 }
246 #endif
247 
248 #endif /* _SYS_BYTEORDER_H */
249