1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_truncdfhf2
3 
4 #include <stdio.h>
5 
6 #include "fp_test.h"
7 
8 TYPE_FP16 __truncdfhf2(double a);
9 
test__truncdfhf2(double a,uint16_t expected)10 int test__truncdfhf2(double a, uint16_t expected)
11 {
12     TYPE_FP16 x = __truncdfhf2(a);
13     int ret = compareResultH(x, expected);
14 
15     if (ret){
16         printf("error in test__truncdfhf2(%lf) = %#.4x, "
17                "expected %#.4x\n", a, toRep16(x), expected);
18     }
19     return ret;
20 }
21 
22 char assumption_1[sizeof(__fp16) * CHAR_BIT == 16] = {0};
23 
main()24 int main()
25 {
26     // qNaN
27     if (test__truncdfhf2(makeQNaN64(),
28                          UINT16_C(0x7e00)))
29         return 1;
30     // NaN
31     if (test__truncdfhf2(makeNaN64(UINT64_C(0x8000)),
32                          UINT16_C(0x7e00)))
33         return 1;
34     // inf
35     if (test__truncdfhf2(makeInf64(),
36                          UINT16_C(0x7c00)))
37         return 1;
38     if (test__truncdfhf2(-makeInf64(),
39                          UINT16_C(0xfc00)))
40         return 1;
41     // zero
42     if (test__truncdfhf2(0.0, UINT16_C(0x0)))
43         return 1;
44     if (test__truncdfhf2(-0.0, UINT16_C(0x8000)))
45         return 1;
46 
47     if (test__truncdfhf2(3.1415926535,
48                          UINT16_C(0x4248)))
49         return 1;
50     if (test__truncdfhf2(-3.1415926535,
51                          UINT16_C(0xc248)))
52         return 1;
53     if (test__truncdfhf2(0x1.987124876876324p+1000,
54                          UINT16_C(0x7c00)))
55         return 1;
56     if (test__truncdfhf2(0x1.987124876876324p+12,
57                          UINT16_C(0x6e62)))
58         return 1;
59     if (test__truncdfhf2(0x1.0p+0,
60                          UINT16_C(0x3c00)))
61         return 1;
62     if (test__truncdfhf2(0x1.0p-14,
63                          UINT16_C(0x0400)))
64         return 1;
65     // denormal
66     if (test__truncdfhf2(0x1.0p-20,
67                          UINT16_C(0x0010)))
68         return 1;
69     if (test__truncdfhf2(0x1.0p-24,
70                          UINT16_C(0x0001)))
71         return 1;
72     if (test__truncdfhf2(-0x1.0p-24,
73                          UINT16_C(0x8001)))
74         return 1;
75     if (test__truncdfhf2(0x1.5p-25,
76                          UINT16_C(0x0001)))
77         return 1;
78     // and back to zero
79     if (test__truncdfhf2(0x1.0p-25,
80                          UINT16_C(0x0000)))
81         return 1;
82     if (test__truncdfhf2(-0x1.0p-25,
83                          UINT16_C(0x8000)))
84         return 1;
85     // max (precise)
86     if (test__truncdfhf2(65504.0,
87                          UINT16_C(0x7bff)))
88         return 1;
89     // max (rounded)
90     if (test__truncdfhf2(65519.0,
91                          UINT16_C(0x7bff)))
92         return 1;
93     // max (to +inf)
94     if (test__truncdfhf2(65520.0,
95                          UINT16_C(0x7c00)))
96         return 1;
97     if (test__truncdfhf2(-65520.0,
98                          UINT16_C(0xfc00)))
99         return 1;
100     if (test__truncdfhf2(65536.0,
101                          UINT16_C(0x7c00)))
102         return 1;
103     return 0;
104 }
105