FreeBSD/Linux Kernel Cross Reference
sys/sys/endian.h
1 /* $NetBSD: endian.h,v 1.7.2.1 2004/07/02 18:18:01 he Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)endian.h 8.1 (Berkeley) 6/11/93
32 */
33
34 #ifndef _SYS_ENDIAN_H_
35 #define _SYS_ENDIAN_H_
36
37 #include <sys/featuretest.h>
38
39 /*
40 * Definitions for byte order, according to byte significance from low
41 * address to high.
42 */
43 #define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
44 #define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
45 #define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
46
47 #include <machine/endian_machdep.h>
48
49 /*
50 * Define the order of 32-bit words in 64-bit words.
51 */
52 #if _BYTE_ORDER == _LITTLE_ENDIAN
53 #define _QUAD_HIGHWORD 1
54 #define _QUAD_LOWWORD 0
55 #endif
56
57 #if _BYTE_ORDER == _BIG_ENDIAN
58 #define _QUAD_HIGHWORD 0
59 #define _QUAD_LOWWORD 1
60 #endif
61
62
63 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
64 /*
65 * Traditional names for byteorder. These are defined as the numeric
66 * sequences so that third party code can "#define XXX_ENDIAN" and not
67 * cause errors.
68 */
69 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
70 #define BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
71 #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
72 #define BYTE_ORDER _BYTE_ORDER
73
74 #ifndef _LOCORE
75 /* C-family endian-ness definitions */
76
77 #include <sys/ansi.h>
78 #include <sys/cdefs.h>
79 #include <sys/types.h>
80
81 #ifndef in_addr_t
82 typedef __in_addr_t in_addr_t;
83 #define in_addr_t __in_addr_t
84 #endif
85
86 #ifndef in_port_t
87 typedef __in_port_t in_port_t;
88 #define in_port_t __in_port_t
89 #endif
90
91 __BEGIN_DECLS
92 uint32_t htonl __P((uint32_t)) __attribute__((__const__));
93 uint16_t htons __P((uint16_t)) __attribute__((__const__));
94 uint32_t ntohl __P((uint32_t)) __attribute__((__const__));
95 uint16_t ntohs __P((uint16_t)) __attribute__((__const__));
96 __END_DECLS
97
98 /*
99 * Macros for network/external number representation conversion.
100 */
101 #if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
102 #define ntohl(x) (x)
103 #define ntohs(x) (x)
104 #define htonl(x) (x)
105 #define htons(x) (x)
106
107 #define NTOHL(x) (void) (x)
108 #define NTOHS(x) (void) (x)
109 #define HTONL(x) (void) (x)
110 #define HTONS(x) (void) (x)
111
112 #else /* LITTLE_ENDIAN || !defined(__lint__) */
113
114 #define NTOHL(x) (x) = ntohl((uint32_t)(x))
115 #define NTOHS(x) (x) = ntohs((uint16_t)(x))
116 #define HTONL(x) (x) = htonl((uint32_t)(x))
117 #define HTONS(x) (x) = htons((uint16_t)(x))
118 #endif /* LITTLE_ENDIAN || !defined(__lint__) */
119
120 /*
121 * Macros to convert to a specific endianness.
122 */
123
124 #include <machine/bswap.h>
125
126 #if BYTE_ORDER == BIG_ENDIAN
127
128 #define htobe16(x) (x)
129 #define htobe32(x) (x)
130 #define htobe64(x) (x)
131 #define htole16(x) bswap16((u_int16_t)(x))
132 #define htole32(x) bswap32((u_int32_t)(x))
133 #define htole64(x) bswap64((u_int64_t)(x))
134
135 #define HTOBE16(x) (void) (x)
136 #define HTOBE32(x) (void) (x)
137 #define HTOBE64(x) (void) (x)
138 #define HTOLE16(x) (x) = bswap16((u_int16_t)(x))
139 #define HTOLE32(x) (x) = bswap32((u_int32_t)(x))
140 #define HTOLE64(x) (x) = bswap64((u_int64_t)(x))
141
142 #else /* LITTLE_ENDIAN */
143
144 #define htobe16(x) bswap16((u_int16_t)(x))
145 #define htobe32(x) bswap32((u_int32_t)(x))
146 #define htobe64(x) bswap64((u_int64_t)(x))
147 #define htole16(x) (x)
148 #define htole32(x) (x)
149 #define htole64(x) (x)
150
151 #define HTOBE16(x) (x) = bswap16((u_int16_t)(x))
152 #define HTOBE32(x) (x) = bswap32((u_int32_t)(x))
153 #define HTOBE64(x) (x) = bswap64((u_int64_t)(x))
154 #define HTOLE16(x) (void) (x)
155 #define HTOLE32(x) (void) (x)
156 #define HTOLE64(x) (void) (x)
157
158 #endif /* LITTLE_ENDIAN */
159
160 #define be16toh(x) htobe16(x)
161 #define be32toh(x) htobe32(x)
162 #define be64toh(x) htobe64(x)
163 #define le16toh(x) htole16(x)
164 #define le32toh(x) htole32(x)
165 #define le64toh(x) htole64(x)
166
167 #define BE16TOH(x) HTOBE16(x)
168 #define BE32TOH(x) HTOBE32(x)
169 #define BE64TOH(x) HTOBE64(x)
170 #define LE16TOH(x) HTOLE16(x)
171 #define LE32TOH(x) HTOLE32(x)
172 #define LE64TOH(x) HTOLE64(x)
173
174 #endif /* !_LOCORE */
175 #endif /* !_POSIX_SOURCE */
176 #endif /* !_SYS_ENDIAN_H_ */
Cache object: a907f97cf63c92a2add007803f0c3f45
|