]> mj.ucw.cz Git - libucw.git/blob - lib/ff-binary.h
150413c1dde668d9bca742f3d6a320cbd81ff5a2
[libucw.git] / lib / 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 "lib/fastbuf.h"
15 #include "lib/unaligned.h"
16
17 int bgetw_slow(struct fastbuf *f);
18 static inline int bgetw(struct fastbuf *f)
19 {
20   int w;
21   if (bavailr(f) >= 2)
22     {
23       w = GET_U16(f->bptr);
24       f->bptr += 2;
25       return w;
26     }
27   else
28     return bgetw_slow(f);
29 }
30
31 u32 bgetl_slow(struct fastbuf *f);
32 static inline u32 bgetl(struct fastbuf *f)
33 {
34   u32 l;
35   if (bavailr(f) >= 4)
36     {
37       l = GET_U32(f->bptr);
38       f->bptr += 4;
39       return l;
40     }
41   else
42     return bgetl_slow(f);
43 }
44
45 u64 bgetq_slow(struct fastbuf *f);
46 static inline u64 bgetq(struct fastbuf *f)
47 {
48   u64 l;
49   if (bavailr(f) >= 8)
50     {
51       l = GET_U64(f->bptr);
52       f->bptr += 8;
53       return l;
54     }
55   else
56     return bgetq_slow(f);
57 }
58
59 u64 bget5_slow(struct fastbuf *f);
60 static inline u64 bget5(struct fastbuf *f)
61 {
62   u64 l;
63   if (bavailr(f) >= 5)
64     {
65       l = GET_U40(f->bptr);
66       f->bptr += 5;
67       return l;
68     }
69   else
70     return bget5_slow(f);
71 }
72
73 void bputw_slow(struct fastbuf *f, uns w);
74 static inline void bputw(struct fastbuf *f, uns w)
75 {
76   if (bavailw(f) >= 2)
77     {
78       PUT_U16(f->bptr, w);
79       f->bptr += 2;
80     }
81   else
82     bputw_slow(f, w);
83 }
84
85 void bputl_slow(struct fastbuf *f, u32 l);
86 static inline void bputl(struct fastbuf *f, u32 l)
87 {
88   if (bavailw(f) >= 4)
89     {
90       PUT_U32(f->bptr, l);
91       f->bptr += 4;
92     }
93   else
94     bputl_slow(f, l);
95 }
96
97 void bputq_slow(struct fastbuf *f, u64 l);
98 static inline void bputq(struct fastbuf *f, u64 l)
99 {
100   if (bavailw(f) >= 8)
101     {
102       PUT_U64(f->bptr, l);
103       f->bptr += 8;
104     }
105   else
106     bputq_slow(f, l);
107 }
108
109 void bput5_slow(struct fastbuf *f, u64 l);
110 static inline void bput5(struct fastbuf *f, u64 l)
111 {
112   if (bavailw(f) >= 5)
113     {
114       PUT_U40(f->bptr, l);
115       f->bptr += 5;
116     }
117   else
118     bput5_slow(f, l);
119 }
120
121 /* I/O on uintptr_t */
122
123 #ifdef CPU_64BIT_POINTERS
124 #define bputa(x,p) bputq(x,p)
125 #define bgeta(x) bgetq(x)
126 #else
127 #define bputa(x,p) bputl(x,p)
128 #define bgeta(x) bgetl(x)
129 #endif
130
131 #endif