]> wimlib.net Git - wimlib/blob - include/wimlib/endianness.h
mount_image.c: add fallback definitions of RENAME_* constants
[wimlib] / include / wimlib / endianness.h
1 /*
2  * endianness.h - macros and inline functions for endianness conversion
3  *
4  * Copyright 2022 Eric Biggers
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #ifndef _WIMLIB_ENDIANNESS_H
29 #define _WIMLIB_ENDIANNESS_H
30
31 #include "wimlib/compiler.h"
32 #include "wimlib/types.h"
33
34 #ifdef HAVE_SYS_ENDIAN_H
35    /* Needed on NetBSD to stop system bswap macros from messing things up */
36 #  include <sys/endian.h>
37 #  undef bswap16
38 #  undef bswap32
39 #  undef bswap64
40 #endif
41
42 /* Watch out for conflict with ntfs-3g/endians.h ... */
43 #ifndef _NTFS_ENDIANS_H
44
45 #define bswap16_const(n)                        \
46         ((((u16)(n) & 0x00FF) << 8)     |       \
47          (((u16)(n) & 0xFF00) >> 8))
48
49 #define bswap32_const(n)                                \
50         ((((u32)(n) & 0x000000FF) << 24)        |       \
51          (((u32)(n) & 0x0000FF00) << 8)         |       \
52          (((u32)(n) & 0x00FF0000) >> 8)         |       \
53          (((u32)(n) & 0xFF000000) >> 24))
54
55 #define bswap64_const(n)                                        \
56         ((((u64)(n) & 0x00000000000000FF) << 56)        |       \
57          (((u64)(n) & 0x000000000000FF00) << 40)        |       \
58          (((u64)(n) & 0x0000000000FF0000) << 24)        |       \
59          (((u64)(n) & 0x00000000FF000000) << 8)         |       \
60          (((u64)(n) & 0x000000FF00000000) >> 8)         |       \
61          (((u64)(n) & 0x0000FF0000000000) >> 24)        |       \
62          (((u64)(n) & 0x00FF000000000000) >> 40)        |       \
63          (((u64)(n) & 0xFF00000000000000) >> 56))
64
65 static forceinline u16 do_bswap16(u16 n)
66 {
67 #if GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
68         return __builtin_bswap16(n);
69 #else
70         return bswap16_const(n);
71 #endif
72 }
73
74 static forceinline u32 do_bswap32(u32 n)
75 {
76 #if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
77         return __builtin_bswap32(n);
78 #else
79         return bswap32_const(n);
80 #endif
81 }
82
83 static forceinline u64 do_bswap64(u64 n)
84 {
85 #if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
86         return __builtin_bswap64(n);
87 #else
88         return bswap64_const(n);
89 #endif
90 }
91
92 #define bswap16(n) (__builtin_constant_p(n) ? bswap16_const(n) : do_bswap16(n))
93 #define bswap32(n) (__builtin_constant_p(n) ? bswap32_const(n) : do_bswap32(n))
94 #define bswap64(n) (__builtin_constant_p(n) ? bswap64_const(n) : do_bswap64(n))
95
96 #if CPU_IS_BIG_ENDIAN()
97 #  define cpu_to_le16(n) ((_force_attr le16)bswap16(n))
98 #  define cpu_to_le32(n) ((_force_attr le32)bswap32(n))
99 #  define cpu_to_le64(n) ((_force_attr le64)bswap64(n))
100 #  define le16_to_cpu(n) bswap16((_force_attr u16)(le16)(n))
101 #  define le32_to_cpu(n) bswap32((_force_attr u32)(le32)(n))
102 #  define le64_to_cpu(n) bswap64((_force_attr u64)(le64)(n))
103 #  define cpu_to_be16(n) ((_force_attr be16)(u16)(n))
104 #  define cpu_to_be32(n) ((_force_attr be32)(u32)(n))
105 #  define cpu_to_be64(n) ((_force_attr be64)(u64)(n))
106 #  define be16_to_cpu(n) ((_force_attr u16)(be16)(n))
107 #  define be32_to_cpu(n) ((_force_attr u32)(be32)(n))
108 #  define be64_to_cpu(n) ((_force_attr u64)(be64)(n))
109 #else
110 #  define cpu_to_le16(n) ((_force_attr le16)(u16)(n))
111 #  define cpu_to_le32(n) ((_force_attr le32)(u32)(n))
112 #  define cpu_to_le64(n) ((_force_attr le64)(u64)(n))
113 #  define le16_to_cpu(n) ((_force_attr u16)(le16)(n))
114 #  define le32_to_cpu(n) ((_force_attr u32)(le32)(n))
115 #  define le64_to_cpu(n) ((_force_attr u64)(le64)(n))
116 #  define cpu_to_be16(n) ((_force_attr be16)bswap16(n))
117 #  define cpu_to_be32(n) ((_force_attr be32)bswap32(n))
118 #  define cpu_to_be64(n) ((_force_attr be64)bswap64(n))
119 #  define be16_to_cpu(n) bswap16((_force_attr u16)(be16)(n))
120 #  define be32_to_cpu(n) bswap32((_force_attr u32)(be32)(n))
121 #  define be64_to_cpu(n) bswap64((_force_attr u64)(be64)(n))
122 #endif
123
124 #endif /* _NTFS_ENDIANS_H */
125 #endif /* _WIMLIB_ENDIANNESS_H */