xref: /illumos-gate/usr/src/lib/libc/i386/gen/byteorder.S (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 2007 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.h"
30
31	/*
32	 * NOTE: htonl/ntohl are identical routines, as are htons/ntohs.
33	 * As such, they could be implemented as a single routine, using
34	 * 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.  With the endian
37	 * functions we do the same, even though it's similarly
38	 * repetitive.
39	 */
40
41/	unsigned long htonl( hl )
42/	unsigned long ntohl( hl )
43/	long hl;
44/	reverses the byte order of 'long hl'
45/
46	ENTRY(htonl)
47	movl	4(%esp), %eax	/ %eax = hl
48	bswap	%eax		/ reverses the byte order of %eax
49	ret			/ return (%eax)
50	SET_SIZE(htonl)
51
52	ENTRY(ntohl)
53	movl	4(%esp), %eax	/ %eax = hl
54	bswap	%eax		/ reverses the byte order of %eax
55	ret			/ return (%eax)
56	SET_SIZE(ntohl)
57
58/	unsigned short htons( hs )
59/	short hs;
60/
61/	reverses the byte order in hs.
62/
63	ENTRY(htons)
64	movl	4(%esp), %eax	/ %eax = hs
65	bswap	%eax		/ reverses the byte order of %eax
66	shrl	$16, %eax	/ moves high 16-bit to low 16-bit
67	ret			/ return (%eax)
68	SET_SIZE(htons)
69
70	ENTRY(ntohs)
71	movl	4(%esp), %eax	/ %eax = hs
72	bswap	%eax		/ reverses the byte order of %eax
73	shrl	$16, %eax	/ moves high 16-bit to low 16-bit
74	ret			/ return (%eax)
75	SET_SIZE(ntohs)
76
77/	uint16_t htobe16(uint16_t in)
78/
79/	Convert in to big endian, eg. htons()
80/
81	ENTRY(htobe16)
82	movl	4(%esp), %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(htobe16)
87
88/	uint32_t htobe32(uint32_t in)
89/
90/	Convert in to big endian, eg. htonl()
91/
92	ENTRY(htobe32)
93	movl	4(%esp), %eax	/ %eax = hl
94	bswap	%eax		/ reverses the byte order of %eax
95	ret			/ return (%eax)
96	SET_SIZE(htobe32)
97
98/	uint16_t betoh16(uint16_t in)
99/	uint16_t be16toh(uint16_t in)
100/
101/	Convert in to little endian, eg. ntohs()
102/
103	ENTRY(betoh16)
104	movl	4(%esp), %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(betoh16)
109
110	ENTRY(be16toh)
111	movl	4(%esp), %eax	/ %eax = hs
112	bswap	%eax		/ reverses the byte order of %eax
113	shrl	$16, %eax	/ moves high 16-bit to low 16-bit
114	ret			/ return (%eax)
115	SET_SIZE(be16toh)
116
117
118/	uint32_t be32toh(uint32_t in)
119/	uint32_t betoh32(uint32_t in)
120/
121/	Convert in to little endian, eg. ntohl()
122/
123	ENTRY(be32toh)
124	movl	4(%esp), %eax	/ %eax = hl
125	bswap	%eax		/ reverses the byte order of %eax
126	ret			/ return (%eax)
127	SET_SIZE(be32toh)
128
129	ENTRY(betoh32)
130	movl	4(%esp), %eax	/ %eax = hl
131	bswap	%eax		/ reverses the byte order of %eax
132	ret			/ return (%eax)
133	SET_SIZE(betoh32)
134