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