xref: /aosp_15_r20/external/compiler-rt/test/builtins/Unit/popcountti2_test.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- popcountti2_test.c - Test __popcountti2 ----------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is dual licensed under the MIT and the University of Illinois Open
6*7c3d14c8STreehugger Robot // Source Licenses. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file tests __popcountti2 for the compiler_rt library.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot #include "int_lib.h"
15*7c3d14c8STreehugger Robot #include <stdio.h>
16*7c3d14c8STreehugger Robot #include <stdlib.h>
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot #ifdef CRT_HAS_128BIT
19*7c3d14c8STreehugger Robot 
20*7c3d14c8STreehugger Robot // Returns: count of 1 bits
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot COMPILER_RT_ABI si_int __popcountti2(ti_int a);
23*7c3d14c8STreehugger Robot 
naive_popcount(ti_int a)24*7c3d14c8STreehugger Robot int naive_popcount(ti_int a)
25*7c3d14c8STreehugger Robot {
26*7c3d14c8STreehugger Robot     int r = 0;
27*7c3d14c8STreehugger Robot     for (; a; a = (tu_int)a >> 1)
28*7c3d14c8STreehugger Robot         r += a & 1;
29*7c3d14c8STreehugger Robot     return r;
30*7c3d14c8STreehugger Robot }
31*7c3d14c8STreehugger Robot 
test__popcountti2(ti_int a)32*7c3d14c8STreehugger Robot int test__popcountti2(ti_int a)
33*7c3d14c8STreehugger Robot {
34*7c3d14c8STreehugger Robot     si_int x = __popcountti2(a);
35*7c3d14c8STreehugger Robot     si_int expected = naive_popcount(a);
36*7c3d14c8STreehugger Robot     if (x != expected)
37*7c3d14c8STreehugger Robot     {
38*7c3d14c8STreehugger Robot         twords at;
39*7c3d14c8STreehugger Robot         at.all = a;
40*7c3d14c8STreehugger Robot         printf("error in __popcountti2(0x%.16llX%.16llX) = %d, expected %d\n",
41*7c3d14c8STreehugger Robot                at.s.high, at.s.low, x, expected);
42*7c3d14c8STreehugger Robot     }
43*7c3d14c8STreehugger Robot     return x != expected;
44*7c3d14c8STreehugger Robot }
45*7c3d14c8STreehugger Robot 
46*7c3d14c8STreehugger Robot char assumption_1[sizeof(ti_int) == 2*sizeof(di_int)] = {0};
47*7c3d14c8STreehugger Robot char assumption_2[sizeof(di_int)*CHAR_BIT == 64] = {0};
48*7c3d14c8STreehugger Robot 
49*7c3d14c8STreehugger Robot #endif
50*7c3d14c8STreehugger Robot 
main()51*7c3d14c8STreehugger Robot int main()
52*7c3d14c8STreehugger Robot {
53*7c3d14c8STreehugger Robot #ifdef CRT_HAS_128BIT
54*7c3d14c8STreehugger Robot     if (test__popcountti2(0))
55*7c3d14c8STreehugger Robot         return 1;
56*7c3d14c8STreehugger Robot     if (test__popcountti2(1))
57*7c3d14c8STreehugger Robot         return 1;
58*7c3d14c8STreehugger Robot     if (test__popcountti2(2))
59*7c3d14c8STreehugger Robot         return 1;
60*7c3d14c8STreehugger Robot     if (test__popcountti2(0xFFFFFFFFFFFFFFFDLL))
61*7c3d14c8STreehugger Robot         return 1;
62*7c3d14c8STreehugger Robot     if (test__popcountti2(0xFFFFFFFFFFFFFFFELL))
63*7c3d14c8STreehugger Robot         return 1;
64*7c3d14c8STreehugger Robot     if (test__popcountti2(0xFFFFFFFFFFFFFFFFLL))
65*7c3d14c8STreehugger Robot         return 1;
66*7c3d14c8STreehugger Robot     if (test__popcountti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFDLL)))
67*7c3d14c8STreehugger Robot         return 1;
68*7c3d14c8STreehugger Robot     if (test__popcountti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFELL)))
69*7c3d14c8STreehugger Robot         return 1;
70*7c3d14c8STreehugger Robot     if (test__popcountti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL)))
71*7c3d14c8STreehugger Robot         return 1;
72*7c3d14c8STreehugger Robot     int i;
73*7c3d14c8STreehugger Robot     for (i = 0; i < 10000; ++i)
74*7c3d14c8STreehugger Robot         if (test__popcountti2(((ti_int)rand() << 96) | ((ti_int)rand() << 64) |
75*7c3d14c8STreehugger Robot                               ((ti_int)rand() << 32) | rand()))
76*7c3d14c8STreehugger Robot             return 1;
77*7c3d14c8STreehugger Robot 
78*7c3d14c8STreehugger Robot #else
79*7c3d14c8STreehugger Robot     printf("skipped\n");
80*7c3d14c8STreehugger Robot #endif
81*7c3d14c8STreehugger Robot    return 0;
82*7c3d14c8STreehugger Robot }
83