xref: /aosp_15_r20/external/compiler-rt/test/builtins/Unit/parityti2_test.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- parityti2_test.c - Test __parityti2 -------------------------------===//
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 __parityti2 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: 1 if number of bits is odd else returns 0
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot COMPILER_RT_ABI si_int __parityti2(ti_int a);
23*7c3d14c8STreehugger Robot 
naive_parity(ti_int a)24*7c3d14c8STreehugger Robot int naive_parity(ti_int a)
25*7c3d14c8STreehugger Robot {
26*7c3d14c8STreehugger Robot     int r = 0;
27*7c3d14c8STreehugger Robot     for (; a; a = a & (a - 1))
28*7c3d14c8STreehugger Robot         r = ~r;
29*7c3d14c8STreehugger Robot     return r & 1;
30*7c3d14c8STreehugger Robot }
31*7c3d14c8STreehugger Robot 
test__parityti2(ti_int a)32*7c3d14c8STreehugger Robot int test__parityti2(ti_int a)
33*7c3d14c8STreehugger Robot {
34*7c3d14c8STreehugger Robot     si_int x = __parityti2(a);
35*7c3d14c8STreehugger Robot     si_int expected = naive_parity(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 __parityti2(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     int i;
55*7c3d14c8STreehugger Robot     for (i = 0; i < 10000; ++i)
56*7c3d14c8STreehugger Robot         if (test__parityti2(((ti_int)rand() << 96) + ((ti_int)rand() << 64) +
57*7c3d14c8STreehugger Robot                             ((ti_int)rand() << 32) + rand()))
58*7c3d14c8STreehugger Robot             return 1;
59*7c3d14c8STreehugger Robot 
60*7c3d14c8STreehugger Robot #else
61*7c3d14c8STreehugger Robot     printf("skipped\n");
62*7c3d14c8STreehugger Robot #endif
63*7c3d14c8STreehugger Robot    return 0;
64*7c3d14c8STreehugger Robot }
65