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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright (c) 2015, Joyent, Inc.
29 #include <sys/asm_linkage.h>
32 * NOTE: htonll/ntohll, htonl/ntohl, and htons/ntohs are identical
33 * routines. As such, they could be implemented as a single routine,
34 * using multiple ALTENTRY/SET_SIZE definitions. We don't do this so
35 * that they will have unique addresses, allowing DTrace and
36 * other debuggers to tell them apart.
40 * unsigned long long htonll( hll )
41 * unsigned long long ntohll( hll )
42 * unsigned long long hll;
43 * reverses the byte order of 'uint64_t hll' on little endian machines
46 movq
%rdi
, %rax
/* %rax = hll */
47 bswapq
%rax
/* reverses the byte order of %rax */
48 ret
/* return (%rax) */
52 movq
%rdi
, %rax
/* %rax = hll */
53 bswapq
%rax
/* reverses the byte order of %rax */
54 ret
/* return (%rax) */
59 * unsigned long htonl( hl )
60 * unsigned long ntohl( hl )
62 * reverses the byte order of 'uint32_t hl' on little endian machines
65 movl
%edi
, %eax
/* %eax = hl */
66 bswap
%eax
/* reverses the byte order of %eax */
67 ret
/* return (%eax) */
71 movl
%edi
, %eax
/* %eax = hl */
72 bswap
%eax
/* reverses the byte order of %eax */
73 ret
/* return (%eax) */
77 * unsigned short htons( hs )
79 * reverses the byte order of 'uint16_t hs' on little endian machines.
82 movl
%edi
, %eax
/* %eax = hs */
83 bswap
%eax
/* reverses the byte order of %eax */
84 shrl $
16, %eax
/* moves high 16-bit to low 16-bit */
85 ret
/* return (%eax) */
89 movl
%edi
, %eax
/* %eax = hs */
90 bswap
%eax
/* reverses the byte order of %eax */
91 shrl $
16, %eax
/* moves high 16-bit to low 16-bit */
92 ret
/* return (%eax) */
96 * uint16_t htobe16(uint16_t in);
97 * uint32_t htobe32(uint32_t in);
98 * uint64_t htobe64(uint64_t in);
100 * Byte swap 16, 32, and 64 bits respectively.
101 * eg. htons(), htonl(), and htonll().
104 movl
%edi
, %eax
/* %eax = hs */
105 bswap
%eax
/* reverses the byte order of %eax */
106 shrl $
16, %eax
/* moves high 16-bit to low 16-bit */
107 ret
/* return (%eax) */
111 movl
%edi
, %eax
/* %eax = hl */
112 bswap
%eax
/* reverses the byte order of %eax */
113 ret
/* return (%eax) */
117 movq
%rdi
, %rax
/* %rax = hll */
118 bswapq
%rax
/* reverses the byte order of %rax */
119 ret
/* return (%rax) */
124 * uint16_t betoh16(uint16_t in)
125 * uint16_t be16toh(uint16_t in)
127 * Convert in to little endian, eg. ntohs()
130 movl
%edi
, %eax
/* %eax = hs */
131 bswap
%eax
/* reverses the byte order of %eax */
132 shrl $
16, %eax
/* moves high 16-bit to low 16-bit */
133 ret
/* return (%eax) */
137 movl
%edi
, %eax
/* %eax = hs */
138 bswap
%eax
/* reverses the byte order of %eax */
139 shrl $
16, %eax
/* moves high 16-bit to low 16-bit */
140 ret
/* return (%eax) */
145 * uint32_t betoh32(uint32_t in)
146 * uint32_t be32toh(uint32_t in)
148 * Convert in to little endian, eg. ntohl()
151 movl
%edi
, %eax
/* %eax = hl */
152 bswap
%eax
/* reverses the byte order of %eax */
153 ret
/* return (%eax) */
157 movl
%edi
, %eax
/* %eax = hl */
158 bswap
%eax
/* reverses the byte order of %eax */
159 ret
/* return (%eax) */
164 * uint64_t betoh64(uint64_t in)
165 * uint64_t be64toh(uint64_t in)
167 * Convert in to little endian, eg. ntohll()
170 movq
%rdi
, %rax
/* %rax = hll */
171 bswapq
%rax
/* reverses the byte order of %rax */
172 ret
/* return (%rax) */
176 movq
%rdi
, %rax
/* %rax = hll */
177 bswapq
%rax
/* reverses the byte order of %rax */
178 ret
/* return (%rax) */