xref: /freebsd/sys/x86/include/endian.h (revision 95b1d16df593d3ebfa48ac7d27e57281ea51533b)
1*95b1d16dSTijl Coosemans /*-
2*95b1d16dSTijl Coosemans  * Copyright (c) 1987, 1991 Regents of the University of California.
3*95b1d16dSTijl Coosemans  * All rights reserved.
4*95b1d16dSTijl Coosemans  *
5*95b1d16dSTijl Coosemans  * Redistribution and use in source and binary forms, with or without
6*95b1d16dSTijl Coosemans  * modification, are permitted provided that the following conditions
7*95b1d16dSTijl Coosemans  * are met:
8*95b1d16dSTijl Coosemans  * 1. Redistributions of source code must retain the above copyright
9*95b1d16dSTijl Coosemans  *    notice, this list of conditions and the following disclaimer.
10*95b1d16dSTijl Coosemans  * 2. Redistributions in binary form must reproduce the above copyright
11*95b1d16dSTijl Coosemans  *    notice, this list of conditions and the following disclaimer in the
12*95b1d16dSTijl Coosemans  *    documentation and/or other materials provided with the distribution.
13*95b1d16dSTijl Coosemans  * 4. Neither the name of the University nor the names of its contributors
14*95b1d16dSTijl Coosemans  *    may be used to endorse or promote products derived from this software
15*95b1d16dSTijl Coosemans  *    without specific prior written permission.
16*95b1d16dSTijl Coosemans  *
17*95b1d16dSTijl Coosemans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*95b1d16dSTijl Coosemans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*95b1d16dSTijl Coosemans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*95b1d16dSTijl Coosemans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*95b1d16dSTijl Coosemans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*95b1d16dSTijl Coosemans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*95b1d16dSTijl Coosemans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*95b1d16dSTijl Coosemans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*95b1d16dSTijl Coosemans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*95b1d16dSTijl Coosemans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*95b1d16dSTijl Coosemans  * SUCH DAMAGE.
28*95b1d16dSTijl Coosemans  *
29*95b1d16dSTijl Coosemans  *	@(#)endian.h	7.8 (Berkeley) 4/3/91
30*95b1d16dSTijl Coosemans  * $FreeBSD$
31*95b1d16dSTijl Coosemans  */
32*95b1d16dSTijl Coosemans 
33*95b1d16dSTijl Coosemans #ifndef _MACHINE_ENDIAN_H_
34*95b1d16dSTijl Coosemans #define	_MACHINE_ENDIAN_H_
35*95b1d16dSTijl Coosemans 
36*95b1d16dSTijl Coosemans #include <sys/cdefs.h>
37*95b1d16dSTijl Coosemans #include <sys/_types.h>
38*95b1d16dSTijl Coosemans 
39*95b1d16dSTijl Coosemans #ifdef __cplusplus
40*95b1d16dSTijl Coosemans extern "C" {
41*95b1d16dSTijl Coosemans #endif
42*95b1d16dSTijl Coosemans 
43*95b1d16dSTijl Coosemans /*
44*95b1d16dSTijl Coosemans  * Define the order of 32-bit words in 64-bit words.
45*95b1d16dSTijl Coosemans  */
46*95b1d16dSTijl Coosemans #define	_QUAD_HIGHWORD 1
47*95b1d16dSTijl Coosemans #define	_QUAD_LOWWORD 0
48*95b1d16dSTijl Coosemans 
49*95b1d16dSTijl Coosemans /*
50*95b1d16dSTijl Coosemans  * Definitions for byte order, according to byte significance from low
51*95b1d16dSTijl Coosemans  * address to high.
52*95b1d16dSTijl Coosemans  */
53*95b1d16dSTijl Coosemans #define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
54*95b1d16dSTijl Coosemans #define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
55*95b1d16dSTijl Coosemans #define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
56*95b1d16dSTijl Coosemans 
57*95b1d16dSTijl Coosemans #define	_BYTE_ORDER	_LITTLE_ENDIAN
58*95b1d16dSTijl Coosemans 
59*95b1d16dSTijl Coosemans /*
60*95b1d16dSTijl Coosemans  * Deprecated variants that don't have enough underscores to be useful in more
61*95b1d16dSTijl Coosemans  * strict namespaces.
62*95b1d16dSTijl Coosemans  */
63*95b1d16dSTijl Coosemans #if __BSD_VISIBLE
64*95b1d16dSTijl Coosemans #define	LITTLE_ENDIAN	_LITTLE_ENDIAN
65*95b1d16dSTijl Coosemans #define	BIG_ENDIAN	_BIG_ENDIAN
66*95b1d16dSTijl Coosemans #define	PDP_ENDIAN	_PDP_ENDIAN
67*95b1d16dSTijl Coosemans #define	BYTE_ORDER	_BYTE_ORDER
68*95b1d16dSTijl Coosemans #endif
69*95b1d16dSTijl Coosemans 
70*95b1d16dSTijl Coosemans #if defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE_BUILTIN_CONSTANT_P)
71*95b1d16dSTijl Coosemans 
72*95b1d16dSTijl Coosemans #define	__bswap16_const(_x)	(__uint16_t)((_x) << 8 | (_x) >> 8)
73*95b1d16dSTijl Coosemans 
74*95b1d16dSTijl Coosemans #define	__bswap16(_x)			\
75*95b1d16dSTijl Coosemans 	(__builtin_constant_p(_x) ?	\
76*95b1d16dSTijl Coosemans 	    __bswap16_const((__uint16_t)(_x)) : __bswap16_var(_x))
77*95b1d16dSTijl Coosemans 
78*95b1d16dSTijl Coosemans #define	__bswap32_const(_x)		\
79*95b1d16dSTijl Coosemans 	(((__uint32_t)__bswap16(_x) << 16) | __bswap16((_x) >> 16))
80*95b1d16dSTijl Coosemans 
81*95b1d16dSTijl Coosemans #define	__bswap32(_x)			\
82*95b1d16dSTijl Coosemans 	(__builtin_constant_p(_x) ?	\
83*95b1d16dSTijl Coosemans 	    __bswap32_const((__uint32_t)(_x)) : __bswap32_var(_x))
84*95b1d16dSTijl Coosemans 
85*95b1d16dSTijl Coosemans #define	__bswap64_const(_x)		\
86*95b1d16dSTijl Coosemans 	(((__uint64_t)__bswap32(_x) << 32) | __bswap32((_x) >> 32))
87*95b1d16dSTijl Coosemans 
88*95b1d16dSTijl Coosemans #define	__bswap64(_x)			\
89*95b1d16dSTijl Coosemans 	(__builtin_constant_p(_x) ?	\
90*95b1d16dSTijl Coosemans 	    __bswap64_const((__uint64_t)(_x)) : __bswap64_var(_x))
91*95b1d16dSTijl Coosemans 
92*95b1d16dSTijl Coosemans static __inline __uint16_t
93*95b1d16dSTijl Coosemans __bswap16_var(__uint16_t _x)
94*95b1d16dSTijl Coosemans {
95*95b1d16dSTijl Coosemans 
96*95b1d16dSTijl Coosemans 	return (__bswap16_const(_x));
97*95b1d16dSTijl Coosemans }
98*95b1d16dSTijl Coosemans 
99*95b1d16dSTijl Coosemans static __inline __uint32_t
100*95b1d16dSTijl Coosemans __bswap32_var(__uint32_t _x)
101*95b1d16dSTijl Coosemans {
102*95b1d16dSTijl Coosemans 
103*95b1d16dSTijl Coosemans 	__asm ("bswap %0" : "+r" (_x));
104*95b1d16dSTijl Coosemans 	return (_x);
105*95b1d16dSTijl Coosemans }
106*95b1d16dSTijl Coosemans 
107*95b1d16dSTijl Coosemans static __inline __uint64_t
108*95b1d16dSTijl Coosemans __bswap64_var(__uint64_t _x)
109*95b1d16dSTijl Coosemans {
110*95b1d16dSTijl Coosemans #ifdef	_LP64
111*95b1d16dSTijl Coosemans 	__asm ("bswap %0" : "+r" (_x));
112*95b1d16dSTijl Coosemans 	return (_x);
113*95b1d16dSTijl Coosemans #else
114*95b1d16dSTijl Coosemans 	return (__bswap64_const(_x));
115*95b1d16dSTijl Coosemans #endif
116*95b1d16dSTijl Coosemans }
117*95b1d16dSTijl Coosemans 
118*95b1d16dSTijl Coosemans #define	__htonl(x)	__bswap32(x)
119*95b1d16dSTijl Coosemans #define	__htons(x)	__bswap16(x)
120*95b1d16dSTijl Coosemans #define	__ntohl(x)	__bswap32(x)
121*95b1d16dSTijl Coosemans #define	__ntohs(x)	__bswap16(x)
122*95b1d16dSTijl Coosemans 
123*95b1d16dSTijl Coosemans #else /* !(__GNUCLIKE_ASM && __GNUCLIKE_BUILTIN_CONSTANT_P) */
124*95b1d16dSTijl Coosemans 
125*95b1d16dSTijl Coosemans /*
126*95b1d16dSTijl Coosemans  * No optimizations are available for this compiler.  Fall back to
127*95b1d16dSTijl Coosemans  * non-optimized functions by defining the constant usually used to prevent
128*95b1d16dSTijl Coosemans  * redefinition.
129*95b1d16dSTijl Coosemans  */
130*95b1d16dSTijl Coosemans #define	_BYTEORDER_FUNC_DEFINED
131*95b1d16dSTijl Coosemans 
132*95b1d16dSTijl Coosemans #endif /* __GNUCLIKE_ASM && __GNUCLIKE_BUILTIN_CONSTANT_P */
133*95b1d16dSTijl Coosemans 
134*95b1d16dSTijl Coosemans #ifdef __cplusplus
135*95b1d16dSTijl Coosemans }
136*95b1d16dSTijl Coosemans #endif
137*95b1d16dSTijl Coosemans 
138*95b1d16dSTijl Coosemans #endif /* !_MACHINE_ENDIAN_H_ */
139