xref: /onnv-gate/usr/src/uts/intel/asm/byteorder.h (revision 7421:8b7f030a1d82)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7421SDaniel.Anderson@Sun.COM  * Common Development and Distribution License (the "License").
6*7421SDaniel.Anderson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7421SDaniel.Anderson@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #ifndef _ASM_BYTEORDER_H
270Sstevel@tonic-gate #define	_ASM_BYTEORDER_H
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #ifdef	__cplusplus
320Sstevel@tonic-gate extern "C" {
330Sstevel@tonic-gate #endif
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #if !defined(__lint) && defined(__GNUC__)
360Sstevel@tonic-gate 
37*7421SDaniel.Anderson@Sun.COM /*
38*7421SDaniel.Anderson@Sun.COM  * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs()
39*7421SDaniel.Anderson@Sun.COM  * These functions reverse the byte order of the input parameter and returns
40*7421SDaniel.Anderson@Sun.COM  * the result.  This is to convert the byte order from host byte order
41*7421SDaniel.Anderson@Sun.COM  * (little endian) to network byte order (big endian), or vice versa.
42*7421SDaniel.Anderson@Sun.COM  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 
45*7421SDaniel.Anderson@Sun.COM #if defined(__i386) || defined(__amd64)
460Sstevel@tonic-gate 
htons(uint16_t value)470Sstevel@tonic-gate extern __inline__ uint16_t htons(uint16_t value)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate #if defined(__amd64)
500Sstevel@tonic-gate 	__asm__("xchgb %h0, %b0" : "+Q" (value));
510Sstevel@tonic-gate #elif defined(__i386)
520Sstevel@tonic-gate 	__asm__("xchgb %h0, %b0" : "+q" (value));
530Sstevel@tonic-gate #endif
540Sstevel@tonic-gate 	return (value);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
ntohs(uint16_t value)570Sstevel@tonic-gate extern __inline__ uint16_t ntohs(uint16_t value)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate #if defined(__amd64)
600Sstevel@tonic-gate 	__asm__("xchgb %h0, %b0" : "+Q" (value));
610Sstevel@tonic-gate #elif defined(__i386)
620Sstevel@tonic-gate 	__asm__("xchgb %h0, %b0" : "+q" (value));
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate 	return (value);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
htonl(uint32_t value)67*7421SDaniel.Anderson@Sun.COM extern __inline__ uint32_t htonl(uint32_t value)
68*7421SDaniel.Anderson@Sun.COM {
69*7421SDaniel.Anderson@Sun.COM 	__asm__("bswap %0" : "+r" (value));
70*7421SDaniel.Anderson@Sun.COM 	return (value);
71*7421SDaniel.Anderson@Sun.COM }
72*7421SDaniel.Anderson@Sun.COM 
ntohl(uint32_t value)73*7421SDaniel.Anderson@Sun.COM extern __inline__ uint32_t ntohl(uint32_t value)
74*7421SDaniel.Anderson@Sun.COM {
75*7421SDaniel.Anderson@Sun.COM 	__asm__("bswap %0" : "+r" (value));
76*7421SDaniel.Anderson@Sun.COM 	return (value);
77*7421SDaniel.Anderson@Sun.COM }
78*7421SDaniel.Anderson@Sun.COM 
79*7421SDaniel.Anderson@Sun.COM #if defined(__amd64)
htonll(uint64_t value)80*7421SDaniel.Anderson@Sun.COM extern __inline__ uint64_t htonll(uint64_t value)
81*7421SDaniel.Anderson@Sun.COM {
82*7421SDaniel.Anderson@Sun.COM 	__asm__("bswapq %0" : "+r" (value));
83*7421SDaniel.Anderson@Sun.COM 	return (value);
84*7421SDaniel.Anderson@Sun.COM }
85*7421SDaniel.Anderson@Sun.COM 
ntohll(uint64_t value)86*7421SDaniel.Anderson@Sun.COM extern __inline__ uint64_t ntohll(uint64_t value)
87*7421SDaniel.Anderson@Sun.COM {
88*7421SDaniel.Anderson@Sun.COM 	__asm__("bswapq %0" : "+r" (value));
89*7421SDaniel.Anderson@Sun.COM 	return (value);
90*7421SDaniel.Anderson@Sun.COM }
91*7421SDaniel.Anderson@Sun.COM 
92*7421SDaniel.Anderson@Sun.COM #elif defined(__i386)
93*7421SDaniel.Anderson@Sun.COM /* Use the htonl() and ntohl() inline functions defined above */
htonll(uint64_t value)94*7421SDaniel.Anderson@Sun.COM extern __inline__ uint64_t htonll(uint64_t value)
95*7421SDaniel.Anderson@Sun.COM {
96*7421SDaniel.Anderson@Sun.COM 	return (htonl(value >> 32) | ((uint64_t)htonl(value) << 32));
97*7421SDaniel.Anderson@Sun.COM }
98*7421SDaniel.Anderson@Sun.COM 
ntohll(uint64_t value)99*7421SDaniel.Anderson@Sun.COM extern __inline__ uint64_t ntohll(uint64_t value)
100*7421SDaniel.Anderson@Sun.COM {
101*7421SDaniel.Anderson@Sun.COM 	return (ntohl(value >> 32) | (uint64_t)ntohl(value) << 32);
102*7421SDaniel.Anderson@Sun.COM }
103*7421SDaniel.Anderson@Sun.COM #endif	/* __amd64 */
104*7421SDaniel.Anderson@Sun.COM 
1050Sstevel@tonic-gate #endif	/* __i386 || __amd64 */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate #endif	/* !__lint && __GNUC__ */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #ifdef	__cplusplus
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #endif	/* _ASM_BYTEORDER_H */
114