xref: /illumos-gate/usr/src/uts/common/sys/byteorder.h (revision fc0e6fa2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52b616c6cSwesolows  * Common Development and Distribution License (the "License").
62b616c6cSwesolows  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212b616c6cSwesolows 
227c478bd9Sstevel@tonic-gate /*
234b56a003SDaniel Anderson  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifndef _SYS_BYTEORDER_H
417c478bd9Sstevel@tonic-gate #define	_SYS_BYTEORDER_H
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
447c478bd9Sstevel@tonic-gate #include <sys/int_types.h>
457c478bd9Sstevel@tonic-gate 
462b616c6cSwesolows #if defined(__GNUC__) && defined(_ASM_INLINES) && \
472b616c6cSwesolows 	(defined(__i386) || defined(__amd64))
487c478bd9Sstevel@tonic-gate #include <asm/byteorder.h>
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
527c478bd9Sstevel@tonic-gate extern "C" {
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * macros for conversion between host and (internet) network byte order
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
607c478bd9Sstevel@tonic-gate /* big-endian */
617c478bd9Sstevel@tonic-gate #define	ntohl(x)	(x)
624b56a003SDaniel Anderson #define	ntohll(x)	(x)
637c478bd9Sstevel@tonic-gate #define	ntohs(x)	(x)
647c478bd9Sstevel@tonic-gate #define	htonl(x)	(x)
654b56a003SDaniel Anderson #define	htonll(x)	(x)
667c478bd9Sstevel@tonic-gate #define	htons(x)	(x)
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #elif !defined(ntohl) /* little-endian */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #ifndef	_IN_PORT_T
717c478bd9Sstevel@tonic-gate #define	_IN_PORT_T
727c478bd9Sstevel@tonic-gate typedef uint16_t in_port_t;
737c478bd9Sstevel@tonic-gate #endif
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #ifndef	_IN_ADDR_T
767c478bd9Sstevel@tonic-gate #define	_IN_ADDR_T
777c478bd9Sstevel@tonic-gate typedef uint32_t in_addr_t;
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
817c478bd9Sstevel@tonic-gate extern	uint32_t htonl(uint32_t);
827c478bd9Sstevel@tonic-gate extern	uint16_t htons(uint16_t);
834b56a003SDaniel Anderson extern	uint32_t ntohl(uint32_t);
847c478bd9Sstevel@tonic-gate extern	uint16_t ntohs(uint16_t);
857c478bd9Sstevel@tonic-gate #else
867c478bd9Sstevel@tonic-gate extern	in_addr_t htonl(in_addr_t);
877c478bd9Sstevel@tonic-gate extern	in_port_t htons(in_port_t);
884b56a003SDaniel Anderson extern	in_addr_t ntohl(in_addr_t);
897c478bd9Sstevel@tonic-gate extern	in_port_t ntohs(in_port_t);
907c478bd9Sstevel@tonic-gate #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */
914b56a003SDaniel Anderson #if !(defined(_XPG4_2) || defined(_XPG5)) || defined(__EXTENSIONS__)
924b56a003SDaniel Anderson extern	uint64_t htonll(uint64_t);
934b56a003SDaniel Anderson extern	uint64_t ntohll(uint64_t);
944b56a003SDaniel Anderson #endif	/* !(_XPG4_2||_XPG5) || __EXTENSIONS__ */
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * Macros to reverse byte order
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate #define	BSWAP_8(x)	((x) & 0xff)
103*fc0e6fa2SDaniel Anderson #if !defined(__i386) && !defined(__amd64)
1047c478bd9Sstevel@tonic-gate #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
105*fc0e6fa2SDaniel Anderson #define	BSWAP_32(x)	(((uint32_t)(x) << 24) | \
106*fc0e6fa2SDaniel Anderson 			(((uint32_t)(x) << 8) & 0xff0000) | \
107*fc0e6fa2SDaniel Anderson 			(((uint32_t)(x) >> 8) & 0xff00) | \
108*fc0e6fa2SDaniel Anderson 			((uint32_t)(x)  >> 24))
109*fc0e6fa2SDaniel Anderson #else /* x86 */
110*fc0e6fa2SDaniel Anderson #define	BSWAP_16(x)	htons(x)
111*fc0e6fa2SDaniel Anderson #define	BSWAP_32(x)	htonl(x)
112*fc0e6fa2SDaniel Anderson #endif	/* !__i386 && !__amd64 */
113*fc0e6fa2SDaniel Anderson 
114*fc0e6fa2SDaniel Anderson #if (!defined(__i386) && !defined(__amd64)) || \
115*fc0e6fa2SDaniel Anderson 	((defined(_XPG4_2) || defined(_XPG5)) && !defined(__EXTENSIONS__))
116*fc0e6fa2SDaniel Anderson #define	BSWAP_64(x)	(((uint64_t)(x) << 56) | \
117*fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
118*fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
119*fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) << 8)  & 0xff00000000ULL) | \
120*fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) >> 8)  & 0xff000000ULL) | \
121*fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) >> 24) & 0xff0000ULL) | \
122*fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) >> 40) & 0xff00ULL) | \
123*fc0e6fa2SDaniel Anderson 			((uint64_t)(x)  >> 56))
124*fc0e6fa2SDaniel Anderson #else /* x86 with non-XPG extensions allowed */
125*fc0e6fa2SDaniel Anderson #define	BSWAP_64(x)	htonll(x)
126*fc0e6fa2SDaniel Anderson #endif	/* (!__i386&&!__amd64) || ((_XPG4_2||_XPG5) && !__EXTENSIONS__) */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	BMASK_8(x)	((x) & 0xff)
1297c478bd9Sstevel@tonic-gate #define	BMASK_16(x)	((x) & 0xffff)
1307c478bd9Sstevel@tonic-gate #define	BMASK_32(x)	((x) & 0xffffffff)
1317c478bd9Sstevel@tonic-gate #define	BMASK_64(x)	(x)
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * Macros to convert from a specific byte order to/from native byte order
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
1377c478bd9Sstevel@tonic-gate #define	BE_8(x)		BMASK_8(x)
1387c478bd9Sstevel@tonic-gate #define	BE_16(x)	BMASK_16(x)
1397c478bd9Sstevel@tonic-gate #define	BE_32(x)	BMASK_32(x)
1407c478bd9Sstevel@tonic-gate #define	BE_64(x)	BMASK_64(x)
1417c478bd9Sstevel@tonic-gate #define	LE_8(x)		BSWAP_8(x)
1427c478bd9Sstevel@tonic-gate #define	LE_16(x)	BSWAP_16(x)
1437c478bd9Sstevel@tonic-gate #define	LE_32(x)	BSWAP_32(x)
1447c478bd9Sstevel@tonic-gate #define	LE_64(x)	BSWAP_64(x)
1457c478bd9Sstevel@tonic-gate #else
1467c478bd9Sstevel@tonic-gate #define	LE_8(x)		BMASK_8(x)
1477c478bd9Sstevel@tonic-gate #define	LE_16(x)	BMASK_16(x)
1487c478bd9Sstevel@tonic-gate #define	LE_32(x)	BMASK_32(x)
1497c478bd9Sstevel@tonic-gate #define	LE_64(x)	BMASK_64(x)
1507c478bd9Sstevel@tonic-gate #define	BE_8(x)		BSWAP_8(x)
1517c478bd9Sstevel@tonic-gate #define	BE_16(x)	BSWAP_16(x)
1527c478bd9Sstevel@tonic-gate #define	BE_32(x)	BSWAP_32(x)
1537c478bd9Sstevel@tonic-gate #define	BE_64(x)	BSWAP_64(x)
1547c478bd9Sstevel@tonic-gate #endif
1557c478bd9Sstevel@tonic-gate 
156da6c28aaSamw /*
157da6c28aaSamw  * Macros to read unaligned values from a specific byte order to
158da6c28aaSamw  * native byte order
159da6c28aaSamw  */
160da6c28aaSamw 
161da6c28aaSamw #define	BE_IN8(xa) \
162da6c28aaSamw 	*((uint8_t *)(xa))
163da6c28aaSamw 
164*fc0e6fa2SDaniel Anderson #if !defined(__i386) && !defined(__amd64)
165da6c28aaSamw #define	BE_IN16(xa) \
166*fc0e6fa2SDaniel Anderson 	(((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa) + 1))
167da6c28aaSamw 
168da6c28aaSamw #define	BE_IN32(xa) \
169*fc0e6fa2SDaniel Anderson 	(((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa) + 2))
170da6c28aaSamw 
171*fc0e6fa2SDaniel Anderson #else /* x86 */
172*fc0e6fa2SDaniel Anderson #define	BE_IN16(xa) htons(*((uint16_t *)(void *)(xa)))
173*fc0e6fa2SDaniel Anderson #define	BE_IN32(xa) htonl(*((uint32_t *)(void *)(xa)))
174*fc0e6fa2SDaniel Anderson #endif	/* !__i386 && !__amd64 */
175*fc0e6fa2SDaniel Anderson 
176*fc0e6fa2SDaniel Anderson #if (!defined(__i386) && !defined(__amd64)) || \
177*fc0e6fa2SDaniel Anderson 	((defined(_XPG4_2) || defined(_XPG5)) && !defined(__EXTENSIONS__))
178da6c28aaSamw #define	BE_IN64(xa) \
179*fc0e6fa2SDaniel Anderson 	(((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa) + 4))
180*fc0e6fa2SDaniel Anderson #else /* x86 with non-XPG extensions allowed */
181*fc0e6fa2SDaniel Anderson #define	BE_IN64(xa) htonll(*((uint64_t *)(void *)(xa)))
182*fc0e6fa2SDaniel Anderson #endif	/* (!__i386&&!__amd64) || ((_XPG4_2||_XPG5) && !__EXTENSIONS__) */
183da6c28aaSamw 
184da6c28aaSamw #define	LE_IN8(xa) \
185da6c28aaSamw 	*((uint8_t *)(xa))
186da6c28aaSamw 
187da6c28aaSamw #define	LE_IN16(xa) \
188da6c28aaSamw 	(((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa))
189da6c28aaSamw 
190da6c28aaSamw #define	LE_IN32(xa) \
191da6c28aaSamw 	(((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa))
192da6c28aaSamw 
193da6c28aaSamw #define	LE_IN64(xa) \
194da6c28aaSamw 	(((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa))
195da6c28aaSamw 
196da6c28aaSamw /*
197da6c28aaSamw  * Macros to write unaligned values from native byte order to a specific byte
198da6c28aaSamw  * order.
199da6c28aaSamw  */
200da6c28aaSamw 
201da6c28aaSamw #define	BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
202da6c28aaSamw 
203da6c28aaSamw #define	BE_OUT16(xa, yv) \
204da6c28aaSamw 	BE_OUT8((uint8_t *)(xa) + 1, yv); \
205da6c28aaSamw 	BE_OUT8((uint8_t *)(xa), (yv) >> 8);
206da6c28aaSamw 
207da6c28aaSamw #define	BE_OUT32(xa, yv) \
208da6c28aaSamw 	BE_OUT16((uint8_t *)(xa) + 2, yv); \
209da6c28aaSamw 	BE_OUT16((uint8_t *)(xa), (yv) >> 16);
210da6c28aaSamw 
211*fc0e6fa2SDaniel Anderson #if (!defined(__i386) && !defined(__amd64)) || \
212*fc0e6fa2SDaniel Anderson 	((defined(_XPG4_2) || defined(_XPG5)) && !defined(__EXTENSIONS__))
213da6c28aaSamw #define	BE_OUT64(xa, yv) \
214da6c28aaSamw 	BE_OUT32((uint8_t *)(xa) + 4, yv); \
215da6c28aaSamw 	BE_OUT32((uint8_t *)(xa), (yv) >> 32);
216*fc0e6fa2SDaniel Anderson #else /* x86 with non-XPG extensions allowed */
217*fc0e6fa2SDaniel Anderson #define	BE_OUT64(xa, yv) *((uint64_t *)(void *)(xa)) = htonll((uint64_t)(yv));
218*fc0e6fa2SDaniel Anderson #endif	/* (!__i386&&!__amd64) || ((_XPG4_2||_XPG5) && !__EXTENSIONS__) */
219da6c28aaSamw 
220da6c28aaSamw #define	LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
221da6c28aaSamw 
222da6c28aaSamw #define	LE_OUT16(xa, yv) \
223da6c28aaSamw 	LE_OUT8((uint8_t *)(xa), yv); \
224da6c28aaSamw 	LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8);
225da6c28aaSamw 
226da6c28aaSamw #define	LE_OUT32(xa, yv) \
227da6c28aaSamw 	LE_OUT16((uint8_t *)(xa), yv); \
228da6c28aaSamw 	LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16);
229da6c28aaSamw 
230da6c28aaSamw #define	LE_OUT64(xa, yv) \
231da6c28aaSamw 	LE_OUT32((uint8_t *)(xa), yv); \
232da6c28aaSamw 	LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32);
233da6c28aaSamw 
2347c478bd9Sstevel@tonic-gate #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate #endif
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate #endif /* _SYS_BYTEORDER_H */
241