xref: /rk3399_ARM-atf/lib/compiler-rt/builtins/popcountdi2.c (revision 8a6a9560b5dcccfb68064c0c8c9b4b47981c6ac7)
1*8a6a9560SDaniel Boulby //===-- popcountdi2.c - Implement __popcountdi2 ---------------------------===//
2*8a6a9560SDaniel Boulby //
3*8a6a9560SDaniel Boulby // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*8a6a9560SDaniel Boulby // See https://llvm.org/LICENSE.txt for license information.
5*8a6a9560SDaniel Boulby // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*8a6a9560SDaniel Boulby //
7*8a6a9560SDaniel Boulby //===----------------------------------------------------------------------===//
8*8a6a9560SDaniel Boulby //
9*8a6a9560SDaniel Boulby // This file implements __popcountdi2 for the compiler_rt library.
10*8a6a9560SDaniel Boulby //
11*8a6a9560SDaniel Boulby //===----------------------------------------------------------------------===//
12162fc183SLionel Debieve 
13162fc183SLionel Debieve #include "int_lib.h"
14162fc183SLionel Debieve 
15*8a6a9560SDaniel Boulby // Returns: count of 1 bits
16162fc183SLionel Debieve 
17*8a6a9560SDaniel Boulby COMPILER_RT_ABI int __popcountdi2(di_int a) {
18162fc183SLionel Debieve   du_int x2 = (du_int)a;
19162fc183SLionel Debieve   x2 = x2 - ((x2 >> 1) & 0x5555555555555555uLL);
20*8a6a9560SDaniel Boulby   // Every 2 bits holds the sum of every pair of bits (32)
21162fc183SLionel Debieve   x2 = ((x2 >> 2) & 0x3333333333333333uLL) + (x2 & 0x3333333333333333uLL);
22*8a6a9560SDaniel Boulby   // Every 4 bits holds the sum of every 4-set of bits (3 significant bits) (16)
23162fc183SLionel Debieve   x2 = (x2 + (x2 >> 4)) & 0x0F0F0F0F0F0F0F0FuLL;
24*8a6a9560SDaniel Boulby   // Every 8 bits holds the sum of every 8-set of bits (4 significant bits) (8)
25162fc183SLionel Debieve   su_int x = (su_int)(x2 + (x2 >> 32));
26*8a6a9560SDaniel Boulby   // The lower 32 bits hold four 16 bit sums (5 significant bits).
27*8a6a9560SDaniel Boulby   //   Upper 32 bits are garbage
28162fc183SLionel Debieve   x = x + (x >> 16);
29*8a6a9560SDaniel Boulby   // The lower 16 bits hold two 32 bit sums (6 significant bits).
30*8a6a9560SDaniel Boulby   //   Upper 16 bits are garbage
31*8a6a9560SDaniel Boulby   return (x + (x >> 8)) & 0x0000007F; // (7 significant bits)
32162fc183SLionel Debieve }
33