xref: /freebsd/sys/x86/include/endian.h (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
195b1d16dSTijl Coosemans /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
495b1d16dSTijl Coosemans  * Copyright (c) 1987, 1991 Regents of the University of California.
595b1d16dSTijl Coosemans  * All rights reserved.
695b1d16dSTijl Coosemans  *
795b1d16dSTijl Coosemans  * Redistribution and use in source and binary forms, with or without
895b1d16dSTijl Coosemans  * modification, are permitted provided that the following conditions
995b1d16dSTijl Coosemans  * are met:
1095b1d16dSTijl Coosemans  * 1. Redistributions of source code must retain the above copyright
1195b1d16dSTijl Coosemans  *    notice, this list of conditions and the following disclaimer.
1295b1d16dSTijl Coosemans  * 2. Redistributions in binary form must reproduce the above copyright
1395b1d16dSTijl Coosemans  *    notice, this list of conditions and the following disclaimer in the
1495b1d16dSTijl Coosemans  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1695b1d16dSTijl Coosemans  *    may be used to endorse or promote products derived from this software
1795b1d16dSTijl Coosemans  *    without specific prior written permission.
1895b1d16dSTijl Coosemans  *
1995b1d16dSTijl Coosemans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2095b1d16dSTijl Coosemans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2195b1d16dSTijl Coosemans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2295b1d16dSTijl Coosemans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2395b1d16dSTijl Coosemans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2495b1d16dSTijl Coosemans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2595b1d16dSTijl Coosemans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2695b1d16dSTijl Coosemans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2795b1d16dSTijl Coosemans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2895b1d16dSTijl Coosemans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2995b1d16dSTijl Coosemans  * SUCH DAMAGE.
3095b1d16dSTijl Coosemans  *
3195b1d16dSTijl Coosemans  *	@(#)endian.h	7.8 (Berkeley) 4/3/91
3295b1d16dSTijl Coosemans  * $FreeBSD$
3395b1d16dSTijl Coosemans  */
3495b1d16dSTijl Coosemans 
3595b1d16dSTijl Coosemans #ifndef _MACHINE_ENDIAN_H_
3695b1d16dSTijl Coosemans #define	_MACHINE_ENDIAN_H_
3795b1d16dSTijl Coosemans 
3895b1d16dSTijl Coosemans #include <sys/cdefs.h>
3995b1d16dSTijl Coosemans #include <sys/_types.h>
4095b1d16dSTijl Coosemans 
4195b1d16dSTijl Coosemans /*
4295b1d16dSTijl Coosemans  * Define the order of 32-bit words in 64-bit words.
4395b1d16dSTijl Coosemans  */
4495b1d16dSTijl Coosemans #define	_QUAD_HIGHWORD 1
4595b1d16dSTijl Coosemans #define	_QUAD_LOWWORD 0
4695b1d16dSTijl Coosemans 
4795b1d16dSTijl Coosemans /*
4895b1d16dSTijl Coosemans  * Definitions for byte order, according to byte significance from low
4995b1d16dSTijl Coosemans  * address to high.
5095b1d16dSTijl Coosemans  */
5195b1d16dSTijl Coosemans #define	_LITTLE_ENDIAN	1234	/* LSB first: i386, vax */
5295b1d16dSTijl Coosemans #define	_BIG_ENDIAN	4321	/* MSB first: 68000, ibm, net */
5395b1d16dSTijl Coosemans #define	_PDP_ENDIAN	3412	/* LSB first in word, MSW first in long */
5495b1d16dSTijl Coosemans 
5595b1d16dSTijl Coosemans #define	_BYTE_ORDER	_LITTLE_ENDIAN
5695b1d16dSTijl Coosemans 
5795b1d16dSTijl Coosemans /*
5895b1d16dSTijl Coosemans  * Deprecated variants that don't have enough underscores to be useful in more
5995b1d16dSTijl Coosemans  * strict namespaces.
6095b1d16dSTijl Coosemans  */
6195b1d16dSTijl Coosemans #if __BSD_VISIBLE
6295b1d16dSTijl Coosemans #define	LITTLE_ENDIAN	_LITTLE_ENDIAN
6395b1d16dSTijl Coosemans #define	BIG_ENDIAN	_BIG_ENDIAN
6495b1d16dSTijl Coosemans #define	PDP_ENDIAN	_PDP_ENDIAN
6595b1d16dSTijl Coosemans #define	BYTE_ORDER	_BYTE_ORDER
6695b1d16dSTijl Coosemans #endif
6795b1d16dSTijl Coosemans 
684715a95fSDimitry Andric #define	__bswap16_gen(x)	(__uint16_t)((x) << 8 | (x) >> 8)
6905024677STijl Coosemans #define	__bswap32_gen(x)		\
70a80f8859SDimitry Andric 	(((__uint32_t)__bswap16((x) & 0xffff) << 16) | __bswap16((x) >> 16))
7105024677STijl Coosemans #define	__bswap64_gen(x)		\
72a80f8859SDimitry Andric 	(((__uint64_t)__bswap32((x) & 0xffffffff) << 32) | __bswap32((x) >> 32))
7395b1d16dSTijl Coosemans 
7405024677STijl Coosemans #ifdef __GNUCLIKE_BUILTIN_CONSTANT_P
7505024677STijl Coosemans #define	__bswap16(x)				\
7663d094a7SDimitry Andric 	((__uint16_t)(__builtin_constant_p(x) ?	\
7763d094a7SDimitry Andric 	    __bswap16_gen((__uint16_t)(x)) : __bswap16_var(x)))
7805024677STijl Coosemans #define	__bswap32(x)			\
7905024677STijl Coosemans 	(__builtin_constant_p(x) ?	\
8005024677STijl Coosemans 	    __bswap32_gen((__uint32_t)(x)) : __bswap32_var(x))
8105024677STijl Coosemans #define	__bswap64(x)			\
8205024677STijl Coosemans 	(__builtin_constant_p(x) ?	\
8305024677STijl Coosemans 	    __bswap64_gen((__uint64_t)(x)) : __bswap64_var(x))
8405024677STijl Coosemans #else
8505024677STijl Coosemans /* XXX these are broken for use in static initializers. */
8605024677STijl Coosemans #define	__bswap16(x)	__bswap16_var(x)
8705024677STijl Coosemans #define	__bswap32(x)	__bswap32_var(x)
8805024677STijl Coosemans #define	__bswap64(x)	__bswap64_var(x)
8905024677STijl Coosemans #endif
9095b1d16dSTijl Coosemans 
9105024677STijl Coosemans /* These are defined as functions to avoid multiple evaluation of x. */
9295b1d16dSTijl Coosemans 
9395b1d16dSTijl Coosemans static __inline __uint16_t
9495b1d16dSTijl Coosemans __bswap16_var(__uint16_t _x)
9595b1d16dSTijl Coosemans {
9695b1d16dSTijl Coosemans 
9705024677STijl Coosemans 	return (__bswap16_gen(_x));
9895b1d16dSTijl Coosemans }
9995b1d16dSTijl Coosemans 
10095b1d16dSTijl Coosemans static __inline __uint32_t
10195b1d16dSTijl Coosemans __bswap32_var(__uint32_t _x)
10295b1d16dSTijl Coosemans {
10395b1d16dSTijl Coosemans 
10405024677STijl Coosemans #ifdef __GNUCLIKE_ASM
10595b1d16dSTijl Coosemans 	__asm("bswap %0" : "+r" (_x));
10695b1d16dSTijl Coosemans 	return (_x);
10705024677STijl Coosemans #else
10805024677STijl Coosemans 	return (__bswap32_gen(_x));
10905024677STijl Coosemans #endif
11095b1d16dSTijl Coosemans }
11195b1d16dSTijl Coosemans 
11295b1d16dSTijl Coosemans static __inline __uint64_t
11395b1d16dSTijl Coosemans __bswap64_var(__uint64_t _x)
11495b1d16dSTijl Coosemans {
11505024677STijl Coosemans 
11605024677STijl Coosemans #if defined(__amd64__) && defined(__GNUCLIKE_ASM)
11795b1d16dSTijl Coosemans 	__asm("bswap %0" : "+r" (_x));
11895b1d16dSTijl Coosemans 	return (_x);
11995b1d16dSTijl Coosemans #else
12005024677STijl Coosemans 	/*
12105024677STijl Coosemans 	 * It is important for the optimizations that the following is not
12205024677STijl Coosemans 	 * really generic, but expands to 2 __bswap32_var()'s.
12305024677STijl Coosemans 	 */
12405024677STijl Coosemans 	return (__bswap64_gen(_x));
12595b1d16dSTijl Coosemans #endif
12695b1d16dSTijl Coosemans }
12795b1d16dSTijl Coosemans 
12895b1d16dSTijl Coosemans #define	__htonl(x)	__bswap32(x)
12995b1d16dSTijl Coosemans #define	__htons(x)	__bswap16(x)
13095b1d16dSTijl Coosemans #define	__ntohl(x)	__bswap32(x)
13195b1d16dSTijl Coosemans #define	__ntohs(x)	__bswap16(x)
13295b1d16dSTijl Coosemans 
13395b1d16dSTijl Coosemans #endif /* !_MACHINE_ENDIAN_H_ */
134