xref: /aosp_15_r20/external/compiler-rt/test/builtins/Unit/fixunstfti_test.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- fixunstfti_test.c - Test __fixunstfti -----------------------------===//
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 __fixunstfti 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 #if __LDBL_MANT_DIG__ == 113
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot #include "fp_test.h"
19*7c3d14c8STreehugger Robot #include "int_lib.h"
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot // Returns: convert a to a unsigned long long, rounding toward zero.
22*7c3d14c8STreehugger Robot //          Negative values all become zero.
23*7c3d14c8STreehugger Robot 
24*7c3d14c8STreehugger Robot // Assumption: long double is a 128 bit floating point type
25*7c3d14c8STreehugger Robot //             tu_int is a 128 bit integral type
26*7c3d14c8STreehugger Robot //             value in long double is representable in tu_int or is negative
27*7c3d14c8STreehugger Robot //                 (no range checking performed)
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot COMPILER_RT_ABI tu_int __fixunstfti(long double a);
30*7c3d14c8STreehugger Robot 
test__fixunstfti(long double a,tu_int expected)31*7c3d14c8STreehugger Robot int test__fixunstfti(long double a, tu_int expected)
32*7c3d14c8STreehugger Robot {
33*7c3d14c8STreehugger Robot     tu_int x = __fixunstfti(a);
34*7c3d14c8STreehugger Robot     if (x != expected)
35*7c3d14c8STreehugger Robot     {
36*7c3d14c8STreehugger Robot         twords xt;
37*7c3d14c8STreehugger Robot         xt.all = x;
38*7c3d14c8STreehugger Robot 
39*7c3d14c8STreehugger Robot         twords expectedt;
40*7c3d14c8STreehugger Robot         expectedt.all = expected;
41*7c3d14c8STreehugger Robot 
42*7c3d14c8STreehugger Robot         printf("error in __fixunstfti(%.20Lf) = 0x%.16llX%.16llX, "
43*7c3d14c8STreehugger Robot                "expected 0x%.16llX%.16llX\n",
44*7c3d14c8STreehugger Robot                a, xt.s.high, xt.s.low, expectedt.s.high, expectedt.s.low);
45*7c3d14c8STreehugger Robot     }
46*7c3d14c8STreehugger Robot     return x != expected;
47*7c3d14c8STreehugger Robot }
48*7c3d14c8STreehugger Robot 
49*7c3d14c8STreehugger Robot char assumption_1[sizeof(tu_int) == 4*sizeof(su_int)] = {0};
50*7c3d14c8STreehugger Robot char assumption_2[sizeof(tu_int)*CHAR_BIT == 128] = {0};
51*7c3d14c8STreehugger Robot char assumption_3[sizeof(long double)*CHAR_BIT == 128] = {0};
52*7c3d14c8STreehugger Robot 
53*7c3d14c8STreehugger Robot #endif
54*7c3d14c8STreehugger Robot 
main()55*7c3d14c8STreehugger Robot int main()
56*7c3d14c8STreehugger Robot {
57*7c3d14c8STreehugger Robot #if __LDBL_MANT_DIG__ == 113
58*7c3d14c8STreehugger Robot     if (test__fixunstfti(makeInf128(), make_ti(0xffffffffffffffffLL,
59*7c3d14c8STreehugger Robot                                                0xffffffffffffffffLL)))
60*7c3d14c8STreehugger Robot         return 1;
61*7c3d14c8STreehugger Robot 
62*7c3d14c8STreehugger Robot     if (test__fixunstfti(0.0, 0))
63*7c3d14c8STreehugger Robot         return 1;
64*7c3d14c8STreehugger Robot 
65*7c3d14c8STreehugger Robot     if (test__fixunstfti(0.5, 0))
66*7c3d14c8STreehugger Robot         return 1;
67*7c3d14c8STreehugger Robot     if (test__fixunstfti(0.99, 0))
68*7c3d14c8STreehugger Robot         return 1;
69*7c3d14c8STreehugger Robot     if (test__fixunstfti(1.0, 1))
70*7c3d14c8STreehugger Robot         return 1;
71*7c3d14c8STreehugger Robot     if (test__fixunstfti(1.5, 1))
72*7c3d14c8STreehugger Robot         return 1;
73*7c3d14c8STreehugger Robot     if (test__fixunstfti(1.99, 1))
74*7c3d14c8STreehugger Robot         return 1;
75*7c3d14c8STreehugger Robot     if (test__fixunstfti(2.0, 2))
76*7c3d14c8STreehugger Robot         return 1;
77*7c3d14c8STreehugger Robot     if (test__fixunstfti(2.01, 2))
78*7c3d14c8STreehugger Robot         return 1;
79*7c3d14c8STreehugger Robot     if (test__fixunstfti(-0.01, 0))
80*7c3d14c8STreehugger Robot         return 1;
81*7c3d14c8STreehugger Robot     if (test__fixunstfti(-0.99, 0))
82*7c3d14c8STreehugger Robot         return 1;
83*7c3d14c8STreehugger Robot 
84*7c3d14c8STreehugger Robot     if (test__fixunstfti(0x1.p+128, make_ti(0xffffffffffffffffLL,
85*7c3d14c8STreehugger Robot                                             0xffffffffffffffffLL)))
86*7c3d14c8STreehugger Robot         return 1;
87*7c3d14c8STreehugger Robot 
88*7c3d14c8STreehugger Robot     if (test__fixunstfti(0x1.FFFFFEp+126, make_ti(0x7fffff8000000000LL, 0x0)))
89*7c3d14c8STreehugger Robot         return 1;
90*7c3d14c8STreehugger Robot     if (test__fixunstfti(0x1.FFFFFEp+127, make_ti(0xffffff0000000000LL, 0x0)))
91*7c3d14c8STreehugger Robot         return 1;
92*7c3d14c8STreehugger Robot     if (test__fixunstfti(0x1.FFFFFEp+128, make_ti(0xffffffffffffffffLL,
93*7c3d14c8STreehugger Robot                                                   0xffffffffffffffffLL)))
94*7c3d14c8STreehugger Robot         return 1;
95*7c3d14c8STreehugger Robot     if (test__fixunstfti(0x1.FFFFFEp+129, make_ti(0xffffffffffffffffLL,
96*7c3d14c8STreehugger Robot                                                   0xffffffffffffffffLL)))
97*7c3d14c8STreehugger Robot         return 1;
98*7c3d14c8STreehugger Robot 
99*7c3d14c8STreehugger Robot #else
100*7c3d14c8STreehugger Robot     printf("skipped\n");
101*7c3d14c8STreehugger Robot #endif
102*7c3d14c8STreehugger Robot    return 0;
103*7c3d14c8STreehugger Robot }
104