xref: /illumos-gate/usr/src/uts/common/sys/byteorder.h (revision b4203d75)
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 /*
2330e7468fSPeter Dunlap  * Copyright 2009 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	*/
28*b4203d75SMarcel Telka /*	  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)
627c478bd9Sstevel@tonic-gate #define	ntohs(x)	(x)
637c478bd9Sstevel@tonic-gate #define	htonl(x)	(x)
647c478bd9Sstevel@tonic-gate #define	htons(x)	(x)
652d726cc1SDaniel OpenSolaris Anderson #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
662d726cc1SDaniel OpenSolaris Anderson #define	ntohll(x)	(x)
672d726cc1SDaniel OpenSolaris Anderson #define	htonll(x)	(x)
682d726cc1SDaniel OpenSolaris Anderson #endif	/* !_XPG4_2 || __EXTENSIONS__ */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #elif !defined(ntohl) /* little-endian */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #ifndef	_IN_PORT_T
737c478bd9Sstevel@tonic-gate #define	_IN_PORT_T
747c478bd9Sstevel@tonic-gate typedef uint16_t in_port_t;
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #ifndef	_IN_ADDR_T
787c478bd9Sstevel@tonic-gate #define	_IN_ADDR_T
797c478bd9Sstevel@tonic-gate typedef uint32_t in_addr_t;
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
837c478bd9Sstevel@tonic-gate extern	uint32_t htonl(uint32_t);
847c478bd9Sstevel@tonic-gate extern	uint16_t htons(uint16_t);
854b56a003SDaniel Anderson extern	uint32_t ntohl(uint32_t);
867c478bd9Sstevel@tonic-gate extern	uint16_t ntohs(uint16_t);
877c478bd9Sstevel@tonic-gate #else
887c478bd9Sstevel@tonic-gate extern	in_addr_t htonl(in_addr_t);
897c478bd9Sstevel@tonic-gate extern	in_port_t htons(in_port_t);
904b56a003SDaniel Anderson extern	in_addr_t ntohl(in_addr_t);
917c478bd9Sstevel@tonic-gate extern	in_port_t ntohs(in_port_t);
922d726cc1SDaniel OpenSolaris Anderson #endif	/* !_XPG4_2 || __EXTENSIONS__ || _XPG5 */
932d726cc1SDaniel OpenSolaris Anderson 
942d726cc1SDaniel OpenSolaris Anderson #if defined(_LP64) || defined(_LONGLONG_TYPE)
952d726cc1SDaniel OpenSolaris Anderson #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
964b56a003SDaniel Anderson extern	uint64_t htonll(uint64_t);
974b56a003SDaniel Anderson extern	uint64_t ntohll(uint64_t);
982d726cc1SDaniel OpenSolaris Anderson #endif	/* !_XPG4_2 || __EXTENSIONS__ */
992d726cc1SDaniel OpenSolaris Anderson #endif	/* _LP64 || _LONGLONG_TYPE  */
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Macros to reverse byte order
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate #define	BSWAP_8(x)	((x) & 0xff)
108fc0e6fa2SDaniel Anderson #if !defined(__i386) && !defined(__amd64)
1097c478bd9Sstevel@tonic-gate #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
110fc0e6fa2SDaniel Anderson #define	BSWAP_32(x)	(((uint32_t)(x) << 24) | \
111fc0e6fa2SDaniel Anderson 			(((uint32_t)(x) << 8) & 0xff0000) | \
112fc0e6fa2SDaniel Anderson 			(((uint32_t)(x) >> 8) & 0xff00) | \
113fc0e6fa2SDaniel Anderson 			((uint32_t)(x)  >> 24))
114fc0e6fa2SDaniel Anderson #else /* x86 */
115fc0e6fa2SDaniel Anderson #define	BSWAP_16(x)	htons(x)
116fc0e6fa2SDaniel Anderson #define	BSWAP_32(x)	htonl(x)
117fc0e6fa2SDaniel Anderson #endif	/* !__i386 && !__amd64 */
118fc0e6fa2SDaniel Anderson 
1192d726cc1SDaniel OpenSolaris Anderson #if defined(_LP64) || defined(_LONGLONG_TYPE)
1202d726cc1SDaniel OpenSolaris Anderson #if (!defined(__i386) && !defined(__amd64))
121fc0e6fa2SDaniel Anderson #define	BSWAP_64(x)	(((uint64_t)(x) << 56) | \
122fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
123fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
124fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) << 8)  & 0xff00000000ULL) | \
125fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) >> 8)  & 0xff000000ULL) | \
126fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) >> 24) & 0xff0000ULL) | \
127fc0e6fa2SDaniel Anderson 			(((uint64_t)(x) >> 40) & 0xff00ULL) | \
128fc0e6fa2SDaniel Anderson 			((uint64_t)(x)  >> 56))
1292d726cc1SDaniel OpenSolaris Anderson #else /* x86 */
130fc0e6fa2SDaniel Anderson #define	BSWAP_64(x)	htonll(x)
1312d726cc1SDaniel OpenSolaris Anderson #endif	/* !__i386 && !__amd64 */
1322d726cc1SDaniel OpenSolaris Anderson #else /* no uint64_t */
1332d726cc1SDaniel OpenSolaris Anderson #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
1342d726cc1SDaniel OpenSolaris Anderson #endif	/* _LP64 || _LONGLONG_TYPE  */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define	BMASK_8(x)	((x) & 0xff)
1377c478bd9Sstevel@tonic-gate #define	BMASK_16(x)	((x) & 0xffff)
1387c478bd9Sstevel@tonic-gate #define	BMASK_32(x)	((x) & 0xffffffff)
1397c478bd9Sstevel@tonic-gate #define	BMASK_64(x)	(x)
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * Macros to convert from a specific byte order to/from native byte order
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
1457c478bd9Sstevel@tonic-gate #define	BE_8(x)		BMASK_8(x)
1467c478bd9Sstevel@tonic-gate #define	BE_16(x)	BMASK_16(x)
1477c478bd9Sstevel@tonic-gate #define	BE_32(x)	BMASK_32(x)
1487c478bd9Sstevel@tonic-gate #define	BE_64(x)	BMASK_64(x)
1497c478bd9Sstevel@tonic-gate #define	LE_8(x)		BSWAP_8(x)
1507c478bd9Sstevel@tonic-gate #define	LE_16(x)	BSWAP_16(x)
1517c478bd9Sstevel@tonic-gate #define	LE_32(x)	BSWAP_32(x)
1527c478bd9Sstevel@tonic-gate #define	LE_64(x)	BSWAP_64(x)
1537c478bd9Sstevel@tonic-gate #else
1547c478bd9Sstevel@tonic-gate #define	LE_8(x)		BMASK_8(x)
1557c478bd9Sstevel@tonic-gate #define	LE_16(x)	BMASK_16(x)
1567c478bd9Sstevel@tonic-gate #define	LE_32(x)	BMASK_32(x)
1577c478bd9Sstevel@tonic-gate #define	LE_64(x)	BMASK_64(x)
1587c478bd9Sstevel@tonic-gate #define	BE_8(x)		BSWAP_8(x)
1597c478bd9Sstevel@tonic-gate #define	BE_16(x)	BSWAP_16(x)
1607c478bd9Sstevel@tonic-gate #define	BE_32(x)	BSWAP_32(x)
1617c478bd9Sstevel@tonic-gate #define	BE_64(x)	BSWAP_64(x)
1627c478bd9Sstevel@tonic-gate #endif
1637c478bd9Sstevel@tonic-gate 
164da6c28aaSamw /*
165da6c28aaSamw  * Macros to read unaligned values from a specific byte order to
166da6c28aaSamw  * native byte order
167da6c28aaSamw  */
168da6c28aaSamw 
169da6c28aaSamw #define	BE_IN8(xa) \
170da6c28aaSamw 	*((uint8_t *)(xa))
171da6c28aaSamw 
172fc0e6fa2SDaniel Anderson #if !defined(__i386) && !defined(__amd64)
173da6c28aaSamw #define	BE_IN16(xa) \
174fc0e6fa2SDaniel Anderson 	(((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa) + 1))
175da6c28aaSamw 
176da6c28aaSamw #define	BE_IN32(xa) \
177fc0e6fa2SDaniel Anderson 	(((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa) + 2))
178da6c28aaSamw 
179fc0e6fa2SDaniel Anderson #else /* x86 */
180fc0e6fa2SDaniel Anderson #define	BE_IN16(xa) htons(*((uint16_t *)(void *)(xa)))
181fc0e6fa2SDaniel Anderson #define	BE_IN32(xa) htonl(*((uint32_t *)(void *)(xa)))
182fc0e6fa2SDaniel Anderson #endif	/* !__i386 && !__amd64 */
183fc0e6fa2SDaniel Anderson 
18430e7468fSPeter Dunlap #if (!defined(__i386) && !defined(__amd64)) || \
18530e7468fSPeter Dunlap 	(!defined(_LP64) && !defined(_LONGLONG_TYPE))
186da6c28aaSamw #define	BE_IN64(xa) \
187fc0e6fa2SDaniel Anderson 	(((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa) + 4))
1882d726cc1SDaniel OpenSolaris Anderson #else /* x86 */
189fc0e6fa2SDaniel Anderson #define	BE_IN64(xa) htonll(*((uint64_t *)(void *)(xa)))
19030e7468fSPeter Dunlap #endif /* (!__i386 && !__amd64) || (!_LP64 && !_LONGLONG_TYPE) */
191da6c28aaSamw 
192da6c28aaSamw #define	LE_IN8(xa) \
193da6c28aaSamw 	*((uint8_t *)(xa))
194da6c28aaSamw 
195da6c28aaSamw #define	LE_IN16(xa) \
196da6c28aaSamw 	(((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa))
197da6c28aaSamw 
198da6c28aaSamw #define	LE_IN32(xa) \
199da6c28aaSamw 	(((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa))
200da6c28aaSamw 
201da6c28aaSamw #define	LE_IN64(xa) \
202da6c28aaSamw 	(((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa))
203da6c28aaSamw 
204da6c28aaSamw /*
205da6c28aaSamw  * Macros to write unaligned values from native byte order to a specific byte
206da6c28aaSamw  * order.
207da6c28aaSamw  */
208da6c28aaSamw 
209da6c28aaSamw #define	BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
210da6c28aaSamw 
211da6c28aaSamw #define	BE_OUT16(xa, yv) \
212da6c28aaSamw 	BE_OUT8((uint8_t *)(xa) + 1, yv); \
213da6c28aaSamw 	BE_OUT8((uint8_t *)(xa), (yv) >> 8);
214da6c28aaSamw 
215da6c28aaSamw #define	BE_OUT32(xa, yv) \
216da6c28aaSamw 	BE_OUT16((uint8_t *)(xa) + 2, yv); \
217da6c28aaSamw 	BE_OUT16((uint8_t *)(xa), (yv) >> 16);
218da6c28aaSamw 
219fc0e6fa2SDaniel Anderson #if (!defined(__i386) && !defined(__amd64)) || \
2202d726cc1SDaniel OpenSolaris Anderson 	(!defined(_LP64) && !defined(_LONGLONG_TYPE))
221da6c28aaSamw #define	BE_OUT64(xa, yv) \
222da6c28aaSamw 	BE_OUT32((uint8_t *)(xa) + 4, yv); \
223da6c28aaSamw 	BE_OUT32((uint8_t *)(xa), (yv) >> 32);
2242d726cc1SDaniel OpenSolaris Anderson #else /* x86 with uint64_t */
225fc0e6fa2SDaniel Anderson #define	BE_OUT64(xa, yv) *((uint64_t *)(void *)(xa)) = htonll((uint64_t)(yv));
2262d726cc1SDaniel OpenSolaris Anderson #endif	/* (!__i386 && !__amd64) || (!_LP64 && !_LONGLONG_TYPE) */
227da6c28aaSamw 
228da6c28aaSamw #define	LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
229da6c28aaSamw 
230da6c28aaSamw #define	LE_OUT16(xa, yv) \
231da6c28aaSamw 	LE_OUT8((uint8_t *)(xa), yv); \
232da6c28aaSamw 	LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8);
233da6c28aaSamw 
234da6c28aaSamw #define	LE_OUT32(xa, yv) \
235da6c28aaSamw 	LE_OUT16((uint8_t *)(xa), yv); \
236da6c28aaSamw 	LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16);
237da6c28aaSamw 
238da6c28aaSamw #define	LE_OUT64(xa, yv) \
239da6c28aaSamw 	LE_OUT32((uint8_t *)(xa), yv); \
240da6c28aaSamw 	LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32);
241da6c28aaSamw 
2422d726cc1SDaniel OpenSolaris Anderson #endif	/* !_XPG4_2 || __EXTENSIONS__ */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate #endif
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate #endif /* _SYS_BYTEORDER_H */
249