popcountsi2.c revision 276789
161931Scokane/* ===-- popcountsi2.c - Implement __popcountsi2 ---------------------------=== 274534Scokane * 361931Scokane * The LLVM Compiler Infrastructure 461931Scokane * 561931Scokane * This file is dual licensed under the MIT and the University of Illinois Open 661931Scokane * Source Licenses. See LICENSE.TXT for details. 761931Scokane * 861931Scokane * ===----------------------------------------------------------------------=== 961931Scokane * 1061931Scokane * This file implements __popcountsi2 for the compiler_rt library. 1161931Scokane * 1261931Scokane * ===----------------------------------------------------------------------=== 1361931Scokane */ 1461931Scokane 1561931Scokane#include "int_lib.h" 1661931Scokane 1761931Scokane/* Returns: count of 1 bits */ 1861931Scokane 1961931ScokaneCOMPILER_RT_ABI si_int 2061931Scokane__popcountsi2(si_int a) 2161931Scokane{ 2261931Scokane su_int x = (su_int)a; 2361931Scokane x = x - ((x >> 1) & 0x55555555); 2461931Scokane /* Every 2 bits holds the sum of every pair of bits */ 2561931Scokane x = ((x >> 2) & 0x33333333) + (x & 0x33333333); 2661931Scokane /* Every 4 bits holds the sum of every 4-set of bits (3 significant bits) */ 2761931Scokane x = (x + (x >> 4)) & 0x0F0F0F0F; 2861931Scokane /* Every 8 bits holds the sum of every 8-set of bits (4 significant bits) */ 2961931Scokane x = (x + (x >> 16)); 3061931Scokane /* The lower 16 bits hold two 8 bit sums (5 significant bits).*/ 3161931Scokane /* Upper 16 bits are garbage */ 3261931Scokane return (x + (x >> 8)) & 0x0000003F; /* (6 significant bits) */ 3361931Scokane} 3461911Scokane