pkg: ship usr/lib/security/amd64/*.so links
[unleashed.git] / include / sys / byteorder.h
blob4992519e6b5c8bf7a3caf2e62ed74418a00453de
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright 2009 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
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 #ifndef _SYS_BYTEORDER_H
41 #define _SYS_BYTEORDER_H
43 #include <sys/isa_defs.h>
44 #include <sys/int_types.h>
46 #if defined(__GNUC__) && defined(_ASM_INLINES) && \
47 (defined(__i386) || defined(__amd64))
48 #include <asm/byteorder.h>
49 #endif
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
56 * macros for conversion between host and (internet) network byte order
59 #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
60 /* big-endian */
61 #define ntohl(x) (x)
62 #define ntohs(x) (x)
63 #define htonl(x) (x)
64 #define htons(x) (x)
65 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
66 #define ntohll(x) (x)
67 #define htonll(x) (x)
68 #endif /* !_XPG4_2 || __EXTENSIONS__ */
70 #elif !defined(ntohl) /* little-endian */
72 #ifndef _IN_PORT_T
73 #define _IN_PORT_T
74 typedef uint16_t in_port_t;
75 #endif
77 #ifndef _IN_ADDR_T
78 #define _IN_ADDR_T
79 typedef uint32_t in_addr_t;
80 #endif
82 #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
83 extern uint32_t htonl(uint32_t);
84 extern uint16_t htons(uint16_t);
85 extern uint32_t ntohl(uint32_t);
86 extern uint16_t ntohs(uint16_t);
87 #else
88 extern in_addr_t htonl(in_addr_t);
89 extern in_port_t htons(in_port_t);
90 extern in_addr_t ntohl(in_addr_t);
91 extern in_port_t ntohs(in_port_t);
92 #endif /* !_XPG4_2 || __EXTENSIONS__ || _XPG5 */
94 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
95 extern uint64_t htonll(uint64_t);
96 extern uint64_t ntohll(uint64_t);
97 #endif /* !_XPG4_2 || __EXTENSIONS__ */
98 #endif
100 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
103 * Macros to reverse byte order
105 #define _BSWAP_8(x) ((x) & 0xff)
106 #define _BSWAP_16(x) ((_BSWAP_8(x) << 8) | _BSWAP_8((x) >> 8))
107 #define _BSWAP_32(x) (((uint32_t)(x) << 24) | \
108 (((uint32_t)(x) << 8) & 0xff0000) | \
109 (((uint32_t)(x) >> 8) & 0xff00) | \
110 ((uint32_t)(x) >> 24))
111 #define _BSWAP_64(x) (((uint64_t)(x) << 56) | \
112 (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
113 (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
114 (((uint64_t)(x) << 8) & 0xff00000000ULL) | \
115 (((uint64_t)(x) >> 8) & 0xff000000ULL) | \
116 (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
117 (((uint64_t)(x) >> 40) & 0xff00ULL) | \
118 ((uint64_t)(x) >> 56))
121 * We do this convoluted compile time constant check because we need to
122 * decide if we want to use the above C version to byteswap or the inline
123 * assembly. While modern compilers are smart enough to turn the above C
124 * into the appropriate bswap instruction on x86, gcc 4.4.4 isn't modern
125 * enough. So, we want to avoid using _BSWAP_XX unconditionally.
127 * At the same time, we don't want to use hton* functions unconditionally
128 * even though they are simple inline functions because that prevents us
129 * from using BSWAP_XX in contexts where the result must be a compile time
130 * constant. For example, assigning a global to a constant:
132 * uint32_t abc = BSWAP_32(0x12345678);
134 * Since the argument is a compile time constant, the byte-swapped value is
135 * also a compile time constant. So, in order for the compiler to be able
136 * to calculate the constant, we must let it use the C implementation
137 * instead of the inline assembly.
139 * Once we've moved to a more modern compiler, we can always use the C
140 * version.
142 #if (!defined(__i386) && !defined(__amd64)) || !defined(__GNUC__)
143 #define BSWAP_8(x) _BSWAP_8(x)
144 #define BSWAP_16(x) _BSWAP_16(x)
145 #define BSWAP_32(x) _BSWAP_32(x)
146 #define BSWAP_64(x) _BSWAP_64(x)
147 #else /* x86 */
148 #define BSWAP_8(x) _BSWAP_8(x)
149 #define BSWAP_16(x) (__builtin_constant_p(x) ? _BSWAP_16(x) : htons(x))
150 #define BSWAP_32(x) (__builtin_constant_p(x) ? _BSWAP_32(x) : htonl(x))
151 #define BSWAP_64(x) (__builtin_constant_p(x) ? _BSWAP_64(x) : htonll(x))
152 #endif /* !__i386 && !__amd64 */
154 #define BMASK_8(x) ((x) & 0xff)
155 #define BMASK_16(x) ((x) & 0xffff)
156 #define BMASK_32(x) ((x) & 0xffffffff)
157 #define BMASK_64(x) (x)
160 * Macros to convert from a specific byte order to/from native byte order
162 #ifdef _BIG_ENDIAN
163 #define BE_8(x) BMASK_8(x)
164 #define BE_16(x) BMASK_16(x)
165 #define BE_32(x) BMASK_32(x)
166 #define BE_64(x) BMASK_64(x)
167 #define LE_8(x) BSWAP_8(x)
168 #define LE_16(x) BSWAP_16(x)
169 #define LE_32(x) BSWAP_32(x)
170 #define LE_64(x) BSWAP_64(x)
171 #else
172 #define LE_8(x) BMASK_8(x)
173 #define LE_16(x) BMASK_16(x)
174 #define LE_32(x) BMASK_32(x)
175 #define LE_64(x) BMASK_64(x)
176 #define BE_8(x) BSWAP_8(x)
177 #define BE_16(x) BSWAP_16(x)
178 #define BE_32(x) BSWAP_32(x)
179 #define BE_64(x) BSWAP_64(x)
180 #endif
183 * Macros to read unaligned values from a specific byte order to
184 * native byte order
187 #define BE_IN8(xa) \
188 *((uint8_t *)(xa))
190 #if !defined(__i386) && !defined(__amd64)
191 #define BE_IN16(xa) \
192 (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa) + 1))
194 #define BE_IN32(xa) \
195 (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa) + 2))
197 #else /* x86 */
198 #define BE_IN16(xa) htons(*((uint16_t *)(void *)(xa)))
199 #define BE_IN32(xa) htonl(*((uint32_t *)(void *)(xa)))
200 #endif /* !__i386 && !__amd64 */
202 #define BE_IN64(xa) htonll(*((uint64_t *)(void *)(xa)))
204 #define LE_IN8(xa) \
205 *((uint8_t *)(xa))
207 #define LE_IN16(xa) \
208 (((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa))
210 #define LE_IN32(xa) \
211 (((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa))
213 #define LE_IN64(xa) \
214 (((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa))
217 * Macros to write unaligned values from native byte order to a specific byte
218 * order.
221 #define BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
223 #define BE_OUT16(xa, yv) \
224 BE_OUT8((uint8_t *)(xa) + 1, yv); \
225 BE_OUT8((uint8_t *)(xa), (yv) >> 8);
227 #define BE_OUT32(xa, yv) \
228 BE_OUT16((uint8_t *)(xa) + 2, yv); \
229 BE_OUT16((uint8_t *)(xa), (yv) >> 16);
231 #define BE_OUT64(xa, yv) *((uint64_t *)(void *)(xa)) = htonll((uint64_t)(yv));
233 #define LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
235 #define LE_OUT16(xa, yv) \
236 LE_OUT8((uint8_t *)(xa), yv); \
237 LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8);
239 #define LE_OUT32(xa, yv) \
240 LE_OUT16((uint8_t *)(xa), yv); \
241 LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16);
243 #define LE_OUT64(xa, yv) \
244 LE_OUT32((uint8_t *)(xa), yv); \
245 LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32);
247 #endif /* !_XPG4_2 || __EXTENSIONS__ */
249 #ifdef __cplusplus
251 #endif
253 #endif /* _SYS_BYTEORDER_H */