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 5*4b56a003SDaniel Anderson * Common Development and Distribution License (the "License"). 6*4b56a003SDaniel Anderson * 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 /* 22*4b56a003SDaniel Anderson * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #ifndef _ASM_BYTEORDER_H 277c478bd9Sstevel@tonic-gate #define _ASM_BYTEORDER_H 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #ifdef __cplusplus 327c478bd9Sstevel@tonic-gate extern "C" { 337c478bd9Sstevel@tonic-gate #endif 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #if !defined(__lint) && defined(__GNUC__) 367c478bd9Sstevel@tonic-gate 37*4b56a003SDaniel Anderson /* 38*4b56a003SDaniel Anderson * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs() 39*4b56a003SDaniel Anderson * These functions reverse the byte order of the input parameter and returns 40*4b56a003SDaniel Anderson * the result. This is to convert the byte order from host byte order 41*4b56a003SDaniel Anderson * (little endian) to network byte order (big endian), or vice versa. 42*4b56a003SDaniel Anderson */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate 45*4b56a003SDaniel Anderson #if defined(__i386) || defined(__amd64) 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate extern __inline__ uint16_t htons(uint16_t value) 487c478bd9Sstevel@tonic-gate { 497c478bd9Sstevel@tonic-gate #if defined(__amd64) 507c478bd9Sstevel@tonic-gate __asm__("xchgb %h0, %b0" : "+Q" (value)); 517c478bd9Sstevel@tonic-gate #elif defined(__i386) 527c478bd9Sstevel@tonic-gate __asm__("xchgb %h0, %b0" : "+q" (value)); 537c478bd9Sstevel@tonic-gate #endif 547c478bd9Sstevel@tonic-gate return (value); 557c478bd9Sstevel@tonic-gate } 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate extern __inline__ uint16_t ntohs(uint16_t value) 587c478bd9Sstevel@tonic-gate { 597c478bd9Sstevel@tonic-gate #if defined(__amd64) 607c478bd9Sstevel@tonic-gate __asm__("xchgb %h0, %b0" : "+Q" (value)); 617c478bd9Sstevel@tonic-gate #elif defined(__i386) 627c478bd9Sstevel@tonic-gate __asm__("xchgb %h0, %b0" : "+q" (value)); 637c478bd9Sstevel@tonic-gate #endif 647c478bd9Sstevel@tonic-gate return (value); 657c478bd9Sstevel@tonic-gate } 667c478bd9Sstevel@tonic-gate 67*4b56a003SDaniel Anderson extern __inline__ uint32_t htonl(uint32_t value) 68*4b56a003SDaniel Anderson { 69*4b56a003SDaniel Anderson __asm__("bswap %0" : "+r" (value)); 70*4b56a003SDaniel Anderson return (value); 71*4b56a003SDaniel Anderson } 72*4b56a003SDaniel Anderson 73*4b56a003SDaniel Anderson extern __inline__ uint32_t ntohl(uint32_t value) 74*4b56a003SDaniel Anderson { 75*4b56a003SDaniel Anderson __asm__("bswap %0" : "+r" (value)); 76*4b56a003SDaniel Anderson return (value); 77*4b56a003SDaniel Anderson } 78*4b56a003SDaniel Anderson 79*4b56a003SDaniel Anderson #if defined(__amd64) 80*4b56a003SDaniel Anderson extern __inline__ uint64_t htonll(uint64_t value) 81*4b56a003SDaniel Anderson { 82*4b56a003SDaniel Anderson __asm__("bswapq %0" : "+r" (value)); 83*4b56a003SDaniel Anderson return (value); 84*4b56a003SDaniel Anderson } 85*4b56a003SDaniel Anderson 86*4b56a003SDaniel Anderson extern __inline__ uint64_t ntohll(uint64_t value) 87*4b56a003SDaniel Anderson { 88*4b56a003SDaniel Anderson __asm__("bswapq %0" : "+r" (value)); 89*4b56a003SDaniel Anderson return (value); 90*4b56a003SDaniel Anderson } 91*4b56a003SDaniel Anderson 92*4b56a003SDaniel Anderson #elif defined(__i386) 93*4b56a003SDaniel Anderson /* Use the htonl() and ntohl() inline functions defined above */ 94*4b56a003SDaniel Anderson extern __inline__ uint64_t htonll(uint64_t value) 95*4b56a003SDaniel Anderson { 96*4b56a003SDaniel Anderson return (htonl(value >> 32) | ((uint64_t)htonl(value) << 32)); 97*4b56a003SDaniel Anderson } 98*4b56a003SDaniel Anderson 99*4b56a003SDaniel Anderson extern __inline__ uint64_t ntohll(uint64_t value) 100*4b56a003SDaniel Anderson { 101*4b56a003SDaniel Anderson return (ntohl(value >> 32) | (uint64_t)ntohl(value) << 32); 102*4b56a003SDaniel Anderson } 103*4b56a003SDaniel Anderson #endif /* __amd64 */ 104*4b56a003SDaniel Anderson 1057c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */ 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate #endif /* !__lint && __GNUC__ */ 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate #endif 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate #endif /* _ASM_BYTEORDER_H */ 114