import less(1)
[unleashed/tickless.git] / usr / src / uts / intel / asm / byteorder.h
blobe09a61a13bfc7cc7eeab057954af13f98d122ed9
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #ifndef _ASM_BYTEORDER_H
27 #define _ASM_BYTEORDER_H
29 #include <sys/ccompile.h>
30 #include <sys/types.h>
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
38 * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs()
39 * These functions reverse the byte order of the input parameter and returns
40 * the result. This is to convert the byte order from host byte order
41 * (little endian) to network byte order (big endian), or vice versa.
45 #if defined(__i386) || defined(__amd64)
47 extern __GNU_INLINE uint16_t
48 htons(uint16_t value)
50 #if defined(__amd64)
51 __asm__("xchgb %h0, %b0" : "+Q" (value));
52 #elif defined(__i386)
53 __asm__("xchgb %h0, %b0" : "+q" (value));
54 #endif
55 return (value);
58 extern __GNU_INLINE uint16_t
59 ntohs(uint16_t value)
61 #if defined(__amd64)
62 __asm__("xchgb %h0, %b0" : "+Q" (value));
63 #elif defined(__i386)
64 __asm__("xchgb %h0, %b0" : "+q" (value));
65 #endif
66 return (value);
69 extern __GNU_INLINE uint32_t
70 htonl(uint32_t value)
72 __asm__("bswap %0" : "+r" (value));
73 return (value);
76 extern __GNU_INLINE uint32_t
77 ntohl(uint32_t value)
79 __asm__("bswap %0" : "+r" (value));
80 return (value);
83 #if defined(__amd64)
84 extern __GNU_INLINE uint64_t
85 htonll(uint64_t value)
87 __asm__("bswapq %0" : "+r" (value));
88 return (value);
91 extern __GNU_INLINE uint64_t
92 ntohll(uint64_t value)
94 __asm__("bswapq %0" : "+r" (value));
95 return (value);
98 #elif defined(__i386)
99 /* Use the htonl() and ntohl() inline functions defined above */
100 extern __GNU_INLINE uint64_t
101 htonll(uint64_t value)
103 return (htonl(value >> 32) | ((uint64_t)htonl(value) << 32));
106 extern __GNU_INLINE uint64_t
107 ntohll(uint64_t value)
109 return (ntohl(value >> 32) | (uint64_t)ntohl(value) << 32);
111 #endif /* __amd64 */
113 #endif /* __i386 || __amd64 */
116 #ifdef __cplusplus
118 #endif
120 #endif /* _ASM_BYTEORDER_H */