2 * UCW Library -- Fast Access to Unaligned Data
4 * (c) 1997--2003 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #ifndef _UCW_UNALIGNED_H
11 #define _UCW_UNALIGNED_H
13 #ifdef CPU_ALLOW_UNALIGNED
14 #define GET_U16(p) (*((u16 *)(p)))
15 #define GET_U32(p) (*((u32 *)(p)))
16 #define GET_U32_16(p) (*((u32 *)(p)))
17 #define GET_U64(p) (*((u64 *)(p)))
18 #define PUT_U16(p,x) *((u16 *)(p)) = (x)
19 #define PUT_U32(p,x) *((u32 *)(p)) = (x)
20 #define PUT_U64(p,x) *((u64 *)(p)) = (x)
22 #define GET_U64(p) ({ u64 _x; memcpy(&_x, p, 8); _x; })
23 #define PUT_U64(p,x) do { u64 _x=x; memcpy(p, &_x, 8); } while(0)
25 #define GET_U16(p) (((p)[0] << 8) | (p)[1])
26 #define GET_U32(p) (((p)[0] << 24) | ((p)[1] << 16) | ((p)[2] << 8) | (p)[3])
27 #define PUT_U16(p,x) (void)(((p)[0] = ((x) >> 8)), (((p)[1]) = (x)))
28 #define PUT_U32(p,x) (void)(((p)[0] = ((x) >> 24)), (((p)[1]) = ((x) >> 16)), (((p)[2]) = ((x) >> 8)), (((p)[3]) = (x)))
30 #define GET_U16(p) (((p)[1] << 8) | (p)[0])
31 #define GET_U32(p) (((p)[3] << 24) | ((p)[2] << 16) | ((p)[1] << 8) | (p)[0])
32 #define PUT_U16(p,x) (void)(((p)[1] = ((x) >> 8)), (((p)[0]) = (x)))
33 #define PUT_U32(p,x) (void)(((p)[3] = ((x) >> 24)), (((p)[2]) = ((x) >> 16)), (((p)[1]) = ((x) >> 8)), (((p)[0]) = (x)))
37 #define GET_U32_BE16(p) (((p)[0] << 16) | (p)[1])
40 #define GET_U40(p) (((u64) (p)[0] << 32) | GET_U32((p)+1))
41 #define PUT_U40(p,x) do { (p)[0] = ((x) >> 32); PUT_U32(((p)+1), x); } while(0)
43 #define GET_U40(p) (((u64) (p)[4] << 32) | GET_U32(p))
44 #define PUT_U40(p,x) do { (p)[4] = ((x) >> 32); PUT_U32(p, x); } while(0)
47 /* Just for completeness */
49 #define GET_U8(p) (*(byte *)(p))
50 #define PUT_U8(p,x) *((byte *)(p)) = (x)