Print this page
5007142 Add ntohll and htonll to sys/byteorder.h
6717509 Need to use bswap/bswapq for byte swap of 64-bit integer on x32/x64
PSARC 2008/474
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/sys/byteorder.h
+++ new/usr/src/uts/common/sys/byteorder.h
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 - * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 + * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 28 /* All Rights Reserved */
29 29
30 30 /*
31 31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 32 * The Regents of the University of California
33 33 * All Rights Reserved
34 34 *
35 35 * University Acknowledgment- Portions of this document are derived from
36 36 * software developed by the University of California, Berkeley, and its
37 37 * contributors.
38 38 */
39 39
40 40 #ifndef _SYS_BYTEORDER_H
41 41 #define _SYS_BYTEORDER_H
42 42
43 -#pragma ident "%Z%%M% %I% %E% SMI"
44 -
45 43 #include <sys/isa_defs.h>
46 44 #include <sys/int_types.h>
47 45
48 46 #if defined(__GNUC__) && defined(_ASM_INLINES) && \
49 47 (defined(__i386) || defined(__amd64))
50 48 #include <asm/byteorder.h>
51 49 #endif
52 50
53 51 #ifdef __cplusplus
54 52 extern "C" {
55 53 #endif
56 54
57 55 /*
58 56 * macros for conversion between host and (internet) network byte order
59 57 */
60 58
61 59 #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
62 60 /* big-endian */
63 61 #define ntohl(x) (x)
62 +#define ntohll(x) (x)
64 63 #define ntohs(x) (x)
65 64 #define htonl(x) (x)
65 +#define htonll(x) (x)
66 66 #define htons(x) (x)
67 67
68 68 #elif !defined(ntohl) /* little-endian */
69 69
70 70 #ifndef _IN_PORT_T
71 71 #define _IN_PORT_T
72 72 typedef uint16_t in_port_t;
73 73 #endif
74 74
75 75 #ifndef _IN_ADDR_T
76 76 #define _IN_ADDR_T
77 77 typedef uint32_t in_addr_t;
78 78 #endif
79 79
80 80 #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
81 81 extern uint32_t htonl(uint32_t);
82 82 extern uint16_t htons(uint16_t);
83 -extern uint32_t ntohl(uint32_t);
83 +extern uint32_t ntohl(uint32_t);
84 84 extern uint16_t ntohs(uint16_t);
85 85 #else
86 86 extern in_addr_t htonl(in_addr_t);
87 87 extern in_port_t htons(in_port_t);
88 -extern in_addr_t ntohl(in_addr_t);
88 +extern in_addr_t ntohl(in_addr_t);
89 89 extern in_port_t ntohs(in_port_t);
90 90 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */
91 +#if !(defined(_XPG4_2) || defined(_XPG5)) || defined(__EXTENSIONS__)
92 +extern uint64_t htonll(uint64_t);
93 +extern uint64_t ntohll(uint64_t);
94 +#endif /* !(_XPG4_2||_XPG5) || __EXTENSIONS__ */
91 95 #endif
92 96
93 97 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
94 98
95 99 /*
96 100 * Macros to reverse byte order
97 101 */
98 102 #define BSWAP_8(x) ((x) & 0xff)
99 103 #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
100 104 #define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
101 105 #define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
102 106
103 107 #define BMASK_8(x) ((x) & 0xff)
104 108 #define BMASK_16(x) ((x) & 0xffff)
105 109 #define BMASK_32(x) ((x) & 0xffffffff)
106 110 #define BMASK_64(x) (x)
107 111
108 112 /*
109 113 * Macros to convert from a specific byte order to/from native byte order
110 114 */
111 115 #ifdef _BIG_ENDIAN
112 116 #define BE_8(x) BMASK_8(x)
113 117 #define BE_16(x) BMASK_16(x)
114 118 #define BE_32(x) BMASK_32(x)
115 119 #define BE_64(x) BMASK_64(x)
116 120 #define LE_8(x) BSWAP_8(x)
117 121 #define LE_16(x) BSWAP_16(x)
118 122 #define LE_32(x) BSWAP_32(x)
119 123 #define LE_64(x) BSWAP_64(x)
120 124 #else
121 125 #define LE_8(x) BMASK_8(x)
122 126 #define LE_16(x) BMASK_16(x)
123 127 #define LE_32(x) BMASK_32(x)
124 128 #define LE_64(x) BMASK_64(x)
125 129 #define BE_8(x) BSWAP_8(x)
126 130 #define BE_16(x) BSWAP_16(x)
127 131 #define BE_32(x) BSWAP_32(x)
128 132 #define BE_64(x) BSWAP_64(x)
129 133 #endif
130 134
131 135 /*
132 136 * Macros to read unaligned values from a specific byte order to
133 137 * native byte order
134 138 */
135 139
136 140 #define BE_IN8(xa) \
137 141 *((uint8_t *)(xa))
138 142
139 143 #define BE_IN16(xa) \
140 144 (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa)+1))
141 145
142 146 #define BE_IN32(xa) \
143 147 (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa)+2))
144 148
145 149 #define BE_IN64(xa) \
146 150 (((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa)+4))
147 151
148 152 #define LE_IN8(xa) \
149 153 *((uint8_t *)(xa))
150 154
151 155 #define LE_IN16(xa) \
152 156 (((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa))
153 157
154 158 #define LE_IN32(xa) \
155 159 (((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa))
156 160
157 161 #define LE_IN64(xa) \
158 162 (((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa))
159 163
160 164 /*
161 165 * Macros to write unaligned values from native byte order to a specific byte
162 166 * order.
163 167 */
164 168
165 169 #define BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
166 170
167 171 #define BE_OUT16(xa, yv) \
168 172 BE_OUT8((uint8_t *)(xa) + 1, yv); \
169 173 BE_OUT8((uint8_t *)(xa), (yv) >> 8);
170 174
171 175 #define BE_OUT32(xa, yv) \
172 176 BE_OUT16((uint8_t *)(xa) + 2, yv); \
173 177 BE_OUT16((uint8_t *)(xa), (yv) >> 16);
174 178
175 179 #define BE_OUT64(xa, yv) \
176 180 BE_OUT32((uint8_t *)(xa) + 4, yv); \
177 181 BE_OUT32((uint8_t *)(xa), (yv) >> 32);
178 182
179 183 #define LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
180 184
181 185 #define LE_OUT16(xa, yv) \
182 186 LE_OUT8((uint8_t *)(xa), yv); \
183 187 LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8);
184 188
185 189 #define LE_OUT32(xa, yv) \
186 190 LE_OUT16((uint8_t *)(xa), yv); \
187 191 LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16);
188 192
189 193 #define LE_OUT64(xa, yv) \
190 194 LE_OUT32((uint8_t *)(xa), yv); \
191 195 LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32);
192 196
193 197 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
194 198
195 199 #ifdef __cplusplus
196 200 }
197 201 #endif
198 202
199 203 #endif /* _SYS_BYTEORDER_H */
↓ open down ↓ |
99 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX