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());
478 // Test for correct zero sign when answer is exactly zero.
479 // fma(1.0, -1.0, 1.0) -> +ve 0.
484 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
485 EXPECT_TRUE(!f1.isNegative() && f1.isZero());
488 // Test for correct zero sign when answer is exactly zero and rounding towards
490 // fma(1.0, -1.0, 1.0) -> +ve 0.
495 f1.fusedMultiplyAdd(f2, f3, APFloat::rmTowardNegative);
496 EXPECT_TRUE(f1.isNegative() && f1.isZero());
499 // Test for correct (in this case -ve) sign when adding like signed zeros.
500 // Test fma(0.0, -0.0, -0.0) -> -ve 0.
505 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
506 EXPECT_TRUE(f1.isNegative() && f1.isZero());
509 // Test -ve sign preservation when small negative results underflow.
511 APFloat f1(APFloat::IEEEdouble, "-0x1p-1074");
512 APFloat f2(APFloat::IEEEdouble, "+0x1p-1074");
514 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
515 EXPECT_TRUE(f1.isNegative() && f1.isZero());
518 // Test x87 extended precision case from http://llvm.org/PR20728.
520 APFloat M1(APFloat::x87DoubleExtended, 1.0);
521 APFloat M2(APFloat::x87DoubleExtended, 1.0);
522 APFloat A(APFloat::x87DoubleExtended, 3.0);
524 bool losesInfo = false;
525 M1.fusedMultiplyAdd(M1, A, APFloat::rmNearestTiesToEven);
526 M1.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
527 EXPECT_FALSE(losesInfo);
528 EXPECT_EQ(4.0f, M1.convertToFloat());
532 TEST(APFloatTest, MinNum) {
535 APFloat nan = APFloat::getNaN(APFloat::IEEEdouble);
537 EXPECT_EQ(1.0, minnum(f1, f2).convertToDouble());
538 EXPECT_EQ(1.0, minnum(f2, f1).convertToDouble());
539 EXPECT_EQ(1.0, minnum(f1, nan).convertToDouble());
540 EXPECT_EQ(1.0, minnum(nan, f1).convertToDouble());
543 TEST(APFloatTest, MaxNum) {
546 APFloat nan = APFloat::getNaN(APFloat::IEEEdouble);
548 EXPECT_EQ(2.0, maxnum(f1, f2).convertToDouble());
549 EXPECT_EQ(2.0, maxnum(f2, f1).convertToDouble());
550 EXPECT_EQ(1.0, maxnum(f1, nan).convertToDouble());
551 EXPECT_EQ(1.0, minnum(nan, f1).convertToDouble());
554 TEST(APFloatTest, Denormal) {
555 APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;
557 // Test single precision
559 const char *MinNormalStr = "1.17549435082228750797e-38";
560 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, MinNormalStr).isDenormal());
561 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, 0.0).isDenormal());
563 APFloat Val2(APFloat::IEEEsingle, 2.0e0);
564 APFloat T(APFloat::IEEEsingle, MinNormalStr);
565 T.divide(Val2, rdmd);
566 EXPECT_TRUE(T.isDenormal());
569 // Test double precision
571 const char *MinNormalStr = "2.22507385850720138309e-308";
572 EXPECT_FALSE(APFloat(APFloat::IEEEdouble, MinNormalStr).isDenormal());
573 EXPECT_FALSE(APFloat(APFloat::IEEEdouble, 0.0).isDenormal());
575 APFloat Val2(APFloat::IEEEdouble, 2.0e0);
576 APFloat T(APFloat::IEEEdouble, MinNormalStr);
577 T.divide(Val2, rdmd);
578 EXPECT_TRUE(T.isDenormal());
581 // Test Intel double-ext
583 const char *MinNormalStr = "3.36210314311209350626e-4932";
584 EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, MinNormalStr).isDenormal());
585 EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, 0.0).isDenormal());
587 APFloat Val2(APFloat::x87DoubleExtended, 2.0e0);
588 APFloat T(APFloat::x87DoubleExtended, MinNormalStr);
589 T.divide(Val2, rdmd);
590 EXPECT_TRUE(T.isDenormal());
593 // Test quadruple precision
595 const char *MinNormalStr = "3.36210314311209350626267781732175260e-4932";
596 EXPECT_FALSE(APFloat(APFloat::IEEEquad, MinNormalStr).isDenormal());
597 EXPECT_FALSE(APFloat(APFloat::IEEEquad, 0.0).isDenormal());
599 APFloat Val2(APFloat::IEEEquad, 2.0e0);
600 APFloat T(APFloat::IEEEquad, MinNormalStr);
601 T.divide(Val2, rdmd);
602 EXPECT_TRUE(T.isDenormal());
606 TEST(APFloatTest, Zero) {
607 EXPECT_EQ(0.0f, APFloat(0.0f).convertToFloat());
608 EXPECT_EQ(-0.0f, APFloat(-0.0f).convertToFloat());
609 EXPECT_TRUE(APFloat(-0.0f).isNegative());
611 EXPECT_EQ(0.0, APFloat(0.0).convertToDouble());
612 EXPECT_EQ(-0.0, APFloat(-0.0).convertToDouble());
613 EXPECT_TRUE(APFloat(-0.0).isNegative());
616 TEST(APFloatTest, DecimalStringsWithoutNullTerminators) {
617 // Make sure that we can parse strings without null terminators.
619 APFloat Val(APFloat::IEEEdouble);
620 Val.convertFromString(StringRef("0.00", 3),
621 llvm::APFloat::rmNearestTiesToEven);
622 EXPECT_EQ(Val.convertToDouble(), 0.0);
623 Val.convertFromString(StringRef("0.01", 3),
624 llvm::APFloat::rmNearestTiesToEven);
625 EXPECT_EQ(Val.convertToDouble(), 0.0);
626 Val.convertFromString(StringRef("0.09", 3),
627 llvm::APFloat::rmNearestTiesToEven);
628 EXPECT_EQ(Val.convertToDouble(), 0.0);
629 Val.convertFromString(StringRef("0.095", 4),
630 llvm::APFloat::rmNearestTiesToEven);
631 EXPECT_EQ(Val.convertToDouble(), 0.09);
632 Val.convertFromString(StringRef("0.00e+3", 7),
633 llvm::APFloat::rmNearestTiesToEven);
634 EXPECT_EQ(Val.convertToDouble(), 0.00);
635 Val.convertFromString(StringRef("0e+3", 4),
636 llvm::APFloat::rmNearestTiesToEven);
637 EXPECT_EQ(Val.convertToDouble(), 0.00);
641 TEST(APFloatTest, fromZeroDecimalString) {
642 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0").convertToDouble());
643 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0").convertToDouble());
644 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0").convertToDouble());
646 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.").convertToDouble());
647 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.").convertToDouble());
648 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.").convertToDouble());
650 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0").convertToDouble());
651 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0").convertToDouble());
652 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0").convertToDouble());
654 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0").convertToDouble());
655 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0").convertToDouble());
656 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0").convertToDouble());
658 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "00000.").convertToDouble());
659 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+00000.").convertToDouble());
660 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-00000.").convertToDouble());
662 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, ".00000").convertToDouble());
663 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.00000").convertToDouble());
664 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.00000").convertToDouble());
666 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0000.00000").convertToDouble());
667 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0000.00000").convertToDouble());
668 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0000.00000").convertToDouble());
671 TEST(APFloatTest, fromZeroDecimalSingleExponentString) {
672 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e1").convertToDouble());
673 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e1").convertToDouble());
674 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e1").convertToDouble());
676 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e+1").convertToDouble());
677 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1").convertToDouble());
678 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1").convertToDouble());
680 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e-1").convertToDouble());
681 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1").convertToDouble());
682 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1").convertToDouble());
685 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e1").convertToDouble());
686 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e1").convertToDouble());
687 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e1").convertToDouble());
689 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e+1").convertToDouble());
690 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e+1").convertToDouble());
691 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e+1").convertToDouble());
693 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e-1").convertToDouble());
694 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e-1").convertToDouble());
695 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e-1").convertToDouble());
697 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e1").convertToDouble());
698 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e1").convertToDouble());
699 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e1").convertToDouble());
701 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e+1").convertToDouble());
702 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e+1").convertToDouble());
703 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e+1").convertToDouble());
705 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e-1").convertToDouble());
706 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e-1").convertToDouble());
707 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e-1").convertToDouble());
710 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e1").convertToDouble());
711 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e1").convertToDouble());
712 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e1").convertToDouble());
714 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e+1").convertToDouble());
715 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e+1").convertToDouble());
716 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e+1").convertToDouble());
718 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e-1").convertToDouble());
719 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e-1").convertToDouble());
720 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e-1").convertToDouble());
723 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "000.0000e1").convertToDouble());
724 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+000.0000e+1").convertToDouble());
725 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-000.0000e+1").convertToDouble());
728 TEST(APFloatTest, fromZeroDecimalLargeExponentString) {
729 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e1234").convertToDouble());
730 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e1234").convertToDouble());
731 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e1234").convertToDouble());
733 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e+1234").convertToDouble());
734 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1234").convertToDouble());
735 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1234").convertToDouble());
737 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e-1234").convertToDouble());
738 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1234").convertToDouble());
739 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1234").convertToDouble());
741 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, "000.0000e1234").convertToDouble());
742 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, "000.0000e-1234").convertToDouble());
744 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, StringRef("0e1234\02", 6)).convertToDouble());
747 TEST(APFloatTest, fromZeroHexadecimalString) {
748 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p1").convertToDouble());
749 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p1").convertToDouble());
750 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1").convertToDouble());
752 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p+1").convertToDouble());
753 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p+1").convertToDouble());
754 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p+1").convertToDouble());
756 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p-1").convertToDouble());
757 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p-1").convertToDouble());
758 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p-1").convertToDouble());
761 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1").convertToDouble());
762 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p1").convertToDouble());
763 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p1").convertToDouble());
765 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p+1").convertToDouble());
766 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p+1").convertToDouble());
767 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p+1").convertToDouble());
769 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p-1").convertToDouble());
770 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p-1").convertToDouble());
771 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p-1").convertToDouble());
774 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p1").convertToDouble());
775 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p1").convertToDouble());
776 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p1").convertToDouble());
778 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p+1").convertToDouble());
779 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p+1").convertToDouble());
780 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p+1").convertToDouble());
782 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p-1").convertToDouble());
783 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p-1").convertToDouble());
784 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p-1").convertToDouble());
787 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p1").convertToDouble());
788 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p1").convertToDouble());
789 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p1").convertToDouble());
791 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p+1").convertToDouble());
792 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p+1").convertToDouble());
793 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p+1").convertToDouble());
795 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p-1").convertToDouble());
796 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p-1").convertToDouble());
797 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p-1").convertToDouble());
800 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1").convertToDouble());
801 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1").convertToDouble());
802 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1").convertToDouble());
803 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1").convertToDouble());
804 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p1234").convertToDouble());
805 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1234").convertToDouble());
806 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1234").convertToDouble());
807 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1234").convertToDouble());
808 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1234").convertToDouble());
809 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1234").convertToDouble());
812 TEST(APFloatTest, fromDecimalString) {
813 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "1").convertToDouble());
814 EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble, "2.").convertToDouble());
815 EXPECT_EQ(0.5, APFloat(APFloat::IEEEdouble, ".5").convertToDouble());
816 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "1.0").convertToDouble());
817 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-2").convertToDouble());
818 EXPECT_EQ(-4.0, APFloat(APFloat::IEEEdouble, "-4.").convertToDouble());
819 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble, "-.5").convertToDouble());
820 EXPECT_EQ(-1.5, APFloat(APFloat::IEEEdouble, "-1.5").convertToDouble());
821 EXPECT_EQ(1.25e12, APFloat(APFloat::IEEEdouble, "1.25e12").convertToDouble());
822 EXPECT_EQ(1.25e+12, APFloat(APFloat::IEEEdouble, "1.25e+12").convertToDouble());
823 EXPECT_EQ(1.25e-12, APFloat(APFloat::IEEEdouble, "1.25e-12").convertToDouble());
824 EXPECT_EQ(1024.0, APFloat(APFloat::IEEEdouble, "1024.").convertToDouble());
825 EXPECT_EQ(1024.05, APFloat(APFloat::IEEEdouble, "1024.05000").convertToDouble());
826 EXPECT_EQ(0.05, APFloat(APFloat::IEEEdouble, ".05000").convertToDouble());
827 EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble, "2.").convertToDouble());
828 EXPECT_EQ(2.0e2, APFloat(APFloat::IEEEdouble, "2.e2").convertToDouble());
829 EXPECT_EQ(2.0e+2, APFloat(APFloat::IEEEdouble, "2.e+2").convertToDouble());
830 EXPECT_EQ(2.0e-2, APFloat(APFloat::IEEEdouble, "2.e-2").convertToDouble());
831 EXPECT_EQ(2.05e2, APFloat(APFloat::IEEEdouble, "002.05000e2").convertToDouble());
832 EXPECT_EQ(2.05e+2, APFloat(APFloat::IEEEdouble, "002.05000e+2").convertToDouble());
833 EXPECT_EQ(2.05e-2, APFloat(APFloat::IEEEdouble, "002.05000e-2").convertToDouble());
834 EXPECT_EQ(2.05e12, APFloat(APFloat::IEEEdouble, "002.05000e12").convertToDouble());
835 EXPECT_EQ(2.05e+12, APFloat(APFloat::IEEEdouble, "002.05000e+12").convertToDouble());
836 EXPECT_EQ(2.05e-12, APFloat(APFloat::IEEEdouble, "002.05000e-12").convertToDouble());
838 // These are "carefully selected" to overflow the fast log-base
839 // calculations in APFloat.cpp
840 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "99e99999").isInfinity());
841 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-99e99999").isInfinity());
842 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "1e-99999").isPosZero());
843 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-1e-99999").isNegZero());
845 EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
848 TEST(APFloatTest, fromHexadecimalString) {
849 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p0").convertToDouble());
850 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p0").convertToDouble());
851 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p0").convertToDouble());
853 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p+0").convertToDouble());
854 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p+0").convertToDouble());
855 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p+0").convertToDouble());
857 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p-0").convertToDouble());
858 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p-0").convertToDouble());
859 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p-0").convertToDouble());
862 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble, "0x1p1").convertToDouble());
863 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p1").convertToDouble());
864 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p1").convertToDouble());
866 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble, "0x1p+1").convertToDouble());
867 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p+1").convertToDouble());
868 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p+1").convertToDouble());
870 EXPECT_EQ( 0.5, APFloat(APFloat::IEEEdouble, "0x1p-1").convertToDouble());
871 EXPECT_EQ(+0.5, APFloat(APFloat::IEEEdouble, "+0x1p-1").convertToDouble());
872 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble, "-0x1p-1").convertToDouble());
875 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble, "0x1.8p1").convertToDouble());
876 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p1").convertToDouble());
877 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p1").convertToDouble());
879 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble, "0x1.8p+1").convertToDouble());
880 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p+1").convertToDouble());
881 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p+1").convertToDouble());
883 EXPECT_EQ( 0.75, APFloat(APFloat::IEEEdouble, "0x1.8p-1").convertToDouble());
884 EXPECT_EQ(+0.75, APFloat(APFloat::IEEEdouble, "+0x1.8p-1").convertToDouble());
885 EXPECT_EQ(-0.75, APFloat(APFloat::IEEEdouble, "-0x1.8p-1").convertToDouble());
888 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000.000p1").convertToDouble());
889 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p1").convertToDouble());
890 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p1").convertToDouble());
892 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000.000p+1").convertToDouble());
893 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p+1").convertToDouble());
894 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p+1").convertToDouble());
896 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble, "0x1000.000p-1").convertToDouble());
897 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p-1").convertToDouble());
898 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p-1").convertToDouble());
901 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000p1").convertToDouble());
902 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p1").convertToDouble());
903 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p1").convertToDouble());
905 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000p+1").convertToDouble());
906 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p+1").convertToDouble());
907 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p+1").convertToDouble());
909 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble, "0x1000p-1").convertToDouble());
910 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000p-1").convertToDouble());
911 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000p-1").convertToDouble());
914 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble, "0x10p10").convertToDouble());
915 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p10").convertToDouble());
916 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p10").convertToDouble());
918 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble, "0x10p+10").convertToDouble());
919 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p+10").convertToDouble());
920 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p+10").convertToDouble());
922 EXPECT_EQ( 0.015625, APFloat(APFloat::IEEEdouble, "0x10p-10").convertToDouble());
923 EXPECT_EQ(+0.015625, APFloat(APFloat::IEEEdouble, "+0x10p-10").convertToDouble());
924 EXPECT_EQ(-0.015625, APFloat(APFloat::IEEEdouble, "-0x10p-10").convertToDouble());
926 EXPECT_EQ(1.0625, APFloat(APFloat::IEEEdouble, "0x1.1p0").convertToDouble());
927 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "0x1p0").convertToDouble());
929 EXPECT_EQ(convertToDoubleFromString("0x1p-150"),
930 convertToDoubleFromString("+0x800000000000000001.p-221"));
931 EXPECT_EQ(2251799813685248.5,
932 convertToDoubleFromString("0x80000000000004000000.010p-28"));
935 TEST(APFloatTest, toString) {
936 ASSERT_EQ("10", convertToString(10.0, 6, 3));
937 ASSERT_EQ("1.0E+1", convertToString(10.0, 6, 0));
938 ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2));
939 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 4, 2));
940 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 5, 1));
941 ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2));
942 ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2));
943 ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 5, 1));
944 ASSERT_EQ("0.78539816339744828", convertToString(0.78539816339744830961, 0, 3));
945 ASSERT_EQ("4.9406564584124654E-324", convertToString(4.9406564584124654e-324, 0, 3));
946 ASSERT_EQ("873.18340000000001", convertToString(873.1834, 0, 1));
947 ASSERT_EQ("8.7318340000000001E+2", convertToString(873.1834, 0, 0));
948 ASSERT_EQ("1.7976931348623157E+308", convertToString(1.7976931348623157E+308, 0, 0));
951 TEST(APFloatTest, toInteger) {
952 bool isExact = false;
953 APSInt result(5, /*isUnsigned=*/true);
955 EXPECT_EQ(APFloat::opOK,
956 APFloat(APFloat::IEEEdouble, "10")
957 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
958 EXPECT_TRUE(isExact);
959 EXPECT_EQ(APSInt(APInt(5, 10), true), result);
961 EXPECT_EQ(APFloat::opInvalidOp,
962 APFloat(APFloat::IEEEdouble, "-10")
963 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
964 EXPECT_FALSE(isExact);
965 EXPECT_EQ(APSInt::getMinValue(5, true), result);
967 EXPECT_EQ(APFloat::opInvalidOp,
968 APFloat(APFloat::IEEEdouble, "32")
969 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
970 EXPECT_FALSE(isExact);
971 EXPECT_EQ(APSInt::getMaxValue(5, true), result);
973 EXPECT_EQ(APFloat::opInexact,
974 APFloat(APFloat::IEEEdouble, "7.9")
975 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
976 EXPECT_FALSE(isExact);
977 EXPECT_EQ(APSInt(APInt(5, 7), true), result);
979 result.setIsUnsigned(false);
980 EXPECT_EQ(APFloat::opOK,
981 APFloat(APFloat::IEEEdouble, "-10")
982 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
983 EXPECT_TRUE(isExact);
984 EXPECT_EQ(APSInt(APInt(5, -10, true), false), result);
986 EXPECT_EQ(APFloat::opInvalidOp,
987 APFloat(APFloat::IEEEdouble, "-17")
988 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
989 EXPECT_FALSE(isExact);
990 EXPECT_EQ(APSInt::getMinValue(5, false), result);
992 EXPECT_EQ(APFloat::opInvalidOp,
993 APFloat(APFloat::IEEEdouble, "16")
994 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
995 EXPECT_FALSE(isExact);
996 EXPECT_EQ(APSInt::getMaxValue(5, false), result);
999 static APInt nanbits(const fltSemantics &Sem,
1000 bool SNaN, bool Negative, uint64_t fill) {
1001 APInt apfill(64, fill);
1003 return APFloat::getSNaN(Sem, Negative, &apfill).bitcastToAPInt();
1005 return APFloat::getQNaN(Sem, Negative, &apfill).bitcastToAPInt();
1008 TEST(APFloatTest, makeNaN) {
1009 ASSERT_EQ(0x7fc00000, nanbits(APFloat::IEEEsingle, false, false, 0));
1010 ASSERT_EQ(0xffc00000, nanbits(APFloat::IEEEsingle, false, true, 0));
1011 ASSERT_EQ(0x7fc0ae72, nanbits(APFloat::IEEEsingle, false, false, 0xae72));
1012 ASSERT_EQ(0x7fffae72, nanbits(APFloat::IEEEsingle, false, false, 0xffffae72));
1013 ASSERT_EQ(0x7fa00000, nanbits(APFloat::IEEEsingle, true, false, 0));
1014 ASSERT_EQ(0xffa00000, nanbits(APFloat::IEEEsingle, true, true, 0));
1015 ASSERT_EQ(0x7f80ae72, nanbits(APFloat::IEEEsingle, true, false, 0xae72));
1016 ASSERT_EQ(0x7fbfae72, nanbits(APFloat::IEEEsingle, true, false, 0xffffae72));
1018 ASSERT_EQ(0x7ff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, false, 0));
1019 ASSERT_EQ(0xfff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, true, 0));
1020 ASSERT_EQ(0x7ff800000000ae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xae72));
1021 ASSERT_EQ(0x7fffffffffffae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xffffffffffffae72ULL));
1022 ASSERT_EQ(0x7ff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, false, 0));
1023 ASSERT_EQ(0xfff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, true, 0));
1024 ASSERT_EQ(0x7ff000000000ae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xae72));
1025 ASSERT_EQ(0x7ff7ffffffffae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xffffffffffffae72ULL));
1028 #ifdef GTEST_HAS_DEATH_TEST
1030 TEST(APFloatTest, SemanticsDeath) {
1031 EXPECT_DEATH(APFloat(APFloat::IEEEsingle, 0.0f).convertToDouble(), "Float semantics are not IEEEdouble");
1032 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, 0.0 ).convertToFloat(), "Float semantics are not IEEEsingle");
1035 TEST(APFloatTest, StringDecimalDeath) {
1036 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ""), "Invalid string length");
1037 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+"), "String has no digits");
1038 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-"), "String has no digits");
1040 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("\0", 1)), "Invalid character in significand");
1041 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\0", 2)), "Invalid character in significand");
1042 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02", 3)), "Invalid character in significand");
1043 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02e1", 5)), "Invalid character in significand");
1044 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e\0", 3)), "Invalid character in exponent");
1045 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\0", 4)), "Invalid character in exponent");
1046 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\02", 5)), "Invalid character in exponent");
1048 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0f"), "Invalid character in significand");
1050 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".."), "String contains multiple dots");
1051 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "..0"), "String contains multiple dots");
1052 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0.0"), "String contains multiple dots");
1055 TEST(APFloatTest, StringDecimalSignificandDeath) {
1056 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "."), "Significand has no digits");
1057 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+."), "Significand has no digits");
1058 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-."), "Significand has no digits");
1061 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "e"), "Significand has no digits");
1062 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e"), "Significand has no digits");
1063 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e"), "Significand has no digits");
1065 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "e1"), "Significand has no digits");
1066 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e1"), "Significand has no digits");
1067 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e1"), "Significand has no digits");
1069 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".e1"), "Significand has no digits");
1070 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e1"), "Significand has no digits");
1071 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e1"), "Significand has no digits");
1074 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".e"), "Significand has no digits");
1075 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e"), "Significand has no digits");
1076 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e"), "Significand has no digits");
1079 TEST(APFloatTest, StringDecimalExponentDeath) {
1080 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e"), "Exponent has no digits");
1081 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1e"), "Exponent has no digits");
1082 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1e"), "Exponent has no digits");
1084 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.e"), "Exponent has no digits");
1085 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1.e"), "Exponent has no digits");
1086 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1.e"), "Exponent has no digits");
1088 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e"), "Exponent has no digits");
1089 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.1e"), "Exponent has no digits");
1090 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.1e"), "Exponent has no digits");
1092 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.1e"), "Exponent has no digits");
1093 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1.1e"), "Exponent has no digits");
1094 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1.1e"), "Exponent has no digits");
1097 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e+"), "Exponent has no digits");
1098 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e-"), "Exponent has no digits");
1100 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e"), "Exponent has no digits");
1101 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e+"), "Exponent has no digits");
1102 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e-"), "Exponent has no digits");
1104 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e"), "Exponent has no digits");
1105 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e+"), "Exponent has no digits");
1106 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e-"), "Exponent has no digits");
1109 TEST(APFloatTest, StringHexadecimalDeath) {
1110 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x"), "Invalid string");
1111 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x"), "Invalid string");
1112 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x"), "Invalid string");
1114 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0"), "Hex strings require an exponent");
1115 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0"), "Hex strings require an exponent");
1116 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0"), "Hex strings require an exponent");
1118 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0."), "Hex strings require an exponent");
1119 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0."), "Hex strings require an exponent");
1120 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0."), "Hex strings require an exponent");
1122 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.0"), "Hex strings require an exponent");
1123 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.0"), "Hex strings require an exponent");
1124 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.0"), "Hex strings require an exponent");
1126 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0.0"), "Hex strings require an exponent");
1127 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0.0"), "Hex strings require an exponent");
1128 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0.0"), "Hex strings require an exponent");
1130 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x\0", 3)), "Invalid character in significand");
1131 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\0", 4)), "Invalid character in significand");
1132 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02", 5)), "Invalid character in significand");
1133 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02p1", 7)), "Invalid character in significand");
1134 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p\0", 5)), "Invalid character in exponent");
1135 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\0", 6)), "Invalid character in exponent");
1136 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\02", 7)), "Invalid character in exponent");
1138 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p0f"), "Invalid character in exponent");
1140 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..p1"), "String contains multiple dots");
1141 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..0p1"), "String contains multiple dots");
1142 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.0.0p1"), "String contains multiple dots");
1145 TEST(APFloatTest, StringHexadecimalSignificandDeath) {
1146 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x."), "Significand has no digits");
1147 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x."), "Significand has no digits");
1148 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x."), "Significand has no digits");
1150 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp"), "Significand has no digits");
1151 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp"), "Significand has no digits");
1152 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp"), "Significand has no digits");
1154 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp+"), "Significand has no digits");
1155 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp+"), "Significand has no digits");
1156 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp+"), "Significand has no digits");
1158 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp-"), "Significand has no digits");
1159 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp-"), "Significand has no digits");
1160 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp-"), "Significand has no digits");
1163 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p"), "Significand has no digits");
1164 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p"), "Significand has no digits");
1165 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p"), "Significand has no digits");
1167 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p+"), "Significand has no digits");
1168 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p+"), "Significand has no digits");
1169 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p+"), "Significand has no digits");
1171 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p-"), "Significand has no digits");
1172 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p-"), "Significand has no digits");
1173 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p-"), "Significand has no digits");
1176 TEST(APFloatTest, StringHexadecimalExponentDeath) {
1177 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p"), "Exponent has no digits");
1178 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p"), "Exponent has no digits");
1179 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p"), "Exponent has no digits");
1181 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p+"), "Exponent has no digits");
1182 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p+"), "Exponent has no digits");
1183 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p+"), "Exponent has no digits");
1185 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p-"), "Exponent has no digits");
1186 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p-"), "Exponent has no digits");
1187 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p-"), "Exponent has no digits");
1190 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p"), "Exponent has no digits");
1191 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p"), "Exponent has no digits");
1192 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p"), "Exponent has no digits");
1194 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p+"), "Exponent has no digits");
1195 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p+"), "Exponent has no digits");
1196 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p+"), "Exponent has no digits");
1198 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p-"), "Exponent has no digits");
1199 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p-"), "Exponent has no digits");
1200 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p-"), "Exponent has no digits");
1203 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p"), "Exponent has no digits");
1204 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p"), "Exponent has no digits");
1205 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p"), "Exponent has no digits");
1207 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p+"), "Exponent has no digits");
1208 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p+"), "Exponent has no digits");
1209 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p+"), "Exponent has no digits");
1211 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p-"), "Exponent has no digits");
1212 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p-"), "Exponent has no digits");
1213 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p-"), "Exponent has no digits");
1216 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p"), "Exponent has no digits");
1217 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p"), "Exponent has no digits");
1218 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p"), "Exponent has no digits");
1220 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p+"), "Exponent has no digits");
1221 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p+"), "Exponent has no digits");
1222 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p+"), "Exponent has no digits");
1224 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p-"), "Exponent has no digits");
1225 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p-"), "Exponent has no digits");
1226 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p-"), "Exponent has no digits");
1231 TEST(APFloatTest, exactInverse) {
1234 // Trivial operation.
1235 EXPECT_TRUE(APFloat(2.0).getExactInverse(&inv));
1236 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5)));
1237 EXPECT_TRUE(APFloat(2.0f).getExactInverse(&inv));
1238 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5f)));
1239 EXPECT_TRUE(APFloat(APFloat::IEEEquad, "2.0").getExactInverse(&inv));
1240 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::IEEEquad, "0.5")));
1241 EXPECT_TRUE(APFloat(APFloat::PPCDoubleDouble, "2.0").getExactInverse(&inv));
1242 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::PPCDoubleDouble, "0.5")));
1243 EXPECT_TRUE(APFloat(APFloat::x87DoubleExtended, "2.0").getExactInverse(&inv));
1244 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::x87DoubleExtended, "0.5")));
1247 EXPECT_TRUE(APFloat(1.17549435e-38f).getExactInverse(&inv));
1248 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(8.5070592e+37f)));
1250 // Large float, inverse is a denormal.
1251 EXPECT_FALSE(APFloat(1.7014118e38f).getExactInverse(nullptr));
1253 EXPECT_FALSE(APFloat(0.0).getExactInverse(nullptr));
1254 // Denormalized float
1255 EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(nullptr));
1258 TEST(APFloatTest, roundToIntegral) {
1259 APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble)), P(0.0);
1262 P.roundToIntegral(APFloat::rmTowardZero);
1263 EXPECT_EQ(-0.0, P.convertToDouble());
1265 P.roundToIntegral(APFloat::rmTowardNegative);
1266 EXPECT_EQ(-1.0, P.convertToDouble());
1268 P.roundToIntegral(APFloat::rmTowardPositive);
1269 EXPECT_EQ(-0.0, P.convertToDouble());
1271 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1272 EXPECT_EQ(-0.0, P.convertToDouble());
1275 P.roundToIntegral(APFloat::rmTowardZero);
1276 EXPECT_EQ(3.0, P.convertToDouble());
1278 P.roundToIntegral(APFloat::rmTowardNegative);
1279 EXPECT_EQ(3.0, P.convertToDouble());
1281 P.roundToIntegral(APFloat::rmTowardPositive);
1282 EXPECT_EQ(4.0, P.convertToDouble());
1284 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1285 EXPECT_EQ(3.0, P.convertToDouble());
1288 P.roundToIntegral(APFloat::rmTowardZero);
1289 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1291 P.roundToIntegral(APFloat::rmTowardNegative);
1292 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1294 P.roundToIntegral(APFloat::rmTowardPositive);
1295 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1297 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1298 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1300 P = APFloat::getZero(APFloat::IEEEdouble);
1301 P.roundToIntegral(APFloat::rmTowardZero);
1302 EXPECT_EQ(0.0, P.convertToDouble());
1303 P = APFloat::getZero(APFloat::IEEEdouble, true);
1304 P.roundToIntegral(APFloat::rmTowardZero);
1305 EXPECT_EQ(-0.0, P.convertToDouble());
1306 P = APFloat::getNaN(APFloat::IEEEdouble);
1307 P.roundToIntegral(APFloat::rmTowardZero);
1308 EXPECT_TRUE(IsNAN(P.convertToDouble()));
1309 P = APFloat::getInf(APFloat::IEEEdouble);
1310 P.roundToIntegral(APFloat::rmTowardZero);
1311 EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() > 0.0);
1312 P = APFloat::getInf(APFloat::IEEEdouble, true);
1313 P.roundToIntegral(APFloat::rmTowardZero);
1314 EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() < 0.0);
1318 TEST(APFloatTest, getLargest) {
1319 EXPECT_EQ(3.402823466e+38f, APFloat::getLargest(APFloat::IEEEsingle).convertToFloat());
1320 EXPECT_EQ(1.7976931348623158e+308, APFloat::getLargest(APFloat::IEEEdouble).convertToDouble());
1323 TEST(APFloatTest, getSmallest) {
1324 APFloat test = APFloat::getSmallest(APFloat::IEEEsingle, false);
1325 APFloat expected = APFloat(APFloat::IEEEsingle, "0x0.000002p-126");
1326 EXPECT_FALSE(test.isNegative());
1327 EXPECT_TRUE(test.isFiniteNonZero());
1328 EXPECT_TRUE(test.isDenormal());
1329 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1331 test = APFloat::getSmallest(APFloat::IEEEsingle, true);
1332 expected = APFloat(APFloat::IEEEsingle, "-0x0.000002p-126");
1333 EXPECT_TRUE(test.isNegative());
1334 EXPECT_TRUE(test.isFiniteNonZero());
1335 EXPECT_TRUE(test.isDenormal());
1336 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1338 test = APFloat::getSmallest(APFloat::IEEEquad, false);
1339 expected = APFloat(APFloat::IEEEquad, "0x0.0000000000000000000000000001p-16382");
1340 EXPECT_FALSE(test.isNegative());
1341 EXPECT_TRUE(test.isFiniteNonZero());
1342 EXPECT_TRUE(test.isDenormal());
1343 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1345 test = APFloat::getSmallest(APFloat::IEEEquad, true);
1346 expected = APFloat(APFloat::IEEEquad, "-0x0.0000000000000000000000000001p-16382");
1347 EXPECT_TRUE(test.isNegative());
1348 EXPECT_TRUE(test.isFiniteNonZero());
1349 EXPECT_TRUE(test.isDenormal());
1350 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1353 TEST(APFloatTest, getSmallestNormalized) {
1354 APFloat test = APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
1355 APFloat expected = APFloat(APFloat::IEEEsingle, "0x1p-126");
1356 EXPECT_FALSE(test.isNegative());
1357 EXPECT_TRUE(test.isFiniteNonZero());
1358 EXPECT_FALSE(test.isDenormal());
1359 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1361 test = APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
1362 expected = APFloat(APFloat::IEEEsingle, "-0x1p-126");
1363 EXPECT_TRUE(test.isNegative());
1364 EXPECT_TRUE(test.isFiniteNonZero());
1365 EXPECT_FALSE(test.isDenormal());
1366 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1368 test = APFloat::getSmallestNormalized(APFloat::IEEEquad, false);
1369 expected = APFloat(APFloat::IEEEquad, "0x1p-16382");
1370 EXPECT_FALSE(test.isNegative());
1371 EXPECT_TRUE(test.isFiniteNonZero());
1372 EXPECT_FALSE(test.isDenormal());
1373 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1375 test = APFloat::getSmallestNormalized(APFloat::IEEEquad, true);
1376 expected = APFloat(APFloat::IEEEquad, "-0x1p-16382");
1377 EXPECT_TRUE(test.isNegative());
1378 EXPECT_TRUE(test.isFiniteNonZero());
1379 EXPECT_FALSE(test.isDenormal());
1380 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1383 TEST(APFloatTest, getZero) {
1385 const fltSemantics *semantics;
1387 const unsigned long long bitPattern[2];
1388 const unsigned bitPatternLength;
1389 } const GetZeroTest[] = {
1390 { &APFloat::IEEEhalf, false, {0, 0}, 1},
1391 { &APFloat::IEEEhalf, true, {0x8000ULL, 0}, 1},
1392 { &APFloat::IEEEsingle, false, {0, 0}, 1},
1393 { &APFloat::IEEEsingle, true, {0x80000000ULL, 0}, 1},
1394 { &APFloat::IEEEdouble, false, {0, 0}, 1},
1395 { &APFloat::IEEEdouble, true, {0x8000000000000000ULL, 0}, 1},
1396 { &APFloat::IEEEquad, false, {0, 0}, 2},
1397 { &APFloat::IEEEquad, true, {0, 0x8000000000000000ULL}, 2},
1398 { &APFloat::PPCDoubleDouble, false, {0, 0}, 2},
1399 { &APFloat::PPCDoubleDouble, true, {0x8000000000000000ULL, 0}, 2},
1400 { &APFloat::x87DoubleExtended, false, {0, 0}, 2},
1401 { &APFloat::x87DoubleExtended, true, {0, 0x8000ULL}, 2},
1403 const unsigned NumGetZeroTests = 12;
1404 for (unsigned i = 0; i < NumGetZeroTests; ++i) {
1405 APFloat test = APFloat::getZero(*GetZeroTest[i].semantics,
1406 GetZeroTest[i].sign);
1407 const char *pattern = GetZeroTest[i].sign? "-0x0p+0" : "0x0p+0";
1408 APFloat expected = APFloat(*GetZeroTest[i].semantics,
1410 EXPECT_TRUE(test.isZero());
1411 EXPECT_TRUE(GetZeroTest[i].sign? test.isNegative() : !test.isNegative());
1412 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1413 for (unsigned j = 0, je = GetZeroTest[i].bitPatternLength; j < je; ++j) {
1414 EXPECT_EQ(GetZeroTest[i].bitPattern[j],
1415 test.bitcastToAPInt().getRawData()[j]);
1420 TEST(APFloatTest, copySign) {
1421 EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
1422 APFloat::copySign(APFloat(42.0), APFloat(-1.0))));
1423 EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
1424 APFloat::copySign(APFloat(-42.0), APFloat(1.0))));
1425 EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
1426 APFloat::copySign(APFloat(-42.0), APFloat(-1.0))));
1427 EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
1428 APFloat::copySign(APFloat(42.0), APFloat(1.0))));
1431 TEST(APFloatTest, convert) {
1433 APFloat test(APFloat::IEEEdouble, "1.0");
1434 test.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1435 EXPECT_EQ(1.0f, test.convertToFloat());
1436 EXPECT_FALSE(losesInfo);
1438 test = APFloat(APFloat::x87DoubleExtended, "0x1p-53");
1439 test.add(APFloat(APFloat::x87DoubleExtended, "1.0"), APFloat::rmNearestTiesToEven);
1440 test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1441 EXPECT_EQ(1.0, test.convertToDouble());
1442 EXPECT_TRUE(losesInfo);
1444 test = APFloat(APFloat::IEEEquad, "0x1p-53");
1445 test.add(APFloat(APFloat::IEEEquad, "1.0"), APFloat::rmNearestTiesToEven);
1446 test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1447 EXPECT_EQ(1.0, test.convertToDouble());
1448 EXPECT_TRUE(losesInfo);
1450 test = APFloat(APFloat::x87DoubleExtended, "0xf.fffffffp+28");
1451 test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1452 EXPECT_EQ(4294967295.0, test.convertToDouble());
1453 EXPECT_FALSE(losesInfo);
1455 test = APFloat::getSNaN(APFloat::IEEEsingle);
1456 APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended);
1457 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1459 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1460 EXPECT_FALSE(losesInfo);
1462 test = APFloat::getQNaN(APFloat::IEEEsingle);
1463 APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended);
1464 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1466 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1467 EXPECT_FALSE(losesInfo);
1469 test = APFloat::getSNaN(APFloat::x87DoubleExtended);
1470 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1472 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1473 EXPECT_FALSE(losesInfo);
1475 test = APFloat::getQNaN(APFloat::x87DoubleExtended);
1476 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1478 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1479 EXPECT_FALSE(losesInfo);
1482 TEST(APFloatTest, PPCDoubleDouble) {
1483 APFloat test(APFloat::PPCDoubleDouble, "1.0");
1484 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1485 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1487 test.divide(APFloat(APFloat::PPCDoubleDouble, "3.0"), APFloat::rmNearestTiesToEven);
1488 EXPECT_EQ(0x3fd5555555555555ull, test.bitcastToAPInt().getRawData()[0]);
1489 EXPECT_EQ(0x3c75555555555556ull, test.bitcastToAPInt().getRawData()[1]);
1492 test = APFloat(APFloat::PPCDoubleDouble, "1.79769313486231580793728971405301e+308");
1493 EXPECT_EQ(0x7fefffffffffffffull, test.bitcastToAPInt().getRawData()[0]);
1494 EXPECT_EQ(0x7c8ffffffffffffeull, test.bitcastToAPInt().getRawData()[1]);
1497 test = APFloat(APFloat::PPCDoubleDouble, "2.00416836000897277799610805135016e-292");
1498 EXPECT_EQ(0x0360000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1499 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1501 test = APFloat(APFloat::PPCDoubleDouble, "1.0");
1502 test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-105"), APFloat::rmNearestTiesToEven);
1503 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1504 EXPECT_EQ(0x3960000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1506 test = APFloat(APFloat::PPCDoubleDouble, "1.0");
1507 test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-106"), APFloat::rmNearestTiesToEven);
1508 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1510 // This is what we would expect with a true double-double implementation
1511 EXPECT_EQ(0x3950000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1513 // This is what we get with our 106-bit mantissa approximation
1514 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1518 TEST(APFloatTest, isNegative) {
1519 APFloat t(APFloat::IEEEsingle, "0x1p+0");
1520 EXPECT_FALSE(t.isNegative());
1521 t = APFloat(APFloat::IEEEsingle, "-0x1p+0");
1522 EXPECT_TRUE(t.isNegative());
1524 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNegative());
1525 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle, true).isNegative());
1527 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNegative());
1528 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle, true).isNegative());
1530 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isNegative());
1531 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle, true).isNegative());
1533 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNegative());
1534 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, true).isNegative());
1537 TEST(APFloatTest, isNormal) {
1538 APFloat t(APFloat::IEEEsingle, "0x1p+0");
1539 EXPECT_TRUE(t.isNormal());
1541 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNormal());
1542 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNormal());
1543 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isNormal());
1544 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNormal());
1545 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-149").isNormal());
1548 TEST(APFloatTest, isFinite) {
1549 APFloat t(APFloat::IEEEsingle, "0x1p+0");
1550 EXPECT_TRUE(t.isFinite());
1551 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isFinite());
1552 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle, false).isFinite());
1553 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isFinite());
1554 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isFinite());
1555 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p-149").isFinite());
1558 TEST(APFloatTest, isInfinity) {
1559 APFloat t(APFloat::IEEEsingle, "0x1p+0");
1560 EXPECT_FALSE(t.isInfinity());
1561 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle, false).isInfinity());
1562 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isInfinity());
1563 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isInfinity());
1564 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isInfinity());
1565 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-149").isInfinity());
1568 TEST(APFloatTest, isNaN) {
1569 APFloat t(APFloat::IEEEsingle, "0x1p+0");
1570 EXPECT_FALSE(t.isNaN());
1571 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNaN());
1572 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNaN());
1573 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle, false).isNaN());
1574 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNaN());
1575 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-149").isNaN());
1578 TEST(APFloatTest, isFiniteNonZero) {
1579 // Test positive/negative normal value.
1580 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p+0").isFiniteNonZero());
1581 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "-0x1p+0").isFiniteNonZero());
1583 // Test positive/negative denormal value.
1584 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p-149").isFiniteNonZero());
1585 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "-0x1p-149").isFiniteNonZero());
1587 // Test +/- Infinity.
1588 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isFiniteNonZero());
1589 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, true).isFiniteNonZero());
1592 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isFiniteNonZero());
1593 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, true).isFiniteNonZero());
1595 // Test +/- qNaN. +/- dont mean anything with qNaN but paranoia can't hurt in
1597 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isFiniteNonZero());
1598 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, true).isFiniteNonZero());
1600 // Test +/- sNaN. +/- dont mean anything with sNaN but paranoia can't hurt in
1602 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isFiniteNonZero());
1603 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, true).isFiniteNonZero());
1606 TEST(APFloatTest, add) {
1607 // Test Special Cases against each other and normal values.
1610 // 1. Since we perform only default exception handling all operations with
1611 // signaling NaNs should have a result that is a quiet NaN. Currently they
1614 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
1615 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
1616 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
1617 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
1618 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
1619 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
1620 APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
1621 APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
1622 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
1623 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
1624 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
1625 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
1626 APFloat PSmallestNormalized =
1627 APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
1628 APFloat MSmallestNormalized =
1629 APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
1631 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
1633 const unsigned NumTests = 169;
1640 } SpecialCaseTests[NumTests] = {
1641 { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1642 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1643 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1644 { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1645 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1648 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1650 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1651 { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1652 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1653 { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1654 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1655 { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1656 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1657 { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1658 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1659 { MInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1660 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1661 { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1662 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1665 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1667 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1668 { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1669 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1670 { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1671 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1672 { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1673 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1674 { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1675 { PZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1676 { PZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1677 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1678 { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1679 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1682 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1684 { PZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1685 { PZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1686 { PZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1687 { PZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1688 { PZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1689 { PZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1690 { PZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1691 { PZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1692 { MZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1693 { MZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1694 { MZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1695 { MZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
1696 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1699 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1701 { MZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1702 { MZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1703 { MZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1704 { MZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1705 { MZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1706 { MZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1707 { MZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1708 { MZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1709 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
1710 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
1711 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
1712 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
1713 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1716 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1718 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
1719 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
1720 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1721 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1722 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1723 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1724 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
1725 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
1728 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1729 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1730 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1731 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1732 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1733 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1734 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1735 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1736 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1737 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1738 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1739 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1740 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1741 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1743 { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1744 { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1745 { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1746 { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1747 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1750 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1752 { PNormalValue, PNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal },
1753 { PNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1754 { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1755 { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1756 { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1757 { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1758 { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1759 { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1760 { MNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1761 { MNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1762 { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1763 { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1764 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1767 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1769 { MNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1770 { MNormalValue, MNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal },
1771 { MNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1772 { MNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1773 { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1774 { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1775 { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1776 { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1777 { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1778 { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1779 { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1780 { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1781 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1784 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1786 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1787 { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1788 { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
1789 { PLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1790 { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1791 { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1792 { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1793 { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1794 { MLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1795 { MLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1796 { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1797 { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1798 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1801 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1803 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1804 { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1805 { MLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1806 { MLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
1807 { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1808 { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1809 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1810 { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1811 { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1812 { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1813 { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1814 { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1815 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1818 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1820 { PSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1821 { PSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1822 { PSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1823 { PSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1824 { PSmallestValue, PSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal },
1825 { PSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1826 { PSmallestValue, PSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1827 { PSmallestValue, MSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1828 { MSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1829 { MSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1830 { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1831 { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1832 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1835 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1837 { MSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1838 { MSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1839 { MSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1840 { MSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1841 { MSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1842 { MSmallestValue, MSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal },
1843 { MSmallestValue, PSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1844 { MSmallestValue, MSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1845 { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1846 { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1847 { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1848 { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1849 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1852 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1854 { PSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1855 { PSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1856 { PSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1857 { PSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1858 { PSmallestNormalized, PSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1859 { PSmallestNormalized, MSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1860 { PSmallestNormalized, PSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
1861 { PSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1862 { MSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1863 { MSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1864 { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1865 { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1866 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1869 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1871 { MSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1872 { MSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1873 { MSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1874 { MSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1875 { MSmallestNormalized, PSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1876 { MSmallestNormalized, MSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1877 { MSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1878 { MSmallestNormalized, MSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal }
1881 for (size_t i = 0; i < NumTests; ++i) {
1882 APFloat x(SpecialCaseTests[i].x);
1883 APFloat y(SpecialCaseTests[i].y);
1884 APFloat::opStatus status = x.add(y, APFloat::rmNearestTiesToEven);
1886 APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result);
1888 EXPECT_TRUE(result.bitwiseIsEqual(x));
1889 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
1890 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
1894 TEST(APFloatTest, subtract) {
1895 // Test Special Cases against each other and normal values.
1898 // 1. Since we perform only default exception handling all operations with
1899 // signaling NaNs should have a result that is a quiet NaN. Currently they
1902 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
1903 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
1904 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
1905 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
1906 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
1907 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
1908 APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
1909 APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
1910 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
1911 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
1912 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
1913 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
1914 APFloat PSmallestNormalized =
1915 APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
1916 APFloat MSmallestNormalized =
1917 APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
1919 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
1921 const unsigned NumTests = 169;
1928 } SpecialCaseTests[NumTests] = {
1929 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1930 { PInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1931 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1932 { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1933 { PInf, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
1936 { PInf, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
1938 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1939 { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1940 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1941 { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1942 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1943 { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1944 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1945 { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1946 { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1947 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1948 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1949 { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1950 { MInf, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
1953 { MInf, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
1955 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1956 { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1957 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1958 { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1959 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1960 { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1961 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1962 { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1963 { PZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1964 { PZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1965 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1966 { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1967 { PZero, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
1970 { PZero, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
1972 { PZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1973 { PZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1974 { PZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1975 { PZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1976 { PZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1977 { PZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1978 { PZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1979 { PZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1980 { MZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1981 { MZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1982 { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
1983 { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1984 { MZero, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
1987 { MZero, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
1989 { MZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1990 { MZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1991 { MZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1992 { MZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1993 { MZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1994 { MZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1995 { MZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1996 { MZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1997 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
1998 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
1999 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2000 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2001 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2004 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2006 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2007 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2008 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2009 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2010 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2011 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2012 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2013 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2016 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2017 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2018 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2019 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2020 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2021 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2022 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2023 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2024 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2025 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2026 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2027 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2028 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2029 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2031 { PNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2032 { PNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2033 { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2034 { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2035 { PNormalValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2038 { PNormalValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2040 { PNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2041 { PNormalValue, MNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal },
2042 { PNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2043 { PNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2044 { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2045 { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2046 { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2047 { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2048 { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2049 { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2050 { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2051 { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2052 { MNormalValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2055 { MNormalValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2057 { MNormalValue, PNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal },
2058 { MNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2059 { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2060 { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2061 { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2062 { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2063 { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2064 { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2065 { PLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2066 { PLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2067 { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2068 { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2069 { PLargestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2072 { PLargestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2074 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2075 { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2076 { PLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2077 { PLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2078 { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2079 { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2080 { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2081 { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2082 { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2083 { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2084 { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2085 { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2086 { MLargestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2089 { MLargestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2091 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2092 { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2093 { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2094 { MLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2095 { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2096 { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2097 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2098 { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2099 { PSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2100 { PSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2101 { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2102 { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2103 { PSmallestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2106 { PSmallestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2108 { PSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2109 { PSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2110 { PSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2111 { PSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2112 { PSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2113 { PSmallestValue, MSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal },
2114 { PSmallestValue, PSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2115 { PSmallestValue, MSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2116 { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2117 { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2118 { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2119 { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2120 { MSmallestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2123 { MSmallestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2125 { MSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2126 { MSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2127 { MSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2128 { MSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2129 { MSmallestValue, PSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal },
2130 { MSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2131 { MSmallestValue, PSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2132 { MSmallestValue, MSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2133 { PSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2134 { PSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2135 { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2136 { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2137 { PSmallestNormalized, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2140 { PSmallestNormalized, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2142 { PSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2143 { PSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2144 { PSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2145 { PSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2146 { PSmallestNormalized, PSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2147 { PSmallestNormalized, MSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2148 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2149 { PSmallestNormalized, MSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
2150 { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2151 { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2152 { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2153 { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2154 { MSmallestNormalized, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2157 { MSmallestNormalized, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2159 { MSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2160 { MSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2161 { MSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2162 { MSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2163 { MSmallestNormalized, PSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2164 { MSmallestNormalized, MSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2165 { MSmallestNormalized, PSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
2166 { MSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }
2169 for (size_t i = 0; i < NumTests; ++i) {
2170 APFloat x(SpecialCaseTests[i].x);
2171 APFloat y(SpecialCaseTests[i].y);
2172 APFloat::opStatus status = x.subtract(y, APFloat::rmNearestTiesToEven);
2174 APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result);
2176 EXPECT_TRUE(result.bitwiseIsEqual(x));
2177 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2178 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2182 TEST(APFloatTest, multiply) {
2183 // Test Special Cases against each other and normal values.
2186 // 1. Since we perform only default exception handling all operations with
2187 // signaling NaNs should have a result that is a quiet NaN. Currently they
2190 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
2191 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
2192 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
2193 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
2194 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
2195 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
2196 APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
2197 APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
2198 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
2199 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
2200 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
2201 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
2202 APFloat PSmallestNormalized =
2203 APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
2204 APFloat MSmallestNormalized =
2205 APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
2207 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2208 const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact;
2210 const unsigned NumTests = 169;
2217 } SpecialCaseTests[NumTests] = {
2218 { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2219 { PInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2220 { PInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2221 { PInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2222 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2225 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2227 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2228 { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2229 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2230 { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2231 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2232 { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2233 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2234 { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2235 { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2236 { MInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2237 { MInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2238 { MInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2239 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2242 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2244 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2245 { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2246 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2247 { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2248 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2249 { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2250 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2251 { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2252 { PZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2253 { PZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2254 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2255 { PZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2256 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2259 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2261 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2262 { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2263 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2264 { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2265 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2266 { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2267 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2268 { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2269 { MZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2270 { MZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2271 { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2272 { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2273 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2276 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2278 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2279 { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2280 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2281 { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2282 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2283 { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2284 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2285 { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2286 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2287 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2288 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2289 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2290 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2293 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2295 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2296 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2297 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2298 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2299 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2300 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2301 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2302 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2305 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2306 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2307 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2308 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2309 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2310 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2311 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2312 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2313 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2314 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2315 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2316 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2317 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2318 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2320 { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2321 { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2322 { PNormalValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2323 { PNormalValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2324 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2327 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2329 { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2330 { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2331 { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2332 { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2333 { PNormalValue, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2334 { PNormalValue, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2335 { PNormalValue, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2336 { PNormalValue, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2337 { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2338 { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2339 { MNormalValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2340 { MNormalValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2341 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2344 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2346 { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2347 { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2348 { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2349 { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2350 { MNormalValue, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2351 { MNormalValue, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2352 { MNormalValue, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2353 { MNormalValue, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2354 { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2355 { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2356 { PLargestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2357 { PLargestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2358 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2361 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2363 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2364 { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2365 { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2366 { PLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2367 { PLargestValue, PSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2368 { PLargestValue, MSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2369 { PLargestValue, PSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2370 { PLargestValue, MSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2371 { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2372 { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2373 { MLargestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2374 { MLargestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2375 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2378 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2380 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2381 { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2382 { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2383 { MLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2384 { MLargestValue, PSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2385 { MLargestValue, MSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2386 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2387 { MLargestValue, MSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2388 { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2389 { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2390 { PSmallestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2391 { PSmallestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2392 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2395 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2397 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2398 { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2399 { PSmallestValue, PLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2400 { PSmallestValue, MLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2401 { PSmallestValue, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2402 { PSmallestValue, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2403 { PSmallestValue, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2404 { PSmallestValue, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2405 { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2406 { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2407 { MSmallestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2408 { MSmallestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2409 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2412 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2414 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2415 { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2416 { MSmallestValue, PLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2417 { MSmallestValue, MLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2418 { MSmallestValue, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2419 { MSmallestValue, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2420 { MSmallestValue, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2421 { MSmallestValue, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2422 { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2423 { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2424 { PSmallestNormalized, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2425 { PSmallestNormalized, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2426 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2429 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2431 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2432 { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2433 { PSmallestNormalized, PLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2434 { PSmallestNormalized, MLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2435 { PSmallestNormalized, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2436 { PSmallestNormalized, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2437 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2438 { PSmallestNormalized, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2439 { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2440 { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2441 { MSmallestNormalized, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2442 { MSmallestNormalized, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2443 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2446 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2448 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2449 { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2450 { MSmallestNormalized, PLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2451 { MSmallestNormalized, MLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2452 { MSmallestNormalized, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2453 { MSmallestNormalized, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2454 { MSmallestNormalized, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2455 { MSmallestNormalized, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero }
2458 for (size_t i = 0; i < NumTests; ++i) {
2459 APFloat x(SpecialCaseTests[i].x);
2460 APFloat y(SpecialCaseTests[i].y);
2461 APFloat::opStatus status = x.multiply(y, APFloat::rmNearestTiesToEven);
2463 APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result);
2465 EXPECT_TRUE(result.bitwiseIsEqual(x));
2466 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2467 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2471 TEST(APFloatTest, divide) {
2472 // Test Special Cases against each other and normal values.
2475 // 1. Since we perform only default exception handling all operations with
2476 // signaling NaNs should have a result that is a quiet NaN. Currently they
2479 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
2480 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
2481 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
2482 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
2483 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
2484 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
2485 APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
2486 APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
2487 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
2488 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
2489 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
2490 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
2491 APFloat PSmallestNormalized =
2492 APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
2493 APFloat MSmallestNormalized =
2494 APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
2496 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2497 const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact;
2499 const unsigned NumTests = 169;
2506 } SpecialCaseTests[NumTests] = {
2507 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2508 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2509 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2510 { PInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2511 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2514 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2516 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2517 { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2518 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2519 { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2520 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2521 { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2522 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2523 { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2524 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2525 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2526 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2527 { MInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2528 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2531 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2533 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2534 { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2535 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2536 { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2537 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2538 { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2539 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2540 { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2541 { PZero, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2542 { PZero, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2543 { PZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2544 { PZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2545 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2548 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2550 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2551 { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2552 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2553 { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2554 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2555 { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2556 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2557 { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2558 { MZero, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2559 { MZero, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2560 { MZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2561 { MZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2562 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2565 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2567 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2568 { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2569 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2570 { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2571 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2572 { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2573 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2574 { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2575 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2576 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2577 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2578 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2579 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2582 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2584 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2585 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2586 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2587 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2588 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2589 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2590 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2591 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2594 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2595 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2596 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2597 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2598 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2599 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2600 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2601 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2602 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2603 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2604 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2605 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2606 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2607 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2609 { PNormalValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2610 { PNormalValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2611 { PNormalValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2612 { PNormalValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2613 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2616 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2618 { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2619 { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2620 { PNormalValue, PLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal },
2621 { PNormalValue, MLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal },
2622 { PNormalValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2623 { PNormalValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2624 { PNormalValue, PSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal },
2625 { PNormalValue, MSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal },
2626 { MNormalValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2627 { MNormalValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2628 { MNormalValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2629 { MNormalValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2630 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2633 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2635 { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2636 { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2637 { MNormalValue, PLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal },
2638 { MNormalValue, MLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal },
2639 { MNormalValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2640 { MNormalValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2641 { MNormalValue, PSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal },
2642 { MNormalValue, MSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal },
2643 { PLargestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2644 { PLargestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2645 { PLargestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2646 { PLargestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2647 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2650 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2652 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2653 { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2654 { PLargestValue, PLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2655 { PLargestValue, MLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2656 { PLargestValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2657 { PLargestValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2658 { PLargestValue, PSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity },
2659 { PLargestValue, MSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity },
2660 { MLargestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2661 { MLargestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2662 { MLargestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2663 { MLargestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2664 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2667 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2669 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2670 { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2671 { MLargestValue, PLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2672 { MLargestValue, MLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2673 { MLargestValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2674 { MLargestValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2675 { MLargestValue, PSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity },
2676 { MLargestValue, MSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity },
2677 { PSmallestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2678 { PSmallestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2679 { PSmallestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2680 { PSmallestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2681 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2684 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2686 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2687 { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2688 { PSmallestValue, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2689 { PSmallestValue, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2690 { PSmallestValue, PSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2691 { PSmallestValue, MSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2692 { PSmallestValue, PSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal },
2693 { PSmallestValue, MSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal },
2694 { MSmallestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2695 { MSmallestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2696 { MSmallestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2697 { MSmallestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2698 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2701 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2703 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2704 { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2705 { MSmallestValue, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2706 { MSmallestValue, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2707 { MSmallestValue, PSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2708 { MSmallestValue, MSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2709 { MSmallestValue, PSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal },
2710 { MSmallestValue, MSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal },
2711 { PSmallestNormalized, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2712 { PSmallestNormalized, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2713 { PSmallestNormalized, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2714 { PSmallestNormalized, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2715 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2718 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2720 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2721 { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2722 { PSmallestNormalized, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2723 { PSmallestNormalized, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2724 { PSmallestNormalized, PSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal },
2725 { PSmallestNormalized, MSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal },
2726 { PSmallestNormalized, PSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2727 { PSmallestNormalized, MSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2728 { MSmallestNormalized, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2729 { MSmallestNormalized, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2730 { MSmallestNormalized, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2731 { MSmallestNormalized, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2732 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2735 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2737 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2738 { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2739 { MSmallestNormalized, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2740 { MSmallestNormalized, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2741 { MSmallestNormalized, PSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal },
2742 { MSmallestNormalized, MSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal },
2743 { MSmallestNormalized, PSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2744 { MSmallestNormalized, MSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2747 for (size_t i = 0; i < NumTests; ++i) {
2748 APFloat x(SpecialCaseTests[i].x);
2749 APFloat y(SpecialCaseTests[i].y);
2750 APFloat::opStatus status = x.divide(y, APFloat::rmNearestTiesToEven);
2752 APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result);
2754 EXPECT_TRUE(result.bitwiseIsEqual(x));
2755 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2756 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2760 TEST(APFloatTest, operatorOverloads) {
2761 // This is mostly testing that these operator overloads compile.
2762 APFloat One = APFloat(APFloat::IEEEsingle, "0x1p+0");
2763 APFloat Two = APFloat(APFloat::IEEEsingle, "0x2p+0");
2764 EXPECT_TRUE(Two.bitwiseIsEqual(One + One));
2765 EXPECT_TRUE(One.bitwiseIsEqual(Two - One));
2766 EXPECT_TRUE(Two.bitwiseIsEqual(One * Two));
2767 EXPECT_TRUE(One.bitwiseIsEqual(Two / Two));
2770 TEST(APFloatTest, abs) {
2771 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
2772 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
2773 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
2774 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
2775 APFloat PQNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
2776 APFloat MQNaN = APFloat::getNaN(APFloat::IEEEsingle, true);
2777 APFloat PSNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
2778 APFloat MSNaN = APFloat::getSNaN(APFloat::IEEEsingle, true);
2779 APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
2780 APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
2781 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
2782 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
2783 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
2784 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
2785 APFloat PSmallestNormalized =
2786 APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
2787 APFloat MSmallestNormalized =
2788 APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
2790 EXPECT_TRUE(PInf.bitwiseIsEqual(abs(PInf)));
2791 EXPECT_TRUE(PInf.bitwiseIsEqual(abs(MInf)));
2792 EXPECT_TRUE(PZero.bitwiseIsEqual(abs(PZero)));
2793 EXPECT_TRUE(PZero.bitwiseIsEqual(abs(MZero)));
2794 EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(PQNaN)));
2795 EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(MQNaN)));
2796 EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(PSNaN)));
2797 EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(MSNaN)));
2798 EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(PNormalValue)));
2799 EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(MNormalValue)));
2800 EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(PLargestValue)));
2801 EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(MLargestValue)));
2802 EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(PSmallestValue)));
2803 EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(MSmallestValue)));
2804 EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(PSmallestNormalized)));
2805 EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(MSmallestNormalized)));
2808 TEST(APFloatTest, ilogb) {
2809 EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle, "0x1p+0")));
2810 EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle, "-0x1p+0")));
2811 EXPECT_EQ(42, ilogb(APFloat(APFloat::IEEEsingle, "0x1p+42")));
2812 EXPECT_EQ(-42, ilogb(APFloat(APFloat::IEEEsingle, "0x1p-42")));
2814 EXPECT_EQ(APFloat::IEK_Inf,
2815 ilogb(APFloat::getInf(APFloat::IEEEsingle, false)));
2816 EXPECT_EQ(APFloat::IEK_Inf,
2817 ilogb(APFloat::getInf(APFloat::IEEEsingle, true)));
2818 EXPECT_EQ(APFloat::IEK_Zero,
2819 ilogb(APFloat::getZero(APFloat::IEEEsingle, false)));
2820 EXPECT_EQ(APFloat::IEK_Zero,
2821 ilogb(APFloat::getZero(APFloat::IEEEsingle, true)));
2822 EXPECT_EQ(APFloat::IEK_NaN,
2823 ilogb(APFloat::getNaN(APFloat::IEEEsingle, false)));
2824 EXPECT_EQ(APFloat::IEK_NaN,
2825 ilogb(APFloat::getSNaN(APFloat::IEEEsingle, false)));
2827 EXPECT_EQ(127, ilogb(APFloat::getLargest(APFloat::IEEEsingle, false)));
2828 EXPECT_EQ(127, ilogb(APFloat::getLargest(APFloat::IEEEsingle, true)));
2829 EXPECT_EQ(-126, ilogb(APFloat::getSmallest(APFloat::IEEEsingle, false)));
2830 EXPECT_EQ(-126, ilogb(APFloat::getSmallest(APFloat::IEEEsingle, true)));
2832 ilogb(APFloat::getSmallestNormalized(APFloat::IEEEsingle, false)));
2834 ilogb(APFloat::getSmallestNormalized(APFloat::IEEEsingle, true)));
2837 TEST(APFloatTest, scalbn) {
2839 APFloat(APFloat::IEEEsingle, "0x1p+0")
2840 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), 0)));
2842 APFloat(APFloat::IEEEsingle, "0x1p+42")
2843 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), 42)));
2845 APFloat(APFloat::IEEEsingle, "0x1p-42")
2846 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), -42)));
2848 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
2849 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
2850 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
2851 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
2852 APFloat QPNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
2853 APFloat QMNaN = APFloat::getNaN(APFloat::IEEEsingle, true);
2854 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
2856 EXPECT_TRUE(PInf.bitwiseIsEqual(scalbn(PInf, 0)));
2857 EXPECT_TRUE(MInf.bitwiseIsEqual(scalbn(MInf, 0)));
2858 EXPECT_TRUE(PZero.bitwiseIsEqual(scalbn(PZero, 0)));
2859 EXPECT_TRUE(MZero.bitwiseIsEqual(scalbn(MZero, 0)));
2860 EXPECT_TRUE(QPNaN.bitwiseIsEqual(scalbn(QPNaN, 0)));
2861 EXPECT_TRUE(QMNaN.bitwiseIsEqual(scalbn(QMNaN, 0)));
2862 EXPECT_TRUE(SNaN.bitwiseIsEqual(scalbn(SNaN, 0)));
2865 PInf.bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), 128)));
2866 EXPECT_TRUE(MInf.bitwiseIsEqual(
2867 scalbn(APFloat(APFloat::IEEEsingle, "-0x1p+0"), 128)));
2869 PInf.bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+127"), 1)));
2870 EXPECT_TRUE(PZero.bitwiseIsEqual(
2871 scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), -127)));
2872 EXPECT_TRUE(MZero.bitwiseIsEqual(
2873 scalbn(APFloat(APFloat::IEEEsingle, "-0x1p+0"), -127)));
2874 EXPECT_TRUE(PZero.bitwiseIsEqual(
2875 scalbn(APFloat(APFloat::IEEEsingle, "0x1p-126"), -1)));
2876 EXPECT_TRUE(PZero.bitwiseIsEqual(
2877 scalbn(APFloat(APFloat::IEEEsingle, "0x1p-126"), -1)));