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