xref: /titanic_44/usr/src/lib/libc/amd64/gen/byteorder.s (revision 577bdc16588431dcec0742144b1ba2ef4b0581fe)
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
5dfb96a4fSab196087 * Common Development and Distribution License (the "License").
6dfb96a4fSab196087 * 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.
24*577bdc16SRobert Mustacchi * Copyright (c) 2015, Joyent, Inc.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
279a70fc3bSMark J. Nelson	.file	"byteorder.s"
287c478bd9Sstevel@tonic-gate
294b56a003SDaniel Anderson#include <sys/asm_linkage.h>
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate	/*
324b56a003SDaniel Anderson	 * NOTE: htonll/ntohll, htonl/ntohl, and htons/ntohs are identical
334b56a003SDaniel Anderson	 * routines. As such, they could be implemented as a single routine,
344b56a003SDaniel Anderson	 * using multiple ALTENTRY/SET_SIZE definitions. We don't do this so
35dfb96a4fSab196087	 * that they will have unique addresses, allowing DTrace and
36dfb96a4fSab196087	 * other debuggers to tell them apart.
37dfb96a4fSab196087	 */
38dfb96a4fSab196087
394b56a003SDaniel Anderson/*
404b56a003SDaniel Anderson *	unsigned long long htonll( hll )
414b56a003SDaniel Anderson *	unsigned long long ntohll( hll )
424b56a003SDaniel Anderson *	unsigned long long hll;
434b56a003SDaniel Anderson *	reverses the byte order of 'uint64_t hll' on little endian machines
444b56a003SDaniel Anderson */
454b56a003SDaniel Anderson	ENTRY(htonll)
464b56a003SDaniel Anderson	movq	%rdi, %rax	/* %rax = hll */
474b56a003SDaniel Anderson	bswapq	%rax		/* reverses the byte order of %rax */
484b56a003SDaniel Anderson	ret			/* return (%rax) */
494b56a003SDaniel Anderson	SET_SIZE(htonll)
504b56a003SDaniel Anderson
514b56a003SDaniel Anderson	ENTRY(ntohll)
524b56a003SDaniel Anderson	movq	%rdi, %rax	/* %rax = hll */
534b56a003SDaniel Anderson	bswapq	%rax		/* reverses the byte order of %rax */
544b56a003SDaniel Anderson	ret			/* return (%rax) */
554b56a003SDaniel Anderson	SET_SIZE(ntohll)
564b56a003SDaniel Anderson
57dfb96a4fSab196087
58dfb96a4fSab196087/*
597c478bd9Sstevel@tonic-gate *	unsigned long htonl( hl )
607c478bd9Sstevel@tonic-gate *	unsigned long ntohl( hl )
614b56a003SDaniel Anderson *	unsigned long hl;
624b56a003SDaniel Anderson *	reverses the byte order of 'uint32_t hl' on little endian machines
637c478bd9Sstevel@tonic-gate */
647c478bd9Sstevel@tonic-gate	ENTRY(htonl)
657c478bd9Sstevel@tonic-gate	movl	%edi, %eax	/* %eax = hl */
667c478bd9Sstevel@tonic-gate	bswap	%eax		/* reverses the byte order of %eax */
677c478bd9Sstevel@tonic-gate	ret			/* return (%eax) */
687c478bd9Sstevel@tonic-gate	SET_SIZE(htonl)
69dfb96a4fSab196087
70dfb96a4fSab196087	ENTRY(ntohl)
71dfb96a4fSab196087	movl	%edi, %eax	/* %eax = hl */
72dfb96a4fSab196087	bswap	%eax		/* reverses the byte order of %eax */
73dfb96a4fSab196087	ret			/* return (%eax) */
747c478bd9Sstevel@tonic-gate	SET_SIZE(ntohl)
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate/*
777c478bd9Sstevel@tonic-gate *	unsigned short htons( hs )
784b56a003SDaniel Anderson *	unsigned short hs;
794b56a003SDaniel Anderson *	reverses the byte order of 'uint16_t hs' on little endian machines.
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate	ENTRY(htons)
827c478bd9Sstevel@tonic-gate	movl	%edi, %eax	/* %eax = hs */
837c478bd9Sstevel@tonic-gate	bswap	%eax		/* reverses the byte order of %eax */
847c478bd9Sstevel@tonic-gate	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
857c478bd9Sstevel@tonic-gate	ret			/* return (%eax) */
867c478bd9Sstevel@tonic-gate	SET_SIZE(htons)
87dfb96a4fSab196087
88dfb96a4fSab196087	ENTRY(ntohs)
89dfb96a4fSab196087	movl	%edi, %eax	/* %eax = hs */
90dfb96a4fSab196087	bswap	%eax		/* reverses the byte order of %eax */
91dfb96a4fSab196087	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
92dfb96a4fSab196087	ret			/* return (%eax) */
937c478bd9Sstevel@tonic-gate	SET_SIZE(ntohs)
94*577bdc16SRobert Mustacchi
95*577bdc16SRobert Mustacchi/*
96*577bdc16SRobert Mustacchi *	uint16_t htobe16(uint16_t in);
97*577bdc16SRobert Mustacchi *	uint32_t htobe32(uint32_t in);
98*577bdc16SRobert Mustacchi *	uint64_t htobe64(uint64_t in);
99*577bdc16SRobert Mustacchi *
100*577bdc16SRobert Mustacchi *	Byte swap 16, 32, and 64 bits respectively.
101*577bdc16SRobert Mustacchi *	eg. htons(), htonl(), and htonll().
102*577bdc16SRobert Mustacchi */
103*577bdc16SRobert Mustacchi	ENTRY(htobe16)
104*577bdc16SRobert Mustacchi	movl	%edi, %eax	/* %eax = hs */
105*577bdc16SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
106*577bdc16SRobert Mustacchi	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
107*577bdc16SRobert Mustacchi	ret			/* return (%eax) */
108*577bdc16SRobert Mustacchi	SET_SIZE(htobe16)
109*577bdc16SRobert Mustacchi
110*577bdc16SRobert Mustacchi	ENTRY(htobe32)
111*577bdc16SRobert Mustacchi	movl	%edi, %eax	/* %eax = hl */
112*577bdc16SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
113*577bdc16SRobert Mustacchi	ret			/* return (%eax) */
114*577bdc16SRobert Mustacchi	SET_SIZE(htobe32)
115*577bdc16SRobert Mustacchi
116*577bdc16SRobert Mustacchi	ENTRY(htobe64)
117*577bdc16SRobert Mustacchi	movq	%rdi, %rax	/* %rax = hll */
118*577bdc16SRobert Mustacchi	bswapq	%rax		/* reverses the byte order of %rax */
119*577bdc16SRobert Mustacchi	ret			/* return (%rax) */
120*577bdc16SRobert Mustacchi	SET_SIZE(htobe64)
121*577bdc16SRobert Mustacchi
122*577bdc16SRobert Mustacchi
123*577bdc16SRobert Mustacchi/*
124*577bdc16SRobert Mustacchi *	uint16_t betoh16(uint16_t in)
125*577bdc16SRobert Mustacchi * 	uint16_t be16toh(uint16_t in)
126*577bdc16SRobert Mustacchi *
127*577bdc16SRobert Mustacchi *	Convert in to little endian, eg. ntohs()
128*577bdc16SRobert Mustacchi */
129*577bdc16SRobert Mustacchi	ENTRY(betoh16)
130*577bdc16SRobert Mustacchi	movl	%edi, %eax	/* %eax = hs */
131*577bdc16SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
132*577bdc16SRobert Mustacchi	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
133*577bdc16SRobert Mustacchi	ret			/* return (%eax) */
134*577bdc16SRobert Mustacchi	SET_SIZE(betoh16)
135*577bdc16SRobert Mustacchi
136*577bdc16SRobert Mustacchi	ENTRY(be16toh)
137*577bdc16SRobert Mustacchi	movl	%edi, %eax	/* %eax = hs */
138*577bdc16SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
139*577bdc16SRobert Mustacchi	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
140*577bdc16SRobert Mustacchi	ret			/* return (%eax) */
141*577bdc16SRobert Mustacchi	SET_SIZE(be16toh)
142*577bdc16SRobert Mustacchi
143*577bdc16SRobert Mustacchi
144*577bdc16SRobert Mustacchi/*
145*577bdc16SRobert Mustacchi *	uint32_t betoh32(uint32_t in)
146*577bdc16SRobert Mustacchi *	uint32_t be32toh(uint32_t in)
147*577bdc16SRobert Mustacchi *
148*577bdc16SRobert Mustacchi *	Convert in to little endian, eg. ntohl()
149*577bdc16SRobert Mustacchi */
150*577bdc16SRobert Mustacchi	ENTRY(betoh32)
151*577bdc16SRobert Mustacchi	movl	%edi, %eax	/* %eax = hl */
152*577bdc16SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
153*577bdc16SRobert Mustacchi	ret			/* return (%eax) */
154*577bdc16SRobert Mustacchi	SET_SIZE(betoh32)
155*577bdc16SRobert Mustacchi
156*577bdc16SRobert Mustacchi	ENTRY(be32toh)
157*577bdc16SRobert Mustacchi	movl	%edi, %eax	/* %eax = hl */
158*577bdc16SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
159*577bdc16SRobert Mustacchi	ret			/* return (%eax) */
160*577bdc16SRobert Mustacchi	SET_SIZE(be32toh)
161*577bdc16SRobert Mustacchi
162*577bdc16SRobert Mustacchi
163*577bdc16SRobert Mustacchi/*
164*577bdc16SRobert Mustacchi *	uint64_t betoh64(uint64_t in)
165*577bdc16SRobert Mustacchi *	uint64_t be64toh(uint64_t in)
166*577bdc16SRobert Mustacchi *
167*577bdc16SRobert Mustacchi *	Convert in to little endian, eg. ntohll()
168*577bdc16SRobert Mustacchi */
169*577bdc16SRobert Mustacchi	ENTRY(betoh64)
170*577bdc16SRobert Mustacchi	movq	%rdi, %rax	/* %rax = hll */
171*577bdc16SRobert Mustacchi	bswapq	%rax		/* reverses the byte order of %rax */
172*577bdc16SRobert Mustacchi	ret			/* return (%rax) */
173*577bdc16SRobert Mustacchi	SET_SIZE(betoh64)
174*577bdc16SRobert Mustacchi
175*577bdc16SRobert Mustacchi	ENTRY(be64toh)
176*577bdc16SRobert Mustacchi	movq	%rdi, %rax	/* %rax = hll */
177*577bdc16SRobert Mustacchi	bswapq	%rax		/* reverses the byte order of %rax */
178*577bdc16SRobert Mustacchi	ret			/* return (%rax) */
179*577bdc16SRobert Mustacchi	SET_SIZE(be64toh)
180