[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Re: [dm-devel] [PATCH 01/10] string: introduce memweight
- From: Matthew Wilcox <matthew wil cx>
- To: Akinobu Mita <akinobu mita gmail com>
- Cc: Theodore Ts'o <tytso mit edu>, linux-ext4 vger kernel org, Jan Kara <jack suse cz>, Mark Fasheh <mfasheh suse com>, Anders Larsen <al alarsen net>, linux-kernel vger kernel org, dm-devel redhat com, ocfs2-devel oss oracle com, Laurent Pinchart <laurent pinchart ideasonboard com>, linux-fsdevel vger kernel org, Andreas Dilger <adilger kernel dilger ca>, akpm linux-foundation org, Joel Becker <jlbec evilplan org>, Alasdair Kergon <agk redhat com>, linux-media vger kernel org
- Subject: Re: [dm-devel] [PATCH 01/10] string: introduce memweight
- Date: Wed, 23 May 2012 07:16:00 -0600
On Wed, May 23, 2012 at 09:12:18PM +0900, Akinobu Mita wrote:
> size_t memweight(const void *ptr, size_t bytes)
Why should this return size_t instead of unsigned long?
> {
> size_t w = 0;
> size_t longs;
> const unsigned char *bitmap = ptr;
>
> for (; bytes > 0 && ((unsigned long)bitmap) % sizeof(long);
> bytes--, bitmap++)
> w += hweight8(*bitmap);
>
> longs = bytes / sizeof(long);
> BUG_ON(longs >= INT_MAX / BITS_PER_LONG);
> w += bitmap_weight((unsigned long *)bitmap, longs * BITS_PER_LONG);
> bytes -= longs * sizeof(long);
> bitmap += longs * sizeof(long);
>
> for (; bytes > 0; bytes--, bitmap++)
> w += hweight8(*bitmap);
>
> return w;
> }
bitmap_weight copes with a bitmask that isn't a multiple of BITS_PER_LONG
in size already. So I think this can be done as:
unsigned long memweight(const void *s, size_t n)
{
const unsigned char *ptr = s;
unsigned long r = 0;
while (n > 0 && (unsigned long)ptr % sizeof(long)) {
r += hweight8(*ptr);
n--;
ptr++;
}
BUG_ON(n >= INT_MAX / 8)
return r + bitmap_weight((unsigned long *)ptr, n * 8);
}
--
Matthew Wilcox Intel Open Source Technology Centre
"Bill, look, we understand that you're interested in selling us this
operating system, but compare it to ours. We can't possibly take such
a retrograde step."
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]