4 * Copyright (C) 2012 Eric Biggers
6 * wimlib - Library for working with WIM files
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
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.
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
22 #ifndef _WIMLIB_ENDIANNESS_H
23 #define _WIMLIB_ENDIANNESS_H
29 /* Changes the endianness of a 32-bit value. */
30 static inline uint32_t bswap32(uint32_t n)
33 return __builtin_bswap32(n);
35 return (n << 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8) |
40 #ifdef WORDS_BIGENDIAN
44 /* Changes the endianness of a 16-bit value. */
45 static inline uint16_t bswap16(uint16_t n)
47 return (n << 8) | (n >> 8);
51 /* Changes the endianness of a 64-bit value. */
52 static inline uint64_t bswap64(uint64_t n)
55 return __builtin_bswap64(n);
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);
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)
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))
77 static inline void array_to_le16(uint16_t *p, uint64_t n)
82 static inline void array_to_le32(uint32_t *p, uint64_t n)
87 static inline void array_to_le64(uint64_t *p, uint64_t n)
98 #define to_le16(n) (n)
99 #define to_le32(n) (n)
100 #define to_le64(n) (n)
102 #define to_be16(n) (bswap16(n))
103 #define to_be32(n) (bswap32(n))
104 #define to_be64(n) (bswap64(n))
111 #define array_to_le16(p, n)
112 #define array_to_le32(p, n)
113 #define array_to_le64(p, n)
118 #endif /* _WIMLIB_ENDIANNESS_H */