3bd6d542b2b1f05b03da6d877fd5a32dcda0b850
[wimlib] / include / wimlib / unaligned.h
1 /*
2  * unaligned.h
3  *
4  * Inline functions for unaligned memory accesses.
5  *
6  * Author:      Eric Biggers
7  * Year:        2014, 2015
8  *
9  * The author dedicates this file to the public domain.
10  * You can do whatever you want with this file.
11  */
12
13 #ifndef _WIMLIB_UNALIGNED_H
14 #define _WIMLIB_UNALIGNED_H
15
16 #include "wimlib/compiler.h"
17 #include "wimlib/endianness.h"
18 #include "wimlib/types.h"
19
20 #define DEFINE_UNALIGNED_TYPE(type)                             \
21 struct type##_unaligned {                                       \
22         type v;                                                 \
23 } _packed_attribute;                                            \
24                                                                 \
25 static inline type                                              \
26 load_##type##_unaligned(const void *p)                          \
27 {                                                               \
28         return ((const struct type##_unaligned *)p)->v;         \
29 }                                                               \
30                                                                 \
31 static inline void                                              \
32 store_##type##_unaligned(type val, void *p)                     \
33 {                                                               \
34         ((struct type##_unaligned *)p)->v = val;                \
35 }
36
37 DEFINE_UNALIGNED_TYPE(u16);
38 DEFINE_UNALIGNED_TYPE(u32);
39 DEFINE_UNALIGNED_TYPE(u64);
40 DEFINE_UNALIGNED_TYPE(le16);
41 DEFINE_UNALIGNED_TYPE(le32);
42 DEFINE_UNALIGNED_TYPE(le64);
43 DEFINE_UNALIGNED_TYPE(be16);
44 DEFINE_UNALIGNED_TYPE(be32);
45 DEFINE_UNALIGNED_TYPE(be64);
46 DEFINE_UNALIGNED_TYPE(size_t);
47 DEFINE_UNALIGNED_TYPE(machine_word_t);
48
49 #define load_word_unaligned     load_machine_word_t_unaligned
50 #define store_word_unaligned    store_machine_word_t_unaligned
51
52 static inline u16
53 get_unaligned_le16(const void *p)
54 {
55         u16 v;
56
57         if (UNALIGNED_ACCESS_IS_FAST) {
58                 v = le16_to_cpu(load_le16_unaligned(p));
59         } else {
60                 const u8 *p8 = p;
61                 v = 0;
62                 v |= (u16)p8[0] << 0;
63                 v |= (u16)p8[1] << 8;
64         }
65         return v;
66 }
67
68 static inline u32
69 get_unaligned_le32(const void *p)
70 {
71         u32 v;
72
73         if (UNALIGNED_ACCESS_IS_FAST) {
74                 v = le32_to_cpu(load_le32_unaligned(p));
75         } else {
76                 const u8 *p8 = p;
77                 v = 0;
78                 v |= (u32)p8[0] << 0;
79                 v |= (u32)p8[1] << 8;
80                 v |= (u32)p8[2] << 16;
81                 v |= (u32)p8[3] << 24;
82         }
83         return v;
84 }
85
86 static inline void
87 put_unaligned_le16(u16 v, void *p)
88 {
89         if (UNALIGNED_ACCESS_IS_FAST) {
90                 store_le16_unaligned(cpu_to_le16(v), p);
91         } else {
92                 u8 *p8 = p;
93                 p8[0] = (v >> 0) & 0xFF;
94                 p8[1] = (v >> 8) & 0xFF;
95         }
96 }
97
98 static inline void
99 put_unaligned_le32(u32 v, void *p)
100 {
101         if (UNALIGNED_ACCESS_IS_FAST) {
102                 store_le32_unaligned(cpu_to_le32(v), p);
103         } else {
104                 u8 *p8 = p;
105                 p8[0] = (v >> 0) & 0xFF;
106                 p8[1] = (v >> 8) & 0xFF;
107                 p8[2] = (v >> 16) & 0xFF;
108                 p8[3] = (v >> 24) & 0xFF;
109         }
110 }
111
112 /*
113  * Given a 32-bit value that was loaded with the platform's native endianness,
114  * return a 32-bit value whose high-order 8 bits are 0 and whose low-order 24
115  * bits contain the first 3 bytes, arranged in octets in a platform-dependent
116  * order, at the memory location from which the input 32-bit value was loaded.
117  */
118 static inline u32
119 loaded_u32_to_u24(u32 v)
120 {
121         if (CPU_IS_LITTLE_ENDIAN)
122                 return v & 0xFFFFFF;
123         else
124                 return v >> 8;
125 }
126
127 /*
128  * Load the next 3 bytes from the memory location @p into the 24 low-order bits
129  * of a 32-bit value.  The order in which the 3 bytes will be arranged as octets
130  * in the 24 bits is platform-dependent.  At least LOAD_U24_REQUIRED_NBYTES
131  * bytes must be available at @p; note that this may be more than 3.
132  */
133 static inline u32
134 load_u24_unaligned(const u8 *p)
135 {
136 #if UNALIGNED_ACCESS_IS_FAST
137 #  define LOAD_U24_REQUIRED_NBYTES 4
138         return loaded_u32_to_u24(load_u32_unaligned(p));
139 #else
140 #  define LOAD_U24_REQUIRED_NBYTES 3
141 #  if CPU_IS_BIG_ENDIAN
142         return ((u32)p[2] << 0) | ((u32)p[1] << 8) | ((u32)p[0] << 16);
143 #  else
144         return ((u32)p[0] << 0) | ((u32)p[1] << 8) | ((u32)p[2] << 16);
145 #  endif
146 #endif
147 }
148
149
150 #endif /* _WIMLIB_UNALIGNED_H */