151aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe#include "hweight.h" 251aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe 351aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboeunsigned int hweight8(uint8_t w) 451aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe{ 551aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe unsigned int res = w - ((w >> 1) & 0x55); 651aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe 751aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe res = (res & 0x33) + ((res >> 2) & 0x33); 851aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe return (res + (res >> 4)) & 0x0F; 951aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe} 1051aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe 1151aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboeunsigned int hweight32(uint32_t w) 1251aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe{ 1351aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe unsigned int res = w - ((w >> 1) & 0x55555555); 1451aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe 1551aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe res = (res & 0x33333333) + ((res >> 2) & 0x33333333); 1651aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe res = (res + (res >> 4)) & 0x0F0F0F0F; 1751aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe res = res + (res >> 8); 1851aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe return (res + (res >> 16)) & 0x000000FF; 1951aa2da8cf422a06ddfa1ce673f3bfc03f96b86eJens Axboe} 20def823d406ad78d3f9690ee8804e743e1d5a8affJens Axboe 21def823d406ad78d3f9690ee8804e743e1d5a8affJens Axboeunsigned int hweight64(uint64_t w) 22def823d406ad78d3f9690ee8804e743e1d5a8affJens Axboe{ 23def823d406ad78d3f9690ee8804e743e1d5a8affJens Axboe#if BITS_PER_LONG == 32 24def823d406ad78d3f9690ee8804e743e1d5a8affJens Axboe return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w); 25def823d406ad78d3f9690ee8804e743e1d5a8affJens Axboe#else 26a240e441dd3ebf9705d9edc9d6f35f48acdb0241Jens Axboe uint64_t res = w - ((w >> 1) & 0x5555555555555555ULL); 27a240e441dd3ebf9705d9edc9d6f35f48acdb0241Jens Axboe res = (res & 0x3333333333333333ULL) + ((res >> 2) & 0x3333333333333333ULL); 28a240e441dd3ebf9705d9edc9d6f35f48acdb0241Jens Axboe res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0FULL; 29def823d406ad78d3f9690ee8804e743e1d5a8affJens Axboe res = res + (res >> 8); 30def823d406ad78d3f9690ee8804e743e1d5a8affJens Axboe res = res + (res >> 16); 31a240e441dd3ebf9705d9edc9d6f35f48acdb0241Jens Axboe return (res + (res >> 32)) & 0x00000000000000FFULL; 32def823d406ad78d3f9690ee8804e743e1d5a8affJens Axboe#endif 33def823d406ad78d3f9690ee8804e743e1d5a8affJens Axboe} 34