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