1 // RUN: %clang_builtins %s %librt -o %t && %run %t
2 // REQUIRES: librt_has_fixxfti
3 // REQUIRES: x86-target-arch
4 
5 #include "int_lib.h"
6 #include <stdio.h>
7 
8 #if defined(CRT_HAS_128BIT) && HAS_80_BIT_LONG_DOUBLE
9 
10 // Returns: convert a to a signed long long, rounding toward zero.
11 
12 // Assumption: long double is an intel 80 bit floating point type padded with 6 bytes
13 //             su_int is a 32 bit integral type
14 //             value in long double is representable in ti_int (no range checking performed)
15 
16 // gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
17 // 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
18 
19 COMPILER_RT_ABI ti_int __fixxfti(long double a);
20 
test__fixxfti(long double a,ti_int expected)21 int test__fixxfti(long double a, ti_int expected)
22 {
23     ti_int x = __fixxfti(a);
24     if (x != expected)
25     {
26         utwords xt;
27         xt.all = x;
28         utwords expectedt;
29         expectedt.all = expected;
30         printf("error in __fixxfti(%LA) = 0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n",
31                a, xt.s.high, xt.s.low, expectedt.s.high, expectedt.s.low);
32     }
33     return x != expected;
34 }
35 
36 COMPILE_TIME_ASSERT(sizeof(ti_int) == 2*sizeof(di_int));
37 COMPILE_TIME_ASSERT(sizeof(su_int)*CHAR_BIT == 32);
38 COMPILE_TIME_ASSERT(sizeof(long double)*CHAR_BIT == 128);
39 
40 #endif
41 
main()42 int main()
43 {
44 #if defined(CRT_HAS_128BIT) && HAS_80_BIT_LONG_DOUBLE
45     if (test__fixxfti(0.0, 0))
46         return 1;
47 
48     if (test__fixxfti(0.5, 0))
49         return 1;
50     if (test__fixxfti(0.99, 0))
51         return 1;
52     if (test__fixxfti(1.0, 1))
53         return 1;
54     if (test__fixxfti(1.5, 1))
55         return 1;
56     if (test__fixxfti(1.99, 1))
57         return 1;
58     if (test__fixxfti(2.0, 2))
59         return 1;
60     if (test__fixxfti(2.01, 2))
61         return 1;
62     if (test__fixxfti(-0.5, 0))
63         return 1;
64     if (test__fixxfti(-0.99, 0))
65         return 1;
66     if (test__fixxfti(-1.0, -1))
67         return 1;
68     if (test__fixxfti(-1.5, -1))
69         return 1;
70     if (test__fixxfti(-1.99, -1))
71         return 1;
72     if (test__fixxfti(-2.0, -2))
73         return 1;
74     if (test__fixxfti(-2.01, -2))
75         return 1;
76 
77     if (test__fixxfti(0x1.FFFFFEp+62, 0x7FFFFF8000000000LL))
78         return 1;
79     if (test__fixxfti(0x1.FFFFFCp+62, 0x7FFFFF0000000000LL))
80         return 1;
81 
82     if (test__fixxfti(-0x1.FFFFFEp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
83                                                0x8000008000000000LL)))
84         return 1;
85     if (test__fixxfti(-0x1.FFFFFCp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
86                                                0x8000010000000000LL)))
87         return 1;
88 
89     if (test__fixxfti(0x1.FFFFFFFFFFFFFp+62, 0x7FFFFFFFFFFFFC00LL))
90         return 1;
91     if (test__fixxfti(0x1.FFFFFFFFFFFFEp+62, 0x7FFFFFFFFFFFF800LL))
92         return 1;
93 
94     if (test__fixxfti(-0x1.FFFFFFFFFFFFFp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
95                                                       0x8000000000000400LL)))
96         return 1;
97     if (test__fixxfti(-0x1.FFFFFFFFFFFFEp+62, make_ti(0xFFFFFFFFFFFFFFFFLL,
98                                                       0x8000000000000800LL)))
99         return 1;
100 
101     if (test__fixxfti(0x1.FFFFFFFFFFFFFFFCp+62L, 0x7FFFFFFFFFFFFFFFLL))
102         return 1;
103     if (test__fixxfti(0x1.FFFFFFFFFFFFFFF8p+62L, 0x7FFFFFFFFFFFFFFELL))
104         return 1;
105 
106     if (test__fixxfti(-0x1.0000000000000000p+63L, make_ti(0xFFFFFFFFFFFFFFFFLL,
107                                                           0x8000000000000000LL)))
108         return 1;
109     if (test__fixxfti(-0x1.FFFFFFFFFFFFFFFCp+62L, make_ti(0xFFFFFFFFFFFFFFFFLL,
110                                                           0x8000000000000001LL)))
111         return 1;
112     if (test__fixxfti(-0x1.FFFFFFFFFFFFFFF8p+62L, make_ti(0xFFFFFFFFFFFFFFFFLL,
113                                                           0x8000000000000002LL)))
114         return 1;
115 
116     if (test__fixxfti(0x1.FFFFFFFFFFFFFFFEp+126L, make_ti(0x7FFFFFFFFFFFFFFFLL,
117                                                           0x8000000000000000LL)))
118         return 1;
119     if (test__fixxfti(0x1.FFFFFFFFFFFFFFFCp+126L, make_ti(0x7FFFFFFFFFFFFFFFLL,
120                                                           0x0000000000000000LL)))
121 
122         return 1;
123 
124     if (test__fixxfti(-0x1.0000000000000000p+127L, make_ti(0x8000000000000000LL,
125                                                            0x0000000000000000LL)))
126         return 1;
127     if (test__fixxfti(-0x1.FFFFFFFFFFFFFFFEp+126L, make_ti(0x8000000000000000LL,
128                                                            0x8000000000000000LL)))
129         return 1;
130     if (test__fixxfti(-0x1.FFFFFFFFFFFFFFFCp+126L, make_ti(0x8000000000000001LL,
131                                                            0x0000000000000000LL)))
132         return 1;
133 #else
134     printf("skipped\n");
135 #endif
136    return 0;
137 }
138