]> wimlib.net Git - wimlib/blob - include/wimlib/unaligned.h
Use more comprehensive public domain dedications
[wimlib] / include / wimlib / unaligned.h
1 /*
2  * unaligned.h - inline functions for unaligned memory accesses
3  *
4  * The following copying information applies to this specific source code file:
5  *
6  * Written in 2014-2015 by Eric Biggers <ebiggers3@gmail.com>
7  *
8  * To the extent possible under law, the author(s) have dedicated all copyright
9  * and related and neighboring rights to this software to the public domain
10  * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
11  * Dedication (the "CC0").
12  *
13  * This software is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
16  *
17  * You should have received a copy of the CC0 along with this software; if not
18  * see <http://creativecommons.org/publicdomain/zero/1.0/>.
19  */
20
21 #ifndef _WIMLIB_UNALIGNED_H
22 #define _WIMLIB_UNALIGNED_H
23
24 #include "wimlib/compiler.h"
25 #include "wimlib/endianness.h"
26 #include "wimlib/types.h"
27
28 #define DEFINE_UNALIGNED_TYPE(type)                             \
29 struct type##_unaligned {                                       \
30         type v;                                                 \
31 } _packed_attribute;                                            \
32                                                                 \
33 static inline type                                              \
34 load_##type##_unaligned(const void *p)                          \
35 {                                                               \
36         return ((const struct type##_unaligned *)p)->v;         \
37 }                                                               \
38                                                                 \
39 static inline void                                              \
40 store_##type##_unaligned(type val, void *p)                     \
41 {                                                               \
42         ((struct type##_unaligned *)p)->v = val;                \
43 }
44
45 DEFINE_UNALIGNED_TYPE(u16);
46 DEFINE_UNALIGNED_TYPE(u32);
47 DEFINE_UNALIGNED_TYPE(u64);
48 DEFINE_UNALIGNED_TYPE(le16);
49 DEFINE_UNALIGNED_TYPE(le32);
50 DEFINE_UNALIGNED_TYPE(le64);
51 DEFINE_UNALIGNED_TYPE(be16);
52 DEFINE_UNALIGNED_TYPE(be32);
53 DEFINE_UNALIGNED_TYPE(be64);
54 DEFINE_UNALIGNED_TYPE(size_t);
55 DEFINE_UNALIGNED_TYPE(machine_word_t);
56
57 #define load_word_unaligned     load_machine_word_t_unaligned
58 #define store_word_unaligned    store_machine_word_t_unaligned
59
60 static inline u16
61 get_unaligned_le16(const u8 *p)
62 {
63         if (UNALIGNED_ACCESS_IS_FAST)
64                 return le16_to_cpu(load_le16_unaligned(p));
65         else
66                 return ((u16)p[1] << 8) | p[0];
67 }
68
69 static inline u32
70 get_unaligned_le32(const u8 *p)
71 {
72         if (UNALIGNED_ACCESS_IS_FAST)
73                 return le32_to_cpu(load_le32_unaligned(p));
74         else
75                 return ((u32)p[3] << 24) | ((u32)p[2] << 16) |
76                         ((u32)p[1] << 8) | p[0];
77 }
78
79 static inline void
80 put_unaligned_le16(u16 v, u8 *p)
81 {
82         if (UNALIGNED_ACCESS_IS_FAST) {
83                 store_le16_unaligned(cpu_to_le16(v), p);
84         } else {
85                 p[0] = (u8)(v >> 0);
86                 p[1] = (u8)(v >> 8);
87         }
88 }
89
90 static inline void
91 put_unaligned_le32(u32 v, u8 *p)
92 {
93         if (UNALIGNED_ACCESS_IS_FAST) {
94                 store_le32_unaligned(cpu_to_le32(v), p);
95         } else {
96                 p[0] = (u8)(v >> 0);
97                 p[1] = (u8)(v >> 8);
98                 p[2] = (u8)(v >> 16);
99                 p[3] = (u8)(v >> 24);
100         }
101 }
102
103 /*
104  * Given a 32-bit value that was loaded with the platform's native endianness,
105  * return a 32-bit value whose high-order 8 bits are 0 and whose low-order 24
106  * bits contain the first 3 bytes, arranged in octets in a platform-dependent
107  * order, at the memory location from which the input 32-bit value was loaded.
108  */
109 static inline u32
110 loaded_u32_to_u24(u32 v)
111 {
112         if (CPU_IS_LITTLE_ENDIAN)
113                 return v & 0xFFFFFF;
114         else
115                 return v >> 8;
116 }
117
118 /*
119  * Load the next 3 bytes from the memory location @p into the 24 low-order bits
120  * of a 32-bit value.  The order in which the 3 bytes will be arranged as octets
121  * in the 24 bits is platform-dependent.  At least LOAD_U24_REQUIRED_NBYTES
122  * bytes must be available at @p; note that this may be more than 3.
123  */
124 static inline u32
125 load_u24_unaligned(const u8 *p)
126 {
127 #if UNALIGNED_ACCESS_IS_FAST
128 #  define LOAD_U24_REQUIRED_NBYTES 4
129         return loaded_u32_to_u24(load_u32_unaligned(p));
130 #else
131 #  define LOAD_U24_REQUIRED_NBYTES 3
132 #  if CPU_IS_BIG_ENDIAN
133         return ((u32)p[2] << 0) | ((u32)p[1] << 8) | ((u32)p[0] << 16);
134 #  else
135         return ((u32)p[0] << 0) | ((u32)p[1] << 8) | ((u32)p[2] << 16);
136 #  endif
137 #endif
138 }
139
140
141 #endif /* _WIMLIB_UNALIGNED_H */