1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_absvti2
3// REQUIRES: int128
4
5#include "int_lib.h"
6#include <stdio.h>
7#include <stdlib.h>
8
9#ifdef CRT_HAS_128BIT
10
11// Returns: absolute value
12
13// Effects: aborts if abs(x) < 0
14
15COMPILER_RT_ABI ti_int __absvti2(ti_int a);
16
17int test__absvti2(ti_int a)
18{
19 ti_int x = __absvti2(a);
20 ti_int expected = a;
21 if (expected < 0)
22 expected = -expected;
23 if (x != expected || expected < 0)
24 {
25 twords at;
26 at.all = a;
27 twords xt;
28 xt.all = x;
29 twords expectedt;
30 expectedt.all = expected;
31 printf("error in __absvti2(0x%llX%.16llX) = "
32 "0x%llX%.16llX, expected positive 0x%llX%.16llX\n",
33 at.s.high, at.s.low, xt.s.high, xt.s.low,
34 expectedt.s.high, expectedt.s.low);
35 }
36 return x != expected;
37}
38
39#endif
40
41int main()
42{
43#ifdef CRT_HAS_128BIT
44
45// if (test__absvti2(make_ti(0x8000000000000000LL, 0))) // should abort
46// return 1;
47 if (test__absvti2(0x0000000000000000LL))
48 return 1;
49 if (test__absvti2(0x0000000000000001LL))
50 return 1;
51 if (test__absvti2(0x0000000000000002LL))
52 return 1;
53 if (test__absvti2(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFELL)))
54 return 1;
55 if (test__absvti2(make_ti(0x7FFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL)))
56 return 1;
57 if (test__absvti2(make_ti(0x8000000000000000LL, 0x0000000000000001LL)))
58 return 1;
59 if (test__absvti2(make_ti(0x8000000000000000LL, 0x0000000000000002LL)))
60 return 1;
61 if (test__absvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFELL)))
62 return 1;
63 if (test__absvti2(make_ti(0xFFFFFFFFFFFFFFFFLL, 0xFFFFFFFFFFFFFFFFLL)))
64 return 1;
65
66 int i;
67 for (i = 0; i < 10000; ++i)
68 if (test__absvti2(make_ti(((ti_int)rand() << 32) | rand(),
69 ((ti_int)rand() << 32) | rand())))
70 return 1;
71#else
72 printf("skipped\n");
73#endif
74 return 0;
75}
76