Weiming Zhao | 9b7bbec | 2017-03-21 05:32:51 +0000 | [diff] [blame] | 1 | // RUN: %clang_builtins %s %librt -o %t && %run %t |
Dan Liew | 5be7eb3 | 2019-10-17 18:12:49 +0000 | [diff] [blame] | 2 | // REQUIRES: librt_has_absvti2 |
Reid Kleckner | dffe5a3 | 2018-11-01 00:00:03 +0000 | [diff] [blame] | 3 | // REQUIRES: int128 |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 4 | |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 5 | #include "int_lib.h" |
| 6 | #include <stdio.h> |
Shantonu Sen | 1467592 | 2009-10-28 15:46:10 +0000 | [diff] [blame] | 7 | #include <stdlib.h> |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 8 | |
Joerg Sonnenberger | 1474312 | 2014-03-18 21:35:30 +0000 | [diff] [blame] | 9 | #ifdef CRT_HAS_128BIT |
| 10 | |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 11 | // Returns: absolute value |
| 12 | |
| 13 | // Effects: aborts if abs(x) < 0 |
| 14 | |
Derek Schuff | eb0ebc3 | 2015-04-24 15:45:57 +0000 | [diff] [blame] | 15 | COMPILER_RT_ABI ti_int __absvti2(ti_int a); |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 16 | |
| 17 | int 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 Dunbar | 6485720 | 2009-10-27 17:49:07 +0000 | [diff] [blame] | 33 | at.s.high, at.s.low, xt.s.high, xt.s.low, |
| 34 | expectedt.s.high, expectedt.s.low); |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 35 | } |
| 36 | return x != expected; |
| 37 | } |
| 38 | |
| 39 | #endif |
| 40 | |
| 41 | int main() |
| 42 | { |
Joerg Sonnenberger | 1474312 | 2014-03-18 21:35:30 +0000 | [diff] [blame] | 43 | #ifdef CRT_HAS_128BIT |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 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; |
Joerg Sonnenberger | d9bcddd | 2011-05-29 21:43:29 +0000 | [diff] [blame] | 71 | #else |
| 72 | printf("skipped\n"); |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 73 | #endif |
| 74 | return 0; |
| 75 | } |