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 
test__subtf3(long double a,long double b,uint64_t expectedHi,uint64_t expectedLo)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 = compareResultLD(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 
main()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__)
63     // Rounding mode tests on supported architectures
64     const long double m = 1234.02L, n = 0.01L;
65 
66     fesetround(FE_UPWARD);
67     if (test__subtf3(m, n,
68                      UINT64_C(0x40093480a3d70a3d),
69                      UINT64_C(0x70a3d70a3d70a3d7)))
70         return 1;
71 
72     fesetround(FE_DOWNWARD);
73     if (test__subtf3(m, n,
74                      UINT64_C(0x40093480a3d70a3d),
75                      UINT64_C(0x70a3d70a3d70a3d6)))
76         return 1;
77 
78     fesetround(FE_TOWARDZERO);
79     if (test__subtf3(m, n,
80                      UINT64_C(0x40093480a3d70a3d),
81                      UINT64_C(0x70a3d70a3d70a3d6)))
82         return 1;
83 
84     fesetround(FE_TONEAREST);
85     if (test__subtf3(m, n,
86                      UINT64_C(0x40093480a3d70a3d),
87                      UINT64_C(0x70a3d70a3d70a3d7)))
88         return 1;
89 #endif
90 
91 #else
92     printf("skipped\n");
93 
94 #endif
95     return 0;
96 }
97