]>
git.proxmox.com Git - mirror_zfs.git/blob - lib/libspl/include/sys/byteorder.h
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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 #ifndef _SYS_BYTEORDER_H
41 #define _SYS_BYTEORDER_H
45 #include <sys/isa_defs.h>
46 #include <sys/int_types.h>
48 #if defined(__GNUC__) && defined(_ASM_INLINES) && \
49 (defined(__i386) || defined(__amd64))
50 #include <asm/byteorder.h>
58 * macros for conversion between host and (internet) network byte order
61 #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
68 #elif !defined(ntohl) /* little-endian */
72 typedef uint16_t in_port_t
;
77 typedef uint32_t in_addr_t
;
80 #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
81 extern uint32_t htonl(uint32_t);
82 extern uint16_t htons(uint16_t);
83 extern uint32_t ntohl(uint32_t);
84 extern uint16_t ntohs(uint16_t);
86 extern in_addr_t
htonl(in_addr_t
);
87 extern in_port_t
htons(in_port_t
);
88 extern in_addr_t
ntohl(in_addr_t
);
89 extern in_port_t
ntohs(in_port_t
);
90 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */
93 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
96 * Macros to reverse byte order
98 #define BSWAP_8(x) ((x) & 0xff)
99 #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
100 #define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
101 #define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
103 #define BMASK_8(x) ((x) & 0xff)
104 #define BMASK_16(x) ((x) & 0xffff)
105 #define BMASK_32(x) ((x) & 0xffffffff)
106 #define BMASK_64(x) (x)
109 * Macros to convert from a specific byte order to/from native byte order
112 #define BE_8(x) BMASK_8(x)
113 #define BE_16(x) BMASK_16(x)
114 #define BE_32(x) BMASK_32(x)
115 #define BE_64(x) BMASK_64(x)
116 #define LE_8(x) BSWAP_8(x)
117 #define LE_16(x) BSWAP_16(x)
118 #define LE_32(x) BSWAP_32(x)
119 #define LE_64(x) BSWAP_64(x)
121 #define LE_8(x) BMASK_8(x)
122 #define LE_16(x) BMASK_16(x)
123 #define LE_32(x) BMASK_32(x)
124 #define LE_64(x) BMASK_64(x)
125 #define BE_8(x) BSWAP_8(x)
126 #define BE_16(x) BSWAP_16(x)
127 #define BE_32(x) BSWAP_32(x)
128 #define BE_64(x) BSWAP_64(x)
132 * Macros to read unaligned values from a specific byte order to
139 #define BE_IN16(xa) \
140 (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa)+1))
142 #define BE_IN32(xa) \
143 (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa)+2))
145 #define BE_IN64(xa) \
146 (((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa)+4))
151 #define LE_IN16(xa) \
152 (((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa))
154 #define LE_IN32(xa) \
155 (((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa))
157 #define LE_IN64(xa) \
158 (((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa))
161 * Macros to write unaligned values from native byte order to a specific byte
165 #define BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
167 #define BE_OUT16(xa, yv) \
168 BE_OUT8((uint8_t *)(xa) + 1, yv); \
169 BE_OUT8((uint8_t *)(xa), (yv) >> 8);
171 #define BE_OUT32(xa, yv) \
172 BE_OUT16((uint8_t *)(xa) + 2, yv); \
173 BE_OUT16((uint8_t *)(xa), (yv) >> 16);
175 #define BE_OUT64(xa, yv) \
176 BE_OUT32((uint8_t *)(xa) + 4, yv); \
177 BE_OUT32((uint8_t *)(xa), (yv) >> 32);
179 #define LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
181 #define LE_OUT16(xa, yv) \
182 LE_OUT8((uint8_t *)(xa), yv); \
183 LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8);
185 #define LE_OUT32(xa, yv) \
186 LE_OUT16((uint8_t *)(xa), yv); \
187 LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16);
189 #define LE_OUT64(xa, yv) \
190 LE_OUT32((uint8_t *)(xa), yv); \
191 LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32);
193 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
199 #endif /* _SYS_BYTEORDER_H */