blob: cf4fc53647fe12c0b140556bb02711d3be406ad2 [file] [log] [blame]
Weiming Zhao9b7bbec2017-03-21 05:32:51 +00001// RUN: %clang_builtins %s %librt -o %t && %run %t
Dan Liew5be7eb32019-10-17 18:12:49 +00002// REQUIRES: librt_has_absvti2
Reid Klecknerdffe5a32018-11-01 00:00:03 +00003// REQUIRES: int128
Daniel Dunbarfd089992009-06-26 16:47:03 +00004
Daniel Dunbarfd089992009-06-26 16:47:03 +00005#include "int_lib.h"
6#include <stdio.h>
Shantonu Sen14675922009-10-28 15:46:10 +00007#include <stdlib.h>
Daniel Dunbarfd089992009-06-26 16:47:03 +00008
Joerg Sonnenberger14743122014-03-18 21:35:30 +00009#ifdef CRT_HAS_128BIT
10
Daniel Dunbarfd089992009-06-26 16:47:03 +000011// Returns: absolute value
12
13// Effects: aborts if abs(x) < 0
14
Derek Schuffeb0ebc32015-04-24 15:45:57 +000015COMPILER_RT_ABI ti_int __absvti2(ti_int a);
Daniel Dunbarfd089992009-06-26 16:47:03 +000016
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",
Daniel Dunbar64857202009-10-27 17:49:07 +000033 at.s.high, at.s.low, xt.s.high, xt.s.low,
34 expectedt.s.high, expectedt.s.low);
Daniel Dunbarfd089992009-06-26 16:47:03 +000035 }
36 return x != expected;
37}
38
39#endif
40
41int main()
42{
Joerg Sonnenberger14743122014-03-18 21:35:30 +000043#ifdef CRT_HAS_128BIT
Daniel Dunbarfd089992009-06-26 16:47:03 +000044
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;
Joerg Sonnenbergerd9bcddd2011-05-29 21:43:29 +000071#else
72 printf("skipped\n");
Daniel Dunbarfd089992009-06-26 16:47:03 +000073#endif
74 return 0;
75}