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