1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: int128
3//===-- negvti2_test.c - Test __negvti2 -----------------------------------===//
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 __negvti2 for the compiler_rt library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "int_lib.h"
16#include <stdio.h>
17
18#ifdef CRT_HAS_128BIT
19
20// Returns: -a
21
22// Effects: aborts if -a overflows
23
24COMPILER_RT_ABI ti_int __negvti2(ti_int a);
25COMPILER_RT_ABI ti_int __negti2(ti_int a);
26
27int test__negvti2(ti_int a)
28{
29    ti_int x = __negvti2(a);
30    ti_int expected = __negti2(a);
31    if (x != expected)
32    {
33        twords at;
34        at.all = a;
35        twords xt;
36        xt.all = x;
37        twords expectedt;
38        expectedt.all = expected;
39        printf("error in __negvti2(0x%.16llX%.16llX) = 0x%.16llX%.16llX, "
40               "expected 0x%.16llX%.16llX\n",
41               at.s.high, at.s.low, xt.s.high, xt.s.low, expectedt.s.high, expectedt.s.low);
42    }
43    return x != expected;
44}
45
46#endif
47
48int main()
49{
50#ifdef CRT_HAS_128BIT
51    if (test__negvti2(0))
52        return 1;
53    if (test__negvti2(1))
54        return 1;
55    if (test__negvti2(-1))
56        return 1;
57    if (test__negvti2(2))
58        return 1;
59    if (test__negvti2(-2))
60        return 1;
61    if (test__negvti2(3))
62        return 1;
63    if (test__negvti2(-3))
64        return 1;
65    if (test__negvti2(make_ti(0x0000000000000000LL, 0x00000000FFFFFFFELL)))
66        return 1;
67    if (test__negvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFF00000002LL)))
68        return 1;
69    if (test__negvti2(make_ti(0x0000000000000000LL, 0x00000000FFFFFFFFLL)))
70        return 1;
71    if (test__negvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFF00000001LL)))
72        return 1;
73    if (test__negvti2(make_ti(0x0000000000000000LL, 0x0000000100000000LL)))
74        return 1;
75    if (test__negvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFF00000000LL)))
76        return 1;
77    if (test__negvti2(make_ti(0x0000000000000000LL, 0x0000000200000000LL)))
78        return 1;
79    if (test__negvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFE00000000LL)))
80        return 1;
81    if (test__negvti2(make_ti(0x0000000000000000LL, 0x0000000300000000LL)))
82        return 1;
83    if (test__negvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFD00000000LL)))
84        return 1;
85    if (test__negvti2(make_ti(0x0000000000000000LL, 0x7FFFFFFFFFFFFFFFLL)))
86        return 1;
87    if (test__negvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0x8000000000000001LL)))
88        return 1;
89    if (test__negvti2(make_ti(0x0000000000000000LL, 0x7FFFFFFFFFFFFFFFLL)))
90        return 1;
91    if (test__negvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFE00000000LL)))
92        return 1;
93    if (test__negvti2(make_ti(0x0000000000000000LL, 0x0000000200000000LL)))
94        return 1;
95    if (test__negvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFF00000000LL)))
96        return 1;
97    if (test__negvti2(make_ti(0x0000000000000000LL, 0x0000000100000000LL)))
98        return 1;
99//     if (test__negvti2(make_ti(0x8000000000000000LL, 0x0000000000000000LL))) // abort
100//         return 1;
101    if (test__negvti2(make_ti(0x8000000000000000LL, 0x0000000000000001LL)))
102        return 1;
103    if (test__negvti2(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL)))
104        return 1;
105
106#else
107    printf("skipped\n");
108#endif
109   return 0;
110}
111