1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: int128
3//===-- absvti2_test.c - Test __absvti2 -----------------------------------===//
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 __absvti2 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: absolute value
22
23// Effects: aborts if abs(x) < 0
24
25COMPILER_RT_ABI ti_int __absvti2(ti_int a);
26
27int test__absvti2(ti_int a)
28{
29    ti_int x = __absvti2(a);
30    ti_int expected = a;
31    if (expected < 0)
32        expected = -expected;
33    if (x != expected || expected < 0)
34    {
35        twords at;
36        at.all = a;
37        twords xt;
38        xt.all = x;
39        twords expectedt;
40        expectedt.all = expected;
41        printf("error in __absvti2(0x%llX%.16llX) = "
42               "0x%llX%.16llX, expected positive 0x%llX%.16llX\n",
43               at.s.high, at.s.low, xt.s.high, xt.s.low,
44               expectedt.s.high, expectedt.s.low);
45    }
46    return x != expected;
47}
48
49#endif
50
51int main()
52{
53#ifdef CRT_HAS_128BIT
54
55//     if (test__absvti2(make_ti(0x8000000000000000LL, 0)))  // should abort
56//         return 1;
57    if (test__absvti2(0x0000000000000000LL))
58        return 1;
59    if (test__absvti2(0x0000000000000001LL))
60        return 1;
61    if (test__absvti2(0x0000000000000002LL))
62        return 1;
63    if (test__absvti2(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFELL)))
64        return 1;
65    if (test__absvti2(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL)))
66        return 1;
67    if (test__absvti2(make_ti(0x8000000000000000LL, 0x0000000000000001LL)))
68        return 1;
69    if (test__absvti2(make_ti(0x8000000000000000LL, 0x0000000000000002LL)))
70        return 1;
71    if (test__absvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFELL)))
72        return 1;
73    if (test__absvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL)))
74        return 1;
75
76    int i;
77    for (i = 0; i < 10000; ++i)
78        if (test__absvti2(make_ti(((ti_int)rand() << 32) | rand(),
79                                  ((ti_int)rand() << 32) | rand())))
80            return 1;
81#else
82    printf("skipped\n");
83#endif
84    return 0;
85}
86