Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / builtins / Unit / popcountsi2_test.c
blob768926751474491f608662cd1aaf0952d3b28874
1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_popcountsi2
4 #include "int_lib.h"
5 #include <stdio.h>
6 #include <stdlib.h>
8 // Returns: count of 1 bits
10 COMPILER_RT_ABI int __popcountsi2(si_int a);
12 int naive_popcount(si_int a)
14 int r = 0;
15 for (; a; a = (su_int)a >> 1)
16 r += a & 1;
17 return r;
20 int test__popcountsi2(si_int a)
22 si_int x = __popcountsi2(a);
23 si_int expected = naive_popcount(a);
24 if (x != expected)
25 printf("error in __popcountsi2(0x%X) = %d, expected %d\n",
26 a, x, expected);
27 return x != expected;
30 char assumption_2[sizeof(si_int)*CHAR_BIT == 32] = {0};
32 int main()
34 if (test__popcountsi2(0))
35 return 1;
36 if (test__popcountsi2(1))
37 return 1;
38 if (test__popcountsi2(2))
39 return 1;
40 if (test__popcountsi2(0xFFFFFFFD))
41 return 1;
42 if (test__popcountsi2(0xFFFFFFFE))
43 return 1;
44 if (test__popcountsi2(0xFFFFFFFF))
45 return 1;
46 int i;
47 for (i = 0; i < 10000; ++i)
48 if (test__popcountsi2(rand()))
49 return 1;
51 return 0;