]> mj.ucw.cz Git - libucw.git/blob - lib/unaligned.h
835931453f24ae4aa58dcf67fdfb098c3972a139
[libucw.git] / lib / unaligned.h
1 /*
2  *      Sherlock Library -- Fast Access to Unaligned Data
3  *
4  *      (c) 1997--2001 Martin Mares <mj@ucw.cz>
5  */
6
7 #ifndef _SHERLOCK_UNALIGNED_H
8 #define _SHERLOCK_UNALIGNED_H
9
10 #ifdef CPU_ALLOW_UNALIGNED
11 #define GET_U16(p) (*((u16 *)(p)))
12 #define GET_U32(p) (*((u32 *)(p)))
13 #define GET_U64(p) (*((u64 *)(p)))
14 #define PUT_U16(p,x) *((u16 *)(p)) = (x)
15 #define PUT_U32(p,x) *((u32 *)(p)) = (x)
16 #define PUT_U64(p,x) *((u64 *)(p)) = (x)
17 #else
18 #define GET_U64(p) ({ u64 _x; memcpy(&_x, p, 8); _x; })
19 #define PUT_U64(p,x) do { u64 _x=x; memcpy(p, &_x, 8); } while(0)
20 #ifdef CPU_BIG_ENDIAN
21 #define GET_U16(p) (((p)[0] << 8) | (p)[1])
22 #define GET_U32(p) (((p)[0] << 24) | ((p)[1] << 16) | ((p)[2] << 8) | (p)[3])
23 #define PUT_U16(p,x) (void)(((p)[0] = ((x) >> 8)), (((p)[1]) = (x)))
24 #define PUT_U32(p,x) (void)(((p)[0] = ((x) >> 24)), (((p)[1]) = ((x) >> 16)), (((p)[2]) = ((x) >> 8)), (((p)[3]) = (x)))
25 #else
26 #define GET_U16(p) (((p)[1] << 8) | (p)[0])
27 #define GET_U32(p) (((p)[3] << 24) | ((p)[2] << 16) | ((p)[1] << 8) | (p)[0])
28 #define PUT_U16(p,x) (void)(((p)[1] = ((x) >> 8)), (((p)[0]) = (x)))
29 #define PUT_U32(p,x) (void)(((p)[3] = ((x) >> 24)), (((p)[2]) = ((x) >> 16)), (((p)[1]) = ((x) >> 8)), (((p)[0]) = (x)))
30 #endif
31 #endif
32
33 #ifdef CPU_BIG_ENDIAN
34 #define GET_U40(p) (((u64) (p)[0] << 32) | GET_U32((p)+1))
35 #define PUT_U40(p,x) do { (p)[0] = ((x) >> 32); PUT_U32(((p)+1), x); } while(0)
36 #else
37 #define GET_U40(p) (((u64) (p)[4] << 32) | GET_U32(p))
38 #define PUT_U40(p,x) do { (p)[4] = ((x) >> 32); PUT_U32(p, x); } while(0)
39 #endif
40
41 #endif