[GlobalISel] Avoid repeated hash lookups (NFC) (#124393)
[llvm-project.git] / compiler-rt / test / builtins / Unit / popcountdi2_test.c
blob38066162b5777912c093183ebcae19e681dc99e5
1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_popcountdi2
4 #include "int_lib.h"
5 #include <stdio.h>
6 #include <stdlib.h>
8 // Returns: count of 1 bits
10 COMPILER_RT_ABI int __popcountdi2(di_int a);
12 int naive_popcount(di_int a)
14 int r = 0;
15 for (; a; a = (du_int)a >> 1)
16 r += a & 1;
17 return r;
20 int test__popcountdi2(di_int a)
22 int x = __popcountdi2(a);
23 int expected = naive_popcount(a);
24 if (x != expected)
25 printf("error in __popcountdi2(0x%llX) = %d, expected %d\n",
26 a, x, expected);
27 return x != expected;
30 char assumption_1[sizeof(di_int) == 2*sizeof(si_int)] = {0};
31 char assumption_2[sizeof(si_int)*CHAR_BIT == 32] = {0};
33 int main()
35 if (test__popcountdi2(0))
36 return 1;
37 if (test__popcountdi2(1))
38 return 1;
39 if (test__popcountdi2(2))
40 return 1;
41 if (test__popcountdi2(0xFFFFFFFFFFFFFFFDLL))
42 return 1;
43 if (test__popcountdi2(0xFFFFFFFFFFFFFFFELL))
44 return 1;
45 if (test__popcountdi2(0xFFFFFFFFFFFFFFFFLL))
46 return 1;
47 int i;
48 for (i = 0; i < 10000; ++i)
49 if (test__popcountdi2(((di_int)rand() << 32) | rand()))
50 return 1;
52 return 0;