X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=include%2Fwimlib%2Funaligned.h;h=34240bd0b6e5669b9da266d7b9f9f4099a5bc2a4;hp=5f920e75d30c5a53b39a5bdf099a176bc204b945;hb=aa3a2830cd4154238bc6a342ffea43d9ac69a14c;hpb=226a6dfe2909e054568298196785c944a1b5c4fa diff --git a/include/wimlib/unaligned.h b/include/wimlib/unaligned.h index 5f920e75..34240bd0 100644 --- a/include/wimlib/unaligned.h +++ b/include/wimlib/unaligned.h @@ -3,6 +3,9 @@ * * Inline functions for unaligned memory accesses. * + * Author: Eric Biggers + * Year: 2014, 2015 + * * The author dedicates this file to the public domain. * You can do whatever you want with this file. */ @@ -106,4 +109,38 @@ put_unaligned_u32_le(u32 v, void *p) } } +/* + * Given a 32-bit value that was loaded with the platform's native endianness, + * return a 32-bit value whose high-order 8 bits are 0 and whose low-order 24 + * bits contain the first 3 bytes, arranged in octets in a platform-dependent + * order, at the memory location from which the input 32-bit value was loaded. + */ +static inline u32 +loaded_u32_to_u24(u32 v) +{ + if (CPU_IS_LITTLE_ENDIAN) + return v & 0xFFFFFF; + else + return v >> 8; +} + +/* + * Load the next 3 bytes from the memory location @p into the 24 low-order bits + * of a 32-bit value. The order in which the 3 bytes will be arranged as octets + * in the 24 bits is platform-dependent. At least LOAD_U24_REQUIRED_NBYTES + * bytes must be available at @p; note that this may be more than 3. + */ +static inline u32 +load_u24_unaligned(const u8 *p) +{ +#if UNALIGNED_ACCESS_IS_FAST +# define LOAD_U24_REQUIRED_NBYTES 4 + return loaded_u32_to_u24(load_u32_unaligned(p)); +#else +# define LOAD_U24_REQUIRED_NBYTES 3 + return ((u32)p[0] << 0) | ((u32)p[1] << 8) | ((u32)p[2] << 16); +#endif +} + + #endif /* _WIMLIB_UNALIGNED_H */