]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/ext2fs/bitops.c
2 * bitops.c --- Bitmap frobbing code. See bitops.h for the inlined
5 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
16 #include <sys/types.h>
22 #ifndef _EXT2_HAVE_ASM_BITOPS_
25 * For the benefit of those who are trying to port Linux to another
26 * architecture, here are some C-language equivalents. You should
27 * recode these in the native assembly language, if at all possible.
29 * C language equivalents written by Theodore Ts'o, 9/26/92.
30 * Modified by Pete A. Zaitcev 7/14/95 to be portable to big endian
31 * systems, as well as non-32 bit systems.
34 int ext2fs_set_bit(unsigned int nr
,void * addr
)
37 unsigned char *ADDR
= (unsigned char *) addr
;
40 mask
= 1 << (nr
& 0x07);
41 retval
= mask
& *ADDR
;
46 int ext2fs_clear_bit(unsigned int nr
, void * addr
)
49 unsigned char *ADDR
= (unsigned char *) addr
;
52 mask
= 1 << (nr
& 0x07);
53 retval
= mask
& *ADDR
;
58 int ext2fs_test_bit(unsigned int nr
, const void * addr
)
61 const unsigned char *ADDR
= (const unsigned char *) addr
;
64 mask
= 1 << (nr
& 0x07);
65 return (mask
& *ADDR
);
68 #endif /* !_EXT2_HAVE_ASM_BITOPS_ */
70 void ext2fs_warn_bitmap(errcode_t errcode
, unsigned long arg
,
71 const char *description
)
75 com_err(0, errcode
, "#%lu for %s", arg
, description
);
77 com_err(0, errcode
, "#%lu", arg
);
85 int ext2fs_set_bit64(__u64 nr
, void * addr
)
88 unsigned char *ADDR
= (unsigned char *) addr
;
91 mask
= 1 << (nr
& 0x07);
92 retval
= mask
& *ADDR
;
97 int ext2fs_clear_bit64(__u64 nr
, void * addr
)
100 unsigned char *ADDR
= (unsigned char *) addr
;
103 mask
= 1 << (nr
& 0x07);
104 retval
= mask
& *ADDR
;
109 int ext2fs_test_bit64(__u64 nr
, const void * addr
)
112 const unsigned char *ADDR
= (const unsigned char *) addr
;
115 mask
= 1 << (nr
& 0x07);
116 return (mask
& *ADDR
);
119 static unsigned int popcount8(unsigned int w
)
121 unsigned int res
= w
- ((w
>> 1) & 0x55);
122 res
= (res
& 0x33) + ((res
>> 2) & 0x33);
123 return (res
+ (res
>> 4)) & 0x0F;
126 static unsigned int popcount32(unsigned int w
)
128 unsigned int res
= w
- ((w
>> 1) & 0x55555555);
129 res
= (res
& 0x33333333) + ((res
>> 2) & 0x33333333);
130 res
= (res
+ (res
>> 4)) & 0x0F0F0F0F;
131 res
= res
+ (res
>> 8);
132 return (res
+ (res
>> 16)) & 0x000000FF;
135 unsigned int ext2fs_bitcount(const void *addr
, unsigned int nbytes
)
137 const unsigned char *cp
= addr
;
139 unsigned int res
= 0;
141 while (((((uintptr_t) cp
) & 3) != 0) && (nbytes
> 0)) {
142 res
+= popcount8(*cp
++);
145 p
= (const __u32
*) cp
;
148 res
+= popcount32(*p
++);
151 cp
= (const unsigned char *) p
;
154 res
+= popcount8(*cp
++);