xref: /illumos-gate/usr/src/lib/libc/amd64/gen/byteorder.S (revision 55fea89d)
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 * Copyright (c) 2015, Joyent, Inc.
25 */
26
27	.file	"byteorder.s"
28
29#include <sys/asm_linkage.h>
30
31	/*
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.
37	 */
38
39/*
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
44 */
45	ENTRY(htonll)
46	movq	%rdi, %rax	/* %rax = hll */
47	bswapq	%rax		/* reverses the byte order of %rax */
48	ret			/* return (%rax) */
49	SET_SIZE(htonll)
50
51	ENTRY(ntohll)
52	movq	%rdi, %rax	/* %rax = hll */
53	bswapq	%rax		/* reverses the byte order of %rax */
54	ret			/* return (%rax) */
55	SET_SIZE(ntohll)
56
57
58/*
59 *	unsigned long htonl( hl )
60 *	unsigned long ntohl( hl )
61 *	unsigned long hl;
62 *	reverses the byte order of 'uint32_t hl' on little endian machines
63 */
64	ENTRY(htonl)
65	movl	%edi, %eax	/* %eax = hl */
66	bswap	%eax		/* reverses the byte order of %eax */
67	ret			/* return (%eax) */
68	SET_SIZE(htonl)
69
70	ENTRY(ntohl)
71	movl	%edi, %eax	/* %eax = hl */
72	bswap	%eax		/* reverses the byte order of %eax */
73	ret			/* return (%eax) */
74	SET_SIZE(ntohl)
75
76/*
77 *	unsigned short htons( hs )
78 *	unsigned short hs;
79 *	reverses the byte order of 'uint16_t hs' on little endian machines.
80 */
81	ENTRY(htons)
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) */
86	SET_SIZE(htons)
87
88	ENTRY(ntohs)
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) */
93	SET_SIZE(ntohs)
94
95/*
96 *	uint16_t htobe16(uint16_t in);
97 *	uint32_t htobe32(uint32_t in);
98 *	uint64_t htobe64(uint64_t in);
99 *
100 *	Byte swap 16, 32, and 64 bits respectively.
101 *	eg. htons(), htonl(), and htonll().
102 */
103	ENTRY(htobe16)
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) */
108	SET_SIZE(htobe16)
109
110	ENTRY(htobe32)
111	movl	%edi, %eax	/* %eax = hl */
112	bswap	%eax		/* reverses the byte order of %eax */
113	ret			/* return (%eax) */
114	SET_SIZE(htobe32)
115
116	ENTRY(htobe64)
117	movq	%rdi, %rax	/* %rax = hll */
118	bswapq	%rax		/* reverses the byte order of %rax */
119	ret			/* return (%rax) */
120	SET_SIZE(htobe64)
121
122
123/*
124 *	uint16_t betoh16(uint16_t in)
125 * 	uint16_t be16toh(uint16_t in)
126 *
127 *	Convert in to little endian, eg. ntohs()
128 */
129	ENTRY(betoh16)
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) */
134	SET_SIZE(betoh16)
135
136	ENTRY(be16toh)
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) */
141	SET_SIZE(be16toh)
142
143
144/*
145 *	uint32_t betoh32(uint32_t in)
146 *	uint32_t be32toh(uint32_t in)
147 *
148 *	Convert in to little endian, eg. ntohl()
149 */
150	ENTRY(betoh32)
151	movl	%edi, %eax	/* %eax = hl */
152	bswap	%eax		/* reverses the byte order of %eax */
153	ret			/* return (%eax) */
154	SET_SIZE(betoh32)
155
156	ENTRY(be32toh)
157	movl	%edi, %eax	/* %eax = hl */
158	bswap	%eax		/* reverses the byte order of %eax */
159	ret			/* return (%eax) */
160	SET_SIZE(be32toh)
161
162
163/*
164 *	uint64_t betoh64(uint64_t in)
165 *	uint64_t be64toh(uint64_t in)
166 *
167 *	Convert in to little endian, eg. ntohll()
168 */
169	ENTRY(betoh64)
170	movq	%rdi, %rax	/* %rax = hll */
171	bswapq	%rax		/* reverses the byte order of %rax */
172	ret			/* return (%rax) */
173	SET_SIZE(betoh64)
174
175	ENTRY(be64toh)
176	movq	%rdi, %rax	/* %rax = hll */
177	bswapq	%rax		/* reverses the byte order of %rax */
178	ret			/* return (%rax) */
179	SET_SIZE(be64toh)
180