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