aboutsummaryrefslogtreecommitdiff
path: root/src/lj_def.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_def.h')
-rw-r--r--src/lj_def.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/lj_def.h b/src/lj_def.h
index c60bc118..86f041a4 100644
--- a/src/lj_def.h
+++ b/src/lj_def.h
@@ -184,6 +184,28 @@ static LJ_AINLINE uint64_t lj_bswap64(uint64_t x)
184#error "missing define for lj_bswap()" 184#error "missing define for lj_bswap()"
185#endif 185#endif
186 186
187typedef union __attribute__((packed)) Unaligned16 {
188 uint16_t u;
189 uint8_t b[2];
190} Unaligned16;
191
192typedef union __attribute__((packed)) Unaligned32 {
193 uint32_t u;
194 uint8_t b[4];
195} Unaligned32;
196
197/* Unaligned load of uint16_t. */
198static LJ_AINLINE uint16_t lj_getu16(const void *p)
199{
200 return ((const Unaligned16 *)p)->u;
201}
202
203/* Unaligned load of uint32_t. */
204static LJ_AINLINE uint32_t lj_getu32(const void *p)
205{
206 return ((const Unaligned32 *)p)->u;
207}
208
187#elif defined(_MSC_VER) 209#elif defined(_MSC_VER)
188 210
189#define LJ_NORET __declspec(noreturn) 211#define LJ_NORET __declspec(noreturn)
@@ -208,6 +230,10 @@ static LJ_AINLINE uint32_t lj_fls(uint32_t x)
208#define lj_bswap(x) (_byteswap_ulong((x))) 230#define lj_bswap(x) (_byteswap_ulong((x)))
209#define lj_bswap64(x) (_byteswap_uint64((x))) 231#define lj_bswap64(x) (_byteswap_uint64((x)))
210 232
233/* MSVC is only supported on x86/x64, where unaligned loads are always ok. */
234#define lj_getu16(p) (*(uint16_t *)(p))
235#define lj_getu32(p) (*(uint32_t *)(p))
236
211#else 237#else
212#error "missing defines for your compiler" 238#error "missing defines for your compiler"
213#endif 239#endif