xref: /illumos-gate/usr/src/compat/bhyve/sys/endian.h (revision d0b3c59b)
1bf21cd93STycho Nightingale /*
2bf21cd93STycho Nightingale  * This file and its contents are supplied under the terms of the
3bf21cd93STycho Nightingale  * Common Development and Distribution License ("CDDL"), version 1.0.
4bf21cd93STycho Nightingale  * You may only use this file in accordance with the terms of version
5bf21cd93STycho Nightingale  * 1.0 of the CDDL.
6bf21cd93STycho Nightingale  *
7bf21cd93STycho Nightingale  * A full copy of the text of the CDDL should have accompanied this
8bf21cd93STycho Nightingale  * source.  A copy of the CDDL is also available via the Internet at
9bf21cd93STycho Nightingale  * http://www.illumos.org/license/CDDL.
10bf21cd93STycho Nightingale  */
11bf21cd93STycho Nightingale 
12bf21cd93STycho Nightingale /*
13bf21cd93STycho Nightingale  * Copyright 2014 Pluribus Networks Inc.
144c87aefeSPatrick Mooney  * Copyright 2018 Joyent, Inc.
15bf21cd93STycho Nightingale  */
16bf21cd93STycho Nightingale 
17bf21cd93STycho Nightingale #ifndef _COMPAT_FREEBSD_SYS_ENDIAN_H_
18bf21cd93STycho Nightingale #define	_COMPAT_FREEBSD_SYS_ENDIAN_H_
19bf21cd93STycho Nightingale 
20bf21cd93STycho Nightingale static __inline uint16_t
be16dec(const void * pp)21bf21cd93STycho Nightingale be16dec(const void *pp)
22bf21cd93STycho Nightingale {
23bf21cd93STycho Nightingale 	uint8_t const *p = (uint8_t const *)pp;
24bf21cd93STycho Nightingale 
25bf21cd93STycho Nightingale 	return ((p[0] << 8) | p[1]);
26bf21cd93STycho Nightingale }
27bf21cd93STycho Nightingale 
28bf21cd93STycho Nightingale static __inline uint32_t
be32dec(const void * pp)29bf21cd93STycho Nightingale be32dec(const void *pp)
30bf21cd93STycho Nightingale {
31bf21cd93STycho Nightingale 	uint8_t const *p = (uint8_t const *)pp;
32bf21cd93STycho Nightingale 
33bf21cd93STycho Nightingale 	return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
34bf21cd93STycho Nightingale }
35bf21cd93STycho Nightingale 
36bf21cd93STycho Nightingale static __inline uint64_t
be64dec(const void * pp)37bf21cd93STycho Nightingale be64dec(const void *pp)
38bf21cd93STycho Nightingale {
39bf21cd93STycho Nightingale 	uint8_t const *p = (uint8_t const *)pp;
40bf21cd93STycho Nightingale 
41bf21cd93STycho Nightingale 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
42bf21cd93STycho Nightingale }
43bf21cd93STycho Nightingale 
44bf21cd93STycho Nightingale static __inline uint16_t
le16dec(const void * pp)45bf21cd93STycho Nightingale le16dec(const void *pp)
46bf21cd93STycho Nightingale {
47bf21cd93STycho Nightingale 	uint8_t const *p = (uint8_t const *)pp;
48bf21cd93STycho Nightingale 
49bf21cd93STycho Nightingale 	return ((p[1] << 8) | p[0]);
50bf21cd93STycho Nightingale }
51bf21cd93STycho Nightingale 
52bf21cd93STycho Nightingale static __inline uint32_t
le32dec(const void * pp)53bf21cd93STycho Nightingale le32dec(const void *pp)
54bf21cd93STycho Nightingale {
55bf21cd93STycho Nightingale 	uint8_t const *p = (uint8_t const *)pp;
56bf21cd93STycho Nightingale 
57bf21cd93STycho Nightingale 	return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
58bf21cd93STycho Nightingale }
59bf21cd93STycho Nightingale 
60bf21cd93STycho Nightingale static __inline uint64_t
le64dec(const void * pp)61bf21cd93STycho Nightingale le64dec(const void *pp)
62bf21cd93STycho Nightingale {
63bf21cd93STycho Nightingale 	uint8_t const *p = (uint8_t const *)pp;
64bf21cd93STycho Nightingale 
65bf21cd93STycho Nightingale 	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
66bf21cd93STycho Nightingale }
67bf21cd93STycho Nightingale 
68bf21cd93STycho Nightingale static __inline void
be16enc(void * pp,uint16_t u)69bf21cd93STycho Nightingale be16enc(void *pp, uint16_t u)
70bf21cd93STycho Nightingale {
71bf21cd93STycho Nightingale 	uint8_t *p = (uint8_t *)pp;
72bf21cd93STycho Nightingale 
73bf21cd93STycho Nightingale 	p[0] = (u >> 8) & 0xff;
74bf21cd93STycho Nightingale 	p[1] = u & 0xff;
75bf21cd93STycho Nightingale }
76bf21cd93STycho Nightingale 
77bf21cd93STycho Nightingale static __inline void
be32enc(void * pp,uint32_t u)78bf21cd93STycho Nightingale be32enc(void *pp, uint32_t u)
79bf21cd93STycho Nightingale {
80bf21cd93STycho Nightingale 	uint8_t *p = (uint8_t *)pp;
81bf21cd93STycho Nightingale 
82bf21cd93STycho Nightingale 	p[0] = (u >> 24) & 0xff;
83bf21cd93STycho Nightingale 	p[1] = (u >> 16) & 0xff;
84bf21cd93STycho Nightingale 	p[2] = (u >> 8) & 0xff;
85bf21cd93STycho Nightingale 	p[3] = u & 0xff;
86bf21cd93STycho Nightingale }
87bf21cd93STycho Nightingale 
88bf21cd93STycho Nightingale static __inline void
be64enc(void * pp,uint64_t u)89bf21cd93STycho Nightingale be64enc(void *pp, uint64_t u)
90bf21cd93STycho Nightingale {
91bf21cd93STycho Nightingale 	uint8_t *p = (uint8_t *)pp;
92bf21cd93STycho Nightingale 
93bf21cd93STycho Nightingale 	be32enc(p, (uint32_t)(u >> 32));
94bf21cd93STycho Nightingale 	be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
95bf21cd93STycho Nightingale }
96bf21cd93STycho Nightingale 
97bf21cd93STycho Nightingale static __inline void
le16enc(void * pp,uint16_t u)98bf21cd93STycho Nightingale le16enc(void *pp, uint16_t u)
99bf21cd93STycho Nightingale {
100bf21cd93STycho Nightingale 	uint8_t *p = (uint8_t *)pp;
101bf21cd93STycho Nightingale 
102bf21cd93STycho Nightingale 	p[0] = u & 0xff;
103bf21cd93STycho Nightingale 	p[1] = (u >> 8) & 0xff;
104bf21cd93STycho Nightingale }
105bf21cd93STycho Nightingale 
106bf21cd93STycho Nightingale static __inline void
le32enc(void * pp,uint32_t u)107bf21cd93STycho Nightingale le32enc(void *pp, uint32_t u)
108bf21cd93STycho Nightingale {
109bf21cd93STycho Nightingale 	uint8_t *p = (uint8_t *)pp;
110bf21cd93STycho Nightingale 
111bf21cd93STycho Nightingale 	p[0] = u & 0xff;
112bf21cd93STycho Nightingale 	p[1] = (u >> 8) & 0xff;
113bf21cd93STycho Nightingale 	p[2] = (u >> 16) & 0xff;
114bf21cd93STycho Nightingale 	p[3] = (u >> 24) & 0xff;
115bf21cd93STycho Nightingale }
116bf21cd93STycho Nightingale 
117bf21cd93STycho Nightingale static __inline void
le64enc(void * pp,uint64_t u)118bf21cd93STycho Nightingale le64enc(void *pp, uint64_t u)
119bf21cd93STycho Nightingale {
120bf21cd93STycho Nightingale 	uint8_t *p = (uint8_t *)pp;
121bf21cd93STycho Nightingale 
122bf21cd93STycho Nightingale 	le32enc(p, (uint32_t)(u & 0xffffffffU));
123bf21cd93STycho Nightingale 	le32enc(p + 4, (uint32_t)(u >> 32));
124bf21cd93STycho Nightingale }
125bf21cd93STycho Nightingale 
1264c87aefeSPatrick Mooney #ifdef _LITTLE_ENDIAN
1274c87aefeSPatrick Mooney #define	htole16(x)	((uint16_t)(x))
1284c87aefeSPatrick Mooney #define	htole32(x)	((uint32_t)(x))
1294c87aefeSPatrick Mooney #define	htole64(x)	((uint64_t)(x))
1304c87aefeSPatrick Mooney 
1314c87aefeSPatrick Mooney #define	le16toh(x)	((uint16_t)(x))
1324c87aefeSPatrick Mooney #define	le32toh(x)	((uint32_t)(x))
1334c87aefeSPatrick Mooney #define	le64toh(x)	((uint64_t)(x))
1344c87aefeSPatrick Mooney #endif
1354c87aefeSPatrick Mooney 
136bf21cd93STycho Nightingale #endif	/* _COMPAT_FREEBSD_SYS_ENDIAN_H_ */
137