1b2b3ffcdSSimon Schubert /*-
2b2b3ffcdSSimon Schubert * Copyright (c) 1987, 1991 Regents of the University of California.
3b2b3ffcdSSimon Schubert * All rights reserved.
4b2b3ffcdSSimon Schubert *
5b2b3ffcdSSimon Schubert * Redistribution and use in source and binary forms, with or without
6b2b3ffcdSSimon Schubert * modification, are permitted provided that the following conditions
7b2b3ffcdSSimon Schubert * are met:
8b2b3ffcdSSimon Schubert * 1. Redistributions of source code must retain the above copyright
9b2b3ffcdSSimon Schubert * notice, this list of conditions and the following disclaimer.
10b2b3ffcdSSimon Schubert * 2. Redistributions in binary form must reproduce the above copyright
11b2b3ffcdSSimon Schubert * notice, this list of conditions and the following disclaimer in the
12b2b3ffcdSSimon Schubert * documentation and/or other materials provided with the distribution.
132c64e990Szrj * 3. Neither the name of the University nor the names of its contributors
14b2b3ffcdSSimon Schubert * may be used to endorse or promote products derived from this software
15b2b3ffcdSSimon Schubert * without specific prior written permission.
16b2b3ffcdSSimon Schubert *
17b2b3ffcdSSimon Schubert * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18b2b3ffcdSSimon Schubert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19b2b3ffcdSSimon Schubert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20b2b3ffcdSSimon Schubert * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21b2b3ffcdSSimon Schubert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22b2b3ffcdSSimon Schubert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23b2b3ffcdSSimon Schubert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24b2b3ffcdSSimon Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25b2b3ffcdSSimon Schubert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26b2b3ffcdSSimon Schubert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27b2b3ffcdSSimon Schubert * SUCH DAMAGE.
28b2b3ffcdSSimon Schubert *
29b2b3ffcdSSimon Schubert * @(#)endian.h 7.8 (Berkeley) 4/3/91
30b2b3ffcdSSimon Schubert * $FreeBSD: src/sys/amd64/include/endian.h,v 1.5 2003/09/22 22:37:49 peter Exp $
31b2b3ffcdSSimon Schubert */
32b2b3ffcdSSimon Schubert
33b2b3ffcdSSimon Schubert #ifndef _CPU_ENDIAN_H_
34b2b3ffcdSSimon Schubert #define _CPU_ENDIAN_H_
35b2b3ffcdSSimon Schubert
36b2b3ffcdSSimon Schubert #include <sys/cdefs.h>
37b2b3ffcdSSimon Schubert #include <machine/stdint.h>
38b2b3ffcdSSimon Schubert
39b2b3ffcdSSimon Schubert /*
40b2b3ffcdSSimon Schubert * Definitions for byte order, according to byte significance from low
41b2b3ffcdSSimon Schubert * address to high.
42b2b3ffcdSSimon Schubert */
43b2b3ffcdSSimon Schubert #define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
44b2b3ffcdSSimon Schubert #define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
45b2b3ffcdSSimon Schubert #define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
46b2b3ffcdSSimon Schubert
47b2b3ffcdSSimon Schubert #define _BYTE_ORDER _LITTLE_ENDIAN
48b2b3ffcdSSimon Schubert
49b2b3ffcdSSimon Schubert /*
50b2b3ffcdSSimon Schubert * Deprecated variants that don't have enough underscores to be useful in more
51b2b3ffcdSSimon Schubert * strict namespaces.
52b2b3ffcdSSimon Schubert */
53b2b3ffcdSSimon Schubert #if __BSD_VISIBLE
54b2b3ffcdSSimon Schubert #define LITTLE_ENDIAN _LITTLE_ENDIAN
55b2b3ffcdSSimon Schubert #define BIG_ENDIAN _BIG_ENDIAN
56b2b3ffcdSSimon Schubert #define PDP_ENDIAN _PDP_ENDIAN
57b2b3ffcdSSimon Schubert #define BYTE_ORDER _BYTE_ORDER
58b2b3ffcdSSimon Schubert #endif
59b2b3ffcdSSimon Schubert
60b2b3ffcdSSimon Schubert #ifdef __GNUC__
61b2b3ffcdSSimon Schubert
62b2b3ffcdSSimon Schubert #define __word_swap_int_var(x) \
631705e1a1SMatthew Dillon __extension__ ({ __uint32_t __X = (x); \
64b2b3ffcdSSimon Schubert __asm ("rorl $16, %0" : "+r" (__X)); \
65b2b3ffcdSSimon Schubert __X; })
66b2b3ffcdSSimon Schubert
67b2b3ffcdSSimon Schubert #ifdef __OPTIMIZE__
68b2b3ffcdSSimon Schubert
69b2b3ffcdSSimon Schubert #define __word_swap_int_const(x) \
70b2b3ffcdSSimon Schubert ((((x) & 0xffff0000) >> 16) | \
71b2b3ffcdSSimon Schubert (((x) & 0x0000ffff) << 16))
72b2b3ffcdSSimon Schubert #define __word_swap_int(x) (__builtin_constant_p(x) ? \
73b2b3ffcdSSimon Schubert __word_swap_int_const(x) : __word_swap_int_var(x))
74b2b3ffcdSSimon Schubert
75b2b3ffcdSSimon Schubert #else /* __OPTIMIZE__ */
76b2b3ffcdSSimon Schubert
77b2b3ffcdSSimon Schubert #define __word_swap_int(x) __word_swap_int_var(x)
78b2b3ffcdSSimon Schubert
79b2b3ffcdSSimon Schubert #endif /* __OPTIMIZE__ */
80b2b3ffcdSSimon Schubert
81b2b3ffcdSSimon Schubert #define __byte_swap_int_var(x) \
821705e1a1SMatthew Dillon __extension__ ({ __uint32_t __X = (x); \
83b2b3ffcdSSimon Schubert __asm ("bswap %0" : "+r" (__X)); \
84b2b3ffcdSSimon Schubert __X; })
85b2b3ffcdSSimon Schubert
86b2b3ffcdSSimon Schubert #ifdef __OPTIMIZE__
87b2b3ffcdSSimon Schubert
88b2b3ffcdSSimon Schubert #define __byte_swap_int_const(x) \
89b2b3ffcdSSimon Schubert ((((x) & 0xff000000) >> 24) | \
90b2b3ffcdSSimon Schubert (((x) & 0x00ff0000) >> 8) | \
91b2b3ffcdSSimon Schubert (((x) & 0x0000ff00) << 8) | \
92b2b3ffcdSSimon Schubert (((x) & 0x000000ff) << 24))
93b2b3ffcdSSimon Schubert #define __byte_swap_int(x) (__builtin_constant_p(x) ? \
94b2b3ffcdSSimon Schubert __byte_swap_int_const(x) : __byte_swap_int_var(x))
95b2b3ffcdSSimon Schubert
96b2b3ffcdSSimon Schubert #else /* __OPTIMIZE__ */
97b2b3ffcdSSimon Schubert
98b2b3ffcdSSimon Schubert #define __byte_swap_int(x) __byte_swap_int_var(x)
99b2b3ffcdSSimon Schubert
100b2b3ffcdSSimon Schubert #endif /* __OPTIMIZE__ */
101b2b3ffcdSSimon Schubert
102b2b3ffcdSSimon Schubert #define __byte_swap_long_var(x) \
1031705e1a1SMatthew Dillon __extension__ ({ __uint64_t __X = (x); \
104b2b3ffcdSSimon Schubert __asm ("bswap %0" : "+r" (__X)); \
105b2b3ffcdSSimon Schubert __X; })
106b2b3ffcdSSimon Schubert
107b2b3ffcdSSimon Schubert #define __byte_swap_long_const(x) \
108b2b3ffcdSSimon Schubert (((x >> 56) | \
109b2b3ffcdSSimon Schubert ((x >> 40) & 0xff00) | \
110b2b3ffcdSSimon Schubert ((x >> 24) & 0xff0000) | \
111b2b3ffcdSSimon Schubert ((x >> 8) & 0xff000000) | \
112b2b3ffcdSSimon Schubert ((x << 8) & ((__uint64_t)0xff << 32)) | \
113b2b3ffcdSSimon Schubert ((x << 24) & ((__uint64_t)0xff << 40)) | \
114b2b3ffcdSSimon Schubert ((x << 40) & ((__uint64_t)0xff << 48)) | \
115b2b3ffcdSSimon Schubert ((x << 56))))
116b2b3ffcdSSimon Schubert
117b2b3ffcdSSimon Schubert #ifdef __i386__
118b2b3ffcdSSimon Schubert
119b2b3ffcdSSimon Schubert #define __byte_swap_long(x) __byte_swap_long_const(x)
120b2b3ffcdSSimon Schubert
121b2b3ffcdSSimon Schubert #else
122b2b3ffcdSSimon Schubert
123b2b3ffcdSSimon Schubert #ifdef __OPTIMIZE__
124b2b3ffcdSSimon Schubert #define __byte_swap_long(x) (__builtin_constant_p(x) ? \
125b2b3ffcdSSimon Schubert __byte_swap_long_const(x) : __byte_swap_long_var(x))
126b2b3ffcdSSimon Schubert #else /* __OPTIMIZE__ */
127b2b3ffcdSSimon Schubert #define __byte_swap_long(x) __byte_swap_long_var(x)
128b2b3ffcdSSimon Schubert #endif /* __OPTIMIZE__ */
129b2b3ffcdSSimon Schubert
130b2b3ffcdSSimon Schubert #endif /* __i386__ */
131b2b3ffcdSSimon Schubert
132b2b3ffcdSSimon Schubert #define __byte_swap_word_var(x) \
1331705e1a1SMatthew Dillon __extension__ ({ __uint16_t __X = (x); \
134b2b3ffcdSSimon Schubert __asm ("xchgb %h0, %b0" : "+Q" (__X)); \
135b2b3ffcdSSimon Schubert __X; })
136b2b3ffcdSSimon Schubert
137b2b3ffcdSSimon Schubert #ifdef __OPTIMIZE__
138b2b3ffcdSSimon Schubert
139b2b3ffcdSSimon Schubert #define __byte_swap_word_const(x) \
140b2b3ffcdSSimon Schubert ((((x) & 0xff00) >> 8) | \
141b2b3ffcdSSimon Schubert (((x) & 0x00ff) << 8))
142b2b3ffcdSSimon Schubert
143b2b3ffcdSSimon Schubert #define __byte_swap_word(x) (__builtin_constant_p(x) ? \
144b2b3ffcdSSimon Schubert __byte_swap_word_const(x) : __byte_swap_word_var(x))
145b2b3ffcdSSimon Schubert
146b2b3ffcdSSimon Schubert #else /* __OPTIMIZE__ */
147b2b3ffcdSSimon Schubert
148b2b3ffcdSSimon Schubert #define __byte_swap_word(x) __byte_swap_word_var(x)
149b2b3ffcdSSimon Schubert
150b2b3ffcdSSimon Schubert #endif /* __OPTIMIZE__ */
151b2b3ffcdSSimon Schubert
152c1a70273Szrj static __inline __always_inline __uint64_t
__bswap64(__uint64_t _x)153b2b3ffcdSSimon Schubert __bswap64(__uint64_t _x)
154b2b3ffcdSSimon Schubert {
155350b8183Szrj #if __GNUC_PREREQ__(4, 8) && defined(__cplusplus)
156350b8183Szrj return (__builtin_bswap64(_x));
157350b8183Szrj #else
158b2b3ffcdSSimon Schubert return (__byte_swap_long(_x));
159350b8183Szrj #endif
160b2b3ffcdSSimon Schubert }
161b2b3ffcdSSimon Schubert
162c1a70273Szrj static __inline __always_inline __uint32_t
__bswap32(__uint32_t _x)163b2b3ffcdSSimon Schubert __bswap32(__uint32_t _x)
164b2b3ffcdSSimon Schubert {
165350b8183Szrj #if __GNUC_PREREQ__(4, 8) && defined(__cplusplus)
166350b8183Szrj return (__builtin_bswap32(_x));
167350b8183Szrj #else
168b2b3ffcdSSimon Schubert return (__byte_swap_int(_x));
169350b8183Szrj #endif
170b2b3ffcdSSimon Schubert }
171b2b3ffcdSSimon Schubert
172c1a70273Szrj static __inline __always_inline __uint16_t
__bswap16(__uint16_t _x)173b2b3ffcdSSimon Schubert __bswap16(__uint16_t _x)
174b2b3ffcdSSimon Schubert {
175350b8183Szrj #if __GNUC_PREREQ__(4, 8) && defined(__cplusplus)
176350b8183Szrj return (__builtin_bswap16(_x));
177350b8183Szrj #else
178b2b3ffcdSSimon Schubert return (__byte_swap_word(_x));
179350b8183Szrj #endif
180b2b3ffcdSSimon Schubert }
181b2b3ffcdSSimon Schubert
182b2b3ffcdSSimon Schubert #define __htonl(x) __bswap32(x)
183b2b3ffcdSSimon Schubert #define __htons(x) __bswap16(x)
184b2b3ffcdSSimon Schubert #define __ntohl(x) __bswap32(x)
185b2b3ffcdSSimon Schubert #define __ntohs(x) __bswap16(x)
186b2b3ffcdSSimon Schubert
187b2b3ffcdSSimon Schubert #else /* !__GNUC__ */
188b2b3ffcdSSimon Schubert
189b2b3ffcdSSimon Schubert /*
190b2b3ffcdSSimon Schubert * No optimizations are available for this compiler. Fall back to
191b2b3ffcdSSimon Schubert * non-optimized functions by defining the constant usually used to prevent
192b2b3ffcdSSimon Schubert * redefinition.
193b2b3ffcdSSimon Schubert */
194b2b3ffcdSSimon Schubert #define _BYTEORDER_FUNC_DEFINED
195b2b3ffcdSSimon Schubert
196b2b3ffcdSSimon Schubert #endif /* __GNUC__ */
197b2b3ffcdSSimon Schubert
198a33a0137Szrj /*
199a33a0137Szrj * Userland compatibility double underscore variants to help with DPorts
200a33a0137Szrj * (injected through <sys/types.h>).
201a33a0137Szrj */
202a33a0137Szrj #ifndef _KERNEL
203a33a0137Szrj #define __LITTLE_ENDIAN _LITTLE_ENDIAN
204a33a0137Szrj #define __BIG_ENDIAN _BIG_ENDIAN
205a33a0137Szrj #define __PDP_ENDIAN _PDP_ENDIAN
206a33a0137Szrj #define __BYTE_ORDER _BYTE_ORDER
207*7822e9aeSzrj
208*7822e9aeSzrj #ifndef __FLOAT_WORD_ORDER
209*7822e9aeSzrj #define __FLOAT_WORD_ORDER __BYTE_ORDER
210a33a0137Szrj #endif
211*7822e9aeSzrj #endif /* !_KERNEL */
212a33a0137Szrj
213b2b3ffcdSSimon Schubert #endif /* !_CPU_ENDIAN_H_ */
214