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