blob: 0c0117dfeb457e4232274b228d81b7e780a3e762 [file] [log] [blame]
Weiming Zhao9b7bbec2017-03-21 05:32:51 +00001// RUN: %clang_builtins %s %librt -o %t && %run %t
Daniel Dunbarfd089992009-06-26 16:47:03 +00002//===-- absvti2_test.c - Test __absvti2 -----------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
Howard Hinnant5b791f62010-11-16 22:13:33 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Daniel Dunbarfd089992009-06-26 16:47:03 +00008//
9//===----------------------------------------------------------------------===//
10//
11// This file tests __absvti2 for the compiler_rt library.
12//
13//===----------------------------------------------------------------------===//
14
Daniel Dunbarfd089992009-06-26 16:47:03 +000015#include "int_lib.h"
16#include <stdio.h>
Shantonu Sen14675922009-10-28 15:46:10 +000017#include <stdlib.h>
Daniel Dunbarfd089992009-06-26 16:47:03 +000018
Joerg Sonnenberger14743122014-03-18 21:35:30 +000019#ifdef CRT_HAS_128BIT
20
Daniel Dunbarfd089992009-06-26 16:47:03 +000021// Returns: absolute value
22
23// Effects: aborts if abs(x) < 0
24
Derek Schuffeb0ebc32015-04-24 15:45:57 +000025COMPILER_RT_ABI ti_int __absvti2(ti_int a);
Daniel Dunbarfd089992009-06-26 16:47:03 +000026
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",
Daniel Dunbar64857202009-10-27 17:49:07 +000043 at.s.high, at.s.low, xt.s.high, xt.s.low,
44 expectedt.s.high, expectedt.s.low);
Daniel Dunbarfd089992009-06-26 16:47:03 +000045 }
46 return x != expected;
47}
48
49#endif
50
51int main()
52{
Joerg Sonnenberger14743122014-03-18 21:35:30 +000053#ifdef CRT_HAS_128BIT
Daniel Dunbarfd089992009-06-26 16:47:03 +000054
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;
Joerg Sonnenbergerd9bcddd2011-05-29 21:43:29 +000081#else
82 printf("skipped\n");
Daniel Dunbarfd089992009-06-26 16:47:03 +000083#endif
84 return 0;
85}