2 * linux/fs/hfs/bitops.c
4 * Copyright (C) 1996 Paul H. Hargrove
5 * This file may be distributed under the terms of the GNU Public License.
7 * This file contains functions to handle bitmaps in "left-to-right"
8 * bit-order such that the MSB of a 32-bit big-endian word is bit 0.
9 * (This corresponds to bit 7 of a 32-bit little-endian word.)
11 * I have tested and confirmed that the results are identical on the
12 * Intel x86, PowerPC and DEC Alpha processors.
14 * "XXX" in a comment is a note to myself to consider changing something.
19 /*================ Global functions ================*/
25 * Given a block of memory, its length in bits, and a starting bit number,
26 * determine the number of the first zero bits (in left-to-right ordering)
29 * Returns >= 'size' if no zero bits are found in the range.
31 * Accesses memory in 32-bit aligned chunks of 32-bits and thus
32 * may read beyond the 'size'th bit.
34 hfs_u32
hfs_find_zero_bit(const hfs_u32
*start
, hfs_u32 size
, hfs_u32 offset
)
36 const hfs_u32
*end
= start
+ ((size
+ 31) >> 5);
37 const hfs_u32
*curr
= start
+ (offset
>> 5);
38 int bit
= offset
% 32;
41 /* scan the first partial hfs_u32 for zero bits */
44 if (!hfs_test_bit(bit
, curr
)) {
53 /* scan complete hfs_u32s for the first zero bit */
55 if (*curr
== ~((hfs_u32
)0)) {
58 while (hfs_test_bit(bit
, curr
)) {
66 bit
|= (curr
- start
) << 5;
74 * hfs_count_zero_bits()
77 * Given a block of memory, its length in bits, and a starting bit number,
78 * determine the number of consecutive zero bits (in left-to-right ordering)
81 * Accesses memory in 32-bit aligned chunks of 32-bits and thus
82 * may read beyond the 'size'th bit.
84 hfs_u32
hfs_count_zero_bits(const hfs_u32
*start
, hfs_u32 size
, hfs_u32 offset
)
86 const hfs_u32
*end
= start
+ ((size
+ 31) >> 5);
87 const hfs_u32
*curr
= start
+ (offset
>> 5);
88 int bit
= offset
% 32;
91 /* scan the first partial hfs_u32 for one bits */
94 if (hfs_test_bit(bit
, curr
)) {
103 /* scan complete hfs_u32s for the first one bit */
105 if (*curr
== ((hfs_u32
)0)) {
108 while (!hfs_test_bit(bit
, curr
)) {
116 bit
|= (curr
- start
) << 5;