endian.h (82725ba9bf1fd59746a4006a06f24d4d61d142f2) endian.h (720dc6bcb5a8c4283802576e2ef54f42b33fa8d4)
1/*-
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:

--- 18 unchanged lines hidden (view full) ---

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 */
34
1/*-
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:

--- 18 unchanged lines hidden (view full) ---

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 */
34
35#ifndef _ENDIAN_H_
36#define _ENDIAN_H_
35#ifndef _MACHINE_ENDIAN_H_
36#define _MACHINE_ENDIAN_H_
37
38#include <sys/_types.h>
37
38#include <sys/_types.h>
39#include <sys/_endian.h>
39
40
40/*
41 * Definitions for byte order, according to byte significance from low
42 * address to high.
43 */
44#define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
45#define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
46#define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
47
48#ifdef __ARMEB__
49#define _BYTE_ORDER _BIG_ENDIAN
50#else
51#define _BYTE_ORDER _LITTLE_ENDIAN
52#endif /* __ARMEB__ */
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#ifdef __ARMEB__
62#define _QUAD_HIGHWORD 0
63#define _QUAD_LOWWORD 1
64#define __ntohl(x) ((__uint32_t)(x))
65#define __ntohs(x) ((__uint16_t)(x))
66#define __htonl(x) ((__uint32_t)(x))
67#define __htons(x) ((__uint16_t)(x))
68#else
69#define _QUAD_HIGHWORD 1
70#define _QUAD_LOWWORD 0
71#define __ntohl(x) (__bswap32(x))
72#define __ntohs(x) (__bswap16(x))
73#define __htonl(x) (__bswap32(x))
74#define __htons(x) (__bswap16(x))
75#endif /* __ARMEB__ */
76
77static __inline __uint64_t
78__bswap64(__uint64_t _x)
79{
80
81 return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
82 ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
83 ((_x << 24) & ((__uint64_t)0xff << 40)) |
84 ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
85}
86
87static __inline __uint32_t
88__bswap32_var(__uint32_t v)
89{
90 __uint32_t t1;
91
92 __asm __volatile("eor %1, %0, %0, ror #16\n"
93 "bic %1, %1, #0x00ff0000\n"
94 "mov %0, %0, ror #8\n"
95 "eor %0, %0, %1, lsr #8\n"
96 : "+r" (v), "=r" (t1));
97
98 return (v);
99}
100
101static __inline __uint16_t
102__bswap16_var(__uint16_t v)
103{
104 __uint32_t ret = v & 0xffff;
105
106 __asm __volatile(
107 "mov %0, %0, ror #8\n"
108 "orr %0, %0, %0, lsr #16\n"
109 "bic %0, %0, %0, lsl #16"
110 : "+r" (ret));
111
112 return ((__uint16_t)ret);
113}
114
115#ifdef __OPTIMIZE__
116
117#define __bswap32_constant(x) \
118 ((((x) & 0xff000000U) >> 24) | \
119 (((x) & 0x00ff0000U) >> 8) | \
120 (((x) & 0x0000ff00U) << 8) | \
121 (((x) & 0x000000ffU) << 24))
122
123#define __bswap16_constant(x) \
124 ((((x) & 0xff00) >> 8) | \
125 (((x) & 0x00ff) << 8))
126
127#define __bswap16(x) \
128 ((__uint16_t)(__builtin_constant_p(x) ? \
129 __bswap16_constant(x) : \
130 __bswap16_var(x)))
131
132#define __bswap32(x) \
133 ((__uint32_t)(__builtin_constant_p(x) ? \
134 __bswap32_constant(x) : \
135 __bswap32_var(x)))
136
137#else
138#define __bswap16(x) __bswap16_var(x)
139#define __bswap32(x) __bswap32_var(x)
140
141#endif /* __OPTIMIZE__ */
142#endif /* !_ENDIAN_H_ */
41#endif /* !_MACHINE_ENDIAN_H_ */