xref: /illumos-gate/usr/src/lib/libc/amd64/gen/byteorder.S (revision 55fea89d)
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
5dfb96a4fSab * Common Development and Distribution License (the "License").
6dfb96a4fSab * 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.
247dc9a163SRobert 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
31dfb96a4fSab	/*
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
35dfb96a4fSab	 * that they will have unique addresses, allowing DTrace and
364b56a003SDaniel Anderson	 * other debuggers to tell them apart.
37dfb96a4fSab	 */
38dfb96a4fSab
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
57dfb96a4fSab
587c478bd9Sstevel@tonic-gate/*
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)
69dfb96a4fSab
70dfb96a4fSab	ENTRY(ntohl)
71dfb96a4fSab	movl	%edi, %eax	/* %eax = hl */
72dfb96a4fSab	bswap	%eax		/* reverses the byte order of %eax */
73dfb96a4fSab	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)
87dfb96a4fSab
88dfb96a4fSab	ENTRY(ntohs)
89dfb96a4fSab	movl	%edi, %eax	/* %eax = hs */
90dfb96a4fSab	bswap	%eax		/* reverses the byte order of %eax */
91dfb96a4fSab	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
92dfb96a4fSab	ret			/* return (%eax) */
937c478bd9Sstevel@tonic-gate	SET_SIZE(ntohs)
947dc9a163SRobert Mustacchi
957dc9a163SRobert Mustacchi/*
96*55fea89dSDan Cross *	uint16_t htobe16(uint16_t in);
977dc9a163SRobert Mustacchi *	uint32_t htobe32(uint32_t in);
987dc9a163SRobert Mustacchi *	uint64_t htobe64(uint64_t in);
997dc9a163SRobert Mustacchi *
1007dc9a163SRobert Mustacchi *	Byte swap 16, 32, and 64 bits respectively.
1017dc9a163SRobert Mustacchi *	eg. htons(), htonl(), and htonll().
1027dc9a163SRobert Mustacchi */
1037dc9a163SRobert Mustacchi	ENTRY(htobe16)
1047dc9a163SRobert Mustacchi	movl	%edi, %eax	/* %eax = hs */
1057dc9a163SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
1067dc9a163SRobert Mustacchi	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
1077dc9a163SRobert Mustacchi	ret			/* return (%eax) */
1087dc9a163SRobert Mustacchi	SET_SIZE(htobe16)
1097dc9a163SRobert Mustacchi
1107dc9a163SRobert Mustacchi	ENTRY(htobe32)
1117dc9a163SRobert Mustacchi	movl	%edi, %eax	/* %eax = hl */
1127dc9a163SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
1137dc9a163SRobert Mustacchi	ret			/* return (%eax) */
1147dc9a163SRobert Mustacchi	SET_SIZE(htobe32)
1157dc9a163SRobert Mustacchi
1167dc9a163SRobert Mustacchi	ENTRY(htobe64)
1177dc9a163SRobert Mustacchi	movq	%rdi, %rax	/* %rax = hll */
1187dc9a163SRobert Mustacchi	bswapq	%rax		/* reverses the byte order of %rax */
1197dc9a163SRobert Mustacchi	ret			/* return (%rax) */
1207dc9a163SRobert Mustacchi	SET_SIZE(htobe64)
1217dc9a163SRobert Mustacchi
1227dc9a163SRobert Mustacchi
1237dc9a163SRobert Mustacchi/*
1247dc9a163SRobert Mustacchi *	uint16_t betoh16(uint16_t in)
1257dc9a163SRobert Mustacchi * 	uint16_t be16toh(uint16_t in)
1267dc9a163SRobert Mustacchi *
1277dc9a163SRobert Mustacchi *	Convert in to little endian, eg. ntohs()
1287dc9a163SRobert Mustacchi */
1297dc9a163SRobert Mustacchi	ENTRY(betoh16)
1307dc9a163SRobert Mustacchi	movl	%edi, %eax	/* %eax = hs */
1317dc9a163SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
1327dc9a163SRobert Mustacchi	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
1337dc9a163SRobert Mustacchi	ret			/* return (%eax) */
1347dc9a163SRobert Mustacchi	SET_SIZE(betoh16)
1357dc9a163SRobert Mustacchi
1367dc9a163SRobert Mustacchi	ENTRY(be16toh)
1377dc9a163SRobert Mustacchi	movl	%edi, %eax	/* %eax = hs */
1387dc9a163SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
1397dc9a163SRobert Mustacchi	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
1407dc9a163SRobert Mustacchi	ret			/* return (%eax) */
1417dc9a163SRobert Mustacchi	SET_SIZE(be16toh)
1427dc9a163SRobert Mustacchi
1437dc9a163SRobert Mustacchi
1447dc9a163SRobert Mustacchi/*
1457dc9a163SRobert Mustacchi *	uint32_t betoh32(uint32_t in)
1467dc9a163SRobert Mustacchi *	uint32_t be32toh(uint32_t in)
1477dc9a163SRobert Mustacchi *
1487dc9a163SRobert Mustacchi *	Convert in to little endian, eg. ntohl()
1497dc9a163SRobert Mustacchi */
1507dc9a163SRobert Mustacchi	ENTRY(betoh32)
1517dc9a163SRobert Mustacchi	movl	%edi, %eax	/* %eax = hl */
1527dc9a163SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
1537dc9a163SRobert Mustacchi	ret			/* return (%eax) */
1547dc9a163SRobert Mustacchi	SET_SIZE(betoh32)
1557dc9a163SRobert Mustacchi
1567dc9a163SRobert Mustacchi	ENTRY(be32toh)
1577dc9a163SRobert Mustacchi	movl	%edi, %eax	/* %eax = hl */
1587dc9a163SRobert Mustacchi	bswap	%eax		/* reverses the byte order of %eax */
1597dc9a163SRobert Mustacchi	ret			/* return (%eax) */
1607dc9a163SRobert Mustacchi	SET_SIZE(be32toh)
1617dc9a163SRobert Mustacchi
1627dc9a163SRobert Mustacchi
1637dc9a163SRobert Mustacchi/*
1647dc9a163SRobert Mustacchi *	uint64_t betoh64(uint64_t in)
1657dc9a163SRobert Mustacchi *	uint64_t be64toh(uint64_t in)
1667dc9a163SRobert Mustacchi *
1677dc9a163SRobert Mustacchi *	Convert in to little endian, eg. ntohll()
1687dc9a163SRobert Mustacchi */
1697dc9a163SRobert Mustacchi	ENTRY(betoh64)
1707dc9a163SRobert Mustacchi	movq	%rdi, %rax	/* %rax = hll */
1717dc9a163SRobert Mustacchi	bswapq	%rax		/* reverses the byte order of %rax */
1727dc9a163SRobert Mustacchi	ret			/* return (%rax) */
1737dc9a163SRobert Mustacchi	SET_SIZE(betoh64)
1747dc9a163SRobert Mustacchi
1757dc9a163SRobert Mustacchi	ENTRY(be64toh)
1767dc9a163SRobert Mustacchi	movq	%rdi, %rax	/* %rax = hll */
1777dc9a163SRobert Mustacchi	bswapq	%rax		/* reverses the byte order of %rax */
1787dc9a163SRobert Mustacchi	ret			/* return (%rax) */
1797dc9a163SRobert Mustacchi	SET_SIZE(be64toh)
180