2 * UCW Library -- Fast Buffered I/O on Binary Values
4 * (c) 1997--2007 Martin Mares <mj@ucw.cz>
5 * (c) 2004 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
11 #ifndef _UCW_FF_BINARY_H
12 #define _UCW_FF_BINARY_H
14 #include "ucw/fastbuf.h"
15 #include "ucw/unaligned.h"
23 #define GET_FUNC(type, name, bits, endian) \
24 type bget##name##_##endian##_slow(struct fastbuf *f); \
25 static inline type bget##name##_##endian(struct fastbuf *f) \
27 if (bavailr(f) >= bits/8) \
29 type w = get_u##bits##_##endian(f->bptr); \
34 return bget##name##_##endian##_slow(f); \
37 #define PUT_FUNC(type, name, bits, endian) \
38 void bput##name##_##endian##_slow(struct fastbuf *f, type x); \
39 static inline void bput##name##_##endian(struct fastbuf *f, type x) \
41 if (bavailw(f) >= bits/8) \
43 put_u##bits##_##endian(f->bptr, x); \
47 return bput##name##_##endian##_slow(f, x); \
50 #define FF_ALL_X(type, name, bits, defendian) \
51 GET_FUNC(type, name, bits, be) \
52 GET_FUNC(type, name, bits, le) \
53 PUT_FUNC(type, name, bits, be) \
54 PUT_FUNC(type, name, bits, le) \
55 static inline type bget##name(struct fastbuf *f) { return bget##name##_##defendian(f); } \
56 static inline void bput##name(struct fastbuf *f, type x) { bput##name##_##defendian(f, x); }
58 #define FF_ALL(type, name, bits, defendian) FF_ALL_X(type, name, bits, defendian)
60 FF_ALL(int, w, 16, FF_ENDIAN)
61 FF_ALL(uns, l, 32, FF_ENDIAN)
62 FF_ALL(u64, q, 64, FF_ENDIAN)
63 FF_ALL(u64, 5, 40, FF_ENDIAN)
71 /* I/O on uintptr_t (only native endianity) */
73 #ifdef CPU_64BIT_POINTERS
74 #define bputa(x,p) bputq(x,p)
75 #define bgeta(x) bgetq(x)
77 #define bputa(x,p) bputl(x,p)
78 #define bgeta(x) bgetl(x)