1 | // RUN: %clang_builtins %s %librt -o %t && %run %t |
---|---|
2 | // REQUIRES: librt_has_paritydi2 |
3 | |
4 | #include "int_lib.h" |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | |
8 | // Returns: 1 if number of bits is odd else returns 0 |
9 | |
10 | COMPILER_RT_ABI int __paritydi2(di_int a); |
11 | |
12 | int naive_parity(di_int a) |
13 | { |
14 | int r = 0; |
15 | for (; a; a = a & (a - 1)) |
16 | r = ~r; |
17 | return r & 1; |
18 | } |
19 | |
20 | int test__paritydi2(di_int a) |
21 | { |
22 | si_int x = __paritydi2(a); |
23 | si_int expected = naive_parity(a); |
24 | if (x != expected) |
25 | printf(format: "error in __paritydi2(0x%llX) = %d, expected %d\n", |
26 | a, x, expected); |
27 | return x != expected; |
28 | } |
29 | |
30 | char assumption_1[sizeof(di_int) == 2*sizeof(si_int)] = {0}; |
31 | char assumption_2[sizeof(si_int)*CHAR_BIT == 32] = {0}; |
32 | |
33 | int main() |
34 | { |
35 | int i; |
36 | for (i = 0; i < 10000; ++i) |
37 | if (test__paritydi2(a: ((di_int)rand() << 32) + rand())) |
38 | return 1; |
39 | |
40 | return 0; |
41 | } |
42 |