Back to home page

LXR

 
 

    


0001 #include <linux/export.h>
0002 #include <linux/bug.h>
0003 #include <linux/bitmap.h>
0004 
0005 /**
0006  * memweight - count the total number of bits set in memory area
0007  * @ptr: pointer to the start of the area
0008  * @bytes: the size of the area
0009  */
0010 size_t memweight(const void *ptr, size_t bytes)
0011 {
0012     size_t ret = 0;
0013     size_t longs;
0014     const unsigned char *bitmap = ptr;
0015 
0016     for (; bytes > 0 && ((unsigned long)bitmap) % sizeof(long);
0017             bytes--, bitmap++)
0018         ret += hweight8(*bitmap);
0019 
0020     longs = bytes / sizeof(long);
0021     if (longs) {
0022         BUG_ON(longs >= INT_MAX / BITS_PER_LONG);
0023         ret += bitmap_weight((unsigned long *)bitmap,
0024                 longs * BITS_PER_LONG);
0025         bytes -= longs * sizeof(long);
0026         bitmap += longs * sizeof(long);
0027     }
0028     /*
0029      * The reason that this last loop is distinct from the preceding
0030      * bitmap_weight() call is to compute 1-bits in the last region smaller
0031      * than sizeof(long) properly on big-endian systems.
0032      */
0033     for (; bytes > 0; bytes--, bitmap++)
0034         ret += hweight8(*bitmap);
0035 
0036     return ret;
0037 }
0038 EXPORT_SYMBOL(memweight);