popcountti2.c revision 7f2d7c75e713d778106d01a54e7aef40227bbf2d
137a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan/* ===-- popcountti2.c - Implement __popcountti2 ----------------------------=== 237a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * 337a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * The LLVM Compiler Infrastructure 437a6a455466e5b197311771a777ab241e471ed8aEdward 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. 737a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * 837a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * ===----------------------------------------------------------------------=== 937a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * 1037a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * This file implements __popcountti2 for the compiler_rt library. 1137a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * 1237a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * ===----------------------------------------------------------------------=== 1337a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan */ 14b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar 15b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#include "int_lib.h" 16b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar 177f2d7c75e713d778106d01a54e7aef40227bbf2dChandler Carruth#if __x86_64 187f2d7c75e713d778106d01a54e7aef40227bbf2dChandler Carruth 1937a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan/* Returns: count of 1 bits */ 20b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar 21b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarsi_int 22b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar__popcountti2(ti_int a) 23b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar{ 24b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar tu_int x3 = (tu_int)a; 25b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar x3 = x3 - ((x3 >> 1) & (((tu_int)0x5555555555555555uLL << 64) | 26b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar 0x5555555555555555uLL)); 2737a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan /* Every 2 bits holds the sum of every pair of bits (64) */ 28b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar x3 = ((x3 >> 2) & (((tu_int)0x3333333333333333uLL << 64) | 0x3333333333333333uLL)) 29b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar + (x3 & (((tu_int)0x3333333333333333uLL << 64) | 0x3333333333333333uLL)); 3037a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan /* Every 4 bits holds the sum of every 4-set of bits (3 significant bits) (32) */ 31b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar x3 = (x3 + (x3 >> 4)) 32b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar & (((tu_int)0x0F0F0F0F0F0F0F0FuLL << 64) | 0x0F0F0F0F0F0F0F0FuLL); 3337a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan /* Every 8 bits holds the sum of every 8-set of bits (4 significant bits) (16) */ 34b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar du_int x2 = (du_int)(x3 + (x3 >> 64)); 3537a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan /* Every 8 bits holds the sum of every 8-set of bits (5 significant bits) (8) */ 36b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar su_int x = (su_int)(x2 + (x2 >> 32)); 3737a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan /* Every 8 bits holds the sum of every 8-set of bits (6 significant bits) (4) */ 38b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar x = x + (x >> 16); 3937a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan /* Every 8 bits holds the sum of every 8-set of bits (7 significant bits) (2) */ 4037a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan /* Upper 16 bits are garbage */ 4137a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan return (x + (x >> 8)) & 0xFF; /* (8 significant bits) */ 42b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar} 43b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar 44b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#endif 45