1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_popcountti2
3// REQUIRES: int128
4
5#include "int_lib.h"
6#include <stdio.h>
7#include <stdlib.h>
8
9#ifdef CRT_HAS_128BIT
10
11// Returns: count of 1 bits
12
13COMPILER_RT_ABI int __popcountti2(ti_int a);
14
15int naive_popcount(ti_int a)
16{
17 int r = 0;
18 for (; a; a = (tu_int)a >> 1)
19 r += a & 1;
20 return r;
21}
22
23int test__popcountti2(ti_int a)
24{
25 si_int x = __popcountti2(a);
26 si_int expected = naive_popcount(a);
27 if (x != expected)
28 {
29 twords at;
30 at.all = a;
31 printf(format: "error in __popcountti2(0x%.16llX%.16llX) = %d, expected %d\n",
32 at.s.high, at.s.low, x, expected);
33 }
34 return x != expected;
35}
36
37char assumption_1[sizeof(ti_int) == 2*sizeof(di_int)] = {0};
38char assumption_2[sizeof(di_int)*CHAR_BIT == 64] = {0};
39
40#endif
41
42int main()
43{
44#ifdef CRT_HAS_128BIT
45 if (test__popcountti2(a: 0))
46 return 1;
47 if (test__popcountti2(a: 1))
48 return 1;
49 if (test__popcountti2(a: 2))
50 return 1;
51 if (test__popcountti2(a: 0xFFFFFFFFFFFFFFFDLL))
52 return 1;
53 if (test__popcountti2(a: 0xFFFFFFFFFFFFFFFELL))
54 return 1;
55 if (test__popcountti2(a: 0xFFFFFFFFFFFFFFFFLL))
56 return 1;
57 if (test__popcountti2(a: make_ti(h: 0xFFFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFDLL)))
58 return 1;
59 if (test__popcountti2(a: make_ti(h: 0xFFFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFELL)))
60 return 1;
61 if (test__popcountti2(a: make_ti(h: 0xFFFFFFFFFFFFFFFFLL, l: 0xFFFFFFFFFFFFFFFFLL)))
62 return 1;
63 int i;
64 for (i = 0; i < 10000; ++i)
65 if (test__popcountti2(a: ((ti_int)rand() << 96) | ((ti_int)rand() << 64) |
66 ((ti_int)rand() << 32) | rand()))
67 return 1;
68
69#else
70 printf("skipped\n");
71#endif
72 return 0;
73}
74

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