]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/unaligned.h
2 * Unaligned Data Accesses -- Generic Version, Network Order
4 * (c) 2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
9 #ifndef _BIRD_UNALIGNED_H_
10 #define _BIRD_UNALIGNED_H_
13 * We don't do any clever tricks with unaligned accesses since it's
14 * virtually impossible to figure out what alignment does the CPU want
15 * (unaligned accesses can be emulated by the OS which makes them work,
16 * but unusably slow). We use memcpy and hope GCC will optimize it out
20 #include "sysdep/unix/endian.h"
21 #include "lib/string.h"
30 get_u16(const void *p
)
38 get_u24(const void *P
)
41 return (p
[0] << 16) + (p
[1] << 8) + p
[2];
45 get_u32(const void *p
)
53 get_u64(const void *p
)
58 return (((u64
) ntohl(xh
)) << 32) | ntohl(xl
);
68 put_u16(void *p
, u16 x
)
75 put_u24(void *p
, u32 x
)
78 memcpy(p
, ((char *) &x
) + 1, 3);
82 put_u32(void *p
, u32 x
)
89 put_u64(void *p
, u64 x
)
99 get_u32s(const void *p
, u32
*x
, int n
)
103 for (i
= 0; i
< n
; i
++)
108 put_u32s(void *p
, const u32
*x
, int n
)
111 for (i
= 0; i
< n
; i
++)
112 put_u32((byte
*) p
+ 4*i
, x
[i
]);