1// RUN: %clang_builtins %s %librt -o %t && %run %t
2//===-- paritydi2_test.c - Test __paritydi2 -------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// This file tests __paritydi2 for the compiler_rt library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "int_lib.h"
15#include <stdio.h>
16#include <stdlib.h>
17
18// Returns: 1 if number of bits is odd else returns 0
19
20COMPILER_RT_ABI si_int __paritydi2(di_int a);
21
22int naive_parity(di_int a)
23{
24    int r = 0;
25    for (; a; a = a & (a - 1))
26        r = ~r;
27    return r & 1;
28}
29
30int test__paritydi2(di_int a)
31{
32    si_int x = __paritydi2(a);
33    si_int expected = naive_parity(a);
34    if (x != expected)
35        printf("error in __paritydi2(0x%llX) = %d, expected %d\n",
36               a, x, expected);
37    return x != expected;
38}
39
40char assumption_1[sizeof(di_int) == 2*sizeof(si_int)] = {0};
41char assumption_2[sizeof(si_int)*CHAR_BIT == 32] = {0};
42
43int main()
44{
45    int i;
46    for (i = 0; i < 10000; ++i)
47        if (test__paritydi2(((di_int)rand() << 32) + rand()))
48            return 1;
49
50   return 0;
51}
52