]> mj.ucw.cz Git - libucw.git/blob - ucw/ff-binary.h
UCW doc: Fixed a typo.
[libucw.git] / ucw / ff-binary.h
1 /*
2  *      UCW Library -- Fast Buffered I/O on Binary Values
3  *
4  *      (c) 1997--2007 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_FF_BINARY_H
12 #define _UCW_FF_BINARY_H
13
14 #include "ucw/fastbuf.h"
15 #include "ucw/unaligned.h"
16
17 #ifdef CPU_BIG_ENDIAN
18 #define FF_ENDIAN be
19 #else
20 #define FF_ENDIAN le
21 #endif
22
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)   \
26   {                                                             \
27     if (bavailr(f) >= bits/8)                                   \
28       {                                                         \
29         type w = get_u##bits##_##endian(f->bptr);               \
30         f->bptr += bits/8;                                      \
31         return w;                                               \
32       }                                                         \
33     else                                                        \
34       return bget##name##_##endian##_slow(f);                   \
35   }
36
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)   \
40   {                                                             \
41     if (bavailw(f) >= bits/8)                                   \
42       {                                                         \
43         put_u##bits##_##endian(f->bptr, x);                     \
44         f->bptr += bits/8;                                      \
45       }                                                         \
46     else                                                        \
47       return bput##name##_##endian##_slow(f, x);                \
48   }
49
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); }
57
58 #define FF_ALL(type, name, bits, defendian) FF_ALL_X(type, name, bits, defendian)
59
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)
64
65 #undef GET_FUNC
66 #undef PUT_FUNC
67 #undef FF_ENDIAN
68 #undef FF_ALL_X
69 #undef FF_ALL
70
71 /* I/O on uintptr_t (only native endianity) */
72
73 #ifdef CPU_64BIT_POINTERS
74 #define bputa(x,p) bputq(x,p)
75 #define bgeta(x) bgetq(x)
76 #else
77 #define bputa(x,p) bputl(x,p)
78 #define bgeta(x) bgetl(x)
79 #endif
80
81 #endif