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