| 1 | // RUN: %clang_builtins %s %librt -o %t && %run %t |
| 2 | // REQUIRES: librt_has_subtf3 |
| 3 | |
| 4 | #include <fenv.h> |
| 5 | #include <stdio.h> |
| 6 | |
| 7 | #if __LDBL_MANT_DIG__ == 113 |
| 8 | |
| 9 | #include "int_lib.h" |
| 10 | #include "fp_test.h" |
| 11 | |
| 12 | // Returns: a - b |
| 13 | COMPILER_RT_ABI long double __subtf3(long double a, long double b); |
| 14 | |
| 15 | int test__subtf3(long double a, long double b, |
| 16 | uint64_t expectedHi, uint64_t expectedLo) |
| 17 | { |
| 18 | long double x = __subtf3(a, b); |
| 19 | int ret = compareResultF128(x, expectedHi, expectedLo); |
| 20 | |
| 21 | if (ret){ |
| 22 | printf("error in test__subtf3(%.20Lf, %.20Lf) = %.20Lf, " |
| 23 | "expected %.20Lf\n" , a, b, x, |
| 24 | fromRep128(expectedHi, expectedLo)); |
| 25 | } |
| 26 | return ret; |
| 27 | } |
| 28 | |
| 29 | char assumption_1[sizeof(long double) * CHAR_BIT == 128] = {0}; |
| 30 | |
| 31 | #endif |
| 32 | |
| 33 | int main() |
| 34 | { |
| 35 | #if __LDBL_MANT_DIG__ == 113 |
| 36 | // qNaN - any = qNaN |
| 37 | if (test__subtf3(makeQNaN128(), |
| 38 | 0x1.23456789abcdefp+5L, |
| 39 | UINT64_C(0x7fff800000000000), |
| 40 | UINT64_C(0x0))) |
| 41 | return 1; |
| 42 | // NaN - any = NaN |
| 43 | if (test__subtf3(makeNaN128(UINT64_C(0x800030000000)), |
| 44 | 0x1.23456789abcdefp+5L, |
| 45 | UINT64_C(0x7fff800000000000), |
| 46 | UINT64_C(0x0))) |
| 47 | return 1; |
| 48 | // inf - any = inf |
| 49 | if (test__subtf3(makeInf128(), |
| 50 | 0x1.23456789abcdefp+5L, |
| 51 | UINT64_C(0x7fff000000000000), |
| 52 | UINT64_C(0x0))) |
| 53 | return 1; |
| 54 | // any - any |
| 55 | if (test__subtf3(0x1.234567829a3bcdef5678ade36734p+5L, |
| 56 | 0x1.ee9d7c52354a6936ab8d7654321fp-1L, |
| 57 | UINT64_C(0x40041b8af1915166), |
| 58 | UINT64_C(0xa44a7bca780a166c))) |
| 59 | return 1; |
| 60 | |
| 61 | #if (defined(__arm__) || defined(__aarch64__)) && defined(__ARM_FP) || \ |
| 62 | defined(i386) || defined(__x86_64__) || (defined(__loongarch__) && \ |
| 63 | __loongarch_frlen != 0) |
| 64 | // Rounding mode tests on supported architectures |
| 65 | const long double m = 1234.02L, n = 0.01L; |
| 66 | |
| 67 | fesetround(FE_UPWARD); |
| 68 | if (test__subtf3(m, n, |
| 69 | UINT64_C(0x40093480a3d70a3d), |
| 70 | UINT64_C(0x70a3d70a3d70a3d7))) |
| 71 | return 1; |
| 72 | |
| 73 | fesetround(FE_DOWNWARD); |
| 74 | if (test__subtf3(m, n, |
| 75 | UINT64_C(0x40093480a3d70a3d), |
| 76 | UINT64_C(0x70a3d70a3d70a3d6))) |
| 77 | return 1; |
| 78 | |
| 79 | fesetround(FE_TOWARDZERO); |
| 80 | if (test__subtf3(m, n, |
| 81 | UINT64_C(0x40093480a3d70a3d), |
| 82 | UINT64_C(0x70a3d70a3d70a3d6))) |
| 83 | return 1; |
| 84 | |
| 85 | fesetround(FE_TONEAREST); |
| 86 | if (test__subtf3(m, n, |
| 87 | UINT64_C(0x40093480a3d70a3d), |
| 88 | UINT64_C(0x70a3d70a3d70a3d7))) |
| 89 | return 1; |
| 90 | #endif |
| 91 | |
| 92 | #else |
| 93 | printf(format: "skipped\n" ); |
| 94 | |
| 95 | #endif |
| 96 | return 0; |
| 97 | } |
| 98 | |