]> icculus.org git repositories - icculus/xz.git/blob - src/common/tuklib_integer.h
Use a tuklib module for integer handling.
[icculus/xz.git] / src / common / tuklib_integer.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       tuklib_integer.h
4 /// \brief      Byte swapping and endianness related macros and functions
5 ///
6 /// This file provides macros or functions to do basic endianness related
7 /// integer operations (XX = 16, 32, or 64; Y = b or l):
8 ///   - Byte swapping: bswapXX(num)
9 ///   - Byte order conversions to/from native: convXXYe(num)
10 ///   - Aligned reads: readXXYe(ptr)
11 ///   - Aligned writes: writeXXYe(ptr, num)
12 ///   - Unaligned reads (16/32-bit only): unaligned_readXXYe(ptr)
13 ///   - Unaligned writes (16/32-bit only): unaligned_writeXXYe(ptr, num)
14 ///
15 /// Since they can macros, the arguments should have no side effects since
16 /// they may be evaluated more than once.
17 ///
18 /// \todo       PowerPC and possibly some other architectures support
19 ///             byte swapping load and store instructions. This file
20 ///             doesn't take advantage of those instructions.
21 //
22 //  Author:     Lasse Collin
23 //
24 //  This file has been put into the public domain.
25 //  You can do whatever you want with this file.
26 //
27 ///////////////////////////////////////////////////////////////////////////////
28
29 #ifndef TUKLIB_INTEGER_H
30 #define TUKLIB_INTEGER_H
31
32 #include "tuklib_common.h"
33
34
35 ////////////////////////////////////////
36 // Operating system specific features //
37 ////////////////////////////////////////
38
39 #if defined(HAVE_BYTESWAP_H)
40         // glibc, uClibc, dietlibc
41 #       include <byteswap.h>
42 #       ifdef HAVE_BSWAP_16
43 #               define bswap16(num) bswap_16(num)
44 #       endif
45 #       ifdef HAVE_BSWAP_32
46 #               define bswap32(num) bswap_32(num)
47 #       endif
48 #       ifdef HAVE_BSWAP_64
49 #               define bswap64(num) bswap_64(num)
50 #       endif
51
52 #elif defined(HAVE_SYS_ENDIAN_H)
53         // *BSDs and Darwin
54 #       include <sys/endian.h>
55
56 #elif defined(HAVE_SYS_BYTEORDER_H)
57         // Solaris
58 #       include <sys/byteorder.h>
59 #       ifdef BSWAP_16
60 #               define bswap16(num) BSWAP_16(num)
61 #       endif
62 #       ifdef BSWAP_32
63 #               define bswap32(num) BSWAP_32(num)
64 #       endif
65 #       ifdef BSWAP_64
66 #               define bswap64(num) BSWAP_64(num)
67 #       endif
68 #       ifdef BE_16
69 #               define conv16be(num) BE_16(num)
70 #       endif
71 #       ifdef BE_32
72 #               define conv32be(num) BE_32(num)
73 #       endif
74 #       ifdef BE_64
75 #               define conv64be(num) BE_64(num)
76 #       endif
77 #       ifdef LE_16
78 #               define conv16le(num) LE_16(num)
79 #       endif
80 #       ifdef LE_32
81 #               define conv32le(num) LE_32(num)
82 #       endif
83 #       ifdef LE_64
84 #               define conv64le(num) LE_64(num)
85 #       endif
86 #endif
87
88
89 ///////////////////
90 // Byte swapping //
91 ///////////////////
92
93 #ifndef bswap16
94 #       define bswap16(num) \
95                 (((uint16_t)(num) << 8) | ((uint16_t)(num) >> 8))
96 #endif
97
98 #ifndef bswap32
99 #       define bswap32(num) \
100                 ( (((uint32_t)(num) << 24)                       ) \
101                 | (((uint32_t)(num) <<  8) & UINT32_C(0x00FF0000)) \
102                 | (((uint32_t)(num) >>  8) & UINT32_C(0x0000FF00)) \
103                 | (((uint32_t)(num) >> 24)                       ) )
104 #endif
105
106 #ifndef bswap64
107 #       define bswap64(num) \
108                 ( (((uint64_t)(num) << 56)                               ) \
109                 | (((uint64_t)(num) << 40) & UINT64_C(0x00FF000000000000)) \
110                 | (((uint64_t)(num) << 24) & UINT64_C(0x0000FF0000000000)) \
111                 | (((uint64_t)(num) <<  8) & UINT64_C(0x000000FF00000000)) \
112                 | (((uint64_t)(num) >>  8) & UINT64_C(0x00000000FF000000)) \
113                 | (((uint64_t)(num) >> 24) & UINT64_C(0x0000000000FF0000)) \
114                 | (((uint64_t)(num) >> 40) & UINT64_C(0x000000000000FF00)) \
115                 | (((uint64_t)(num) >> 56)                               ) )
116 #endif
117
118 // Define conversion macros using the basic byte swapping macros.
119 #ifdef WORDS_BIGENDIAN
120 #       ifndef conv16be
121 #               define conv16be(num) ((uint16_t)(num))
122 #       endif
123 #       ifndef conv32be
124 #               define conv32be(num) ((uint32_t)(num))
125 #       endif
126 #       ifndef conv64be
127 #               define conv64be(num) ((uint64_t)(num))
128 #       endif
129 #       ifndef conv16le
130 #               define conv16le(num) bswap16(num)
131 #       endif
132 #       ifndef conv32le
133 #               define conv32le(num) bswap32(num)
134 #       endif
135 #       ifndef conv64le
136 #               define conv64le(num) bswap64(num)
137 #       endif
138 #else
139 #       ifndef conv16be
140 #               define conv16be(num) bswap16(num)
141 #       endif
142 #       ifndef conv32be
143 #               define conv32be(num) bswap32(num)
144 #       endif
145 #       ifndef conv64be
146 #               define conv64be(num) bswap64(num)
147 #       endif
148 #       ifndef conv16le
149 #               define conv16le(num) ((uint16_t)(num))
150 #       endif
151 #       ifndef conv32le
152 #               define conv32le(num) ((uint32_t)(num))
153 #       endif
154 #       ifndef conv64le
155 #               define conv64le(num) ((uint64_t)(num))
156 #       endif
157 #endif
158
159
160 //////////////////////////////
161 // Aligned reads and writes //
162 //////////////////////////////
163
164 static inline uint16_t
165 read16be(const uint8_t *buf)
166 {
167         uint16_t num = *(const uint16_t *)buf;
168         return conv16be(num);
169 }
170
171
172 static inline uint16_t
173 read16le(const uint8_t *buf)
174 {
175         uint16_t num = *(const uint16_t *)buf;
176         return conv16le(num);
177 }
178
179
180 static inline uint32_t
181 read32be(const uint8_t *buf)
182 {
183         uint32_t num = *(const uint32_t *)buf;
184         return conv32be(num);
185 }
186
187
188 static inline uint32_t
189 read32le(const uint8_t *buf)
190 {
191         uint32_t num = *(const uint32_t *)buf;
192         return conv32le(num);
193 }
194
195
196 static inline uint64_t
197 read64be(const uint8_t *buf)
198 {
199         uint64_t num = *(const uint64_t *)buf;
200         return conv64be(num);
201 }
202
203
204 static inline uint64_t
205 read64le(const uint8_t *buf)
206 {
207         uint64_t num = *(const uint64_t *)buf;
208         return conv64le(num);
209 }
210
211
212 // NOTE: Possible byte swapping must be done in a macro to allow GCC
213 // to optimize byte swapping of constants when using glibc's or *BSD's
214 // byte swapping macros. The actual write is done in an inline function
215 // to make type checking of the buf pointer possible similarly to readXXYe()
216 // functions. This also seems to silence a probably bogus GCC warning about
217 // strict aliasing when buf points to the beginning of an uint8_t array.
218
219 #define write16be(buf, num) write16ne((buf), conv16be(num))
220 #define write16le(buf, num) write16ne((buf), conv16le(num))
221 #define write32be(buf, num) write32ne((buf), conv32be(num))
222 #define write32le(buf, num) write32ne((buf), conv32le(num))
223 #define write64be(buf, num) write64ne((buf), conv64be(num))
224 #define write64le(buf, num) write64ne((buf), conv64le(num))
225
226
227 static inline void
228 write16ne(uint8_t *buf, uint16_t num)
229 {
230         *(uint16_t *)buf = num;
231         return;
232 }
233
234
235 static inline void
236 write32ne(uint8_t *buf, uint32_t num)
237 {
238         *(uint32_t *)buf = num;
239         return;
240 }
241
242
243 static inline void
244 write64ne(uint8_t *buf, uint64_t num)
245 {
246         *(uint64_t *)buf = num;
247         return;
248 }
249
250
251 ////////////////////////////////
252 // Unaligned reads and writes //
253 ////////////////////////////////
254
255 // NOTE: TUKLIB_FAST_UNALIGNED_ACCESS indicates only support for 16-bit and
256 // 32-bit unaligned integer loads and stores. It's possible that 64-bit
257 // unaligned access doesn't work or is slower than byte-by-byte access.
258 // Since unaligned 64-bit is probably not needed as often as 16-bit or
259 // 32-bit, we simply don't support 64-bit unaligned access for now.
260 #ifdef TUKLIB_FAST_UNALIGNED_ACCESS
261 #       define unaligned_read16be read16be
262 #       define unaligned_read16le read16le
263 #       define unaligned_read32be read32be
264 #       define unaligned_read32le read32le
265 #       define unaligned_write16be write16be
266 #       define unaligned_write16le write16le
267 #       define unaligned_write32be write32be
268 #       define unaligned_write32le write32le
269
270 #else
271
272 static inline uint16_t
273 unaligned_read16be(const uint8_t *buf)
274 {
275         uint16_t num = ((uint16_t)buf[0] << 8) | buf[1];
276         return num;
277 }
278
279
280 static inline uint16_t
281 unaligned_read16le(const uint8_t *buf)
282 {
283         uint16_t num = ((uint32_t)buf[0]) | ((uint16_t)buf[1] << 8);
284         return num;
285 }
286
287
288 static inline uint32_t
289 unaligned_read32be(const uint8_t *buf)
290 {
291         uint32_t num = (uint32_t)buf[0] << 24;
292         num |= (uint32_t)buf[1] << 16;
293         num |= (uint32_t)buf[2] << 8;
294         num |= (uint32_t)buf[3];
295         return num;
296 }
297
298
299 static inline uint32_t
300 unaligned_read32le(const uint8_t *buf)
301 {
302         uint32_t num = (uint32_t)buf[0];
303         num |= (uint32_t)buf[1] << 8;
304         num |= (uint32_t)buf[2] << 16;
305         num |= (uint32_t)buf[3] << 24;
306         return num;
307 }
308
309
310 static inline void
311 unaligned_write16be(uint8_t *buf, uint16_t num)
312 {
313         buf[0] = num >> 8;
314         buf[1] = num;
315         return;
316 }
317
318
319 static inline void
320 unaligned_write16le(uint8_t *buf, uint16_t num)
321 {
322         buf[0] = num;
323         buf[1] = num >> 8;
324         return;
325 }
326
327
328 static inline void
329 unaligned_write32be(uint8_t *buf, uint32_t num)
330 {
331         buf[0] = num >> 24;
332         buf[1] = num >> 16;
333         buf[2] = num >> 8;
334         buf[3] = num;
335         return;
336 }
337
338
339 static inline void
340 unaligned_write32le(uint8_t *buf, uint32_t num)
341 {
342         buf[0] = num;
343         buf[1] = num >> 8;
344         buf[2] = num >> 16;
345         buf[3] = num >> 24;
346         return;
347 }
348
349 #endif
350 #endif