1/*	$OpenBSD: endian.h,v 1.8 2019/02/17 15:44:26 deraadt Exp $	*/
2/*	$NetBSD: endian.h,v 1.4 2000/03/17 00:09:25 mycroft Exp $	*/
3
4/* Written by Manuel Bouyer. Public domain */
5
6#ifndef _SH_ENDIAN_H_
7#define	_SH_ENDIAN_H_
8
9#ifndef __FROM_SYS__ENDIAN
10#include <sys/_types.h>
11#endif
12
13static __inline __uint16_t
14__swap16md(__uint16_t _x)
15{
16	__uint16_t _rv;
17
18	__asm volatile ("swap.b %1,%0" : "=r"(_rv) : "r"(_x));
19
20	 return (_rv);
21}
22
23static __inline __uint32_t
24__swap32md(__uint32_t _x)
25{
26	__uint32_t _rv;
27
28	__asm volatile ("swap.b %1,%0; swap.w %0,%0; swap.b %0,%0"
29			  : "=r"(_rv) : "r"(_x));
30
31	return (_rv);
32}
33
34static __inline __uint64_t
35__swap64md(__uint64_t _x)
36{
37	__uint64_t _rv;
38
39	_rv = (__uint64_t)__swap32md((__uint32_t)(_x >> 32)) |
40	    (__uint64_t)__swap32md((__uint32_t)_x) << 32;
41
42	return (_rv);
43}
44
45/* Tell sys/endian.h we have MD variants of the swap macros.  */
46#define __HAVE_MD_SWAP
47
48#ifdef __LITTLE_ENDIAN__
49#define	_BYTE_ORDER _LITTLE_ENDIAN
50#else
51#define	_BYTE_ORDER _BIG_ENDIAN
52#endif
53#define	__STRICT_ALIGNMENT
54
55#ifndef __FROM_SYS__ENDIAN
56#include <sys/endian.h>
57#endif
58
59#endif /* !_SH_ENDIAN_H_ */
60