xref: /illumos-gate/usr/src/boot/sys/sys/endian.h (revision 199767f8)
1*199767f8SToomas Soome /*-
2*199767f8SToomas Soome  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
3*199767f8SToomas Soome  * All rights reserved.
4*199767f8SToomas Soome  *
5*199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
6*199767f8SToomas Soome  * modification, are permitted provided that the following conditions
7*199767f8SToomas Soome  * are met:
8*199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9*199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10*199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11*199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12*199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
13*199767f8SToomas Soome  *
14*199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*199767f8SToomas Soome  * SUCH DAMAGE.
25*199767f8SToomas Soome  *
26*199767f8SToomas Soome  * $FreeBSD$
27*199767f8SToomas Soome  */
28*199767f8SToomas Soome 
29*199767f8SToomas Soome #ifndef _SYS_ENDIAN_H_
30*199767f8SToomas Soome #define _SYS_ENDIAN_H_
31*199767f8SToomas Soome 
32*199767f8SToomas Soome #include <sys/cdefs.h>
33*199767f8SToomas Soome #include <sys/_types.h>
34*199767f8SToomas Soome #include <machine/endian.h>
35*199767f8SToomas Soome 
36*199767f8SToomas Soome #ifndef _UINT8_T_DECLARED
37*199767f8SToomas Soome typedef	__uint8_t	uint8_t;
38*199767f8SToomas Soome #define	_UINT8_T_DECLARED
39*199767f8SToomas Soome #endif
40*199767f8SToomas Soome 
41*199767f8SToomas Soome #ifndef _UINT16_T_DECLARED
42*199767f8SToomas Soome typedef	__uint16_t	uint16_t;
43*199767f8SToomas Soome #define	_UINT16_T_DECLARED
44*199767f8SToomas Soome #endif
45*199767f8SToomas Soome 
46*199767f8SToomas Soome #ifndef _UINT32_T_DECLARED
47*199767f8SToomas Soome typedef	__uint32_t	uint32_t;
48*199767f8SToomas Soome #define	_UINT32_T_DECLARED
49*199767f8SToomas Soome #endif
50*199767f8SToomas Soome 
51*199767f8SToomas Soome #ifndef _UINT64_T_DECLARED
52*199767f8SToomas Soome typedef	__uint64_t	uint64_t;
53*199767f8SToomas Soome #define	_UINT64_T_DECLARED
54*199767f8SToomas Soome #endif
55*199767f8SToomas Soome 
56*199767f8SToomas Soome /*
57*199767f8SToomas Soome  * General byte order swapping functions.
58*199767f8SToomas Soome  */
59*199767f8SToomas Soome #define	bswap16(x)	__bswap16(x)
60*199767f8SToomas Soome #define	bswap32(x)	__bswap32(x)
61*199767f8SToomas Soome #define	bswap64(x)	__bswap64(x)
62*199767f8SToomas Soome 
63*199767f8SToomas Soome /*
64*199767f8SToomas Soome  * Host to big endian, host to little endian, big endian to host, and little
65*199767f8SToomas Soome  * endian to host byte order functions as detailed in byteorder(9).
66*199767f8SToomas Soome  */
67*199767f8SToomas Soome #if _BYTE_ORDER == _LITTLE_ENDIAN
68*199767f8SToomas Soome #define	htobe16(x)	bswap16((x))
69*199767f8SToomas Soome #define	htobe32(x)	bswap32((x))
70*199767f8SToomas Soome #define	htobe64(x)	bswap64((x))
71*199767f8SToomas Soome #define	htole16(x)	((uint16_t)(x))
72*199767f8SToomas Soome #define	htole32(x)	((uint32_t)(x))
73*199767f8SToomas Soome #define	htole64(x)	((uint64_t)(x))
74*199767f8SToomas Soome 
75*199767f8SToomas Soome #define	be16toh(x)	bswap16((x))
76*199767f8SToomas Soome #define	be32toh(x)	bswap32((x))
77*199767f8SToomas Soome #define	be64toh(x)	bswap64((x))
78*199767f8SToomas Soome #define	le16toh(x)	((uint16_t)(x))
79*199767f8SToomas Soome #define	le32toh(x)	((uint32_t)(x))
80*199767f8SToomas Soome #define	le64toh(x)	((uint64_t)(x))
81*199767f8SToomas Soome #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
82*199767f8SToomas Soome #define	htobe16(x)	((uint16_t)(x))
83*199767f8SToomas Soome #define	htobe32(x)	((uint32_t)(x))
84*199767f8SToomas Soome #define	htobe64(x)	((uint64_t)(x))
85*199767f8SToomas Soome #define	htole16(x)	bswap16((x))
86*199767f8SToomas Soome #define	htole32(x)	bswap32((x))
87*199767f8SToomas Soome #define	htole64(x)	bswap64((x))
88*199767f8SToomas Soome 
89*199767f8SToomas Soome #define	be16toh(x)	((uint16_t)(x))
90*199767f8SToomas Soome #define	be32toh(x)	((uint32_t)(x))
91*199767f8SToomas Soome #define	be64toh(x)	((uint64_t)(x))
92*199767f8SToomas Soome #define	le16toh(x)	bswap16((x))
93*199767f8SToomas Soome #define	le32toh(x)	bswap32((x))
94*199767f8SToomas Soome #define	le64toh(x)	bswap64((x))
95*199767f8SToomas Soome #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
96*199767f8SToomas Soome 
97*199767f8SToomas Soome /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
98*199767f8SToomas Soome 
99*199767f8SToomas Soome static __inline uint16_t
be16dec(const void * pp)100*199767f8SToomas Soome be16dec(const void *pp)
101*199767f8SToomas Soome {
102*199767f8SToomas Soome 	uint8_t const *p = (uint8_t const *)pp;
103*199767f8SToomas Soome 
104*199767f8SToomas Soome 	return ((p[0] << 8) | p[1]);
105*199767f8SToomas Soome }
106*199767f8SToomas Soome 
107*199767f8SToomas Soome static __inline uint32_t
be32dec(const void * pp)108*199767f8SToomas Soome be32dec(const void *pp)
109*199767f8SToomas Soome {
110*199767f8SToomas Soome 	uint8_t const *p = (uint8_t const *)pp;
111*199767f8SToomas Soome 
112*199767f8SToomas Soome 	return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
113*199767f8SToomas Soome }
114*199767f8SToomas Soome 
115*199767f8SToomas Soome static __inline uint64_t
be64dec(const void * pp)116*199767f8SToomas Soome be64dec(const void *pp)
117*199767f8SToomas Soome {
118*199767f8SToomas Soome 	uint8_t const *p = (uint8_t const *)pp;
119*199767f8SToomas Soome 
120*199767f8SToomas Soome 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
121*199767f8SToomas Soome }
122*199767f8SToomas Soome 
123*199767f8SToomas Soome static __inline uint16_t
le16dec(const void * pp)124*199767f8SToomas Soome le16dec(const void *pp)
125*199767f8SToomas Soome {
126*199767f8SToomas Soome 	uint8_t const *p = (uint8_t const *)pp;
127*199767f8SToomas Soome 
128*199767f8SToomas Soome 	return ((p[1] << 8) | p[0]);
129*199767f8SToomas Soome }
130*199767f8SToomas Soome 
131*199767f8SToomas Soome static __inline uint32_t
le32dec(const void * pp)132*199767f8SToomas Soome le32dec(const void *pp)
133*199767f8SToomas Soome {
134*199767f8SToomas Soome 	uint8_t const *p = (uint8_t const *)pp;
135*199767f8SToomas Soome 
136*199767f8SToomas Soome 	return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
137*199767f8SToomas Soome }
138*199767f8SToomas Soome 
139*199767f8SToomas Soome static __inline uint64_t
le64dec(const void * pp)140*199767f8SToomas Soome le64dec(const void *pp)
141*199767f8SToomas Soome {
142*199767f8SToomas Soome 	uint8_t const *p = (uint8_t const *)pp;
143*199767f8SToomas Soome 
144*199767f8SToomas Soome 	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
145*199767f8SToomas Soome }
146*199767f8SToomas Soome 
147*199767f8SToomas Soome static __inline void
be16enc(void * pp,uint16_t u)148*199767f8SToomas Soome be16enc(void *pp, uint16_t u)
149*199767f8SToomas Soome {
150*199767f8SToomas Soome 	uint8_t *p = (uint8_t *)pp;
151*199767f8SToomas Soome 
152*199767f8SToomas Soome 	p[0] = (u >> 8) & 0xff;
153*199767f8SToomas Soome 	p[1] = u & 0xff;
154*199767f8SToomas Soome }
155*199767f8SToomas Soome 
156*199767f8SToomas Soome static __inline void
be32enc(void * pp,uint32_t u)157*199767f8SToomas Soome be32enc(void *pp, uint32_t u)
158*199767f8SToomas Soome {
159*199767f8SToomas Soome 	uint8_t *p = (uint8_t *)pp;
160*199767f8SToomas Soome 
161*199767f8SToomas Soome 	p[0] = (u >> 24) & 0xff;
162*199767f8SToomas Soome 	p[1] = (u >> 16) & 0xff;
163*199767f8SToomas Soome 	p[2] = (u >> 8) & 0xff;
164*199767f8SToomas Soome 	p[3] = u & 0xff;
165*199767f8SToomas Soome }
166*199767f8SToomas Soome 
167*199767f8SToomas Soome static __inline void
be64enc(void * pp,uint64_t u)168*199767f8SToomas Soome be64enc(void *pp, uint64_t u)
169*199767f8SToomas Soome {
170*199767f8SToomas Soome 	uint8_t *p = (uint8_t *)pp;
171*199767f8SToomas Soome 
172*199767f8SToomas Soome 	be32enc(p, (uint32_t)(u >> 32));
173*199767f8SToomas Soome 	be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
174*199767f8SToomas Soome }
175*199767f8SToomas Soome 
176*199767f8SToomas Soome static __inline void
le16enc(void * pp,uint16_t u)177*199767f8SToomas Soome le16enc(void *pp, uint16_t u)
178*199767f8SToomas Soome {
179*199767f8SToomas Soome 	uint8_t *p = (uint8_t *)pp;
180*199767f8SToomas Soome 
181*199767f8SToomas Soome 	p[0] = u & 0xff;
182*199767f8SToomas Soome 	p[1] = (u >> 8) & 0xff;
183*199767f8SToomas Soome }
184*199767f8SToomas Soome 
185*199767f8SToomas Soome static __inline void
le32enc(void * pp,uint32_t u)186*199767f8SToomas Soome le32enc(void *pp, uint32_t u)
187*199767f8SToomas Soome {
188*199767f8SToomas Soome 	uint8_t *p = (uint8_t *)pp;
189*199767f8SToomas Soome 
190*199767f8SToomas Soome 	p[0] = u & 0xff;
191*199767f8SToomas Soome 	p[1] = (u >> 8) & 0xff;
192*199767f8SToomas Soome 	p[2] = (u >> 16) & 0xff;
193*199767f8SToomas Soome 	p[3] = (u >> 24) & 0xff;
194*199767f8SToomas Soome }
195*199767f8SToomas Soome 
196*199767f8SToomas Soome static __inline void
le64enc(void * pp,uint64_t u)197*199767f8SToomas Soome le64enc(void *pp, uint64_t u)
198*199767f8SToomas Soome {
199*199767f8SToomas Soome 	uint8_t *p = (uint8_t *)pp;
200*199767f8SToomas Soome 
201*199767f8SToomas Soome 	le32enc(p, (uint32_t)(u & 0xffffffffU));
202*199767f8SToomas Soome 	le32enc(p + 4, (uint32_t)(u >> 32));
203*199767f8SToomas Soome }
204*199767f8SToomas Soome 
205*199767f8SToomas Soome #endif	/* _SYS_ENDIAN_H_ */
206