1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_trunctfhf2
3 
4 #include "int_lib.h"
5 #include <stdio.h>
6 
7 #if __LDBL_MANT_DIG__ == 113 && defined(COMPILER_RT_HAS_FLOAT16)
8 
9 #include "fp_test.h"
10 
11 TYPE_FP16 __trunctfhf2(long double a);
12 
test__trunctfhf2(long double a,uint16_t expected)13 int test__trunctfhf2(long double a, uint16_t expected) {
14   TYPE_FP16 x = __trunctfhf2(a);
15   int ret = compareResultH(x, expected);
16 
17   if (ret) {
18     printf("error in test__trunctfhf2(%.20Lf) = %#.4x, "
19            "expected %#.4x\n",
20            a, toRep16(x), expected);
21   }
22   return ret;
23 }
24 
25 char assumption_1[sizeof(TYPE_FP16) * CHAR_BIT == 16] = {0};
26 
27 #endif
28 
main()29 int main() {
30 #if __LDBL_MANT_DIG__ == 113 && defined(COMPILER_RT_HAS_FLOAT16)
31   // qNaN
32   if (test__trunctfhf2(makeQNaN128(),
33                        UINT16_C(0x7e00)))
34     return 1;
35   // NaN
36   if (test__trunctfhf2(makeNaN128(UINT64_C(0x810000000000)),
37                        UINT16_C(0x7e00)))
38     return 1;
39   // inf
40   if (test__trunctfhf2(makeInf128(),
41                        UINT16_C(0x7c00)))
42     return 1;
43   if (test__trunctfhf2(-makeInf128(),
44                        UINT16_C(0xfc00)))
45     return 1;
46   // zero
47   if (test__trunctfhf2(0.0L, UINT16_C(0x0)))
48     return 1;
49   if (test__trunctfhf2(-0.0L, UINT16_C(0x8000)))
50     return 1;
51 
52   if (test__trunctfhf2(3.1415926535L,
53                        UINT16_C(0x4248)))
54     return 1;
55   if (test__trunctfhf2(-3.1415926535L,
56                        UINT16_C(0xc248)))
57     return 1;
58   if (test__trunctfhf2(0x1.987124876876324p+100L,
59                        UINT16_C(0x7c00)))
60     return 1;
61   if (test__trunctfhf2(0x1.987124876876324p+12L,
62                        UINT16_C(0x6e62)))
63     return 1;
64   if (test__trunctfhf2(0x1.0p+0L,
65                        UINT16_C(0x3c00)))
66     return 1;
67   if (test__trunctfhf2(0x1.0p-14L,
68                        UINT16_C(0x0400)))
69     return 1;
70   // denormal
71   if (test__trunctfhf2(0x1.0p-20L,
72                        UINT16_C(0x0010)))
73     return 1;
74   if (test__trunctfhf2(0x1.0p-24L,
75                        UINT16_C(0x0001)))
76     return 1;
77   if (test__trunctfhf2(-0x1.0p-24L,
78                        UINT16_C(0x8001)))
79     return 1;
80   if (test__trunctfhf2(0x1.5p-25L,
81                        UINT16_C(0x0001)))
82     return 1;
83   // and back to zero
84   if (test__trunctfhf2(0x1.0p-25L,
85                        UINT16_C(0x0000)))
86     return 1;
87   if (test__trunctfhf2(-0x1.0p-25L,
88                        UINT16_C(0x8000)))
89     return 1;
90   // max (precise)
91   if (test__trunctfhf2(65504.0L,
92                        UINT16_C(0x7bff)))
93     return 1;
94   // max (rounded)
95   if (test__trunctfhf2(65519.0L,
96                        UINT16_C(0x7bff)))
97     return 1;
98   // max (to +inf)
99   if (test__trunctfhf2(65520.0L,
100                        UINT16_C(0x7c00)))
101     return 1;
102   if (test__trunctfhf2(65536.0L,
103                        UINT16_C(0x7c00)))
104     return 1;
105   if (test__trunctfhf2(-65520.0L,
106                        UINT16_C(0xfc00)))
107     return 1;
108 
109   if (test__trunctfhf2(0x1.23a2abb4a2ddee355f36789abcdep+5L,
110                        UINT16_C(0x508f)))
111     return 1;
112   if (test__trunctfhf2(0x1.e3d3c45bd3abfd98b76a54cc321fp-9L,
113                        UINT16_C(0x1b8f)))
114     return 1;
115   if (test__trunctfhf2(0x1.234eebb5faa678f4488693abcdefp+453L,
116                        UINT16_C(0x7c00)))
117     return 1;
118   if (test__trunctfhf2(0x1.edcba9bb8c76a5a43dd21f334634p-43L,
119                        UINT16_C(0x0)))
120     return 1;
121 #else
122   printf("skipped\n");
123 #endif
124   return 0;
125 }
126