]> mj.ucw.cz Git - libucw.git/blob - lib/ff-binary.c
Unaligned access functions (formerly macros) work in native, big and little endian.
[libucw.git] / lib / ff-binary.c
1 /*
2  *      UCW Library -- Fast Buffered I/O: Binary Numbers
3  *
4  *      (c) 1997--2006 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12 #include "lib/ff-binary.h"
13
14 int bgetw_slow(struct fastbuf *f)
15 {
16   int w1, w2;
17   w1 = bgetc_slow(f);
18   if (w1 < 0)
19     return w1;
20   w2 = bgetc_slow(f);
21   if (w2 < 0)
22     return w2;
23 #ifdef CPU_BIG_ENDIAN
24   return (w1 << 8) | w2;
25 #else
26   return w1 | (w2 << 8);
27 #endif
28 }
29
30 u32 bgetl_slow(struct fastbuf *f)
31 {
32   u32 l = bgetc_slow(f);
33 #ifdef CPU_BIG_ENDIAN
34   l = (l << 8) | bgetc_slow(f);
35   l = (l << 8) | bgetc_slow(f);
36   return (l << 8) | bgetc_slow(f);
37 #else
38   l = (bgetc_slow(f) << 8) | l;
39   l = (bgetc_slow(f) << 16) | l;
40   return (bgetc_slow(f) << 24) | l;
41 #endif
42 }
43
44 u64 bgetq_slow(struct fastbuf *f)
45 {
46   u32 l, h;
47 #ifdef CPU_BIG_ENDIAN
48   h = bgetl_slow(f);
49   l = bgetl_slow(f);
50 #else
51   l = bgetl_slow(f);
52   h = bgetl_slow(f);
53 #endif
54   return ((u64) h << 32) | l;
55 }
56
57 u64 bget5_slow(struct fastbuf *f)
58 {
59   u32 l, h;
60 #ifdef CPU_BIG_ENDIAN
61   h = bgetc_slow(f);
62   l = bgetl_slow(f);
63 #else
64   l = bgetl_slow(f);
65   h = bgetc_slow(f);
66 #endif
67   return ((u64) h << 32) | l;
68 }
69
70 void bputw_slow(struct fastbuf *f, uns w)
71 {
72 #ifdef CPU_BIG_ENDIAN
73   bputc_slow(f, w >> 8);
74   bputc_slow(f, w);
75 #else
76   bputc_slow(f, w);
77   bputc_slow(f, w >> 8);
78 #endif
79 }
80
81 void bputl_slow(struct fastbuf *f, u32 l)
82 {
83 #ifdef CPU_BIG_ENDIAN
84   bputc_slow(f, l >> 24);
85   bputc_slow(f, l >> 16);
86   bputc_slow(f, l >> 8);
87   bputc_slow(f, l);
88 #else
89   bputc_slow(f, l);
90   bputc_slow(f, l >> 8);
91   bputc_slow(f, l >> 16);
92   bputc_slow(f, l >> 24);
93 #endif
94 }
95
96 void bputq_slow(struct fastbuf *f, u64 q)
97 {
98 #ifdef CPU_BIG_ENDIAN
99   bputl_slow(f, q >> 32);
100   bputl_slow(f, q);
101 #else
102   bputl_slow(f, q);
103   bputl_slow(f, q >> 32);
104 #endif
105 }
106
107 void bput5_slow(struct fastbuf *f, u64 o)
108 {
109   u32 hi = o >> 32;
110   u32 low = o;
111 #ifdef CPU_BIG_ENDIAN
112   bputc_slow(f, hi);
113   bputl_slow(f, low);
114 #else
115   bputl_slow(f, low);
116   bputc_slow(f, hi);
117 #endif
118 }