xref: /freebsd/sys/arm64/include/endian.h (revision 412042e2aeb666395b3996808aff3a8e2273438f)
1*412042e2SAndrew Turner /*-
2*412042e2SAndrew Turner  * Copyright (c) 2001 David E. O'Brien
3*412042e2SAndrew Turner  *
4*412042e2SAndrew Turner  * Redistribution and use in source and binary forms, with or without
5*412042e2SAndrew Turner  * modification, are permitted provided that the following conditions
6*412042e2SAndrew Turner  * are met:
7*412042e2SAndrew Turner  * 1. Redistributions of source code must retain the above copyright
8*412042e2SAndrew Turner  *    notice, this list of conditions and the following disclaimer.
9*412042e2SAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
10*412042e2SAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
11*412042e2SAndrew Turner  *    documentation and/or other materials provided with the distribution.
12*412042e2SAndrew Turner  * 3. Neither the name of the University nor the names of its contributors
13*412042e2SAndrew Turner  *    may be used to endorse or promote products derived from this software
14*412042e2SAndrew Turner  *    without specific prior written permission.
15*412042e2SAndrew Turner  *
16*412042e2SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17*412042e2SAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*412042e2SAndrew Turner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*412042e2SAndrew Turner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*412042e2SAndrew Turner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*412042e2SAndrew Turner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*412042e2SAndrew Turner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*412042e2SAndrew Turner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*412042e2SAndrew Turner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*412042e2SAndrew Turner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*412042e2SAndrew Turner  * SUCH DAMAGE.
27*412042e2SAndrew Turner  *
28*412042e2SAndrew Turner  *	@(#)endian.h	8.1 (Berkeley) 6/10/93
29*412042e2SAndrew Turner  * $NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $
30*412042e2SAndrew Turner  * $FreeBSD$
31*412042e2SAndrew Turner  */
32*412042e2SAndrew Turner 
33*412042e2SAndrew Turner #ifndef _MACHINE_ENDIAN_H_
34*412042e2SAndrew Turner #define	_MACHINE_ENDIAN_H_
35*412042e2SAndrew Turner 
36*412042e2SAndrew Turner #include <sys/_types.h>
37*412042e2SAndrew Turner 
38*412042e2SAndrew Turner /*
39*412042e2SAndrew Turner  * Definitions for byte order, according to byte significance from low
40*412042e2SAndrew Turner  * address to high.
41*412042e2SAndrew Turner  */
42*412042e2SAndrew Turner #define	_LITTLE_ENDIAN  1234    /* LSB first: i386, vax */
43*412042e2SAndrew Turner #define	_BIG_ENDIAN     4321    /* MSB first: 68000, ibm, net */
44*412042e2SAndrew Turner #define	_PDP_ENDIAN     3412    /* LSB first in word, MSW first in long */
45*412042e2SAndrew Turner 
46*412042e2SAndrew Turner #define	_BYTE_ORDER	_LITTLE_ENDIAN
47*412042e2SAndrew Turner 
48*412042e2SAndrew Turner #if __BSD_VISIBLE
49*412042e2SAndrew Turner #define	LITTLE_ENDIAN   _LITTLE_ENDIAN
50*412042e2SAndrew Turner #define	BIG_ENDIAN      _BIG_ENDIAN
51*412042e2SAndrew Turner #define	PDP_ENDIAN      _PDP_ENDIAN
52*412042e2SAndrew Turner #define	BYTE_ORDER      _BYTE_ORDER
53*412042e2SAndrew Turner #endif
54*412042e2SAndrew Turner 
55*412042e2SAndrew Turner #define	_QUAD_HIGHWORD  1
56*412042e2SAndrew Turner #define	_QUAD_LOWWORD 0
57*412042e2SAndrew Turner #define	__ntohl(x)        (__bswap32(x))
58*412042e2SAndrew Turner #define	__ntohs(x)        (__bswap16(x))
59*412042e2SAndrew Turner #define	__htonl(x)        (__bswap32(x))
60*412042e2SAndrew Turner #define	__htons(x)        (__bswap16(x))
61*412042e2SAndrew Turner 
62*412042e2SAndrew Turner static __inline __uint64_t
63*412042e2SAndrew Turner __bswap64(__uint64_t x)
64*412042e2SAndrew Turner {
65*412042e2SAndrew Turner 	__uint64_t ret;
66*412042e2SAndrew Turner 
67*412042e2SAndrew Turner 	__asm __volatile("rev %0, %1\n"
68*412042e2SAndrew Turner 			 : "=&r" (ret), "+r" (x));
69*412042e2SAndrew Turner 
70*412042e2SAndrew Turner 	return (ret);
71*412042e2SAndrew Turner }
72*412042e2SAndrew Turner 
73*412042e2SAndrew Turner static __inline __uint32_t
74*412042e2SAndrew Turner __bswap32_var(__uint32_t v)
75*412042e2SAndrew Turner {
76*412042e2SAndrew Turner 	__uint32_t ret;
77*412042e2SAndrew Turner 
78*412042e2SAndrew Turner 	__asm __volatile("rev32 %x0, %x1\n"
79*412042e2SAndrew Turner 			 : "=&r" (ret), "+r" (v));
80*412042e2SAndrew Turner 
81*412042e2SAndrew Turner 	return (ret);
82*412042e2SAndrew Turner }
83*412042e2SAndrew Turner 
84*412042e2SAndrew Turner static __inline __uint16_t
85*412042e2SAndrew Turner __bswap16_var(__uint16_t v)
86*412042e2SAndrew Turner {
87*412042e2SAndrew Turner 	__uint32_t ret;
88*412042e2SAndrew Turner 
89*412042e2SAndrew Turner 	__asm __volatile("rev16 %w0, %w1\n"
90*412042e2SAndrew Turner 			 : "=&r" (ret), "+r" (v));
91*412042e2SAndrew Turner 
92*412042e2SAndrew Turner 	return ((__uint16_t)ret);
93*412042e2SAndrew Turner }
94*412042e2SAndrew Turner 
95*412042e2SAndrew Turner #ifdef __OPTIMIZE__
96*412042e2SAndrew Turner 
97*412042e2SAndrew Turner #define	__bswap32_constant(x)	\
98*412042e2SAndrew Turner     ((((x) & 0xff000000U) >> 24) |	\
99*412042e2SAndrew Turner      (((x) & 0x00ff0000U) >>  8) |	\
100*412042e2SAndrew Turner      (((x) & 0x0000ff00U) <<  8) |	\
101*412042e2SAndrew Turner      (((x) & 0x000000ffU) << 24))
102*412042e2SAndrew Turner 
103*412042e2SAndrew Turner #define	__bswap16_constant(x)	\
104*412042e2SAndrew Turner     ((((x) & 0xff00) >> 8) |		\
105*412042e2SAndrew Turner      (((x) & 0x00ff) << 8))
106*412042e2SAndrew Turner 
107*412042e2SAndrew Turner #define	__bswap16(x)	\
108*412042e2SAndrew Turner     ((__uint16_t)(__builtin_constant_p(x) ?	\
109*412042e2SAndrew Turner      __bswap16_constant(x) :			\
110*412042e2SAndrew Turner      __bswap16_var(x)))
111*412042e2SAndrew Turner 
112*412042e2SAndrew Turner #define	__bswap32(x)	\
113*412042e2SAndrew Turner     ((__uint32_t)(__builtin_constant_p(x) ? 	\
114*412042e2SAndrew Turner      __bswap32_constant(x) :			\
115*412042e2SAndrew Turner      __bswap32_var(x)))
116*412042e2SAndrew Turner 
117*412042e2SAndrew Turner #else
118*412042e2SAndrew Turner #define	__bswap16(x)	__bswap16_var(x)
119*412042e2SAndrew Turner #define	__bswap32(x)	__bswap32_var(x)
120*412042e2SAndrew Turner 
121*412042e2SAndrew Turner #endif /* __OPTIMIZE__ */
122*412042e2SAndrew Turner #endif /* !_MACHINE_ENDIAN_H_ */
123