xref: /illumos-gate/usr/src/uts/intel/asm/byteorder.h (revision 69a119caa6570c7077699161b7c28b6ee9f8b0f4)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _ASM_BYTEORDER_H
27 #define	_ASM_BYTEORDER_H
28 
29 #include <sys/ccompile.h>
30 #include <sys/types.h>
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 #if !defined(__lint) && defined(__GNUC__)
37 
38 /*
39  * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs()
40  * These functions reverse the byte order of the input parameter and returns
41  * the result.  This is to convert the byte order from host byte order
42  * (little endian) to network byte order (big endian), or vice versa.
43  */
44 
45 
46 #if defined(__i386) || defined(__amd64)
47 
48 extern __GNU_INLINE uint16_t
49 htons(uint16_t value)
50 {
51 #if defined(__amd64)
52 	__asm__("xchgb %h0, %b0" : "+Q" (value));
53 #elif defined(__i386)
54 	__asm__("xchgb %h0, %b0" : "+q" (value));
55 #endif
56 	return (value);
57 }
58 
59 extern __GNU_INLINE uint16_t
60 ntohs(uint16_t value)
61 {
62 #if defined(__amd64)
63 	__asm__("xchgb %h0, %b0" : "+Q" (value));
64 #elif defined(__i386)
65 	__asm__("xchgb %h0, %b0" : "+q" (value));
66 #endif
67 	return (value);
68 }
69 
70 extern __GNU_INLINE uint32_t
71 htonl(uint32_t value)
72 {
73 	__asm__("bswap %0" : "+r" (value));
74 	return (value);
75 }
76 
77 extern __GNU_INLINE uint32_t
78 ntohl(uint32_t value)
79 {
80 	__asm__("bswap %0" : "+r" (value));
81 	return (value);
82 }
83 
84 #if defined(__amd64)
85 extern __GNU_INLINE uint64_t
86 htonll(uint64_t value)
87 {
88 	__asm__("bswapq %0" : "+r" (value));
89 	return (value);
90 }
91 
92 extern __GNU_INLINE uint64_t
93 ntohll(uint64_t value)
94 {
95 	__asm__("bswapq %0" : "+r" (value));
96 	return (value);
97 }
98 
99 #elif defined(__i386)
100 /* Use the htonl() and ntohl() inline functions defined above */
101 extern __GNU_INLINE uint64_t
102 htonll(uint64_t value)
103 {
104 	return (htonl(value >> 32) | ((uint64_t)htonl(value) << 32));
105 }
106 
107 extern __GNU_INLINE uint64_t
108 ntohll(uint64_t value)
109 {
110 	return (ntohl(value >> 32) | (uint64_t)ntohl(value) << 32);
111 }
112 #endif	/* __amd64 */
113 
114 #endif	/* __i386 || __amd64 */
115 
116 #endif	/* !__lint && __GNUC__ */
117 
118 #ifdef	__cplusplus
119 }
120 #endif
121 
122 #endif	/* _ASM_BYTEORDER_H */
123