xref: /dragonfly/sys/sys/endian.h (revision 631c21f2)
1 /*-
2  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
3  *
4  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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  * $FreeBSD: src/sys/sys/endian.h,v 1.2.2.1 2002/09/09 05:45:04 imp Exp $
29  */
30 
31 #ifndef _SYS_ENDIAN_H_
32 #define _SYS_ENDIAN_H_
33 
34 #include <sys/types.h>
35 #include <machine/endian.h>
36 
37 /*
38  * Define the order of 32-bit words in 64-bit words.
39  */
40 #if _BYTE_ORDER == _LITTLE_ENDIAN
41 #define	_QUAD_HIGHWORD	1
42 #define	_QUAD_LOWWORD	0
43 #endif
44 
45 #if _BYTE_ORDER == _BIG_ENDIAN
46 #define	_QUAD_HIGHWORD	0
47 #define	_QUAD_LOWWORD	1
48 #endif
49 
50 /*
51  * General byte order swapping functions.
52  */
53 #define	bswap16(x)	__bswap16(x)
54 #define	bswap32(x)	__bswap32(x)
55 #define	bswap64(x)	__bswap64(x)
56 
57 /*
58  * Host to big endian, host to little endian, big endian to host, and little
59  * endian to host byte order functions as detailed in byteorder(9).
60  */
61 #if _BYTE_ORDER == _LITTLE_ENDIAN
62 #define	htobe16(x)	__bswap16((x))
63 #define	htobe32(x)	__bswap32((x))
64 #define	htobe64(x)	__bswap64((x))
65 #define	htole16(x)	((__uint16_t)(x))
66 #define	htole32(x)	((__uint32_t)(x))
67 #define	htole64(x)	((__uint64_t)(x))
68 
69 #define	be16toh(x)	__bswap16((x))
70 #define	be32toh(x)	__bswap32((x))
71 #define	be64toh(x)	__bswap64((x))
72 #define	le16toh(x)	((__uint16_t)(x))
73 #define	le32toh(x)	((__uint32_t)(x))
74 #define	le64toh(x)	((__uint64_t)(x))
75 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
76 #define	htobe16(x)	((__uint16_t)(x))
77 #define	htobe32(x)	((__uint32_t)(x))
78 #define	htobe64(x)	((__uint64_t)(x))
79 #define	htole16(x)	__bswap16((x))
80 #define	htole32(x)	__bswap32((x))
81 #define	htole64(x)	__bswap64((x))
82 
83 #define	be16toh(x)	((__uint16_t)(x))
84 #define	be32toh(x)	((__uint32_t)(x))
85 #define	be64toh(x)	((__uint64_t)(x))
86 #define	le16toh(x)	__bswap16((x))
87 #define	le32toh(x)	__bswap32((x))
88 #define	le64toh(x)	__bswap64((x))
89 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
90 
91 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
92 
93 static __inline __uint16_t
94 be16dec(const void *pp)
95 {
96 	const __uint8_t *p = (const __uint8_t *)pp;
97 
98 	return ((p[0] << 8) | p[1]);
99 }
100 
101 static __inline __uint32_t
102 be32dec(const void *pp)
103 {
104 	const __uint8_t *p = (const __uint8_t *)pp;
105 
106 	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
107 }
108 
109 static __inline __uint64_t
110 be64dec(const void *pp)
111 {
112 	const __uint8_t *p = (const __uint8_t *)pp;
113 
114 	return (((__uint64_t)be32dec(p) << 32) | be32dec(p + 4));
115 }
116 
117 static __inline __uint16_t
118 le16dec(const void *pp)
119 {
120 	const __uint8_t *p = (const __uint8_t *)pp;
121 
122 	return ((p[1] << 8) | p[0]);
123 }
124 
125 static __inline __uint32_t
126 le32dec(const void *pp)
127 {
128 	const __uint8_t *p = (const __uint8_t *)pp;
129 
130 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
131 }
132 
133 static __inline __uint64_t
134 le64dec(const void *pp)
135 {
136 	const __uint8_t *p = (const __uint8_t *)pp;
137 
138 	return (((__uint64_t)le32dec(p + 4) << 32) | le32dec(p));
139 }
140 
141 static __inline void
142 be16enc(void *pp, __uint16_t u)
143 {
144 	__uint8_t *p = (__uint8_t *)pp;
145 
146 	p[0] = (u >> 8) & 0xff;
147 	p[1] = u & 0xff;
148 }
149 
150 static __inline void
151 be32enc(void *pp, __uint32_t u)
152 {
153 	__uint8_t *p = (__uint8_t *)pp;
154 
155 	p[0] = (u >> 24) & 0xff;
156 	p[1] = (u >> 16) & 0xff;
157 	p[2] = (u >> 8) & 0xff;
158 	p[3] = u & 0xff;
159 }
160 
161 static __inline void
162 be64enc(void *pp, __uint64_t u)
163 {
164 	__uint8_t *p = (__uint8_t *)pp;
165 
166 	be32enc(p, u >> 32);
167 	be32enc(p + 4, u & 0xffffffff);
168 }
169 
170 static __inline void
171 le16enc(void *pp, __uint16_t u)
172 {
173 	__uint8_t *p = (__uint8_t *)pp;
174 
175 	p[0] = u & 0xff;
176 	p[1] = (u >> 8) & 0xff;
177 }
178 
179 static __inline void
180 le32enc(void *pp, __uint32_t u)
181 {
182 	__uint8_t *p = (__uint8_t *)pp;
183 
184 	p[0] = u & 0xff;
185 	p[1] = (u >> 8) & 0xff;
186 	p[2] = (u >> 16) & 0xff;
187 	p[3] = (u >> 24) & 0xff;
188 }
189 
190 static __inline void
191 le64enc(void *pp, __uint64_t u)
192 {
193 	__uint8_t *p = (__uint8_t *)pp;
194 
195 	le32enc(p, u & 0xffffffff);
196 	le32enc(p + 4, u >> 32);
197 }
198 
199 #endif	/* _SYS_ENDIAN_H_ */
200