1// RUN: %clang_builtins %s %librt -o %t && %run %t
2// REQUIRES: librt_has_floattitf
3
4#define QUAD_PRECISION
5#include "fp_lib.h"
6#include "int_lib.h"
7#include <float.h>
8#include <stdio.h>
9
10// The testcase currently assumes IEEE TF format, once that has been
11// fixed the defined(CRT_HAS_IEEE_TF) guard can be removed to enable it for
12// IBM 128 floats as well.
13#if defined(CRT_HAS_IEEE_TF)
14
15# include "fp_test.h"
16
17/* Returns: convert a ti_int to a fp_t, rounding toward even. */
18
19/* Assumption: fp_t is a IEEE 128 bit floating point type
20 * ti_int is a 128 bit integral type
21 */
22
23/* seee eeee eeee eeee mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm |
24 * mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
25 */
26
27COMPILER_RT_ABI fp_t __floattitf(ti_int a);
28
29int _test__floattitf(int line, ti_int a, fp_t expected) {
30 fp_t x = __floattitf(a);
31 if (x != expected) {
32 twords at;
33 at.all = a;
34 __uint128_t expected_rep = toRep128(x: expected);
35 __uint128_t res_rep = toRep128(x);
36 printf(format: "%s:%d: error in __floattitf(0x%.16llX%.16llX) = "
37 "0x%.16llX%.16llX, expected 0x%.16llX%.16llX\n",
38 __FILE__, line, at.s.high, at.s.low, (uint64_t)(res_rep >> 64),
39 (uint64_t)res_rep, (uint64_t)(expected_rep >> 64),
40 (uint64_t)expected_rep);
41 }
42 return x != expected;
43}
44
45# define test__floattitf(a, e) _test__floattitf(__LINE__, a, e)
46
47char assumption_1[sizeof(ti_int) == 2 * sizeof(di_int)] = {0};
48char assumption_2[sizeof(ti_int) * CHAR_BIT == 128] = {0};
49char assumption_3[sizeof(fp_t) * CHAR_BIT == 128] = {0};
50
51int main() {
52 if (test__floattitf(0, TF_C(0.0)))
53 return 1;
54
55 if (test__floattitf(1, TF_C(1.0)))
56 return 1;
57 if (test__floattitf(2, TF_C(2.0)))
58 return 1;
59 if (test__floattitf(20, TF_C(20.0)))
60 return 1;
61 if (test__floattitf(-1, -TF_C(1.0)))
62 return 1;
63 if (test__floattitf(-2, -TF_C(2.0)))
64 return 1;
65 if (test__floattitf(-20, -TF_C(20.0)))
66 return 1;
67
68 if (test__floattitf(0x7FFFFF8000000000LL, TF_C(0x1.FFFFFEp+62)))
69 return 1;
70 if (test__floattitf(0x7FFFFFFFFFFFF800LL, TF_C(0x1.FFFFFFFFFFFFEp+62)))
71 return 1;
72 if (test__floattitf(0x7FFFFF0000000000LL, TF_C(0x1.FFFFFCp+62)))
73 return 1;
74 if (test__floattitf(0x7FFFFFFFFFFFF000LL, TF_C(0x1.FFFFFFFFFFFFCp+62)))
75 return 1;
76
77 if (test__floattitf(make_ti(0x8000008000000000LL, 0), -TF_C(0x1.FFFFFEp+126)))
78 return 1;
79 if (test__floattitf(make_ti(0x8000000000000800LL, 0),
80 -TF_C(0x1.FFFFFFFFFFFFEp+126)))
81 return 1;
82 if (test__floattitf(make_ti(0x8000010000000000LL, 0), -TF_C(0x1.FFFFFCp+126)))
83 return 1;
84 if (test__floattitf(make_ti(0x8000000000001000LL, 0),
85 -TF_C(0x1.FFFFFFFFFFFFCp+126)))
86 return 1;
87
88 if (test__floattitf(make_ti(0x8000000000000000LL, 0), -TF_C(0x1.000000p+127)))
89 return 1;
90 if (test__floattitf(make_ti(0x8000000000000001LL, 0),
91 -TF_C(0x1.FFFFFFFFFFFFFFFCp+126)))
92 return 1;
93
94 if (test__floattitf(0x0007FB72E8000000LL, TF_C(0x1.FEDCBAp+50)))
95 return 1;
96
97 if (test__floattitf(0x0007FB72EA000000LL, TF_C(0x1.FEDCBA8p+50)))
98 return 1;
99 if (test__floattitf(0x0007FB72EB000000LL, TF_C(0x1.FEDCBACp+50)))
100 return 1;
101 if (test__floattitf(0x0007FB72EBFFFFFFLL, TF_C(0x1.FEDCBAFFFFFFCp+50)))
102 return 1;
103 if (test__floattitf(0x0007FB72EC000000LL, TF_C(0x1.FEDCBBp+50)))
104 return 1;
105 if (test__floattitf(0x0007FB72E8000001LL, TF_C(0x1.FEDCBA0000004p+50)))
106 return 1;
107
108 if (test__floattitf(0x0007FB72E6000000LL, TF_C(0x1.FEDCB98p+50)))
109 return 1;
110 if (test__floattitf(0x0007FB72E7000000LL, TF_C(0x1.FEDCB9Cp+50)))
111 return 1;
112 if (test__floattitf(0x0007FB72E7FFFFFFLL, TF_C(0x1.FEDCB9FFFFFFCp+50)))
113 return 1;
114 if (test__floattitf(0x0007FB72E4000001LL, TF_C(0x1.FEDCB90000004p+50)))
115 return 1;
116 if (test__floattitf(0x0007FB72E4000000LL, TF_C(0x1.FEDCB9p+50)))
117 return 1;
118
119 if (test__floattitf(0x023479FD0E092DC0LL, TF_C(0x1.1A3CFE870496Ep+57)))
120 return 1;
121 if (test__floattitf(0x023479FD0E092DA1LL, TF_C(0x1.1A3CFE870496D08p+57)))
122 return 1;
123 if (test__floattitf(0x023479FD0E092DB0LL, TF_C(0x1.1A3CFE870496D8p+57)))
124 return 1;
125 if (test__floattitf(0x023479FD0E092DB8LL, TF_C(0x1.1A3CFE870496DCp+57)))
126 return 1;
127 if (test__floattitf(0x023479FD0E092DB6LL, TF_C(0x1.1A3CFE870496DBp+57)))
128 return 1;
129 if (test__floattitf(0x023479FD0E092DBFLL, TF_C(0x1.1A3CFE870496DF8p+57)))
130 return 1;
131 if (test__floattitf(0x023479FD0E092DC1LL, TF_C(0x1.1A3CFE870496E08p+57)))
132 return 1;
133 if (test__floattitf(0x023479FD0E092DC7LL, TF_C(0x1.1A3CFE870496E38p+57)))
134 return 1;
135 if (test__floattitf(0x023479FD0E092DC8LL, TF_C(0x1.1A3CFE870496E4p+57)))
136 return 1;
137 if (test__floattitf(0x023479FD0E092DCFLL, TF_C(0x1.1A3CFE870496E78p+57)))
138 return 1;
139 if (test__floattitf(0x023479FD0E092DD0LL, TF_C(0x1.1A3CFE870496E8p+57)))
140 return 1;
141 if (test__floattitf(0x023479FD0E092DD1LL, TF_C(0x1.1A3CFE870496E88p+57)))
142 return 1;
143 if (test__floattitf(0x023479FD0E092DD8LL, TF_C(0x1.1A3CFE870496ECp+57)))
144 return 1;
145 if (test__floattitf(0x023479FD0E092DDFLL, TF_C(0x1.1A3CFE870496EF8p+57)))
146 return 1;
147 if (test__floattitf(0x023479FD0E092DE0LL, TF_C(0x1.1A3CFE870496Fp+57)))
148 return 1;
149
150 if (test__floattitf(make_ti(0x023479FD0E092DC0LL, 0),
151 TF_C(0x1.1A3CFE870496Ep+121)))
152 return 1;
153 if (test__floattitf(make_ti(0x023479FD0E092DA1LL, 1),
154 TF_C(0x1.1A3CFE870496D08p+121)))
155 return 1;
156 if (test__floattitf(make_ti(0x023479FD0E092DB0LL, 2),
157 TF_C(0x1.1A3CFE870496D8p+121)))
158 return 1;
159 if (test__floattitf(make_ti(0x023479FD0E092DB8LL, 3),
160 TF_C(0x1.1A3CFE870496DCp+121)))
161 return 1;
162 if (test__floattitf(make_ti(0x023479FD0E092DB6LL, 4),
163 TF_C(0x1.1A3CFE870496DBp+121)))
164 return 1;
165 if (test__floattitf(make_ti(0x023479FD0E092DBFLL, 5),
166 TF_C(0x1.1A3CFE870496DF8p+121)))
167 return 1;
168 if (test__floattitf(make_ti(0x023479FD0E092DC1LL, 6),
169 TF_C(0x1.1A3CFE870496E08p+121)))
170 return 1;
171 if (test__floattitf(make_ti(0x023479FD0E092DC7LL, 7),
172 TF_C(0x1.1A3CFE870496E38p+121)))
173 return 1;
174 if (test__floattitf(make_ti(0x023479FD0E092DC8LL, 8),
175 TF_C(0x1.1A3CFE870496E4p+121)))
176 return 1;
177 if (test__floattitf(make_ti(0x023479FD0E092DCFLL, 9),
178 TF_C(0x1.1A3CFE870496E78p+121)))
179 return 1;
180 if (test__floattitf(make_ti(0x023479FD0E092DD0LL, 0),
181 TF_C(0x1.1A3CFE870496E8p+121)))
182 return 1;
183 if (test__floattitf(make_ti(0x023479FD0E092DD1LL, 11),
184 TF_C(0x1.1A3CFE870496E88p+121)))
185 return 1;
186 if (test__floattitf(make_ti(0x023479FD0E092DD8LL, 12),
187 TF_C(0x1.1A3CFE870496ECp+121)))
188 return 1;
189 if (test__floattitf(make_ti(0x023479FD0E092DDFLL, 13),
190 TF_C(0x1.1A3CFE870496EF8p+121)))
191 return 1;
192 if (test__floattitf(make_ti(0x023479FD0E092DE0LL, 14),
193 TF_C(0x1.1A3CFE870496Fp+121)))
194 return 1;
195
196 if (test__floattitf(make_ti(0, 0xFFFFFFFFFFFFFFFFLL),
197 TF_C(0x1.FFFFFFFFFFFFFFFEp+63)))
198 return 1;
199
200 if (test__floattitf(make_ti(0x123456789ABCDEF0LL, 0x123456789ABC2801LL),
201 TF_C(0x1.23456789ABCDEF0123456789ABC3p+124)))
202 return 1;
203 if (test__floattitf(make_ti(0x123456789ABCDEF0LL, 0x123456789ABC3000LL),
204 TF_C(0x1.23456789ABCDEF0123456789ABC3p+124)))
205 return 1;
206 if (test__floattitf(make_ti(0x123456789ABCDEF0LL, 0x123456789ABC37FFLL),
207 TF_C(0x1.23456789ABCDEF0123456789ABC3p+124)))
208 return 1;
209 if (test__floattitf(make_ti(0x123456789ABCDEF0LL, 0x123456789ABC3800LL),
210 TF_C(0x1.23456789ABCDEF0123456789ABC4p+124)))
211 return 1;
212 if (test__floattitf(make_ti(0x123456789ABCDEF0LL, 0x123456789ABC4000LL),
213 TF_C(0x1.23456789ABCDEF0123456789ABC4p+124)))
214 return 1;
215 if (test__floattitf(make_ti(0x123456789ABCDEF0LL, 0x123456789ABC47FFLL),
216 TF_C(0x1.23456789ABCDEF0123456789ABC4p+124)))
217 return 1;
218 if (test__floattitf(make_ti(0x123456789ABCDEF0LL, 0x123456789ABC4800LL),
219 TF_C(0x1.23456789ABCDEF0123456789ABC4p+124)))
220 return 1;
221 if (test__floattitf(make_ti(0x123456789ABCDEF0LL, 0x123456789ABC4801LL),
222 TF_C(0x1.23456789ABCDEF0123456789ABC5p+124)))
223 return 1;
224 if (test__floattitf(make_ti(0x123456789ABCDEF0LL, 0x123456789ABC57FFLL),
225 TF_C(0x1.23456789ABCDEF0123456789ABC5p+124)))
226 return 1;
227 return 0;
228}
229
230#else
231int main() {
232 printf("skipped\n");
233 return 0;
234}
235#endif
236

source code of compiler-rt/test/builtins/Unit/floattitf_test.c