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 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * University Copyright- Copyright (c) 1982, 1986, 1988 32 * The Regents of the University of California 33 * All Rights Reserved 34 * 35 * University Acknowledgment- Portions of this document are derived from 36 * software developed by the University of California, Berkeley, and its 37 * contributors. 38 */ 39 40 #ifndef _SYS_BYTEORDER_H 41 #define _SYS_BYTEORDER_H 42 43 #include <sys/endian.h> 44 #include <netinet/in.h> 45 #include <sys/isa_defs.h> 46 #include <sys/int_types.h> 47 48 #if defined(__GNUC__) && defined(_ASM_INLINES) && \ 49 (defined(__i386) || defined(__amd64)) 50 #include <asm/byteorder.h> 51 #endif 52 53 #ifdef __cplusplus 54 extern "C" { 55 #endif 56 57 /* 58 * macros for conversion between host and (internet) network byte order 59 */ 60 #if !defined(_XPG4_2) || defined(__EXTENSIONS__) 61 62 /* 63 * Macros to reverse byte order 64 */ 65 #define BSWAP_8(x) ((x) & 0xff) 66 #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8)) 67 #define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16)) 68 #define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32)) 69 70 #define BMASK_8(x) ((x) & 0xff) 71 #define BMASK_16(x) ((x) & 0xffff) 72 #define BMASK_32(x) ((x) & 0xffffffff) 73 #define BMASK_64(x) (x) 74 75 /* 76 * Macros to convert from a specific byte order to/from native byte order 77 */ 78 #ifdef _ZFS_BIG_ENDIAN 79 #define BE_8(x) BMASK_8(x) 80 #define BE_16(x) BMASK_16(x) 81 #define BE_32(x) BMASK_32(x) 82 #define BE_64(x) BMASK_64(x) 83 #define LE_8(x) BSWAP_8(x) 84 #define LE_16(x) BSWAP_16(x) 85 #define LE_32(x) BSWAP_32(x) 86 #define LE_64(x) BSWAP_64(x) 87 #else 88 #define LE_8(x) BMASK_8(x) 89 #define LE_16(x) BMASK_16(x) 90 #define LE_32(x) BMASK_32(x) 91 #define LE_64(x) BMASK_64(x) 92 #define BE_8(x) BSWAP_8(x) 93 #define BE_16(x) BSWAP_16(x) 94 #define BE_32(x) BSWAP_32(x) 95 #define BE_64(x) BSWAP_64(x) 96 #endif 97 98 #ifdef _ZFS_BIG_ENDIAN 99 static __inline__ uint64_t 100 htonll(uint64_t n) 101 { 102 return (n); 103 } 104 105 static __inline__ uint64_t 106 ntohll(uint64_t n) 107 { 108 return (n); 109 } 110 #else 111 static __inline__ uint64_t 112 htonll(uint64_t n) 113 { 114 return ((((uint64_t)htonl(n)) << 32) + htonl(n >> 32)); 115 } 116 117 static __inline__ uint64_t 118 ntohll(uint64_t n) 119 { 120 return ((((uint64_t)ntohl(n)) << 32) + ntohl(n >> 32)); 121 } 122 #endif 123 124 /* 125 * Macros to read unaligned values from a specific byte order to 126 * native byte order 127 */ 128 129 #define BE_IN8(xa) \ 130 *((uint8_t *)(xa)) 131 132 #define BE_IN16(xa) \ 133 (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa)+1)) 134 135 #define BE_IN32(xa) \ 136 (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa)+2)) 137 138 #define BE_IN64(xa) \ 139 (((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa)+4)) 140 141 #define LE_IN8(xa) \ 142 *((uint8_t *)(xa)) 143 144 #define LE_IN16(xa) \ 145 (((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa)) 146 147 #define LE_IN32(xa) \ 148 (((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa)) 149 150 #define LE_IN64(xa) \ 151 (((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa)) 152 153 /* 154 * Macros to write unaligned values from native byte order to a specific byte 155 * order. 156 */ 157 158 #define BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 159 160 #define BE_OUT16(xa, yv) \ 161 BE_OUT8((uint8_t *)(xa) + 1, yv); \ 162 BE_OUT8((uint8_t *)(xa), (yv) >> 8); 163 164 #define BE_OUT32(xa, yv) \ 165 BE_OUT16((uint8_t *)(xa) + 2, yv); \ 166 BE_OUT16((uint8_t *)(xa), (yv) >> 16); 167 168 #define BE_OUT64(xa, yv) \ 169 BE_OUT32((uint8_t *)(xa) + 4, yv); \ 170 BE_OUT32((uint8_t *)(xa), (yv) >> 32); 171 172 #define LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv); 173 174 #define LE_OUT16(xa, yv) \ 175 LE_OUT8((uint8_t *)(xa), yv); \ 176 LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8); 177 178 #define LE_OUT32(xa, yv) \ 179 LE_OUT16((uint8_t *)(xa), yv); \ 180 LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16); 181 182 #define LE_OUT64(xa, yv) \ 183 LE_OUT32((uint8_t *)(xa), yv); \ 184 LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32); 185 186 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */ 187 188 #ifdef __cplusplus 189 } 190 #endif 191 192 #endif /* _SYS_BYTEORDER_H */ 193