1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_popcountdi2
3
4#include "int_lib.h"
5#include <stdio.h>
6#include <stdlib.h>
7
8// Returns: count of 1 bits
9
10COMPILER_RT_ABI int __popcountdi2(di_int a);
11
12int naive_popcount(di_int a)
13{
14 int r = 0;
15 for (; a; a = (du_int)a >> 1)
16 r += a & 1;
17 return r;
18}
19
20int test__popcountdi2(di_int a)
21{
22 int x = __popcountdi2(a);
23 int expected = naive_popcount(a);
24 if (x != expected)
25 printf(format: "error in __popcountdi2(0x%llX) = %d, expected %d\n",
26 a, x, expected);
27 return x != expected;
28}
29
30char assumption_1[sizeof(di_int) == 2*sizeof(si_int)] = {0};
31char assumption_2[sizeof(si_int)*CHAR_BIT == 32] = {0};
32
33int main()
34{
35 if (test__popcountdi2(a: 0))
36 return 1;
37 if (test__popcountdi2(a: 1))
38 return 1;
39 if (test__popcountdi2(a: 2))
40 return 1;
41 if (test__popcountdi2(a: 0xFFFFFFFFFFFFFFFDLL))
42 return 1;
43 if (test__popcountdi2(a: 0xFFFFFFFFFFFFFFFELL))
44 return 1;
45 if (test__popcountdi2(a: 0xFFFFFFFFFFFFFFFFLL))
46 return 1;
47 int i;
48 for (i = 0; i < 10000; ++i)
49 if (test__popcountdi2(a: ((di_int)rand() << 32) | rand()))
50 return 1;
51
52 return 0;
53}
54

source code of compiler-rt/test/builtins/Unit/popcountdi2_test.c