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/* 22dfb96a4fSab196087 * Copyright 2007 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 297c478bd9Sstevel@tonic-gate#include "SYS.h" 307c478bd9Sstevel@tonic-gate 31dfb96a4fSab196087 /* 32dfb96a4fSab196087 * NOTE: htonl/ntohl are identical routines, as are htons/ntohs. 33dfb96a4fSab196087 * As such, they could be implemented as a single routine, using 34dfb96a4fSab196087 * multiple ALTENTRY/SET_SIZE definitions. We don't do this so 35dfb96a4fSab196087 * that they will have unique addresses, allowing DTrace and 36*577bdc16SRobert Mustacchi * other debuggers to tell them apart. With the endian 37*577bdc16SRobert Mustacchi * functions we do the same, even though it's similarly 38*577bdc16SRobert Mustacchi * repetitive. 39dfb96a4fSab196087 */ 40dfb96a4fSab196087 417c478bd9Sstevel@tonic-gate/ unsigned long htonl( hl ) 427c478bd9Sstevel@tonic-gate/ unsigned long ntohl( hl ) 437c478bd9Sstevel@tonic-gate/ long hl; 447c478bd9Sstevel@tonic-gate/ reverses the byte order of 'long hl' 457c478bd9Sstevel@tonic-gate/ 467c478bd9Sstevel@tonic-gate ENTRY(htonl) 477c478bd9Sstevel@tonic-gate movl 4(%esp), %eax / %eax = hl 487c478bd9Sstevel@tonic-gate bswap %eax / reverses the byte order of %eax 497c478bd9Sstevel@tonic-gate ret / return (%eax) 507c478bd9Sstevel@tonic-gate SET_SIZE(htonl) 51dfb96a4fSab196087 52dfb96a4fSab196087 ENTRY(ntohl) 53dfb96a4fSab196087 movl 4(%esp), %eax / %eax = hl 54dfb96a4fSab196087 bswap %eax / reverses the byte order of %eax 55dfb96a4fSab196087 ret / return (%eax) 567c478bd9Sstevel@tonic-gate SET_SIZE(ntohl) 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate/ unsigned short htons( hs ) 597c478bd9Sstevel@tonic-gate/ short hs; 607c478bd9Sstevel@tonic-gate/ 617c478bd9Sstevel@tonic-gate/ reverses the byte order in hs. 627c478bd9Sstevel@tonic-gate/ 637c478bd9Sstevel@tonic-gate ENTRY(htons) 647c478bd9Sstevel@tonic-gate movl 4(%esp), %eax / %eax = hs 657c478bd9Sstevel@tonic-gate bswap %eax / reverses the byte order of %eax 667c478bd9Sstevel@tonic-gate shrl $16, %eax / moves high 16-bit to low 16-bit 677c478bd9Sstevel@tonic-gate ret / return (%eax) 687c478bd9Sstevel@tonic-gate SET_SIZE(htons) 69dfb96a4fSab196087 70dfb96a4fSab196087 ENTRY(ntohs) 71dfb96a4fSab196087 movl 4(%esp), %eax / %eax = hs 72dfb96a4fSab196087 bswap %eax / reverses the byte order of %eax 73dfb96a4fSab196087 shrl $16, %eax / moves high 16-bit to low 16-bit 74dfb96a4fSab196087 ret / return (%eax) 757c478bd9Sstevel@tonic-gate SET_SIZE(ntohs) 76*577bdc16SRobert Mustacchi 77*577bdc16SRobert Mustacchi/ uint16_t htobe16(uint16_t in) 78*577bdc16SRobert Mustacchi/ 79*577bdc16SRobert Mustacchi/ Convert in to big endian, eg. htons() 80*577bdc16SRobert Mustacchi/ 81*577bdc16SRobert Mustacchi ENTRY(htobe16) 82*577bdc16SRobert Mustacchi movl 4(%esp), %eax / %eax = hs 83*577bdc16SRobert Mustacchi bswap %eax / reverses the byte order of %eax 84*577bdc16SRobert Mustacchi shrl $16, %eax / moves high 16-bit to low 16-bit 85*577bdc16SRobert Mustacchi ret / return (%eax) 86*577bdc16SRobert Mustacchi SET_SIZE(htobe16) 87*577bdc16SRobert Mustacchi 88*577bdc16SRobert Mustacchi/ uint32_t htobe32(uint32_t in) 89*577bdc16SRobert Mustacchi/ 90*577bdc16SRobert Mustacchi/ Convert in to big endian, eg. htonl() 91*577bdc16SRobert Mustacchi/ 92*577bdc16SRobert Mustacchi ENTRY(htobe32) 93*577bdc16SRobert Mustacchi movl 4(%esp), %eax / %eax = hl 94*577bdc16SRobert Mustacchi bswap %eax / reverses the byte order of %eax 95*577bdc16SRobert Mustacchi ret / return (%eax) 96*577bdc16SRobert Mustacchi SET_SIZE(htobe32) 97*577bdc16SRobert Mustacchi 98*577bdc16SRobert Mustacchi/ uint16_t betoh16(uint16_t in) 99*577bdc16SRobert Mustacchi/ uint16_t be16toh(uint16_t in) 100*577bdc16SRobert Mustacchi/ 101*577bdc16SRobert Mustacchi/ Convert in to little endian, eg. ntohs() 102*577bdc16SRobert Mustacchi/ 103*577bdc16SRobert Mustacchi ENTRY(betoh16) 104*577bdc16SRobert Mustacchi movl 4(%esp), %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(betoh16) 109*577bdc16SRobert Mustacchi 110*577bdc16SRobert Mustacchi ENTRY(be16toh) 111*577bdc16SRobert Mustacchi movl 4(%esp), %eax / %eax = hs 112*577bdc16SRobert Mustacchi bswap %eax / reverses the byte order of %eax 113*577bdc16SRobert Mustacchi shrl $16, %eax / moves high 16-bit to low 16-bit 114*577bdc16SRobert Mustacchi ret / return (%eax) 115*577bdc16SRobert Mustacchi SET_SIZE(be16toh) 116*577bdc16SRobert Mustacchi 117*577bdc16SRobert Mustacchi 118*577bdc16SRobert Mustacchi/ uint32_t be32toh(uint32_t in) 119*577bdc16SRobert Mustacchi/ uint32_t betoh32(uint32_t in) 120*577bdc16SRobert Mustacchi/ 121*577bdc16SRobert Mustacchi/ Convert in to little endian, eg. ntohl() 122*577bdc16SRobert Mustacchi/ 123*577bdc16SRobert Mustacchi ENTRY(be32toh) 124*577bdc16SRobert Mustacchi movl 4(%esp), %eax / %eax = hl 125*577bdc16SRobert Mustacchi bswap %eax / reverses the byte order of %eax 126*577bdc16SRobert Mustacchi ret / return (%eax) 127*577bdc16SRobert Mustacchi SET_SIZE(be32toh) 128*577bdc16SRobert Mustacchi 129*577bdc16SRobert Mustacchi ENTRY(betoh32) 130*577bdc16SRobert Mustacchi movl 4(%esp), %eax / %eax = hl 131*577bdc16SRobert Mustacchi bswap %eax / reverses the byte order of %eax 132*577bdc16SRobert Mustacchi ret / return (%eax) 133*577bdc16SRobert Mustacchi SET_SIZE(betoh32) 134