1*5d9d9091SRichard Lowe/* 2*5d9d9091SRichard Lowe * CDDL HEADER START 3*5d9d9091SRichard Lowe * 4*5d9d9091SRichard Lowe * The contents of this file are subject to the terms of the 5*5d9d9091SRichard Lowe * Common Development and Distribution License (the "License"). 6*5d9d9091SRichard Lowe * You may not use this file except in compliance with the License. 7*5d9d9091SRichard Lowe * 8*5d9d9091SRichard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*5d9d9091SRichard Lowe * or http://www.opensolaris.org/os/licensing. 10*5d9d9091SRichard Lowe * See the License for the specific language governing permissions 11*5d9d9091SRichard Lowe * and limitations under the License. 12*5d9d9091SRichard Lowe * 13*5d9d9091SRichard Lowe * When distributing Covered Code, include this CDDL HEADER in each 14*5d9d9091SRichard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*5d9d9091SRichard Lowe * If applicable, add the following below this CDDL HEADER, with the 16*5d9d9091SRichard Lowe * fields enclosed by brackets "[]" replaced with your own identifying 17*5d9d9091SRichard Lowe * information: Portions Copyright [yyyy] [name of copyright owner] 18*5d9d9091SRichard Lowe * 19*5d9d9091SRichard Lowe * CDDL HEADER END 20*5d9d9091SRichard Lowe */ 21*5d9d9091SRichard Lowe/* 22*5d9d9091SRichard Lowe * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23*5d9d9091SRichard Lowe * Use is subject to license terms. 24*5d9d9091SRichard Lowe * Copyright (c) 2015, Joyent, Inc. 25*5d9d9091SRichard Lowe */ 26*5d9d9091SRichard Lowe 27*5d9d9091SRichard Lowe .file "byteorder.s" 28*5d9d9091SRichard Lowe 29*5d9d9091SRichard Lowe#include "SYS.h" 30*5d9d9091SRichard Lowe 31*5d9d9091SRichard Lowe /* 32*5d9d9091SRichard Lowe * NOTE: htonl/ntohl are identical routines, as are htons/ntohs. 33*5d9d9091SRichard Lowe * As such, they could be implemented as a single routine, using 34*5d9d9091SRichard Lowe * multiple ALTENTRY/SET_SIZE definitions. We don't do this so 35*5d9d9091SRichard Lowe * that they will have unique addresses, allowing DTrace and 36*5d9d9091SRichard Lowe * other debuggers to tell them apart. With the endian 37*5d9d9091SRichard Lowe * functions we do the same, even though it's similarly 38*5d9d9091SRichard Lowe * repetitive. 39*5d9d9091SRichard Lowe */ 40*5d9d9091SRichard Lowe 41*5d9d9091SRichard Lowe/ unsigned long htonl( hl ) 42*5d9d9091SRichard Lowe/ unsigned long ntohl( hl ) 43*5d9d9091SRichard Lowe/ long hl; 44*5d9d9091SRichard Lowe/ reverses the byte order of 'long hl' 45*5d9d9091SRichard Lowe/ 46*5d9d9091SRichard Lowe ENTRY(htonl) 47*5d9d9091SRichard Lowe movl 4(%esp), %eax / %eax = hl 48*5d9d9091SRichard Lowe bswap %eax / reverses the byte order of %eax 49*5d9d9091SRichard Lowe ret / return (%eax) 50*5d9d9091SRichard Lowe SET_SIZE(htonl) 51*5d9d9091SRichard Lowe 52*5d9d9091SRichard Lowe ENTRY(ntohl) 53*5d9d9091SRichard Lowe movl 4(%esp), %eax / %eax = hl 54*5d9d9091SRichard Lowe bswap %eax / reverses the byte order of %eax 55*5d9d9091SRichard Lowe ret / return (%eax) 56*5d9d9091SRichard Lowe SET_SIZE(ntohl) 57*5d9d9091SRichard Lowe 58*5d9d9091SRichard Lowe/ unsigned short htons( hs ) 59*5d9d9091SRichard Lowe/ short hs; 60*5d9d9091SRichard Lowe/ 61*5d9d9091SRichard Lowe/ reverses the byte order in hs. 62*5d9d9091SRichard Lowe/ 63*5d9d9091SRichard Lowe ENTRY(htons) 64*5d9d9091SRichard Lowe movl 4(%esp), %eax / %eax = hs 65*5d9d9091SRichard Lowe bswap %eax / reverses the byte order of %eax 66*5d9d9091SRichard Lowe shrl $16, %eax / moves high 16-bit to low 16-bit 67*5d9d9091SRichard Lowe ret / return (%eax) 68*5d9d9091SRichard Lowe SET_SIZE(htons) 69*5d9d9091SRichard Lowe 70*5d9d9091SRichard Lowe ENTRY(ntohs) 71*5d9d9091SRichard Lowe movl 4(%esp), %eax / %eax = hs 72*5d9d9091SRichard Lowe bswap %eax / reverses the byte order of %eax 73*5d9d9091SRichard Lowe shrl $16, %eax / moves high 16-bit to low 16-bit 74*5d9d9091SRichard Lowe ret / return (%eax) 75*5d9d9091SRichard Lowe SET_SIZE(ntohs) 76*5d9d9091SRichard Lowe 77*5d9d9091SRichard Lowe/ uint16_t htobe16(uint16_t in) 78*5d9d9091SRichard Lowe/ 79*5d9d9091SRichard Lowe/ Convert in to big endian, eg. htons() 80*5d9d9091SRichard Lowe/ 81*5d9d9091SRichard Lowe ENTRY(htobe16) 82*5d9d9091SRichard Lowe movl 4(%esp), %eax / %eax = hs 83*5d9d9091SRichard Lowe bswap %eax / reverses the byte order of %eax 84*5d9d9091SRichard Lowe shrl $16, %eax / moves high 16-bit to low 16-bit 85*5d9d9091SRichard Lowe ret / return (%eax) 86*5d9d9091SRichard Lowe SET_SIZE(htobe16) 87*5d9d9091SRichard Lowe 88*5d9d9091SRichard Lowe/ uint32_t htobe32(uint32_t in) 89*5d9d9091SRichard Lowe/ 90*5d9d9091SRichard Lowe/ Convert in to big endian, eg. htonl() 91*5d9d9091SRichard Lowe/ 92*5d9d9091SRichard Lowe ENTRY(htobe32) 93*5d9d9091SRichard Lowe movl 4(%esp), %eax / %eax = hl 94*5d9d9091SRichard Lowe bswap %eax / reverses the byte order of %eax 95*5d9d9091SRichard Lowe ret / return (%eax) 96*5d9d9091SRichard Lowe SET_SIZE(htobe32) 97*5d9d9091SRichard Lowe 98*5d9d9091SRichard Lowe/ uint16_t betoh16(uint16_t in) 99*5d9d9091SRichard Lowe/ uint16_t be16toh(uint16_t in) 100*5d9d9091SRichard Lowe/ 101*5d9d9091SRichard Lowe/ Convert in to little endian, eg. ntohs() 102*5d9d9091SRichard Lowe/ 103*5d9d9091SRichard Lowe ENTRY(betoh16) 104*5d9d9091SRichard Lowe movl 4(%esp), %eax / %eax = hs 105*5d9d9091SRichard Lowe bswap %eax / reverses the byte order of %eax 106*5d9d9091SRichard Lowe shrl $16, %eax / moves high 16-bit to low 16-bit 107*5d9d9091SRichard Lowe ret / return (%eax) 108*5d9d9091SRichard Lowe SET_SIZE(betoh16) 109*5d9d9091SRichard Lowe 110*5d9d9091SRichard Lowe ENTRY(be16toh) 111*5d9d9091SRichard Lowe movl 4(%esp), %eax / %eax = hs 112*5d9d9091SRichard Lowe bswap %eax / reverses the byte order of %eax 113*5d9d9091SRichard Lowe shrl $16, %eax / moves high 16-bit to low 16-bit 114*5d9d9091SRichard Lowe ret / return (%eax) 115*5d9d9091SRichard Lowe SET_SIZE(be16toh) 116*5d9d9091SRichard Lowe 117*5d9d9091SRichard Lowe 118*5d9d9091SRichard Lowe/ uint32_t be32toh(uint32_t in) 119*5d9d9091SRichard Lowe/ uint32_t betoh32(uint32_t in) 120*5d9d9091SRichard Lowe/ 121*5d9d9091SRichard Lowe/ Convert in to little endian, eg. ntohl() 122*5d9d9091SRichard Lowe/ 123*5d9d9091SRichard Lowe ENTRY(be32toh) 124*5d9d9091SRichard Lowe movl 4(%esp), %eax / %eax = hl 125*5d9d9091SRichard Lowe bswap %eax / reverses the byte order of %eax 126*5d9d9091SRichard Lowe ret / return (%eax) 127*5d9d9091SRichard Lowe SET_SIZE(be32toh) 128*5d9d9091SRichard Lowe 129*5d9d9091SRichard Lowe ENTRY(betoh32) 130*5d9d9091SRichard Lowe movl 4(%esp), %eax / %eax = hl 131*5d9d9091SRichard Lowe bswap %eax / reverses the byte order of %eax 132*5d9d9091SRichard Lowe ret / return (%eax) 133*5d9d9091SRichard Lowe SET_SIZE(betoh32) 134