xref: /freebsd/sys/x86/include/endian.h (revision 050246770776815082ede6a899f445cf252113bf)
195b1d16dSTijl Coosemans /*-
295b1d16dSTijl Coosemans  * Copyright (c) 1987, 1991 Regents of the University of California.
395b1d16dSTijl Coosemans  * All rights reserved.
495b1d16dSTijl Coosemans  *
595b1d16dSTijl Coosemans  * Redistribution and use in source and binary forms, with or without
695b1d16dSTijl Coosemans  * modification, are permitted provided that the following conditions
795b1d16dSTijl Coosemans  * are met:
895b1d16dSTijl Coosemans  * 1. Redistributions of source code must retain the above copyright
995b1d16dSTijl Coosemans  *    notice, this list of conditions and the following disclaimer.
1095b1d16dSTijl Coosemans  * 2. Redistributions in binary form must reproduce the above copyright
1195b1d16dSTijl Coosemans  *    notice, this list of conditions and the following disclaimer in the
1295b1d16dSTijl Coosemans  *    documentation and/or other materials provided with the distribution.
1395b1d16dSTijl Coosemans  * 4. Neither the name of the University nor the names of its contributors
1495b1d16dSTijl Coosemans  *    may be used to endorse or promote products derived from this software
1595b1d16dSTijl Coosemans  *    without specific prior written permission.
1695b1d16dSTijl Coosemans  *
1795b1d16dSTijl Coosemans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1895b1d16dSTijl Coosemans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1995b1d16dSTijl Coosemans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2095b1d16dSTijl Coosemans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2195b1d16dSTijl Coosemans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2295b1d16dSTijl Coosemans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2395b1d16dSTijl Coosemans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2495b1d16dSTijl Coosemans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2595b1d16dSTijl Coosemans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2695b1d16dSTijl Coosemans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2795b1d16dSTijl Coosemans  * SUCH DAMAGE.
2895b1d16dSTijl Coosemans  *
2995b1d16dSTijl Coosemans  *	@(#)endian.h	7.8 (Berkeley) 4/3/91
3095b1d16dSTijl Coosemans  * $FreeBSD$
3195b1d16dSTijl Coosemans  */
3295b1d16dSTijl Coosemans 
3395b1d16dSTijl Coosemans #ifndef _MACHINE_ENDIAN_H_
3495b1d16dSTijl Coosemans #define	_MACHINE_ENDIAN_H_
3595b1d16dSTijl Coosemans 
3695b1d16dSTijl Coosemans #include <sys/cdefs.h>
3795b1d16dSTijl Coosemans #include <sys/_types.h>
3895b1d16dSTijl Coosemans 
3995b1d16dSTijl Coosemans /*
4095b1d16dSTijl Coosemans  * Define the order of 32-bit words in 64-bit words.
4195b1d16dSTijl Coosemans  */
4295b1d16dSTijl Coosemans #define	_QUAD_HIGHWORD 1
4395b1d16dSTijl Coosemans #define	_QUAD_LOWWORD 0
4495b1d16dSTijl Coosemans 
4595b1d16dSTijl Coosemans /*
4695b1d16dSTijl Coosemans  * Definitions for byte order, according to byte significance from low
4795b1d16dSTijl Coosemans  * address to high.
4895b1d16dSTijl Coosemans  */
4995b1d16dSTijl Coosemans #define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
5095b1d16dSTijl Coosemans #define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
5195b1d16dSTijl Coosemans #define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
5295b1d16dSTijl Coosemans 
5395b1d16dSTijl Coosemans #define	_BYTE_ORDER	_LITTLE_ENDIAN
5495b1d16dSTijl Coosemans 
5595b1d16dSTijl Coosemans /*
5695b1d16dSTijl Coosemans  * Deprecated variants that don't have enough underscores to be useful in more
5795b1d16dSTijl Coosemans  * strict namespaces.
5895b1d16dSTijl Coosemans  */
5995b1d16dSTijl Coosemans #if __BSD_VISIBLE
6095b1d16dSTijl Coosemans #define	LITTLE_ENDIAN	_LITTLE_ENDIAN
6195b1d16dSTijl Coosemans #define	BIG_ENDIAN	_BIG_ENDIAN
6295b1d16dSTijl Coosemans #define	PDP_ENDIAN	_PDP_ENDIAN
6395b1d16dSTijl Coosemans #define	BYTE_ORDER	_BYTE_ORDER
6495b1d16dSTijl Coosemans #endif
6595b1d16dSTijl Coosemans 
66*05024677STijl Coosemans #define	__bswap16_gen(x)	(__uint16_t)((x) << 8 | (x) >> 8)
67*05024677STijl Coosemans #define	__bswap32_gen(x)		\
68*05024677STijl Coosemans 	(((__uint32_t)__bswap16(x) << 16) | __bswap16((x) >> 16))
69*05024677STijl Coosemans #define	__bswap64_gen(x)		\
70*05024677STijl Coosemans 	(((__uint64_t)__bswap32(x) << 32) | __bswap32((x) >> 32))
7195b1d16dSTijl Coosemans 
72*05024677STijl Coosemans #ifdef __GNUCLIKE_BUILTIN_CONSTANT_P
73*05024677STijl Coosemans #define	__bswap16(x)			\
74*05024677STijl Coosemans 	(__builtin_constant_p(x) ?	\
75*05024677STijl Coosemans 	    __bswap16_gen((__uint16_t)(x)) : __bswap16_var(x))
76*05024677STijl Coosemans #define	__bswap32(x)			\
77*05024677STijl Coosemans 	(__builtin_constant_p(x) ?	\
78*05024677STijl Coosemans 	    __bswap32_gen((__uint32_t)(x)) : __bswap32_var(x))
79*05024677STijl Coosemans #define	__bswap64(x)			\
80*05024677STijl Coosemans 	(__builtin_constant_p(x) ?	\
81*05024677STijl Coosemans 	    __bswap64_gen((__uint64_t)(x)) : __bswap64_var(x))
82*05024677STijl Coosemans #else
83*05024677STijl Coosemans /* XXX these are broken for use in static initializers. */
84*05024677STijl Coosemans #define	__bswap16(x)	__bswap16_var(x)
85*05024677STijl Coosemans #define	__bswap32(x)	__bswap32_var(x)
86*05024677STijl Coosemans #define	__bswap64(x)	__bswap64_var(x)
87*05024677STijl Coosemans #endif
8895b1d16dSTijl Coosemans 
89*05024677STijl Coosemans /* These are defined as functions to avoid multiple evaluation of x. */
9095b1d16dSTijl Coosemans 
9195b1d16dSTijl Coosemans static __inline __uint16_t
9295b1d16dSTijl Coosemans __bswap16_var(__uint16_t _x)
9395b1d16dSTijl Coosemans {
9495b1d16dSTijl Coosemans 
95*05024677STijl Coosemans 	return (__bswap16_gen(_x));
9695b1d16dSTijl Coosemans }
9795b1d16dSTijl Coosemans 
9895b1d16dSTijl Coosemans static __inline __uint32_t
9995b1d16dSTijl Coosemans __bswap32_var(__uint32_t _x)
10095b1d16dSTijl Coosemans {
10195b1d16dSTijl Coosemans 
102*05024677STijl Coosemans #ifdef __GNUCLIKE_ASM
10395b1d16dSTijl Coosemans 	__asm("bswap %0" : "+r" (_x));
10495b1d16dSTijl Coosemans 	return (_x);
105*05024677STijl Coosemans #else
106*05024677STijl Coosemans 	return (__bswap32_gen(_x));
107*05024677STijl Coosemans #endif
10895b1d16dSTijl Coosemans }
10995b1d16dSTijl Coosemans 
11095b1d16dSTijl Coosemans static __inline __uint64_t
11195b1d16dSTijl Coosemans __bswap64_var(__uint64_t _x)
11295b1d16dSTijl Coosemans {
113*05024677STijl Coosemans 
114*05024677STijl Coosemans #if defined(__amd64__) && defined(__GNUCLIKE_ASM)
11595b1d16dSTijl Coosemans 	__asm("bswap %0" : "+r" (_x));
11695b1d16dSTijl Coosemans 	return (_x);
11795b1d16dSTijl Coosemans #else
118*05024677STijl Coosemans 	/*
119*05024677STijl Coosemans 	 * It is important for the optimizations that the following is not
120*05024677STijl Coosemans 	 * really generic, but expands to 2 __bswap32_var()'s.
121*05024677STijl Coosemans 	 */
122*05024677STijl Coosemans 	return (__bswap64_gen(_x));
12395b1d16dSTijl Coosemans #endif
12495b1d16dSTijl Coosemans }
12595b1d16dSTijl Coosemans 
12695b1d16dSTijl Coosemans #define	__htonl(x)	__bswap32(x)
12795b1d16dSTijl Coosemans #define	__htons(x)	__bswap16(x)
12895b1d16dSTijl Coosemans #define	__ntohl(x)	__bswap32(x)
12995b1d16dSTijl Coosemans #define	__ntohs(x)	__bswap16(x)
13095b1d16dSTijl Coosemans 
13195b1d16dSTijl Coosemans #endif /* !_MACHINE_ENDIAN_H_ */
132