blob: 0cf2c755f2836bf9c2fcdfa6d80ae5ace2770b6e [file] [log] [blame]
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +01001/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * 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#ifdef __ARMEB__
53#define _BYTE_ORDER _BIG_ENDIAN
54#else
55#define _BYTE_ORDER _LITTLE_ENDIAN
56#endif /* __ARMEB__ */
57
58#if __BSD_VISIBLE
59#define LITTLE_ENDIAN _LITTLE_ENDIAN
60#define BIG_ENDIAN _BIG_ENDIAN
61#define PDP_ENDIAN _PDP_ENDIAN
62#define BYTE_ORDER _BYTE_ORDER
63#endif
64
65#ifdef __ARMEB__
66#define _QUAD_HIGHWORD 0
67#define _QUAD_LOWWORD 1
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010068#define __ntohl(x) ((uint32_t)(x))
69#define __ntohs(x) ((uint16_t)(x))
70#define __htonl(x) ((uint32_t)(x))
71#define __htons(x) ((uint16_t)(x))
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010072#else
73#define _QUAD_HIGHWORD 1
74#define _QUAD_LOWWORD 0
75#define __ntohl(x) (__bswap32(x))
76#define __ntohs(x) (__bswap16(x))
77#define __htonl(x) (__bswap32(x))
78#define __htons(x) (__bswap16(x))
79#endif /* __ARMEB__ */
80
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010081static __inline uint64_t
82__bswap64(uint64_t _x)
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010083{
84
85 return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010086 ((_x >> 8) & 0xff000000) | ((_x << 8) & ((uint64_t)0xff << 32)) |
87 ((_x << 24) & ((uint64_t)0xff << 40)) |
88 ((_x << 40) & ((uint64_t)0xff << 48)) | ((_x << 56)));
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010089}
90
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010091static __inline uint32_t
92__bswap32_var(uint32_t v)
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010093{
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +010094 uint32_t t1;
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +010095
96 __asm __volatile("eor %1, %0, %0, ror #16\n"
97 "bic %1, %1, #0x00ff0000\n"
98 "mov %0, %0, ror #8\n"
99 "eor %0, %0, %1, lsr #8\n"
100 : "+r" (v), "=r" (t1));
101
102 return (v);
103}
104
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +0100105static __inline uint16_t
106__bswap16_var(uint16_t v)
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +0100107{
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +0100108 uint32_t ret = v & 0xffff;
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +0100109
110 __asm __volatile(
111 "mov %0, %0, ror #8\n"
112 "orr %0, %0, %0, lsr #16\n"
113 "bic %0, %0, %0, lsl #16"
114 : "+r" (ret));
115
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +0100116 return ((uint16_t)ret);
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +0100117}
118
119#ifdef __OPTIMIZE__
120
121#define __bswap32_constant(x) \
122 ((((x) & 0xff000000U) >> 24) | \
123 (((x) & 0x00ff0000U) >> 8) | \
124 (((x) & 0x0000ff00U) << 8) | \
125 (((x) & 0x000000ffU) << 24))
126
127#define __bswap16_constant(x) \
128 ((((x) & 0xff00) >> 8) | \
129 (((x) & 0x00ff) << 8))
130
131#define __bswap16(x) \
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +0100132 ((uint16_t)(__builtin_constant_p(x) ? \
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +0100133 __bswap16_constant(x) : \
134 __bswap16_var(x)))
135
136#define __bswap32(x) \
Antonio Nino Diaza5d668f2018-08-13 19:41:17 +0100137 ((uint32_t)(__builtin_constant_p(x) ? \
Antonio Nino Diaz7a7d63f2018-08-13 19:39:40 +0100138 __bswap32_constant(x) : \
139 __bswap32_var(x)))
140
141#else
142#define __bswap16(x) __bswap16_var(x)
143#define __bswap32(x) __bswap32_var(x)
144
145#endif /* __OPTIMIZE__ */
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000146#endif /* ENDIAN__H */