• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--------------- truncdfhf2_test.c - Test __truncdfhf2 ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file tests __truncdfhf2 for the compiler_rt library.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include <stdio.h>
15 
16 #include "fp_test.h"
17 
18 uint16_t __truncdfhf2(double a);
19 
test__truncdfhf2(double a,uint16_t expected)20 int test__truncdfhf2(double a, uint16_t expected)
21 {
22     uint16_t x = __truncdfhf2(a);
23     int ret = compareResultH(x, expected);
24 
25     if (ret){
26         printf("error in test__truncdfhf2(%f) = %#.4x, "
27                "expected %#.4x\n", a, x, fromRep16(expected));
28     }
29     return ret;
30 }
31 
32 char assumption_1[sizeof(__fp16) * CHAR_BIT == 16] = {0};
33 
main()34 int main()
35 {
36     // qNaN
37     if (test__truncdfhf2(makeQNaN64(),
38                          UINT16_C(0x7e00)))
39         return 1;
40     // NaN
41     if (test__truncdfhf2(makeNaN64(UINT64_C(0x8000)),
42                          UINT16_C(0x7e00)))
43         return 1;
44     // inf
45     if (test__truncdfhf2(makeInf64(),
46                          UINT16_C(0x7c00)))
47         return 1;
48     if (test__truncdfhf2(-makeInf64(),
49                          UINT16_C(0xfc00)))
50         return 1;
51     // zero
52     if (test__truncdfhf2(0.0, UINT16_C(0x0)))
53         return 1;
54     if (test__truncdfhf2(-0.0, UINT16_C(0x8000)))
55         return 1;
56 
57     if (test__truncdfhf2(3.1415926535,
58                          UINT16_C(0x4248)))
59         return 1;
60     if (test__truncdfhf2(-3.1415926535,
61                          UINT16_C(0xc248)))
62         return 1;
63     if (test__truncdfhf2(0x1.987124876876324p+1000,
64                          UINT16_C(0x7c00)))
65         return 1;
66     if (test__truncdfhf2(0x1.987124876876324p+12,
67                          UINT16_C(0x6e62)))
68         return 1;
69     if (test__truncdfhf2(0x1.0p+0,
70                          UINT16_C(0x3c00)))
71         return 1;
72     if (test__truncdfhf2(0x1.0p-14,
73                          UINT16_C(0x0400)))
74         return 1;
75     // denormal
76     if (test__truncdfhf2(0x1.0p-20,
77                          UINT16_C(0x0010)))
78         return 1;
79     if (test__truncdfhf2(0x1.0p-24,
80                          UINT16_C(0x0001)))
81         return 1;
82     if (test__truncdfhf2(-0x1.0p-24,
83                          UINT16_C(0x8001)))
84         return 1;
85     if (test__truncdfhf2(0x1.5p-25,
86                          UINT16_C(0x0001)))
87         return 1;
88     // and back to zero
89     if (test__truncdfhf2(0x1.0p-25,
90                          UINT16_C(0x0000)))
91         return 1;
92     if (test__truncdfhf2(-0x1.0p-25,
93                          UINT16_C(0x8000)))
94         return 1;
95     // max (precise)
96     if (test__truncdfhf2(65504.0,
97                          UINT16_C(0x7bff)))
98         return 1;
99     // max (rounded)
100     if (test__truncdfhf2(65519.0,
101                          UINT16_C(0x7bff)))
102         return 1;
103     // max (to +inf)
104     if (test__truncdfhf2(65520.0,
105                          UINT16_C(0x7c00)))
106         return 1;
107     if (test__truncdfhf2(-65520.0,
108                          UINT16_C(0xfc00)))
109         return 1;
110     if (test__truncdfhf2(65536.0,
111                          UINT16_C(0x7c00)))
112         return 1;
113     return 0;
114 }
115