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