1 //===-- popcountdi2_test.c - Test __popcountdi2 ----------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file tests __popcountdi2 for the compiler_rt library. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "int_lib.h" 15 #include <stdio.h> 16 #include <stdlib.h> 17 18 // Returns: count of 1 bits 19 20 COMPILER_RT_ABI si_int __popcountdi2(di_int a); 21 22 int naive_popcount(di_int a) 23 { 24 int r = 0; 25 for (; a; a = (du_int)a >> 1) 26 r += a & 1; 27 return r; 28 } 29 30 int test__popcountdi2(di_int a) 31 { 32 si_int x = __popcountdi2(a); 33 si_int expected = naive_popcount(a); 34 if (x != expected) 35 printf("error in __popcountdi2(0x%llX) = %d, expected %d\n", 36 a, x, expected); 37 return x != expected; 38 } 39 40 char assumption_1[sizeof(di_int) == 2*sizeof(si_int)] = {0}; 41 char assumption_2[sizeof(si_int)*CHAR_BIT == 32] = {0}; 42 43 int main() 44 { 45 if (test__popcountdi2(0)) 46 return 1; 47 if (test__popcountdi2(1)) 48 return 1; 49 if (test__popcountdi2(2)) 50 return 1; 51 if (test__popcountdi2(0xFFFFFFFFFFFFFFFDLL)) 52 return 1; 53 if (test__popcountdi2(0xFFFFFFFFFFFFFFFELL)) 54 return 1; 55 if (test__popcountdi2(0xFFFFFFFFFFFFFFFFLL)) 56 return 1; 57 int i; 58 for (i = 0; i < 10000; ++i) 59 if (test__popcountdi2(((di_int)rand() << 32) | rand())) 60 return 1; 61 62 return 0; 63 } 64