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