xref: /titanic_41/usr/src/lib/libc/amd64/gen/byteorder.s (revision 21d23cc145f4b4679726016486c401c0abf3c34a)
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 */
25
26	.file	"byteorder.s"
27
28#include <sys/asm_linkage.h>
29
30	/*
31	 * NOTE: htonll/ntohll, htonl/ntohl, and htons/ntohs are identical
32	 * routines. As such, they could be implemented as a single routine,
33	 * using multiple ALTENTRY/SET_SIZE definitions. We don't do this so
34	 * that they will have unique addresses, allowing DTrace and
35	 * other debuggers to tell them apart.
36	 */
37
38/*
39 *	unsigned long long htonll( hll )
40 *	unsigned long long ntohll( hll )
41 *	unsigned long long hll;
42 *	reverses the byte order of 'uint64_t hll' on little endian machines
43 */
44	ENTRY(htonll)
45	movq	%rdi, %rax	/* %rax = hll */
46	bswapq	%rax		/* reverses the byte order of %rax */
47	ret			/* return (%rax) */
48	SET_SIZE(htonll)
49
50	ENTRY(ntohll)
51	movq	%rdi, %rax	/* %rax = hll */
52	bswapq	%rax		/* reverses the byte order of %rax */
53	ret			/* return (%rax) */
54	SET_SIZE(ntohll)
55
56
57/*
58 *	unsigned long htonl( hl )
59 *	unsigned long ntohl( hl )
60 *	unsigned long hl;
61 *	reverses the byte order of 'uint32_t hl' on little endian machines
62 */
63	ENTRY(htonl)
64	movl	%edi, %eax	/* %eax = hl */
65	bswap	%eax		/* reverses the byte order of %eax */
66	ret			/* return (%eax) */
67	SET_SIZE(htonl)
68
69	ENTRY(ntohl)
70	movl	%edi, %eax	/* %eax = hl */
71	bswap	%eax		/* reverses the byte order of %eax */
72	ret			/* return (%eax) */
73	SET_SIZE(ntohl)
74
75/*
76 *	unsigned short htons( hs )
77 *	unsigned short hs;
78 *	reverses the byte order of 'uint16_t hs' on little endian machines.
79 */
80	ENTRY(htons)
81	movl	%edi, %eax	/* %eax = hs */
82	bswap	%eax		/* reverses the byte order of %eax */
83	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
84	ret			/* return (%eax) */
85	SET_SIZE(htons)
86
87	ENTRY(ntohs)
88	movl	%edi, %eax	/* %eax = hs */
89	bswap	%eax		/* reverses the byte order of %eax */
90	shrl	$16, %eax	/* moves high 16-bit to low 16-bit */
91	ret			/* return (%eax) */
92	SET_SIZE(ntohs)
93