blob: 7c79fd43bfd47be770a35a1cfe60e919001a8308 [file] [log] [blame]
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +01001/*-
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +01002 * SPDX-License-Identifier: BSD-3-Clause
3 *
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +01004 * Copyright (c) 2001 David E. O'Brien
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * @(#)endian.h 8.1 (Berkeley) 6/10/93
31 * $NetBSD: endian.h,v 1.7 1999/08/21 05:53:51 simonb Exp $
32 * $FreeBSD$
33 */
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010034/*
35 * Portions copyright (c) 2018, ARM Limited and Contributors.
36 * All rights reserved.
37 */
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010038
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +000039#ifndef ENDIAN__H
40#define ENDIAN__H
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010041
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010042#include <stdint.h>
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010043
44/*
45 * Definitions for byte order, according to byte significance from low
46 * address to high.
47 */
48#define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
49#define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
50#define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
51
52#define _BYTE_ORDER _LITTLE_ENDIAN
53
54#if __BSD_VISIBLE
55#define LITTLE_ENDIAN _LITTLE_ENDIAN
56#define BIG_ENDIAN _BIG_ENDIAN
57#define PDP_ENDIAN _PDP_ENDIAN
58#define BYTE_ORDER _BYTE_ORDER
59#endif
60
61#define _QUAD_HIGHWORD 1
62#define _QUAD_LOWWORD 0
63#define __ntohl(x) (__bswap32(x))
64#define __ntohs(x) (__bswap16(x))
65#define __htonl(x) (__bswap32(x))
66#define __htons(x) (__bswap16(x))
67
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010068static __inline uint64_t
69__bswap64(uint64_t x)
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010070{
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010071 uint64_t ret;
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010072
73 __asm __volatile("rev %0, %1\n"
74 : "=&r" (ret), "+r" (x));
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010075
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010076 return (ret);
77}
78
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010079static __inline uint32_t
80__bswap32_var(uint32_t v)
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010081{
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010082 uint32_t ret;
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010083
84 __asm __volatile("rev32 %x0, %x1\n"
85 : "=&r" (ret), "+r" (v));
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010086
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010087 return (ret);
88}
89
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010090static __inline uint16_t
91__bswap16_var(uint16_t v)
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010092{
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010093 uint32_t ret;
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010094
95 __asm __volatile("rev16 %w0, %w1\n"
96 : "=&r" (ret), "+r" (v));
97
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010098 return ((uint16_t)ret);
99}
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +0100100
101#ifdef __OPTIMIZE__
102
103#define __bswap32_constant(x) \
104 ((((x) & 0xff000000U) >> 24) | \
105 (((x) & 0x00ff0000U) >> 8) | \
106 (((x) & 0x0000ff00U) << 8) | \
107 (((x) & 0x000000ffU) << 24))
108
109#define __bswap16_constant(x) \
110 ((((x) & 0xff00) >> 8) | \
111 (((x) & 0x00ff) << 8))
112
113#define __bswap16(x) \
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +0100114 ((uint16_t)(__builtin_constant_p(x) ? \
115 __bswap16_constant((uint16_t)(x)) : \
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +0100116 __bswap16_var(x)))
117
118#define __bswap32(x) \
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +0100119 ((uint32_t)(__builtin_constant_p(x) ? \
120 __bswap32_constant((uint32_t)(x)) : \
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +0100121 __bswap32_var(x)))
122
123#else
124#define __bswap16(x) __bswap16_var(x)
125#define __bswap32(x) __bswap32_var(x)
126
127#endif /* __OPTIMIZE__ */
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000128#endif /* ENDIAN__H */