11fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan/* ===-- popcountdi2.c - Implement __popcountdi2 ----------------------------=== 21fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan * 31fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan * The LLVM Compiler Infrastructure 41fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan * 59ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant * This file is dual licensed under the MIT and the University of Illinois Open 69ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant * Source Licenses. See LICENSE.TXT for details. 71fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan * 81fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan * ===----------------------------------------------------------------------=== 91fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan * 101fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan * This file implements __popcountdi2 for the compiler_rt library. 111fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan * 121fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan * ===----------------------------------------------------------------------=== 131fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan */ 14b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar 15b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#include "int_lib.h" 16b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar 171fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan/* Returns: count of 1 bits */ 18b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar 191c5f89b1dd741135a4007ab577723d422f421eecAnton KorobeynikovCOMPILER_RT_ABI si_int 20b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar__popcountdi2(di_int a) 21b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar{ 22b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar du_int x2 = (du_int)a; 23b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar x2 = x2 - ((x2 >> 1) & 0x5555555555555555uLL); 241fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan /* Every 2 bits holds the sum of every pair of bits (32) */ 25b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar x2 = ((x2 >> 2) & 0x3333333333333333uLL) + (x2 & 0x3333333333333333uLL); 261fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan /* Every 4 bits holds the sum of every 4-set of bits (3 significant bits) (16) */ 27b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar x2 = (x2 + (x2 >> 4)) & 0x0F0F0F0F0F0F0F0FuLL; 281fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan /* Every 8 bits holds the sum of every 8-set of bits (4 significant bits) (8) */ 29b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar su_int x = (su_int)(x2 + (x2 >> 32)); 301fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan /* The lower 32 bits hold four 16 bit sums (5 significant bits). */ 311fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan /* Upper 32 bits are garbage */ 32b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar x = x + (x >> 16); 331fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan /* The lower 16 bits hold two 32 bit sums (6 significant bits). */ 341fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan /* Upper 16 bits are garbage */ 351fcb40b79d8fbfcc9acb0966d5f9bba09431f832Edward O'Callaghan return (x + (x >> 8)) & 0x0000007F; /* (7 significant bits) */ 36b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar} 37