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/* 224b56a003SDaniel Anderson * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*3b4b8bdaSRobert 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 317c478bd9Sstevel@tonic-gate /* 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 35dfb96a4fSab196087 * that they will have unique addresses, allowing DTrace and 36dfb96a4fSab196087 * other debuggers to tell them apart. 37dfb96a4fSab196087 */ 38dfb96a4fSab196087 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 57dfb96a4fSab196087 58dfb96a4fSab196087/* 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) 69dfb96a4fSab196087 70dfb96a4fSab196087 ENTRY(ntohl) 71dfb96a4fSab196087 movl %edi, %eax /* %eax = hl */ 72dfb96a4fSab196087 bswap %eax /* reverses the byte order of %eax */ 73dfb96a4fSab196087 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) 87dfb96a4fSab196087 88dfb96a4fSab196087 ENTRY(ntohs) 89dfb96a4fSab196087 movl %edi, %eax /* %eax = hs */ 90dfb96a4fSab196087 bswap %eax /* reverses the byte order of %eax */ 91dfb96a4fSab196087 shrl $16, %eax /* moves high 16-bit to low 16-bit */ 92dfb96a4fSab196087 ret /* return (%eax) */ 937c478bd9Sstevel@tonic-gate SET_SIZE(ntohs) 94*3b4b8bdaSRobert Mustacchi 95*3b4b8bdaSRobert Mustacchi/* 96*3b4b8bdaSRobert Mustacchi * uint16_t htobe16(uint16_t in); 97*3b4b8bdaSRobert Mustacchi * uint32_t htobe32(uint32_t in); 98*3b4b8bdaSRobert Mustacchi * uint64_t htobe64(uint64_t in); 99*3b4b8bdaSRobert Mustacchi * 100*3b4b8bdaSRobert Mustacchi * Byte swap 16, 32, and 64 bits respectively. 101*3b4b8bdaSRobert Mustacchi * eg. htons(), htonl(), and htonll(). 102*3b4b8bdaSRobert Mustacchi */ 103*3b4b8bdaSRobert Mustacchi ENTRY(htobe16) 104*3b4b8bdaSRobert Mustacchi movl %edi, %eax /* %eax = hs */ 105*3b4b8bdaSRobert Mustacchi bswap %eax /* reverses the byte order of %eax */ 106*3b4b8bdaSRobert Mustacchi shrl $16, %eax /* moves high 16-bit to low 16-bit */ 107*3b4b8bdaSRobert Mustacchi ret /* return (%eax) */ 108*3b4b8bdaSRobert Mustacchi SET_SIZE(htobe16) 109*3b4b8bdaSRobert Mustacchi 110*3b4b8bdaSRobert Mustacchi ENTRY(htobe32) 111*3b4b8bdaSRobert Mustacchi movl %edi, %eax /* %eax = hl */ 112*3b4b8bdaSRobert Mustacchi bswap %eax /* reverses the byte order of %eax */ 113*3b4b8bdaSRobert Mustacchi ret /* return (%eax) */ 114*3b4b8bdaSRobert Mustacchi SET_SIZE(htobe32) 115*3b4b8bdaSRobert Mustacchi 116*3b4b8bdaSRobert Mustacchi ENTRY(htobe64) 117*3b4b8bdaSRobert Mustacchi movq %rdi, %rax /* %rax = hll */ 118*3b4b8bdaSRobert Mustacchi bswapq %rax /* reverses the byte order of %rax */ 119*3b4b8bdaSRobert Mustacchi ret /* return (%rax) */ 120*3b4b8bdaSRobert Mustacchi SET_SIZE(htobe64) 121*3b4b8bdaSRobert Mustacchi 122*3b4b8bdaSRobert Mustacchi 123*3b4b8bdaSRobert Mustacchi/* 124*3b4b8bdaSRobert Mustacchi * uint16_t betoh16(uint16_t in) 125*3b4b8bdaSRobert Mustacchi * uint16_t be16toh(uint16_t in) 126*3b4b8bdaSRobert Mustacchi * 127*3b4b8bdaSRobert Mustacchi * Convert in to little endian, eg. ntohs() 128*3b4b8bdaSRobert Mustacchi */ 129*3b4b8bdaSRobert Mustacchi ENTRY(betoh16) 130*3b4b8bdaSRobert Mustacchi movl %edi, %eax /* %eax = hs */ 131*3b4b8bdaSRobert Mustacchi bswap %eax /* reverses the byte order of %eax */ 132*3b4b8bdaSRobert Mustacchi shrl $16, %eax /* moves high 16-bit to low 16-bit */ 133*3b4b8bdaSRobert Mustacchi ret /* return (%eax) */ 134*3b4b8bdaSRobert Mustacchi SET_SIZE(betoh16) 135*3b4b8bdaSRobert Mustacchi 136*3b4b8bdaSRobert Mustacchi ENTRY(be16toh) 137*3b4b8bdaSRobert Mustacchi movl %edi, %eax /* %eax = hs */ 138*3b4b8bdaSRobert Mustacchi bswap %eax /* reverses the byte order of %eax */ 139*3b4b8bdaSRobert Mustacchi shrl $16, %eax /* moves high 16-bit to low 16-bit */ 140*3b4b8bdaSRobert Mustacchi ret /* return (%eax) */ 141*3b4b8bdaSRobert Mustacchi SET_SIZE(be16toh) 142*3b4b8bdaSRobert Mustacchi 143*3b4b8bdaSRobert Mustacchi 144*3b4b8bdaSRobert Mustacchi/* 145*3b4b8bdaSRobert Mustacchi * uint32_t betoh32(uint32_t in) 146*3b4b8bdaSRobert Mustacchi * uint32_t be32toh(uint32_t in) 147*3b4b8bdaSRobert Mustacchi * 148*3b4b8bdaSRobert Mustacchi * Convert in to little endian, eg. ntohl() 149*3b4b8bdaSRobert Mustacchi */ 150*3b4b8bdaSRobert Mustacchi ENTRY(betoh32) 151*3b4b8bdaSRobert Mustacchi movl %edi, %eax /* %eax = hl */ 152*3b4b8bdaSRobert Mustacchi bswap %eax /* reverses the byte order of %eax */ 153*3b4b8bdaSRobert Mustacchi ret /* return (%eax) */ 154*3b4b8bdaSRobert Mustacchi SET_SIZE(betoh32) 155*3b4b8bdaSRobert Mustacchi 156*3b4b8bdaSRobert Mustacchi ENTRY(be32toh) 157*3b4b8bdaSRobert Mustacchi movl %edi, %eax /* %eax = hl */ 158*3b4b8bdaSRobert Mustacchi bswap %eax /* reverses the byte order of %eax */ 159*3b4b8bdaSRobert Mustacchi ret /* return (%eax) */ 160*3b4b8bdaSRobert Mustacchi SET_SIZE(be32toh) 161*3b4b8bdaSRobert Mustacchi 162*3b4b8bdaSRobert Mustacchi 163*3b4b8bdaSRobert Mustacchi/* 164*3b4b8bdaSRobert Mustacchi * uint64_t betoh64(uint64_t in) 165*3b4b8bdaSRobert Mustacchi * uint64_t be64toh(uint64_t in) 166*3b4b8bdaSRobert Mustacchi * 167*3b4b8bdaSRobert Mustacchi * Convert in to little endian, eg. ntohll() 168*3b4b8bdaSRobert Mustacchi */ 169*3b4b8bdaSRobert Mustacchi ENTRY(betoh64) 170*3b4b8bdaSRobert Mustacchi movq %rdi, %rax /* %rax = hll */ 171*3b4b8bdaSRobert Mustacchi bswapq %rax /* reverses the byte order of %rax */ 172*3b4b8bdaSRobert Mustacchi ret /* return (%rax) */ 173*3b4b8bdaSRobert Mustacchi SET_SIZE(betoh64) 174*3b4b8bdaSRobert Mustacchi 175*3b4b8bdaSRobert Mustacchi ENTRY(be64toh) 176*3b4b8bdaSRobert Mustacchi movq %rdi, %rax /* %rax = hll */ 177*3b4b8bdaSRobert Mustacchi bswapq %rax /* reverses the byte order of %rax */ 178*3b4b8bdaSRobert Mustacchi ret /* return (%rax) */ 179*3b4b8bdaSRobert Mustacchi SET_SIZE(be64toh) 180