ccc86f016a3ab9a1638e49a1bf42891e437d0a8e
[wimlib] / include / wimlib / unaligned.h
1 /*
2  * unaligned.h
3  *
4  * Inline functions for unaligned memory accesses.
5  *
6  * The author dedicates this file to the public domain.
7  * You can do whatever you want with this file.
8  */
9
10 #ifndef _WIMLIB_UNALIGNED_H
11 #define _WIMLIB_UNALIGNED_H
12
13 #include "wimlib/compiler.h"
14 #include "wimlib/endianness.h"
15 #include "wimlib/types.h"
16
17 #define DEFINE_UNALIGNED_TYPE(type)                             \
18 struct type##_unaligned {                                       \
19         type v;                                                 \
20 } _packed_attribute;                                            \
21                                                                 \
22 static inline type                                              \
23 load_##type##_unaligned(const void *p)                          \
24 {                                                               \
25         return ((const struct type##_unaligned *)p)->v;         \
26 }                                                               \
27                                                                 \
28 static inline void                                              \
29 store_##type##_unaligned(type val, void *p)                     \
30 {                                                               \
31         ((struct type##_unaligned *)p)->v = val;                \
32 }
33
34 DEFINE_UNALIGNED_TYPE(u16);
35 DEFINE_UNALIGNED_TYPE(u32);
36 DEFINE_UNALIGNED_TYPE(u64);
37 DEFINE_UNALIGNED_TYPE(le16);
38 DEFINE_UNALIGNED_TYPE(le32);
39 DEFINE_UNALIGNED_TYPE(le64);
40 DEFINE_UNALIGNED_TYPE(be16);
41 DEFINE_UNALIGNED_TYPE(be32);
42 DEFINE_UNALIGNED_TYPE(be64);
43 DEFINE_UNALIGNED_TYPE(size_t);
44 DEFINE_UNALIGNED_TYPE(machine_word_t);
45
46 #define load_word_unaligned     load_machine_word_t_unaligned
47 #define store_word_unaligned    store_machine_word_t_unaligned
48
49 static inline void
50 copy_word_unaligned(const void *src, void *dst)
51 {
52         store_word_unaligned(load_word_unaligned(src), dst);
53 }
54
55 static inline machine_word_t
56 repeat_byte(u8 b)
57 {
58         machine_word_t v;
59
60         BUILD_BUG_ON(WORDSIZE != 4 && WORDSIZE != 8);
61
62         v = b;
63         v |= v << 8;
64         v |= v << 16;
65         v |= v << ((WORDSIZE == 8) ? 32 : 0);
66         return v;
67 }
68
69 static inline u16
70 get_unaligned_u16_le(const void *p)
71 {
72         u16 v;
73
74         if (UNALIGNED_ACCESS_IS_FAST) {
75                 v = le16_to_cpu(load_le16_unaligned(p));
76         } else {
77                 const u8 *p8 = p;
78                 v = 0;
79                 v |= (u16)p8[0] << 0;
80                 v |= (u16)p8[1] << 8;
81         }
82         return v;
83 }
84
85 static inline u32
86 get_unaligned_u32_le(const void *p)
87 {
88         u32 v;
89
90         if (UNALIGNED_ACCESS_IS_FAST) {
91                 v = le32_to_cpu(load_le32_unaligned(p));
92         } else {
93                 const u8 *p8 = p;
94                 v = 0;
95                 v |= (u32)p8[0] << 0;
96                 v |= (u32)p8[1] << 8;
97                 v |= (u32)p8[2] << 16;
98                 v |= (u32)p8[3] << 24;
99         }
100         return v;
101 }
102
103 static inline void
104 put_unaligned_u16_le(u16 v, void *p)
105 {
106         if (UNALIGNED_ACCESS_IS_FAST) {
107                 store_le16_unaligned(cpu_to_le16(v), p);
108         } else {
109                 u8 *p8 = p;
110                 p8[0] = (v >> 0) & 0xFF;
111                 p8[1] = (v >> 8) & 0xFF;
112         }
113 }
114
115 static inline void
116 put_unaligned_u32_le(u32 v, void *p)
117 {
118         if (UNALIGNED_ACCESS_IS_FAST) {
119                 store_le32_unaligned(cpu_to_le32(v), p);
120         } else {
121                 u8 *p8 = p;
122                 p8[0] = (v >> 0) & 0xFF;
123                 p8[1] = (v >> 8) & 0xFF;
124                 p8[2] = (v >> 16) & 0xFF;
125                 p8[3] = (v >> 24) & 0xFF;
126         }
127 }
128
129 #endif /* _WIMLIB_UNALIGNED_H */