xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/popcountdi2.c (revision 156cd5872b4a1450b5c9c406f078c770980674de)
1*156cd587Sjoerg /* ===-- popcountdi2.c - Implement __popcountdi2 ----------------------------===
2*156cd587Sjoerg  *
3*156cd587Sjoerg  *                     The LLVM Compiler Infrastructure
4*156cd587Sjoerg  *
5*156cd587Sjoerg  * This file is dual licensed under the MIT and the University of Illinois Open
6*156cd587Sjoerg  * Source Licenses. See LICENSE.TXT for details.
7*156cd587Sjoerg  *
8*156cd587Sjoerg  * ===----------------------------------------------------------------------===
9*156cd587Sjoerg  *
10*156cd587Sjoerg  * This file implements __popcountdi2 for the compiler_rt library.
11*156cd587Sjoerg  *
12*156cd587Sjoerg  * ===----------------------------------------------------------------------===
13*156cd587Sjoerg  */
14*156cd587Sjoerg 
15*156cd587Sjoerg #include "int_lib.h"
16*156cd587Sjoerg 
17*156cd587Sjoerg /* Returns: count of 1 bits */
18*156cd587Sjoerg 
19*156cd587Sjoerg COMPILER_RT_ABI si_int
__popcountdi2(di_int a)20*156cd587Sjoerg __popcountdi2(di_int a)
21*156cd587Sjoerg {
22*156cd587Sjoerg     du_int x2 = (du_int)a;
23*156cd587Sjoerg     x2 = x2 - ((x2 >> 1) & 0x5555555555555555uLL);
24*156cd587Sjoerg     /* Every 2 bits holds the sum of every pair of bits (32) */
25*156cd587Sjoerg     x2 = ((x2 >> 2) & 0x3333333333333333uLL) + (x2 & 0x3333333333333333uLL);
26*156cd587Sjoerg     /* Every 4 bits holds the sum of every 4-set of bits (3 significant bits) (16) */
27*156cd587Sjoerg     x2 = (x2 + (x2 >> 4)) & 0x0F0F0F0F0F0F0F0FuLL;
28*156cd587Sjoerg     /* Every 8 bits holds the sum of every 8-set of bits (4 significant bits) (8) */
29*156cd587Sjoerg     su_int x = (su_int)(x2 + (x2 >> 32));
30*156cd587Sjoerg     /* The lower 32 bits hold four 16 bit sums (5 significant bits). */
31*156cd587Sjoerg     /*   Upper 32 bits are garbage */
32*156cd587Sjoerg     x = x + (x >> 16);
33*156cd587Sjoerg     /* The lower 16 bits hold two 32 bit sums (6 significant bits). */
34*156cd587Sjoerg     /*   Upper 16 bits are garbage */
35*156cd587Sjoerg     return (x + (x >> 8)) & 0x0000007F;  /* (7 significant bits) */
36*156cd587Sjoerg }
37