1 //===- llvm/unittest/ADT/APFloat.cpp - APFloat unit tests ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/APFloat.h"
11 #include "llvm/ADT/APSInt.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "gtest/gtest.h"
21 static double convertToDoubleFromString(const char *Str) {
23 F.convertFromString(Str, llvm::APFloat::rmNearestTiesToEven);
24 return F.convertToDouble();
27 static std::string convertToString(double d, unsigned Prec, unsigned Pad) {
28 llvm::SmallVector<char, 100> Buffer;
30 F.toString(Buffer, Prec, Pad);
31 return std::string(Buffer.data(), Buffer.size());
36 TEST(APFloatTest, isSignaling) {
37 // We test qNaN, -qNaN, +sNaN, -sNaN with and without payloads. *NOTE* The
38 // positive/negative distinction is included only since the getQNaN/getSNaN
39 // API provides the option.
40 APInt payload = APInt::getOneBitSet(4, 2);
41 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle, false).isSignaling());
42 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle, true).isSignaling());
43 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle, false, &payload).isSignaling());
44 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle, true, &payload).isSignaling());
45 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, false).isSignaling());
46 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, true).isSignaling());
47 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, false, &payload).isSignaling());
48 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, true, &payload).isSignaling());
51 TEST(APFloatTest, next) {
53 APFloat test(APFloat::IEEEquad, APFloat::uninitialized);
54 APFloat expected(APFloat::IEEEquad, APFloat::uninitialized);
56 // 1. Test Special Cases Values.
58 // Test all special values for nextUp and nextDown perscribed by IEEE-754R
71 // nextUp(+inf) = +inf.
72 test = APFloat::getInf(APFloat::IEEEquad, false);
73 expected = APFloat::getInf(APFloat::IEEEquad, false);
74 EXPECT_EQ(test.next(false), APFloat::opOK);
75 EXPECT_TRUE(test.isInfinity());
76 EXPECT_TRUE(!test.isNegative());
77 EXPECT_TRUE(test.bitwiseIsEqual(expected));
79 // nextDown(+inf) = -nextUp(-inf) = -(-getLargest()) = getLargest()
80 test = APFloat::getInf(APFloat::IEEEquad, false);
81 expected = APFloat::getLargest(APFloat::IEEEquad, false);
82 EXPECT_EQ(test.next(true), APFloat::opOK);
83 EXPECT_TRUE(!test.isNegative());
84 EXPECT_TRUE(test.bitwiseIsEqual(expected));
86 // nextUp(-inf) = -getLargest()
87 test = APFloat::getInf(APFloat::IEEEquad, true);
88 expected = APFloat::getLargest(APFloat::IEEEquad, true);
89 EXPECT_EQ(test.next(false), APFloat::opOK);
90 EXPECT_TRUE(test.isNegative());
91 EXPECT_TRUE(test.bitwiseIsEqual(expected));
93 // nextDown(-inf) = -nextUp(+inf) = -(+inf) = -inf.
94 test = APFloat::getInf(APFloat::IEEEquad, true);
95 expected = APFloat::getInf(APFloat::IEEEquad, true);
96 EXPECT_EQ(test.next(true), APFloat::opOK);
97 EXPECT_TRUE(test.isInfinity() && test.isNegative());
98 EXPECT_TRUE(test.bitwiseIsEqual(expected));
100 // nextUp(getLargest()) = +inf
101 test = APFloat::getLargest(APFloat::IEEEquad, false);
102 expected = APFloat::getInf(APFloat::IEEEquad, false);
103 EXPECT_EQ(test.next(false), APFloat::opOK);
104 EXPECT_TRUE(test.isInfinity() && !test.isNegative());
105 EXPECT_TRUE(test.bitwiseIsEqual(expected));
107 // nextDown(getLargest()) = -nextUp(-getLargest())
108 // = -(-getLargest() + inc)
109 // = getLargest() - inc.
110 test = APFloat::getLargest(APFloat::IEEEquad, false);
111 expected = APFloat(APFloat::IEEEquad,
112 "0x1.fffffffffffffffffffffffffffep+16383");
113 EXPECT_EQ(test.next(true), APFloat::opOK);
114 EXPECT_TRUE(!test.isInfinity() && !test.isNegative());
115 EXPECT_TRUE(test.bitwiseIsEqual(expected));
117 // nextUp(-getLargest()) = -getLargest() + inc.
118 test = APFloat::getLargest(APFloat::IEEEquad, true);
119 expected = APFloat(APFloat::IEEEquad,
120 "-0x1.fffffffffffffffffffffffffffep+16383");
121 EXPECT_EQ(test.next(false), APFloat::opOK);
122 EXPECT_TRUE(test.bitwiseIsEqual(expected));
124 // nextDown(-getLargest()) = -nextUp(getLargest()) = -(inf) = -inf.
125 test = APFloat::getLargest(APFloat::IEEEquad, true);
126 expected = APFloat::getInf(APFloat::IEEEquad, true);
127 EXPECT_EQ(test.next(true), APFloat::opOK);
128 EXPECT_TRUE(test.isInfinity() && test.isNegative());
129 EXPECT_TRUE(test.bitwiseIsEqual(expected));
131 // nextUp(getSmallest()) = getSmallest() + inc.
132 test = APFloat(APFloat::IEEEquad, "0x0.0000000000000000000000000001p-16382");
133 expected = APFloat(APFloat::IEEEquad,
134 "0x0.0000000000000000000000000002p-16382");
135 EXPECT_EQ(test.next(false), APFloat::opOK);
136 EXPECT_TRUE(test.bitwiseIsEqual(expected));
138 // nextDown(getSmallest()) = -nextUp(-getSmallest()) = -(-0) = +0.
139 test = APFloat(APFloat::IEEEquad, "0x0.0000000000000000000000000001p-16382");
140 expected = APFloat::getZero(APFloat::IEEEquad, false);
141 EXPECT_EQ(test.next(true), APFloat::opOK);
142 EXPECT_TRUE(test.isZero() && !test.isNegative());
143 EXPECT_TRUE(test.bitwiseIsEqual(expected));
145 // nextUp(-getSmallest()) = -0.
146 test = APFloat(APFloat::IEEEquad, "-0x0.0000000000000000000000000001p-16382");
147 expected = APFloat::getZero(APFloat::IEEEquad, true);
148 EXPECT_EQ(test.next(false), APFloat::opOK);
149 EXPECT_TRUE(test.isZero() && test.isNegative());
150 EXPECT_TRUE(test.bitwiseIsEqual(expected));
152 // nextDown(-getSmallest()) = -nextUp(getSmallest()) = -getSmallest() - inc.
153 test = APFloat(APFloat::IEEEquad, "-0x0.0000000000000000000000000001p-16382");
154 expected = APFloat(APFloat::IEEEquad,
155 "-0x0.0000000000000000000000000002p-16382");
156 EXPECT_EQ(test.next(true), APFloat::opOK);
157 EXPECT_TRUE(test.bitwiseIsEqual(expected));
159 // nextUp(qNaN) = qNaN
160 test = APFloat::getQNaN(APFloat::IEEEquad, false);
161 expected = APFloat::getQNaN(APFloat::IEEEquad, false);
162 EXPECT_EQ(test.next(false), APFloat::opOK);
163 EXPECT_TRUE(test.bitwiseIsEqual(expected));
165 // nextDown(qNaN) = qNaN
166 test = APFloat::getQNaN(APFloat::IEEEquad, false);
167 expected = APFloat::getQNaN(APFloat::IEEEquad, false);
168 EXPECT_EQ(test.next(true), APFloat::opOK);
169 EXPECT_TRUE(test.bitwiseIsEqual(expected));
171 // nextUp(sNaN) = qNaN
172 test = APFloat::getSNaN(APFloat::IEEEquad, false);
173 expected = APFloat::getQNaN(APFloat::IEEEquad, false);
174 EXPECT_EQ(test.next(false), APFloat::opInvalidOp);
175 EXPECT_TRUE(test.bitwiseIsEqual(expected));
177 // nextDown(sNaN) = qNaN
178 test = APFloat::getSNaN(APFloat::IEEEquad, false);
179 expected = APFloat::getQNaN(APFloat::IEEEquad, false);
180 EXPECT_EQ(test.next(true), APFloat::opInvalidOp);
181 EXPECT_TRUE(test.bitwiseIsEqual(expected));
183 // nextUp(+0) = +getSmallest()
184 test = APFloat::getZero(APFloat::IEEEquad, false);
185 expected = APFloat::getSmallest(APFloat::IEEEquad, false);
186 EXPECT_EQ(test.next(false), APFloat::opOK);
187 EXPECT_TRUE(test.bitwiseIsEqual(expected));
189 // nextDown(+0) = -nextUp(-0) = -getSmallest()
190 test = APFloat::getZero(APFloat::IEEEquad, false);
191 expected = APFloat::getSmallest(APFloat::IEEEquad, true);
192 EXPECT_EQ(test.next(true), APFloat::opOK);
193 EXPECT_TRUE(test.bitwiseIsEqual(expected));
195 // nextUp(-0) = +getSmallest()
196 test = APFloat::getZero(APFloat::IEEEquad, true);
197 expected = APFloat::getSmallest(APFloat::IEEEquad, false);
198 EXPECT_EQ(test.next(false), APFloat::opOK);
199 EXPECT_TRUE(test.bitwiseIsEqual(expected));
201 // nextDown(-0) = -nextUp(0) = -getSmallest()
202 test = APFloat::getZero(APFloat::IEEEquad, true);
203 expected = APFloat::getSmallest(APFloat::IEEEquad, true);
204 EXPECT_EQ(test.next(true), APFloat::opOK);
205 EXPECT_TRUE(test.bitwiseIsEqual(expected));
207 // 2. Binade Boundary Tests.
209 // 2a. Test denormal <-> normal binade boundaries.
210 // * nextUp(+Largest Denormal) -> +Smallest Normal.
211 // * nextDown(-Largest Denormal) -> -Smallest Normal.
212 // * nextUp(-Smallest Normal) -> -Largest Denormal.
213 // * nextDown(+Smallest Normal) -> +Largest Denormal.
215 // nextUp(+Largest Denormal) -> +Smallest Normal.
216 test = APFloat(APFloat::IEEEquad, "0x0.ffffffffffffffffffffffffffffp-16382");
217 expected = APFloat(APFloat::IEEEquad,
218 "0x1.0000000000000000000000000000p-16382");
219 EXPECT_EQ(test.next(false), APFloat::opOK);
220 EXPECT_FALSE(test.isDenormal());
221 EXPECT_TRUE(test.bitwiseIsEqual(expected));
223 // nextDown(-Largest Denormal) -> -Smallest Normal.
224 test = APFloat(APFloat::IEEEquad,
225 "-0x0.ffffffffffffffffffffffffffffp-16382");
226 expected = APFloat(APFloat::IEEEquad,
227 "-0x1.0000000000000000000000000000p-16382");
228 EXPECT_EQ(test.next(true), APFloat::opOK);
229 EXPECT_FALSE(test.isDenormal());
230 EXPECT_TRUE(test.bitwiseIsEqual(expected));
232 // nextUp(-Smallest Normal) -> -LargestDenormal.
233 test = APFloat(APFloat::IEEEquad,
234 "-0x1.0000000000000000000000000000p-16382");
235 expected = APFloat(APFloat::IEEEquad,
236 "-0x0.ffffffffffffffffffffffffffffp-16382");
237 EXPECT_EQ(test.next(false), APFloat::opOK);
238 EXPECT_TRUE(test.isDenormal());
239 EXPECT_TRUE(test.bitwiseIsEqual(expected));
241 // nextDown(+Smallest Normal) -> +Largest Denormal.
242 test = APFloat(APFloat::IEEEquad,
243 "+0x1.0000000000000000000000000000p-16382");
244 expected = APFloat(APFloat::IEEEquad,
245 "+0x0.ffffffffffffffffffffffffffffp-16382");
246 EXPECT_EQ(test.next(true), APFloat::opOK);
247 EXPECT_TRUE(test.isDenormal());
248 EXPECT_TRUE(test.bitwiseIsEqual(expected));
250 // 2b. Test normal <-> normal binade boundaries.
251 // * nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
252 // * nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
253 // * nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
254 // * nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
256 // nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
257 test = APFloat(APFloat::IEEEquad, "-0x1p+1");
258 expected = APFloat(APFloat::IEEEquad,
259 "-0x1.ffffffffffffffffffffffffffffp+0");
260 EXPECT_EQ(test.next(false), APFloat::opOK);
261 EXPECT_TRUE(test.bitwiseIsEqual(expected));
263 // nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
264 test = APFloat(APFloat::IEEEquad, "0x1p+1");
265 expected = APFloat(APFloat::IEEEquad, "0x1.ffffffffffffffffffffffffffffp+0");
266 EXPECT_EQ(test.next(true), APFloat::opOK);
267 EXPECT_TRUE(test.bitwiseIsEqual(expected));
269 // nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
270 test = APFloat(APFloat::IEEEquad, "0x1.ffffffffffffffffffffffffffffp+0");
271 expected = APFloat(APFloat::IEEEquad, "0x1p+1");
272 EXPECT_EQ(test.next(false), APFloat::opOK);
273 EXPECT_TRUE(test.bitwiseIsEqual(expected));
275 // nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
276 test = APFloat(APFloat::IEEEquad, "-0x1.ffffffffffffffffffffffffffffp+0");
277 expected = APFloat(APFloat::IEEEquad, "-0x1p+1");
278 EXPECT_EQ(test.next(true), APFloat::opOK);
279 EXPECT_TRUE(test.bitwiseIsEqual(expected));
281 // 2c. Test using next at binade boundaries with a direction away from the
282 // binade boundary. Away from denormal <-> normal boundaries.
284 // This is to make sure that even though we are at a binade boundary, since
285 // we are rounding away, we do not trigger the binade boundary code. Thus we
287 // * nextUp(-Largest Denormal) -> -Largest Denormal + inc.
288 // * nextDown(+Largest Denormal) -> +Largest Denormal - inc.
289 // * nextUp(+Smallest Normal) -> +Smallest Normal + inc.
290 // * nextDown(-Smallest Normal) -> -Smallest Normal - inc.
292 // nextUp(-Largest Denormal) -> -Largest Denormal + inc.
293 test = APFloat(APFloat::IEEEquad, "-0x0.ffffffffffffffffffffffffffffp-16382");
294 expected = APFloat(APFloat::IEEEquad,
295 "-0x0.fffffffffffffffffffffffffffep-16382");
296 EXPECT_EQ(test.next(false), APFloat::opOK);
297 EXPECT_TRUE(test.isDenormal());
298 EXPECT_TRUE(test.isNegative());
299 EXPECT_TRUE(test.bitwiseIsEqual(expected));
301 // nextDown(+Largest Denormal) -> +Largest Denormal - inc.
302 test = APFloat(APFloat::IEEEquad, "0x0.ffffffffffffffffffffffffffffp-16382");
303 expected = APFloat(APFloat::IEEEquad,
304 "0x0.fffffffffffffffffffffffffffep-16382");
305 EXPECT_EQ(test.next(true), APFloat::opOK);
306 EXPECT_TRUE(test.isDenormal());
307 EXPECT_TRUE(!test.isNegative());
308 EXPECT_TRUE(test.bitwiseIsEqual(expected));
310 // nextUp(+Smallest Normal) -> +Smallest Normal + inc.
311 test = APFloat(APFloat::IEEEquad, "0x1.0000000000000000000000000000p-16382");
312 expected = APFloat(APFloat::IEEEquad,
313 "0x1.0000000000000000000000000001p-16382");
314 EXPECT_EQ(test.next(false), APFloat::opOK);
315 EXPECT_TRUE(!test.isDenormal());
316 EXPECT_TRUE(!test.isNegative());
317 EXPECT_TRUE(test.bitwiseIsEqual(expected));
319 // nextDown(-Smallest Normal) -> -Smallest Normal - inc.
320 test = APFloat(APFloat::IEEEquad, "-0x1.0000000000000000000000000000p-16382");
321 expected = APFloat(APFloat::IEEEquad,
322 "-0x1.0000000000000000000000000001p-16382");
323 EXPECT_EQ(test.next(true), APFloat::opOK);
324 EXPECT_TRUE(!test.isDenormal());
325 EXPECT_TRUE(test.isNegative());
326 EXPECT_TRUE(test.bitwiseIsEqual(expected));
328 // 2d. Test values which cause our exponent to go to min exponent. This
329 // is to ensure that guards in the code to check for min exponent
331 // * nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
332 // * nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
334 // * nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16382
335 // * nextDown(0x1p-16382) -> 0x1.ffffffffffffffffffffffffffffp-16382
337 // nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
338 test = APFloat(APFloat::IEEEquad, "-0x1p-16381");
339 expected = APFloat(APFloat::IEEEquad,
340 "-0x1.ffffffffffffffffffffffffffffp-16382");
341 EXPECT_EQ(test.next(false), APFloat::opOK);
342 EXPECT_TRUE(test.bitwiseIsEqual(expected));
344 // nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
346 test = APFloat(APFloat::IEEEquad, "-0x1.ffffffffffffffffffffffffffffp-16382");
347 expected = APFloat(APFloat::IEEEquad, "-0x1p-16381");
348 EXPECT_EQ(test.next(true), APFloat::opOK);
349 EXPECT_TRUE(test.bitwiseIsEqual(expected));
351 // nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16381
352 test = APFloat(APFloat::IEEEquad, "0x1.ffffffffffffffffffffffffffffp-16382");
353 expected = APFloat(APFloat::IEEEquad, "0x1p-16381");
354 EXPECT_EQ(test.next(false), APFloat::opOK);
355 EXPECT_TRUE(test.bitwiseIsEqual(expected));
357 // nextDown(0x1p-16381) -> 0x1.ffffffffffffffffffffffffffffp-16382
358 test = APFloat(APFloat::IEEEquad, "0x1p-16381");
359 expected = APFloat(APFloat::IEEEquad,
360 "0x1.ffffffffffffffffffffffffffffp-16382");
361 EXPECT_EQ(test.next(true), APFloat::opOK);
362 EXPECT_TRUE(test.bitwiseIsEqual(expected));
364 // 3. Now we test both denormal/normal computation which will not cause us
365 // to go across binade boundaries. Specifically we test:
366 // * nextUp(+Denormal) -> +Denormal.
367 // * nextDown(+Denormal) -> +Denormal.
368 // * nextUp(-Denormal) -> -Denormal.
369 // * nextDown(-Denormal) -> -Denormal.
370 // * nextUp(+Normal) -> +Normal.
371 // * nextDown(+Normal) -> +Normal.
372 // * nextUp(-Normal) -> -Normal.
373 // * nextDown(-Normal) -> -Normal.
375 // nextUp(+Denormal) -> +Denormal.
376 test = APFloat(APFloat::IEEEquad,
377 "0x0.ffffffffffffffffffffffff000cp-16382");
378 expected = APFloat(APFloat::IEEEquad,
379 "0x0.ffffffffffffffffffffffff000dp-16382");
380 EXPECT_EQ(test.next(false), APFloat::opOK);
381 EXPECT_TRUE(test.isDenormal());
382 EXPECT_TRUE(!test.isNegative());
383 EXPECT_TRUE(test.bitwiseIsEqual(expected));
385 // nextDown(+Denormal) -> +Denormal.
386 test = APFloat(APFloat::IEEEquad,
387 "0x0.ffffffffffffffffffffffff000cp-16382");
388 expected = APFloat(APFloat::IEEEquad,
389 "0x0.ffffffffffffffffffffffff000bp-16382");
390 EXPECT_EQ(test.next(true), APFloat::opOK);
391 EXPECT_TRUE(test.isDenormal());
392 EXPECT_TRUE(!test.isNegative());
393 EXPECT_TRUE(test.bitwiseIsEqual(expected));
395 // nextUp(-Denormal) -> -Denormal.
396 test = APFloat(APFloat::IEEEquad,
397 "-0x0.ffffffffffffffffffffffff000cp-16382");
398 expected = APFloat(APFloat::IEEEquad,
399 "-0x0.ffffffffffffffffffffffff000bp-16382");
400 EXPECT_EQ(test.next(false), APFloat::opOK);
401 EXPECT_TRUE(test.isDenormal());
402 EXPECT_TRUE(test.isNegative());
403 EXPECT_TRUE(test.bitwiseIsEqual(expected));
405 // nextDown(-Denormal) -> -Denormal
406 test = APFloat(APFloat::IEEEquad,
407 "-0x0.ffffffffffffffffffffffff000cp-16382");
408 expected = APFloat(APFloat::IEEEquad,
409 "-0x0.ffffffffffffffffffffffff000dp-16382");
410 EXPECT_EQ(test.next(true), APFloat::opOK);
411 EXPECT_TRUE(test.isDenormal());
412 EXPECT_TRUE(test.isNegative());
413 EXPECT_TRUE(test.bitwiseIsEqual(expected));
415 // nextUp(+Normal) -> +Normal.
416 test = APFloat(APFloat::IEEEquad,
417 "0x1.ffffffffffffffffffffffff000cp-16000");
418 expected = APFloat(APFloat::IEEEquad,
419 "0x1.ffffffffffffffffffffffff000dp-16000");
420 EXPECT_EQ(test.next(false), APFloat::opOK);
421 EXPECT_TRUE(!test.isDenormal());
422 EXPECT_TRUE(!test.isNegative());
423 EXPECT_TRUE(test.bitwiseIsEqual(expected));
425 // nextDown(+Normal) -> +Normal.
426 test = APFloat(APFloat::IEEEquad,
427 "0x1.ffffffffffffffffffffffff000cp-16000");
428 expected = APFloat(APFloat::IEEEquad,
429 "0x1.ffffffffffffffffffffffff000bp-16000");
430 EXPECT_EQ(test.next(true), APFloat::opOK);
431 EXPECT_TRUE(!test.isDenormal());
432 EXPECT_TRUE(!test.isNegative());
433 EXPECT_TRUE(test.bitwiseIsEqual(expected));
435 // nextUp(-Normal) -> -Normal.
436 test = APFloat(APFloat::IEEEquad,
437 "-0x1.ffffffffffffffffffffffff000cp-16000");
438 expected = APFloat(APFloat::IEEEquad,
439 "-0x1.ffffffffffffffffffffffff000bp-16000");
440 EXPECT_EQ(test.next(false), APFloat::opOK);
441 EXPECT_TRUE(!test.isDenormal());
442 EXPECT_TRUE(test.isNegative());
443 EXPECT_TRUE(test.bitwiseIsEqual(expected));
445 // nextDown(-Normal) -> -Normal.
446 test = APFloat(APFloat::IEEEquad,
447 "-0x1.ffffffffffffffffffffffff000cp-16000");
448 expected = APFloat(APFloat::IEEEquad,
449 "-0x1.ffffffffffffffffffffffff000dp-16000");
450 EXPECT_EQ(test.next(true), APFloat::opOK);
451 EXPECT_TRUE(!test.isDenormal());
452 EXPECT_TRUE(test.isNegative());
453 EXPECT_TRUE(test.bitwiseIsEqual(expected));
456 TEST(APFloatTest, FMA) {
457 APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;
463 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
464 EXPECT_EQ(14.75f, f1.convertToFloat());
469 APFloat f1((float)1.17549435e-38F);
470 APFloat f2((float)1.17549435e-38F);
471 f1.divide(Val2, rdmd);
472 f2.divide(Val2, rdmd);
474 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
475 EXPECT_EQ(12.0f, f1.convertToFloat());
479 TEST(APFloatTest, Denormal) {
480 APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;
482 // Test single precision
484 const char *MinNormalStr = "1.17549435082228750797e-38";
485 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, MinNormalStr).isDenormal());
486 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, 0.0).isDenormal());
488 APFloat Val2(APFloat::IEEEsingle, 2.0e0);
489 APFloat T(APFloat::IEEEsingle, MinNormalStr);
490 T.divide(Val2, rdmd);
491 EXPECT_TRUE(T.isDenormal());
494 // Test double precision
496 const char *MinNormalStr = "2.22507385850720138309e-308";
497 EXPECT_FALSE(APFloat(APFloat::IEEEdouble, MinNormalStr).isDenormal());
498 EXPECT_FALSE(APFloat(APFloat::IEEEdouble, 0.0).isDenormal());
500 APFloat Val2(APFloat::IEEEdouble, 2.0e0);
501 APFloat T(APFloat::IEEEdouble, MinNormalStr);
502 T.divide(Val2, rdmd);
503 EXPECT_TRUE(T.isDenormal());
506 // Test Intel double-ext
508 const char *MinNormalStr = "3.36210314311209350626e-4932";
509 EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, MinNormalStr).isDenormal());
510 EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, 0.0).isDenormal());
512 APFloat Val2(APFloat::x87DoubleExtended, 2.0e0);
513 APFloat T(APFloat::x87DoubleExtended, MinNormalStr);
514 T.divide(Val2, rdmd);
515 EXPECT_TRUE(T.isDenormal());
518 // Test quadruple precision
520 const char *MinNormalStr = "3.36210314311209350626267781732175260e-4932";
521 EXPECT_FALSE(APFloat(APFloat::IEEEquad, MinNormalStr).isDenormal());
522 EXPECT_FALSE(APFloat(APFloat::IEEEquad, 0.0).isDenormal());
524 APFloat Val2(APFloat::IEEEquad, 2.0e0);
525 APFloat T(APFloat::IEEEquad, MinNormalStr);
526 T.divide(Val2, rdmd);
527 EXPECT_TRUE(T.isDenormal());
531 TEST(APFloatTest, Zero) {
532 EXPECT_EQ(0.0f, APFloat(0.0f).convertToFloat());
533 EXPECT_EQ(-0.0f, APFloat(-0.0f).convertToFloat());
534 EXPECT_TRUE(APFloat(-0.0f).isNegative());
536 EXPECT_EQ(0.0, APFloat(0.0).convertToDouble());
537 EXPECT_EQ(-0.0, APFloat(-0.0).convertToDouble());
538 EXPECT_TRUE(APFloat(-0.0).isNegative());
541 TEST(APFloatTest, fromZeroDecimalString) {
542 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0").convertToDouble());
543 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0").convertToDouble());
544 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0").convertToDouble());
546 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.").convertToDouble());
547 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.").convertToDouble());
548 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.").convertToDouble());
550 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0").convertToDouble());
551 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0").convertToDouble());
552 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0").convertToDouble());
554 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0").convertToDouble());
555 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0").convertToDouble());
556 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0").convertToDouble());
558 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "00000.").convertToDouble());
559 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+00000.").convertToDouble());
560 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-00000.").convertToDouble());
562 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, ".00000").convertToDouble());
563 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.00000").convertToDouble());
564 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.00000").convertToDouble());
566 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0000.00000").convertToDouble());
567 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0000.00000").convertToDouble());
568 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0000.00000").convertToDouble());
571 TEST(APFloatTest, fromZeroDecimalSingleExponentString) {
572 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e1").convertToDouble());
573 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e1").convertToDouble());
574 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e1").convertToDouble());
576 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e+1").convertToDouble());
577 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1").convertToDouble());
578 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1").convertToDouble());
580 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e-1").convertToDouble());
581 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1").convertToDouble());
582 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1").convertToDouble());
585 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e1").convertToDouble());
586 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e1").convertToDouble());
587 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e1").convertToDouble());
589 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e+1").convertToDouble());
590 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e+1").convertToDouble());
591 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e+1").convertToDouble());
593 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e-1").convertToDouble());
594 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e-1").convertToDouble());
595 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e-1").convertToDouble());
597 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e1").convertToDouble());
598 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e1").convertToDouble());
599 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e1").convertToDouble());
601 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e+1").convertToDouble());
602 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e+1").convertToDouble());
603 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e+1").convertToDouble());
605 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e-1").convertToDouble());
606 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e-1").convertToDouble());
607 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e-1").convertToDouble());
610 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e1").convertToDouble());
611 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e1").convertToDouble());
612 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e1").convertToDouble());
614 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e+1").convertToDouble());
615 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e+1").convertToDouble());
616 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e+1").convertToDouble());
618 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e-1").convertToDouble());
619 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e-1").convertToDouble());
620 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e-1").convertToDouble());
623 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "000.0000e1").convertToDouble());
624 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+000.0000e+1").convertToDouble());
625 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-000.0000e+1").convertToDouble());
628 TEST(APFloatTest, fromZeroDecimalLargeExponentString) {
629 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e1234").convertToDouble());
630 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e1234").convertToDouble());
631 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e1234").convertToDouble());
633 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e+1234").convertToDouble());
634 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1234").convertToDouble());
635 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1234").convertToDouble());
637 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e-1234").convertToDouble());
638 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1234").convertToDouble());
639 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1234").convertToDouble());
641 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, "000.0000e1234").convertToDouble());
642 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, "000.0000e-1234").convertToDouble());
644 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, StringRef("0e1234\02", 6)).convertToDouble());
647 TEST(APFloatTest, fromZeroHexadecimalString) {
648 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p1").convertToDouble());
649 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p1").convertToDouble());
650 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1").convertToDouble());
652 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p+1").convertToDouble());
653 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p+1").convertToDouble());
654 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p+1").convertToDouble());
656 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p-1").convertToDouble());
657 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p-1").convertToDouble());
658 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p-1").convertToDouble());
661 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1").convertToDouble());
662 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p1").convertToDouble());
663 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p1").convertToDouble());
665 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p+1").convertToDouble());
666 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p+1").convertToDouble());
667 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p+1").convertToDouble());
669 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p-1").convertToDouble());
670 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p-1").convertToDouble());
671 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p-1").convertToDouble());
674 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p1").convertToDouble());
675 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p1").convertToDouble());
676 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p1").convertToDouble());
678 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p+1").convertToDouble());
679 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p+1").convertToDouble());
680 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p+1").convertToDouble());
682 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p-1").convertToDouble());
683 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p-1").convertToDouble());
684 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p-1").convertToDouble());
687 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p1").convertToDouble());
688 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p1").convertToDouble());
689 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p1").convertToDouble());
691 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p+1").convertToDouble());
692 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p+1").convertToDouble());
693 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p+1").convertToDouble());
695 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p-1").convertToDouble());
696 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p-1").convertToDouble());
697 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p-1").convertToDouble());
700 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1").convertToDouble());
701 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1").convertToDouble());
702 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1").convertToDouble());
703 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1").convertToDouble());
704 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p1234").convertToDouble());
705 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1234").convertToDouble());
706 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1234").convertToDouble());
707 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1234").convertToDouble());
708 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1234").convertToDouble());
709 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1234").convertToDouble());
712 TEST(APFloatTest, fromDecimalString) {
713 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "1").convertToDouble());
714 EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble, "2.").convertToDouble());
715 EXPECT_EQ(0.5, APFloat(APFloat::IEEEdouble, ".5").convertToDouble());
716 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "1.0").convertToDouble());
717 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-2").convertToDouble());
718 EXPECT_EQ(-4.0, APFloat(APFloat::IEEEdouble, "-4.").convertToDouble());
719 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble, "-.5").convertToDouble());
720 EXPECT_EQ(-1.5, APFloat(APFloat::IEEEdouble, "-1.5").convertToDouble());
721 EXPECT_EQ(1.25e12, APFloat(APFloat::IEEEdouble, "1.25e12").convertToDouble());
722 EXPECT_EQ(1.25e+12, APFloat(APFloat::IEEEdouble, "1.25e+12").convertToDouble());
723 EXPECT_EQ(1.25e-12, APFloat(APFloat::IEEEdouble, "1.25e-12").convertToDouble());
724 EXPECT_EQ(1024.0, APFloat(APFloat::IEEEdouble, "1024.").convertToDouble());
725 EXPECT_EQ(1024.05, APFloat(APFloat::IEEEdouble, "1024.05000").convertToDouble());
726 EXPECT_EQ(0.05, APFloat(APFloat::IEEEdouble, ".05000").convertToDouble());
727 EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble, "2.").convertToDouble());
728 EXPECT_EQ(2.0e2, APFloat(APFloat::IEEEdouble, "2.e2").convertToDouble());
729 EXPECT_EQ(2.0e+2, APFloat(APFloat::IEEEdouble, "2.e+2").convertToDouble());
730 EXPECT_EQ(2.0e-2, APFloat(APFloat::IEEEdouble, "2.e-2").convertToDouble());
731 EXPECT_EQ(2.05e2, APFloat(APFloat::IEEEdouble, "002.05000e2").convertToDouble());
732 EXPECT_EQ(2.05e+2, APFloat(APFloat::IEEEdouble, "002.05000e+2").convertToDouble());
733 EXPECT_EQ(2.05e-2, APFloat(APFloat::IEEEdouble, "002.05000e-2").convertToDouble());
734 EXPECT_EQ(2.05e12, APFloat(APFloat::IEEEdouble, "002.05000e12").convertToDouble());
735 EXPECT_EQ(2.05e+12, APFloat(APFloat::IEEEdouble, "002.05000e+12").convertToDouble());
736 EXPECT_EQ(2.05e-12, APFloat(APFloat::IEEEdouble, "002.05000e-12").convertToDouble());
738 // These are "carefully selected" to overflow the fast log-base
739 // calculations in APFloat.cpp
740 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "99e99999").isInfinity());
741 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-99e99999").isInfinity());
742 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "1e-99999").isPosZero());
743 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-1e-99999").isNegZero());
746 TEST(APFloatTest, fromHexadecimalString) {
747 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p0").convertToDouble());
748 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p0").convertToDouble());
749 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p0").convertToDouble());
751 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p+0").convertToDouble());
752 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p+0").convertToDouble());
753 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p+0").convertToDouble());
755 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p-0").convertToDouble());
756 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p-0").convertToDouble());
757 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p-0").convertToDouble());
760 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble, "0x1p1").convertToDouble());
761 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p1").convertToDouble());
762 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p1").convertToDouble());
764 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble, "0x1p+1").convertToDouble());
765 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p+1").convertToDouble());
766 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p+1").convertToDouble());
768 EXPECT_EQ( 0.5, APFloat(APFloat::IEEEdouble, "0x1p-1").convertToDouble());
769 EXPECT_EQ(+0.5, APFloat(APFloat::IEEEdouble, "+0x1p-1").convertToDouble());
770 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble, "-0x1p-1").convertToDouble());
773 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble, "0x1.8p1").convertToDouble());
774 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p1").convertToDouble());
775 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p1").convertToDouble());
777 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble, "0x1.8p+1").convertToDouble());
778 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p+1").convertToDouble());
779 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p+1").convertToDouble());
781 EXPECT_EQ( 0.75, APFloat(APFloat::IEEEdouble, "0x1.8p-1").convertToDouble());
782 EXPECT_EQ(+0.75, APFloat(APFloat::IEEEdouble, "+0x1.8p-1").convertToDouble());
783 EXPECT_EQ(-0.75, APFloat(APFloat::IEEEdouble, "-0x1.8p-1").convertToDouble());
786 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000.000p1").convertToDouble());
787 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p1").convertToDouble());
788 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p1").convertToDouble());
790 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000.000p+1").convertToDouble());
791 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p+1").convertToDouble());
792 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p+1").convertToDouble());
794 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble, "0x1000.000p-1").convertToDouble());
795 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p-1").convertToDouble());
796 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p-1").convertToDouble());
799 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000p1").convertToDouble());
800 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p1").convertToDouble());
801 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p1").convertToDouble());
803 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000p+1").convertToDouble());
804 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p+1").convertToDouble());
805 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p+1").convertToDouble());
807 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble, "0x1000p-1").convertToDouble());
808 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000p-1").convertToDouble());
809 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000p-1").convertToDouble());
812 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble, "0x10p10").convertToDouble());
813 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p10").convertToDouble());
814 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p10").convertToDouble());
816 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble, "0x10p+10").convertToDouble());
817 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p+10").convertToDouble());
818 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p+10").convertToDouble());
820 EXPECT_EQ( 0.015625, APFloat(APFloat::IEEEdouble, "0x10p-10").convertToDouble());
821 EXPECT_EQ(+0.015625, APFloat(APFloat::IEEEdouble, "+0x10p-10").convertToDouble());
822 EXPECT_EQ(-0.015625, APFloat(APFloat::IEEEdouble, "-0x10p-10").convertToDouble());
824 EXPECT_EQ(1.0625, APFloat(APFloat::IEEEdouble, "0x1.1p0").convertToDouble());
825 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "0x1p0").convertToDouble());
827 EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
830 TEST(APFloatTest, toString) {
831 ASSERT_EQ("10", convertToString(10.0, 6, 3));
832 ASSERT_EQ("1.0E+1", convertToString(10.0, 6, 0));
833 ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2));
834 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 4, 2));
835 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 5, 1));
836 ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2));
837 ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2));
838 ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 5, 1));
839 ASSERT_EQ("0.7853981633974483", convertToString(0.78539816339744830961, 0, 3));
840 ASSERT_EQ("4.940656458412465E-324", convertToString(4.9406564584124654e-324, 0, 3));
841 ASSERT_EQ("873.1834", convertToString(873.1834, 0, 1));
842 ASSERT_EQ("8.731834E+2", convertToString(873.1834, 0, 0));
845 TEST(APFloatTest, toInteger) {
846 bool isExact = false;
847 APSInt result(5, /*isUnsigned=*/true);
849 EXPECT_EQ(APFloat::opOK,
850 APFloat(APFloat::IEEEdouble, "10")
851 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
852 EXPECT_TRUE(isExact);
853 EXPECT_EQ(APSInt(APInt(5, 10), true), result);
855 EXPECT_EQ(APFloat::opInvalidOp,
856 APFloat(APFloat::IEEEdouble, "-10")
857 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
858 EXPECT_FALSE(isExact);
859 EXPECT_EQ(APSInt::getMinValue(5, true), result);
861 EXPECT_EQ(APFloat::opInvalidOp,
862 APFloat(APFloat::IEEEdouble, "32")
863 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
864 EXPECT_FALSE(isExact);
865 EXPECT_EQ(APSInt::getMaxValue(5, true), result);
867 EXPECT_EQ(APFloat::opInexact,
868 APFloat(APFloat::IEEEdouble, "7.9")
869 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
870 EXPECT_FALSE(isExact);
871 EXPECT_EQ(APSInt(APInt(5, 7), true), result);
873 result.setIsUnsigned(false);
874 EXPECT_EQ(APFloat::opOK,
875 APFloat(APFloat::IEEEdouble, "-10")
876 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
877 EXPECT_TRUE(isExact);
878 EXPECT_EQ(APSInt(APInt(5, -10, true), false), result);
880 EXPECT_EQ(APFloat::opInvalidOp,
881 APFloat(APFloat::IEEEdouble, "-17")
882 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
883 EXPECT_FALSE(isExact);
884 EXPECT_EQ(APSInt::getMinValue(5, false), result);
886 EXPECT_EQ(APFloat::opInvalidOp,
887 APFloat(APFloat::IEEEdouble, "16")
888 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
889 EXPECT_FALSE(isExact);
890 EXPECT_EQ(APSInt::getMaxValue(5, false), result);
893 static APInt nanbits(const fltSemantics &Sem,
894 bool SNaN, bool Negative, uint64_t fill) {
895 APInt apfill(64, fill);
897 return APFloat::getSNaN(Sem, Negative, &apfill).bitcastToAPInt();
899 return APFloat::getQNaN(Sem, Negative, &apfill).bitcastToAPInt();
902 TEST(APFloatTest, makeNaN) {
903 ASSERT_EQ(0x7fc00000, nanbits(APFloat::IEEEsingle, false, false, 0));
904 ASSERT_EQ(0xffc00000, nanbits(APFloat::IEEEsingle, false, true, 0));
905 ASSERT_EQ(0x7fc0ae72, nanbits(APFloat::IEEEsingle, false, false, 0xae72));
906 ASSERT_EQ(0x7fffae72, nanbits(APFloat::IEEEsingle, false, false, 0xffffae72));
907 ASSERT_EQ(0x7fa00000, nanbits(APFloat::IEEEsingle, true, false, 0));
908 ASSERT_EQ(0xffa00000, nanbits(APFloat::IEEEsingle, true, true, 0));
909 ASSERT_EQ(0x7f80ae72, nanbits(APFloat::IEEEsingle, true, false, 0xae72));
910 ASSERT_EQ(0x7fbfae72, nanbits(APFloat::IEEEsingle, true, false, 0xffffae72));
912 ASSERT_EQ(0x7ff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, false, 0));
913 ASSERT_EQ(0xfff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, true, 0));
914 ASSERT_EQ(0x7ff800000000ae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xae72));
915 ASSERT_EQ(0x7fffffffffffae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xffffffffffffae72ULL));
916 ASSERT_EQ(0x7ff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, false, 0));
917 ASSERT_EQ(0xfff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, true, 0));
918 ASSERT_EQ(0x7ff000000000ae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xae72));
919 ASSERT_EQ(0x7ff7ffffffffae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xffffffffffffae72ULL));
922 #ifdef GTEST_HAS_DEATH_TEST
924 TEST(APFloatTest, SemanticsDeath) {
925 EXPECT_DEATH(APFloat(APFloat::IEEEsingle, 0.0f).convertToDouble(), "Float semantics are not IEEEdouble");
926 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, 0.0 ).convertToFloat(), "Float semantics are not IEEEsingle");
929 TEST(APFloatTest, StringDecimalDeath) {
930 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ""), "Invalid string length");
931 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+"), "String has no digits");
932 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-"), "String has no digits");
934 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("\0", 1)), "Invalid character in significand");
935 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\0", 2)), "Invalid character in significand");
936 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02", 3)), "Invalid character in significand");
937 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02e1", 5)), "Invalid character in significand");
938 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e\0", 3)), "Invalid character in exponent");
939 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\0", 4)), "Invalid character in exponent");
940 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\02", 5)), "Invalid character in exponent");
942 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0f"), "Invalid character in significand");
944 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".."), "String contains multiple dots");
945 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "..0"), "String contains multiple dots");
946 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0.0"), "String contains multiple dots");
949 TEST(APFloatTest, StringDecimalSignificandDeath) {
950 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "."), "Significand has no digits");
951 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+."), "Significand has no digits");
952 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-."), "Significand has no digits");
955 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "e"), "Significand has no digits");
956 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e"), "Significand has no digits");
957 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e"), "Significand has no digits");
959 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "e1"), "Significand has no digits");
960 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e1"), "Significand has no digits");
961 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e1"), "Significand has no digits");
963 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".e1"), "Significand has no digits");
964 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e1"), "Significand has no digits");
965 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e1"), "Significand has no digits");
968 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".e"), "Significand has no digits");
969 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e"), "Significand has no digits");
970 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e"), "Significand has no digits");
973 TEST(APFloatTest, StringDecimalExponentDeath) {
974 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e"), "Exponent has no digits");
975 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1e"), "Exponent has no digits");
976 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1e"), "Exponent has no digits");
978 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.e"), "Exponent has no digits");
979 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1.e"), "Exponent has no digits");
980 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1.e"), "Exponent has no digits");
982 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e"), "Exponent has no digits");
983 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.1e"), "Exponent has no digits");
984 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.1e"), "Exponent has no digits");
986 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.1e"), "Exponent has no digits");
987 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1.1e"), "Exponent has no digits");
988 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1.1e"), "Exponent has no digits");
991 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e+"), "Exponent has no digits");
992 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e-"), "Exponent has no digits");
994 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e"), "Exponent has no digits");
995 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e+"), "Exponent has no digits");
996 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e-"), "Exponent has no digits");
998 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e"), "Exponent has no digits");
999 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e+"), "Exponent has no digits");
1000 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e-"), "Exponent has no digits");
1003 TEST(APFloatTest, StringHexadecimalDeath) {
1004 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x"), "Invalid string");
1005 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x"), "Invalid string");
1006 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x"), "Invalid string");
1008 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0"), "Hex strings require an exponent");
1009 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0"), "Hex strings require an exponent");
1010 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0"), "Hex strings require an exponent");
1012 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0."), "Hex strings require an exponent");
1013 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0."), "Hex strings require an exponent");
1014 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0."), "Hex strings require an exponent");
1016 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.0"), "Hex strings require an exponent");
1017 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.0"), "Hex strings require an exponent");
1018 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.0"), "Hex strings require an exponent");
1020 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0.0"), "Hex strings require an exponent");
1021 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0.0"), "Hex strings require an exponent");
1022 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0.0"), "Hex strings require an exponent");
1024 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x\0", 3)), "Invalid character in significand");
1025 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\0", 4)), "Invalid character in significand");
1026 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02", 5)), "Invalid character in significand");
1027 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02p1", 7)), "Invalid character in significand");
1028 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p\0", 5)), "Invalid character in exponent");
1029 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\0", 6)), "Invalid character in exponent");
1030 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\02", 7)), "Invalid character in exponent");
1032 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p0f"), "Invalid character in exponent");
1034 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..p1"), "String contains multiple dots");
1035 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..0p1"), "String contains multiple dots");
1036 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.0.0p1"), "String contains multiple dots");
1039 TEST(APFloatTest, StringHexadecimalSignificandDeath) {
1040 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x."), "Significand has no digits");
1041 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x."), "Significand has no digits");
1042 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x."), "Significand has no digits");
1044 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp"), "Significand has no digits");
1045 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp"), "Significand has no digits");
1046 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp"), "Significand has no digits");
1048 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp+"), "Significand has no digits");
1049 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp+"), "Significand has no digits");
1050 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp+"), "Significand has no digits");
1052 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp-"), "Significand has no digits");
1053 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp-"), "Significand has no digits");
1054 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp-"), "Significand has no digits");
1057 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p"), "Significand has no digits");
1058 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p"), "Significand has no digits");
1059 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p"), "Significand has no digits");
1061 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p+"), "Significand has no digits");
1062 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p+"), "Significand has no digits");
1063 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p+"), "Significand has no digits");
1065 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p-"), "Significand has no digits");
1066 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p-"), "Significand has no digits");
1067 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p-"), "Significand has no digits");
1070 TEST(APFloatTest, StringHexadecimalExponentDeath) {
1071 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p"), "Exponent has no digits");
1072 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p"), "Exponent has no digits");
1073 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p"), "Exponent has no digits");
1075 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p+"), "Exponent has no digits");
1076 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p+"), "Exponent has no digits");
1077 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p+"), "Exponent has no digits");
1079 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p-"), "Exponent has no digits");
1080 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p-"), "Exponent has no digits");
1081 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p-"), "Exponent has no digits");
1084 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p"), "Exponent has no digits");
1085 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p"), "Exponent has no digits");
1086 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p"), "Exponent has no digits");
1088 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p+"), "Exponent has no digits");
1089 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p+"), "Exponent has no digits");
1090 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p+"), "Exponent has no digits");
1092 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p-"), "Exponent has no digits");
1093 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p-"), "Exponent has no digits");
1094 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p-"), "Exponent has no digits");
1097 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p"), "Exponent has no digits");
1098 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p"), "Exponent has no digits");
1099 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p"), "Exponent has no digits");
1101 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p+"), "Exponent has no digits");
1102 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p+"), "Exponent has no digits");
1103 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p+"), "Exponent has no digits");
1105 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p-"), "Exponent has no digits");
1106 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p-"), "Exponent has no digits");
1107 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p-"), "Exponent has no digits");
1110 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p"), "Exponent has no digits");
1111 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p"), "Exponent has no digits");
1112 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p"), "Exponent has no digits");
1114 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p+"), "Exponent has no digits");
1115 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p+"), "Exponent has no digits");
1116 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p+"), "Exponent has no digits");
1118 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p-"), "Exponent has no digits");
1119 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p-"), "Exponent has no digits");
1120 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p-"), "Exponent has no digits");
1125 TEST(APFloatTest, exactInverse) {
1128 // Trivial operation.
1129 EXPECT_TRUE(APFloat(2.0).getExactInverse(&inv));
1130 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5)));
1131 EXPECT_TRUE(APFloat(2.0f).getExactInverse(&inv));
1132 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5f)));
1133 EXPECT_TRUE(APFloat(APFloat::IEEEquad, "2.0").getExactInverse(&inv));
1134 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::IEEEquad, "0.5")));
1135 EXPECT_TRUE(APFloat(APFloat::PPCDoubleDouble, "2.0").getExactInverse(&inv));
1136 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::PPCDoubleDouble, "0.5")));
1137 EXPECT_TRUE(APFloat(APFloat::x87DoubleExtended, "2.0").getExactInverse(&inv));
1138 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::x87DoubleExtended, "0.5")));
1141 EXPECT_TRUE(APFloat(1.17549435e-38f).getExactInverse(&inv));
1142 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(8.5070592e+37f)));
1144 // Large float, inverse is a denormal.
1145 EXPECT_FALSE(APFloat(1.7014118e38f).getExactInverse(0));
1147 EXPECT_FALSE(APFloat(0.0).getExactInverse(0));
1148 // Denormalized float
1149 EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(0));
1152 TEST(APFloatTest, roundToIntegral) {
1153 APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble)), P(0.0);
1156 P.roundToIntegral(APFloat::rmTowardZero);
1157 EXPECT_EQ(-0.0, P.convertToDouble());
1159 P.roundToIntegral(APFloat::rmTowardNegative);
1160 EXPECT_EQ(-1.0, P.convertToDouble());
1162 P.roundToIntegral(APFloat::rmTowardPositive);
1163 EXPECT_EQ(-0.0, P.convertToDouble());
1165 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1166 EXPECT_EQ(-0.0, P.convertToDouble());
1169 P.roundToIntegral(APFloat::rmTowardZero);
1170 EXPECT_EQ(3.0, P.convertToDouble());
1172 P.roundToIntegral(APFloat::rmTowardNegative);
1173 EXPECT_EQ(3.0, P.convertToDouble());
1175 P.roundToIntegral(APFloat::rmTowardPositive);
1176 EXPECT_EQ(4.0, P.convertToDouble());
1178 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1179 EXPECT_EQ(3.0, P.convertToDouble());
1182 P.roundToIntegral(APFloat::rmTowardZero);
1183 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1185 P.roundToIntegral(APFloat::rmTowardNegative);
1186 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1188 P.roundToIntegral(APFloat::rmTowardPositive);
1189 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1191 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1192 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1194 P = APFloat::getZero(APFloat::IEEEdouble);
1195 P.roundToIntegral(APFloat::rmTowardZero);
1196 EXPECT_EQ(0.0, P.convertToDouble());
1197 P = APFloat::getZero(APFloat::IEEEdouble, true);
1198 P.roundToIntegral(APFloat::rmTowardZero);
1199 EXPECT_EQ(-0.0, P.convertToDouble());
1200 P = APFloat::getNaN(APFloat::IEEEdouble);
1201 P.roundToIntegral(APFloat::rmTowardZero);
1202 EXPECT_TRUE(IsNAN(P.convertToDouble()));
1203 P = APFloat::getInf(APFloat::IEEEdouble);
1204 P.roundToIntegral(APFloat::rmTowardZero);
1205 EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() > 0.0);
1206 P = APFloat::getInf(APFloat::IEEEdouble, true);
1207 P.roundToIntegral(APFloat::rmTowardZero);
1208 EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() < 0.0);
1212 TEST(APFloatTest, getLargest) {
1213 EXPECT_EQ(3.402823466e+38f, APFloat::getLargest(APFloat::IEEEsingle).convertToFloat());
1214 EXPECT_EQ(1.7976931348623158e+308, APFloat::getLargest(APFloat::IEEEdouble).convertToDouble());
1217 TEST(APFloatTest, getSmallest) {
1218 APFloat test = APFloat::getSmallest(APFloat::IEEEsingle, false);
1219 APFloat expected = APFloat(APFloat::IEEEsingle, "0x0.000002p-126");
1220 EXPECT_FALSE(test.isNegative());
1221 EXPECT_TRUE(test.isNormal());
1222 EXPECT_TRUE(test.isDenormal());
1223 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1225 test = APFloat::getSmallest(APFloat::IEEEsingle, true);
1226 expected = APFloat(APFloat::IEEEsingle, "-0x0.000002p-126");
1227 EXPECT_TRUE(test.isNegative());
1228 EXPECT_TRUE(test.isNormal());
1229 EXPECT_TRUE(test.isDenormal());
1230 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1232 test = APFloat::getSmallest(APFloat::IEEEquad, false);
1233 expected = APFloat(APFloat::IEEEquad, "0x0.0000000000000000000000000001p-16382");
1234 EXPECT_FALSE(test.isNegative());
1235 EXPECT_TRUE(test.isNormal());
1236 EXPECT_TRUE(test.isDenormal());
1237 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1239 test = APFloat::getSmallest(APFloat::IEEEquad, true);
1240 expected = APFloat(APFloat::IEEEquad, "-0x0.0000000000000000000000000001p-16382");
1241 EXPECT_TRUE(test.isNegative());
1242 EXPECT_TRUE(test.isNormal());
1243 EXPECT_TRUE(test.isDenormal());
1244 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1247 TEST(APFloatTest, getSmallestNormalized) {
1248 APFloat test = APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
1249 APFloat expected = APFloat(APFloat::IEEEsingle, "0x1p-126");
1250 EXPECT_FALSE(test.isNegative());
1251 EXPECT_TRUE(test.isNormal());
1252 EXPECT_FALSE(test.isDenormal());
1253 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1255 test = APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
1256 expected = APFloat(APFloat::IEEEsingle, "-0x1p-126");
1257 EXPECT_TRUE(test.isNegative());
1258 EXPECT_TRUE(test.isNormal());
1259 EXPECT_FALSE(test.isDenormal());
1260 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1262 test = APFloat::getSmallestNormalized(APFloat::IEEEquad, false);
1263 expected = APFloat(APFloat::IEEEquad, "0x1p-16382");
1264 EXPECT_FALSE(test.isNegative());
1265 EXPECT_TRUE(test.isNormal());
1266 EXPECT_FALSE(test.isDenormal());
1267 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1269 test = APFloat::getSmallestNormalized(APFloat::IEEEquad, true);
1270 expected = APFloat(APFloat::IEEEquad, "-0x1p-16382");
1271 EXPECT_TRUE(test.isNegative());
1272 EXPECT_TRUE(test.isNormal());
1273 EXPECT_FALSE(test.isDenormal());
1274 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1277 TEST(APFloatTest, getZero) {
1279 const fltSemantics *semantics;
1281 const unsigned long long bitPattern[2];
1282 const unsigned bitPatternLength;
1283 } const GetZeroTest[] = {
1284 { &APFloat::IEEEhalf, false, {0, 0}, 1},
1285 { &APFloat::IEEEhalf, true, {0x8000ULL, 0}, 1},
1286 { &APFloat::IEEEsingle, false, {0, 0}, 1},
1287 { &APFloat::IEEEsingle, true, {0x80000000ULL, 0}, 1},
1288 { &APFloat::IEEEdouble, false, {0, 0}, 1},
1289 { &APFloat::IEEEdouble, true, {0x8000000000000000ULL, 0}, 1},
1290 { &APFloat::IEEEquad, false, {0, 0}, 2},
1291 { &APFloat::IEEEquad, true, {0, 0x8000000000000000ULL}, 2},
1292 { &APFloat::PPCDoubleDouble, false, {0, 0}, 2},
1293 { &APFloat::PPCDoubleDouble, true, {0x8000000000000000ULL, 0}, 2},
1294 { &APFloat::x87DoubleExtended, false, {0, 0}, 2},
1295 { &APFloat::x87DoubleExtended, true, {0, 0x8000ULL}, 2},
1297 const unsigned NumGetZeroTests = 12;
1298 for (unsigned i = 0; i < NumGetZeroTests; ++i) {
1299 APFloat test = APFloat::getZero(*GetZeroTest[i].semantics,
1300 GetZeroTest[i].sign);
1301 const char *pattern = GetZeroTest[i].sign? "-0x0p+0" : "0x0p+0";
1302 APFloat expected = APFloat(*GetZeroTest[i].semantics,
1304 EXPECT_TRUE(test.isZero());
1305 EXPECT_TRUE(GetZeroTest[i].sign? test.isNegative() : !test.isNegative());
1306 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1307 for (unsigned j = 0, je = GetZeroTest[i].bitPatternLength; j < je; ++j) {
1308 EXPECT_EQ(GetZeroTest[i].bitPattern[j],
1309 test.bitcastToAPInt().getRawData()[j]);
1314 TEST(APFloatTest, convert) {
1316 APFloat test(APFloat::IEEEdouble, "1.0");
1317 test.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1318 EXPECT_EQ(1.0f, test.convertToFloat());
1319 EXPECT_FALSE(losesInfo);
1321 test = APFloat(APFloat::x87DoubleExtended, "0x1p-53");
1322 test.add(APFloat(APFloat::x87DoubleExtended, "1.0"), APFloat::rmNearestTiesToEven);
1323 test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1324 EXPECT_EQ(1.0, test.convertToDouble());
1325 EXPECT_TRUE(losesInfo);
1327 test = APFloat(APFloat::IEEEquad, "0x1p-53");
1328 test.add(APFloat(APFloat::IEEEquad, "1.0"), APFloat::rmNearestTiesToEven);
1329 test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1330 EXPECT_EQ(1.0, test.convertToDouble());
1331 EXPECT_TRUE(losesInfo);
1333 test = APFloat(APFloat::x87DoubleExtended, "0xf.fffffffp+28");
1334 test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1335 EXPECT_EQ(4294967295.0, test.convertToDouble());
1336 EXPECT_FALSE(losesInfo);
1338 test = APFloat::getSNaN(APFloat::IEEEsingle);
1339 APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended);
1340 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1342 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1343 EXPECT_FALSE(losesInfo);
1345 test = APFloat::getQNaN(APFloat::IEEEsingle);
1346 APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended);
1347 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1349 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1350 EXPECT_FALSE(losesInfo);
1352 test = APFloat::getSNaN(APFloat::x87DoubleExtended);
1353 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1355 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1356 EXPECT_FALSE(losesInfo);
1358 test = APFloat::getQNaN(APFloat::x87DoubleExtended);
1359 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1361 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1362 EXPECT_FALSE(losesInfo);
1365 TEST(APFloatTest, PPCDoubleDouble) {
1366 APFloat test(APFloat::PPCDoubleDouble, "1.0");
1367 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1368 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1370 test.divide(APFloat(APFloat::PPCDoubleDouble, "3.0"), APFloat::rmNearestTiesToEven);
1371 EXPECT_EQ(0x3fd5555555555555ull, test.bitcastToAPInt().getRawData()[0]);
1372 EXPECT_EQ(0x3c75555555555556ull, test.bitcastToAPInt().getRawData()[1]);
1375 test = APFloat(APFloat::PPCDoubleDouble, "1.79769313486231580793728971405301e+308");
1376 EXPECT_EQ(0x7fefffffffffffffull, test.bitcastToAPInt().getRawData()[0]);
1377 EXPECT_EQ(0x7c8ffffffffffffeull, test.bitcastToAPInt().getRawData()[1]);
1380 test = APFloat(APFloat::PPCDoubleDouble, "2.00416836000897277799610805135016e-292");
1381 EXPECT_EQ(0x0360000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1382 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1384 test = APFloat(APFloat::PPCDoubleDouble, "1.0");
1385 test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-105"), APFloat::rmNearestTiesToEven);
1386 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1387 EXPECT_EQ(0x3960000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1389 test = APFloat(APFloat::PPCDoubleDouble, "1.0");
1390 test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-106"), APFloat::rmNearestTiesToEven);
1391 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1393 // This is what we would expect with a true double-double implementation
1394 EXPECT_EQ(0x3950000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1396 // This is what we get with our 106-bit mantissa approximation
1397 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1401 TEST(APFloatTest, isNegative) {
1402 APFloat t(APFloat::IEEEsingle, "0x1p+0");
1403 EXPECT_FALSE(t.isNegative());
1404 t = APFloat(APFloat::IEEEsingle, "-0x1p+0");
1405 EXPECT_TRUE(t.isNegative());
1407 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNegative());
1408 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle, true).isNegative());
1410 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNegative());
1411 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle, true).isNegative());
1413 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isNegative());
1414 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle, true).isNegative());
1416 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNegative());
1417 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, true).isNegative());
1420 TEST(APFloatTest, isIEEENormal) {
1421 APFloat t(APFloat::IEEEsingle, "0x1p+0");
1422 EXPECT_TRUE(t.isIEEENormal());
1424 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isIEEENormal());
1425 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isIEEENormal());
1426 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isIEEENormal());
1427 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isIEEENormal());
1428 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-159").isIEEENormal());
1431 TEST(APFloatTest, isFinite) {
1432 APFloat t(APFloat::IEEEsingle, "0x1p+0");
1433 EXPECT_TRUE(t.isFinite());
1434 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isFinite());
1435 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle, false).isFinite());
1436 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isFinite());
1437 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isFinite());
1438 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p-159").isFinite());
1441 TEST(APFloatTest, isInfinity) {
1442 APFloat t(APFloat::IEEEsingle, "0x1p+0");
1443 EXPECT_FALSE(t.isInfinity());
1444 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle, false).isInfinity());
1445 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isInfinity());
1446 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isInfinity());
1447 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isInfinity());
1448 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-159").isInfinity());
1451 TEST(APFloatTest, isNaN) {
1452 APFloat t(APFloat::IEEEsingle, "0x1p+0");
1453 EXPECT_FALSE(t.isNaN());
1454 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNaN());
1455 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNaN());
1456 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle, false).isNaN());
1457 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNaN());
1458 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-159").isNaN());