xref: /freebsd/sys/arm/include/endian.h (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
1d8315c79SWarner Losh /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
4d4cfb421SDavid E. O'Brien  * Copyright (c) 2001 David E. O'Brien
5d4cfb421SDavid E. O'Brien  *
6d4cfb421SDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
7d4cfb421SDavid E. O'Brien  * modification, are permitted provided that the following conditions
8d4cfb421SDavid E. O'Brien  * are met:
9d4cfb421SDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
10d4cfb421SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
11d4cfb421SDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
12d4cfb421SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
13d4cfb421SDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
14d4cfb421SDavid E. O'Brien  * 3. Neither the name of the University nor the names of its contributors
15d4cfb421SDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
16d4cfb421SDavid E. O'Brien  *    without specific prior written permission.
17d4cfb421SDavid E. O'Brien  *
18d4cfb421SDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19d4cfb421SDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20d4cfb421SDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21d4cfb421SDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22d4cfb421SDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23d4cfb421SDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24d4cfb421SDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25d4cfb421SDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26d4cfb421SDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27d4cfb421SDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28d4cfb421SDavid E. O'Brien  * SUCH DAMAGE.
29d4cfb421SDavid E. O'Brien  *
30d4cfb421SDavid E. O'Brien  *	@(#)endian.h	8.1 (Berkeley) 6/10/93
31d4cfb421SDavid E. O'Brien  * $NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $
32d4cfb421SDavid E. O'Brien  * $FreeBSD$
33d4cfb421SDavid E. O'Brien  */
34d4cfb421SDavid E. O'Brien 
35d4cfb421SDavid E. O'Brien #ifndef _ENDIAN_H_
36d4cfb421SDavid E. O'Brien #define	_ENDIAN_H_
37d4cfb421SDavid E. O'Brien 
382cbc052aSOlivier Houchard #include <sys/_types.h>
39d4cfb421SDavid E. O'Brien 
402cbc052aSOlivier Houchard /*
412cbc052aSOlivier Houchard  * Definitions for byte order, according to byte significance from low
422cbc052aSOlivier Houchard  * address to high.
432cbc052aSOlivier Houchard  */
442cbc052aSOlivier Houchard #define _LITTLE_ENDIAN  1234    /* LSB first: i386, vax */
452cbc052aSOlivier Houchard #define _BIG_ENDIAN     4321    /* MSB first: 68000, ibm, net */
462cbc052aSOlivier Houchard #define _PDP_ENDIAN     3412    /* LSB first in word, MSW first in long */
472cbc052aSOlivier Houchard 
4812a58da4SOlivier Houchard #ifdef __ARMEB__
4912a58da4SOlivier Houchard #define _BYTE_ORDER	_BIG_ENDIAN
5012a58da4SOlivier Houchard #else
512cbc052aSOlivier Houchard #define	_BYTE_ORDER	_LITTLE_ENDIAN
5212a58da4SOlivier Houchard #endif /* __ARMEB__ */
532cbc052aSOlivier Houchard 
542cbc052aSOlivier Houchard #if __BSD_VISIBLE
552cbc052aSOlivier Houchard #define LITTLE_ENDIAN   _LITTLE_ENDIAN
562cbc052aSOlivier Houchard #define BIG_ENDIAN      _BIG_ENDIAN
572cbc052aSOlivier Houchard #define PDP_ENDIAN      _PDP_ENDIAN
582cbc052aSOlivier Houchard #define BYTE_ORDER      _BYTE_ORDER
592cbc052aSOlivier Houchard #endif
602cbc052aSOlivier Houchard 
6112a58da4SOlivier Houchard #ifdef __ARMEB__
6212a58da4SOlivier Houchard #define _QUAD_HIGHWORD 0
6312a58da4SOlivier Houchard #define _QUAD_LOWWORD 1
6412a58da4SOlivier Houchard #define __ntohl(x)	((__uint32_t)(x))
6512a58da4SOlivier Houchard #define __ntohs(x)	((__uint16_t)(x))
6612a58da4SOlivier Houchard #define __htonl(x)	((__uint32_t)(x))
6712a58da4SOlivier Houchard #define __htons(x)	((__uint16_t)(x))
6812a58da4SOlivier Houchard #else
692cbc052aSOlivier Houchard #define _QUAD_HIGHWORD  1
702cbc052aSOlivier Houchard #define _QUAD_LOWWORD 0
712cbc052aSOlivier Houchard #define __ntohl(x)        (__bswap32(x))
722cbc052aSOlivier Houchard #define __ntohs(x)        (__bswap16(x))
739b60f79dSOlivier Houchard #define __htonl(x)        (__bswap32(x))
749b60f79dSOlivier Houchard #define __htons(x)        (__bswap16(x))
7512a58da4SOlivier Houchard #endif /* __ARMEB__ */
762cbc052aSOlivier Houchard 
772cbc052aSOlivier Houchard static __inline __uint64_t
782cbc052aSOlivier Houchard __bswap64(__uint64_t _x)
792cbc052aSOlivier Houchard {
802cbc052aSOlivier Houchard 
812cbc052aSOlivier Houchard 	return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
822cbc052aSOlivier Houchard 	    ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
832cbc052aSOlivier Houchard 	    ((_x << 24) & ((__uint64_t)0xff << 40)) |
842cbc052aSOlivier Houchard 	    ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
852cbc052aSOlivier Houchard }
862cbc052aSOlivier Houchard 
872cbc052aSOlivier Houchard static __inline __uint32_t
8874e9b5edSOlivier Houchard __bswap32_var(__uint32_t v)
892cbc052aSOlivier Houchard {
902cbc052aSOlivier Houchard 	__uint32_t t1;
912cbc052aSOlivier Houchard 
92fdc05f79SOlivier Houchard 	__asm __volatile("eor %1, %0, %0, ror #16\n"
93fdc05f79SOlivier Houchard 	    		"bic %1, %1, #0x00ff0000\n"
94fdc05f79SOlivier Houchard 			"mov %0, %0, ror #8\n"
95fdc05f79SOlivier Houchard 			"eor %0, %0, %1, lsr #8\n"
96fdc05f79SOlivier Houchard 			 : "+r" (v), "=r" (t1));
972cbc052aSOlivier Houchard 
982cbc052aSOlivier Houchard 	return (v);
992cbc052aSOlivier Houchard }
1002cbc052aSOlivier Houchard 
1012cbc052aSOlivier Houchard static __inline __uint16_t
102fdc05f79SOlivier Houchard __bswap16_var(__uint16_t v)
1032cbc052aSOlivier Houchard {
1044168e66bSOlivier Houchard 	__uint32_t ret = v & 0xffff;
1054168e66bSOlivier Houchard 
1062cbc052aSOlivier Houchard 	__asm __volatile(
107e11fe02dSJohn Baldwin 	    "mov    %0, %0, ror #8\n"
1082cbc052aSOlivier Houchard 	    "orr    %0, %0, %0, lsr #16\n"
1092cbc052aSOlivier Houchard 	    "bic    %0, %0, %0, lsl #16"
1104168e66bSOlivier Houchard 	    : "+r" (ret));
1112cbc052aSOlivier Houchard 
1124168e66bSOlivier Houchard 	return ((__uint16_t)ret);
1132cbc052aSOlivier Houchard }
11474e9b5edSOlivier Houchard 
11574e9b5edSOlivier Houchard #ifdef __OPTIMIZE__
11674e9b5edSOlivier Houchard 
11774e9b5edSOlivier Houchard #define __bswap32_constant(x)	\
11874e9b5edSOlivier Houchard     ((((x) & 0xff000000U) >> 24) |	\
11974e9b5edSOlivier Houchard      (((x) & 0x00ff0000U) >>  8) |	\
12074e9b5edSOlivier Houchard      (((x) & 0x0000ff00U) <<  8) |	\
12174e9b5edSOlivier Houchard      (((x) & 0x000000ffU) << 24))
12274e9b5edSOlivier Houchard 
12374e9b5edSOlivier Houchard #define __bswap16_constant(x)	\
12474e9b5edSOlivier Houchard     ((((x) & 0xff00) >> 8) |		\
12574e9b5edSOlivier Houchard      (((x) & 0x00ff) << 8))
12674e9b5edSOlivier Houchard 
12774e9b5edSOlivier Houchard #define __bswap16(x)	\
128eeaa6910SOlivier Houchard     ((__uint16_t)(__builtin_constant_p(x) ?	\
12974e9b5edSOlivier Houchard      __bswap16_constant(x) :			\
130eeaa6910SOlivier Houchard      __bswap16_var(x)))
13174e9b5edSOlivier Houchard 
13274e9b5edSOlivier Houchard #define __bswap32(x)	\
133eeaa6910SOlivier Houchard     ((__uint32_t)(__builtin_constant_p(x) ? 	\
13474e9b5edSOlivier Houchard      __bswap32_constant(x) :			\
135eeaa6910SOlivier Houchard      __bswap32_var(x)))
13674e9b5edSOlivier Houchard 
13774e9b5edSOlivier Houchard #else
13874e9b5edSOlivier Houchard #define __bswap16(x)	__bswap16_var(x)
13974e9b5edSOlivier Houchard #define __bswap32(x)	__bswap32_var(x)
14074e9b5edSOlivier Houchard 
14174e9b5edSOlivier Houchard #endif /* __OPTIMIZE__ */
142d4cfb421SDavid E. O'Brien #endif /* !_ENDIAN_H_ */
143