]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/bit.c
2 * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
36 #undef setbit /* defined in param.h on Linux */
38 static int getbit(char *ptr
, int bit
);
39 static void setbit(char *ptr
, int bit
, int val
);
53 return (*ptr
& mask
) >> shift
;
93 p
= (char *)obj
+ byteize(bitoff
);
94 bit
= bitoffs(bitoff
);
95 signext
= (flags
& BVSIGNED
) != 0;
96 z4
= ((__psint_t
)p
& 0xf) == 0 && bit
== 0;
97 if (nbits
== 64 && z4
) {
99 return (__int64_t
)INT_GET(*(__int64_t
*)p
, ARCH_CONVERT
);
101 return (__int64_t
)INT_GET(*(__uint64_t
*)p
, ARCH_CONVERT
);
103 z3
= ((__psint_t
)p
& 0x7) == 0 && bit
== 0;
104 if (nbits
== 32 && z3
) {
106 return (__int64_t
)INT_GET(*(__int32_t
*)p
, ARCH_CONVERT
);
108 return (__int64_t
)INT_GET(*(__uint32_t
*)p
, ARCH_CONVERT
);
110 z2
= ((__psint_t
)p
& 0x3) == 0 && bit
== 0;
111 if (nbits
== 16 && z2
) {
113 return (__int64_t
)INT_GET(*(__int16_t
*)p
, ARCH_CONVERT
);
115 return (__int64_t
)INT_GET(*(__uint16_t
*)p
, ARCH_CONVERT
);
117 z1
= ((__psint_t
)p
& 0x1) == 0 && bit
== 0;
118 if (nbits
== 8 && z1
) {
120 return (__int64_t
)INT_GET(*(__int8_t
*)p
, ARCH_CONVERT
);
122 return (__int64_t
)INT_GET(*(__uint8_t
*)p
, ARCH_CONVERT
);
126 for (i
= 0, rval
= 0LL; i
< nbits
; i
++) {
127 if (getbit(p
, bit
+ i
)) {
128 /* If the last bit is on and we care about sign
129 * bits and we don't have a full 64 bit
130 * container, turn all bits on between the
131 * sign bit and the most sig bit.
134 /* handle endian swap here */
135 #if __BYTE_ORDER == LITTLE_ENDIAN
136 if (i
== 0 && signext
&& nbits
< 64)
137 rval
= -1LL << nbits
;
138 rval
|= 1LL << (nbits
- i
- 1);
140 if ((i
== (nbits
- 1)) && signext
&& nbits
< 64)
141 rval
|= (-1LL << nbits
);
142 rval
|= 1LL << (nbits
- i
- 1);
151 void *obuf
, /* buffer to write into */
152 int bitoff
, /* bit offset of where to write */
153 int nbits
, /* number of bits to write */
154 void *ibuf
) /* source bits */
156 char *in
= (char *)ibuf
;
157 char *out
= (char *)obuf
;
161 #if BYTE_ORDER == LITTLE_ENDIAN
167 /* only need to swap LE integers */
168 if (big
|| (nbits
!=16 && nbits
!=32 && nbits
!=64) ) {
169 /* We don't have type info, so we can only assume
170 * that 2,4 & 8 byte values are integers. sigh.
176 for (bit
=0; bit
<nbits
; bit
++)
177 setbit(out
, bit
+bitoff
, getbit(in
, bit
));
179 /* yes - byte copy */
180 memcpy(out
+byteize(bitoff
), in
, byteize(nbits
));
187 /* we need to endian swap this value */
189 out
+=byteize(bitoff
);
190 obit
=bitoffs(bitoff
);
194 for (bit
=0; bit
<nbits
; bit
++) {
195 setbit(out
, bit
+obit
, getbit(in
, ibit
));
196 if (ibit
%NBBY
==NBBY
-1)