]> mj.ucw.cz Git - libucw.git/blob - lib/unaligned.h
sign mismatch fixed
[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_U32_16(p) (*((u32 *)(p)))
14 #define GET_U64(p) (*((u64 *)(p)))
15 #define PUT_U16(p,x) *((u16 *)(p)) = (x)
16 #define PUT_U32(p,x) *((u32 *)(p)) = (x)
17 #define PUT_U64(p,x) *((u64 *)(p)) = (x)
18 #else
19 #define GET_U64(p) ({ u64 _x; memcpy(&_x, p, 8); _x; })
20 #define PUT_U64(p,x) do { u64 _x=x; memcpy(p, &_x, 8); } while(0)
21 #ifdef CPU_BIG_ENDIAN
22 #define GET_U16(p) (((p)[0] << 8) | (p)[1])
23 #define GET_U32(p) (((p)[0] << 24) | ((p)[1] << 16) | ((p)[2] << 8) | (p)[3])
24 #define PUT_U16(p,x) (void)(((p)[0] = ((x) >> 8)), (((p)[1]) = (x)))
25 #define PUT_U32(p,x) (void)(((p)[0] = ((x) >> 24)), (((p)[1]) = ((x) >> 16)), (((p)[2]) = ((x) >> 8)), (((p)[3]) = (x)))
26 #else
27 #define GET_U16(p) (((p)[1] << 8) | (p)[0])
28 #define GET_U32(p) (((p)[3] << 24) | ((p)[2] << 16) | ((p)[1] << 8) | (p)[0])
29 #define PUT_U16(p,x) (void)(((p)[1] = ((x) >> 8)), (((p)[0]) = (x)))
30 #define PUT_U32(p,x) (void)(((p)[3] = ((x) >> 24)), (((p)[2]) = ((x) >> 16)), (((p)[1]) = ((x) >> 8)), (((p)[0]) = (x)))
31 #endif
32 #endif
33
34 #define GET_U32_BE16(p) (((p)[0] << 16) | (p)[1])
35
36 #ifdef CPU_BIG_ENDIAN
37 #define GET_U40(p) (((u64) (p)[0] << 32) | GET_U32((p)+1))
38 #define PUT_U40(p,x) do { (p)[0] = ((x) >> 32); PUT_U32(((p)+1), x); } while(0)
39 #else
40 #define GET_U40(p) (((u64) (p)[4] << 32) | GET_U32(p))
41 #define PUT_U40(p,x) do { (p)[4] = ((x) >> 32); PUT_U32(p, x); } while(0)
42 #endif
43
44 #endif