xref: /illumos-gate/usr/src/uts/intel/asm/byteorder.h (revision 6b7143d7)
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
54b56a003SDaniel Anderson  * Common Development and Distribution License (the "License").
64b56a003SDaniel Anderson  * 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  */
217c478bd9Sstevel@tonic-gate /*
224b56a003SDaniel Anderson  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ifndef _ASM_BYTEORDER_H
277c478bd9Sstevel@tonic-gate #define	_ASM_BYTEORDER_H
287c478bd9Sstevel@tonic-gate 
29*6b7143d7SRichard Lowe #include <sys/ccompile.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
337c478bd9Sstevel@tonic-gate extern "C" {
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #if !defined(__lint) && defined(__GNUC__)
377c478bd9Sstevel@tonic-gate 
384b56a003SDaniel Anderson /*
394b56a003SDaniel Anderson  * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs()
404b56a003SDaniel Anderson  * These functions reverse the byte order of the input parameter and returns
414b56a003SDaniel Anderson  * the result.  This is to convert the byte order from host byte order
424b56a003SDaniel Anderson  * (little endian) to network byte order (big endian), or vice versa.
434b56a003SDaniel Anderson  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
464b56a003SDaniel Anderson #if defined(__i386) || defined(__amd64)
477c478bd9Sstevel@tonic-gate 
48*6b7143d7SRichard Lowe extern __GNU_INLINE uint16_t
htons(uint16_t value)49*6b7143d7SRichard Lowe htons(uint16_t value)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate #if defined(__amd64)
527c478bd9Sstevel@tonic-gate 	__asm__("xchgb %h0, %b0" : "+Q" (value));
537c478bd9Sstevel@tonic-gate #elif defined(__i386)
547c478bd9Sstevel@tonic-gate 	__asm__("xchgb %h0, %b0" : "+q" (value));
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate 	return (value);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
59*6b7143d7SRichard Lowe extern __GNU_INLINE uint16_t
ntohs(uint16_t value)60*6b7143d7SRichard Lowe ntohs(uint16_t value)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate #if defined(__amd64)
637c478bd9Sstevel@tonic-gate 	__asm__("xchgb %h0, %b0" : "+Q" (value));
647c478bd9Sstevel@tonic-gate #elif defined(__i386)
657c478bd9Sstevel@tonic-gate 	__asm__("xchgb %h0, %b0" : "+q" (value));
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 	return (value);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
70*6b7143d7SRichard Lowe extern __GNU_INLINE uint32_t
htonl(uint32_t value)71*6b7143d7SRichard Lowe htonl(uint32_t value)
724b56a003SDaniel Anderson {
734b56a003SDaniel Anderson 	__asm__("bswap %0" : "+r" (value));
744b56a003SDaniel Anderson 	return (value);
754b56a003SDaniel Anderson }
764b56a003SDaniel Anderson 
77*6b7143d7SRichard Lowe extern __GNU_INLINE uint32_t
ntohl(uint32_t value)78*6b7143d7SRichard Lowe ntohl(uint32_t value)
794b56a003SDaniel Anderson {
804b56a003SDaniel Anderson 	__asm__("bswap %0" : "+r" (value));
814b56a003SDaniel Anderson 	return (value);
824b56a003SDaniel Anderson }
834b56a003SDaniel Anderson 
844b56a003SDaniel Anderson #if defined(__amd64)
85*6b7143d7SRichard Lowe extern __GNU_INLINE uint64_t
htonll(uint64_t value)86*6b7143d7SRichard Lowe htonll(uint64_t value)
874b56a003SDaniel Anderson {
884b56a003SDaniel Anderson 	__asm__("bswapq %0" : "+r" (value));
894b56a003SDaniel Anderson 	return (value);
904b56a003SDaniel Anderson }
914b56a003SDaniel Anderson 
92*6b7143d7SRichard Lowe extern __GNU_INLINE uint64_t
ntohll(uint64_t value)93*6b7143d7SRichard Lowe ntohll(uint64_t value)
944b56a003SDaniel Anderson {
954b56a003SDaniel Anderson 	__asm__("bswapq %0" : "+r" (value));
964b56a003SDaniel Anderson 	return (value);
974b56a003SDaniel Anderson }
984b56a003SDaniel Anderson 
994b56a003SDaniel Anderson #elif defined(__i386)
1004b56a003SDaniel Anderson /* Use the htonl() and ntohl() inline functions defined above */
101*6b7143d7SRichard Lowe extern __GNU_INLINE uint64_t
htonll(uint64_t value)102*6b7143d7SRichard Lowe htonll(uint64_t value)
1034b56a003SDaniel Anderson {
1044b56a003SDaniel Anderson 	return (htonl(value >> 32) | ((uint64_t)htonl(value) << 32));
1054b56a003SDaniel Anderson }
1064b56a003SDaniel Anderson 
107*6b7143d7SRichard Lowe extern __GNU_INLINE uint64_t
ntohll(uint64_t value)108*6b7143d7SRichard Lowe ntohll(uint64_t value)
1094b56a003SDaniel Anderson {
1104b56a003SDaniel Anderson 	return (ntohl(value >> 32) | (uint64_t)ntohl(value) << 32);
1114b56a003SDaniel Anderson }
1124b56a003SDaniel Anderson #endif	/* __amd64 */
1134b56a003SDaniel Anderson 
1147c478bd9Sstevel@tonic-gate #endif	/* __i386 || __amd64 */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #endif	/* !__lint && __GNUC__ */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #endif	/* _ASM_BYTEORDER_H */
123