]> mj.ucw.cz Git - libucw.git/blob - lib/unaligned.h
- buck2obj_alloc() initially allocates no buffers
[libucw.git] / lib / unaligned.h
1 /*
2  *      Sherlock Library -- Fast Access to Unaligned Data
3  *
4  *      (c) 1997--2003 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _SHERLOCK_UNALIGNED_H
11 #define _SHERLOCK_UNALIGNED_H
12
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)
21 #else
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)
24 #ifdef CPU_BIG_ENDIAN
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)))
29 #else
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)))
34 #endif
35 #endif
36
37 #define GET_U32_BE16(p) (((p)[0] << 16) | (p)[1])
38
39 #ifdef CPU_BIG_ENDIAN
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)
42 #else
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)
45 #endif
46
47 /* Just for completeness */
48
49 #define GET_U8(p) (*(byte *)(p))
50 #define PUT_U8(p,x) *((byte *)(p)) = (x)
51
52 #endif