]> wimlib.net Git - wimlib/blob - src/endianness.h
Empty file fix
[wimlib] / src / endianness.h
1 /*
2  * endianness.h
3  *
4  * Copyright (C) 2012 Eric Biggers
5  *
6  * wimlib - Library for working with WIM files 
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation; either version 2.1 of the License, or (at your option) any
11  * later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License along
18  * with this library; if not, write to the Free Software Foundation, Inc., 59
19  * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
20  */
21
22 #ifndef _WIMLIB_ENDIANNESS_H
23 #define _WIMLIB_ENDIANNESS_H
24
25
26 #include "config.h"
27 #include <inttypes.h>
28
29 /* Changes the endianness of a 32-bit value. */
30 static inline uint32_t bswap32(uint32_t n)
31 {
32 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
33         return __builtin_bswap32(n);
34 #else
35         return (n << 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8) | 
36                                                         (n >> 24);
37 #endif
38 }
39
40 #ifdef WORDS_BIGENDIAN
41
42 /* Big endian. */
43
44 /* Changes the endianness of a 16-bit value. */
45 static inline uint16_t bswap16(uint16_t n)
46 {
47         return (n << 8) | (n >> 8);
48 }
49
50
51 /* Changes the endianness of a 64-bit value. */
52 static inline uint64_t bswap64(uint64_t n)
53 {
54 #ifdef __GNUC__
55         return __builtin_bswap64(n);
56 #else
57         return (n << 56) | ((n & 0xff00) << 40) | ((n & 0xff0000) << 24) | 
58                         ((n & 0xff000000) << 8) | ((n & 0xff00000000) >> 8) | 
59                         ((n & 0xff0000000000) >> 24) | 
60                         ((n & 0xff000000000000) >> 40) | (n >> 56);
61 #endif
62 }
63
64 /* Not in place */
65 #define to_le16(n) bswap16(n)
66 #define to_le32(n) bswap32(n)
67 #define to_le64(n) bswap64(n)
68 #define to_be16(n) (n)
69 #define to_be32(n) (n)
70 #define to_be64(n) (n)
71
72 /* In place */
73 #define TO_LE16(n) (n = to_le16(n))
74 #define TO_LE32(n) (n = to_le32(n))
75 #define TO_LE64(n) (n = to_le64(n))
76
77 static inline void array_to_le16(uint16_t *p, uint64_t n)
78 {
79         while (n--)
80                 *p++ = to_le16(*p);
81 }
82 static inline void array_to_le32(uint32_t *p, uint64_t n)
83 {
84         while (n--)
85                 *p++ = to_le32(*p);
86 }
87 static inline void array_to_le64(uint64_t *p, uint64_t n)
88 {
89         while (n--)
90                 *p++ = to_le64(*p);
91 }
92
93 #else
94
95 /* Little endian. */
96
97 /* Not in place */
98 #define to_le16(n) (n)
99 #define to_le32(n) (n)
100 #define to_le64(n) (n)
101
102 #define to_be16(n) (bswap16(n))
103 #define to_be32(n) (bswap32(n))
104 #define to_be64(n) (bswap64(n))
105
106 /* In place */
107 #define TO_LE16(n)
108 #define TO_LE32(n)
109 #define TO_LE64(n)
110
111 #define array_to_le16(p, n)
112 #define array_to_le32(p, n)
113 #define array_to_le64(p, n)
114
115 #endif
116
117
118 #endif /* _WIMLIB_ENDIANNESS_H */