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