[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / ADT / APFloatTest.cpp
blob3caa09f364ff412059ca659aa1b954af7659cdbf
1 //===- llvm/unittest/ADT/APFloat.cpp - APFloat unit tests ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/APFloat.h"
10 #include "llvm/ADT/APSInt.h"
11 #include "llvm/ADT/Hashing.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Support/FormatVariadic.h"
15 #include "gtest/gtest.h"
16 #include <cmath>
17 #include <ostream>
18 #include <string>
19 #include <tuple>
21 using namespace llvm;
23 static std::string convertToErrorFromString(StringRef Str) {
24 llvm::APFloat F(0.0);
25 auto StatusOrErr =
26 F.convertFromString(Str, llvm::APFloat::rmNearestTiesToEven);
27 EXPECT_TRUE(!StatusOrErr);
28 return toString(StatusOrErr.takeError());
31 static double convertToDoubleFromString(StringRef Str) {
32 llvm::APFloat F(0.0);
33 auto StatusOrErr =
34 F.convertFromString(Str, llvm::APFloat::rmNearestTiesToEven);
35 EXPECT_FALSE(!StatusOrErr);
36 consumeError(StatusOrErr.takeError());
37 return F.convertToDouble();
40 static std::string convertToString(double d, unsigned Prec, unsigned Pad,
41 bool Tr = true) {
42 llvm::SmallVector<char, 100> Buffer;
43 llvm::APFloat F(d);
44 F.toString(Buffer, Prec, Pad, Tr);
45 return std::string(Buffer.data(), Buffer.size());
48 namespace {
50 TEST(APFloatTest, isSignaling) {
51 // We test qNaN, -qNaN, +sNaN, -sNaN with and without payloads. *NOTE* The
52 // positive/negative distinction is included only since the getQNaN/getSNaN
53 // API provides the option.
54 APInt payload = APInt::getOneBitSet(4, 2);
55 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle(), false).isSignaling());
56 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle(), true).isSignaling());
57 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle(), false, &payload).isSignaling());
58 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle(), true, &payload).isSignaling());
59 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isSignaling());
60 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), true).isSignaling());
61 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), false, &payload).isSignaling());
62 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), true, &payload).isSignaling());
65 TEST(APFloatTest, next) {
67 APFloat test(APFloat::IEEEquad(), APFloat::uninitialized);
68 APFloat expected(APFloat::IEEEquad(), APFloat::uninitialized);
70 // 1. Test Special Cases Values.
72 // Test all special values for nextUp and nextDown perscribed by IEEE-754R
73 // 2008. These are:
74 // 1. +inf
75 // 2. -inf
76 // 3. getLargest()
77 // 4. -getLargest()
78 // 5. getSmallest()
79 // 6. -getSmallest()
80 // 7. qNaN
81 // 8. sNaN
82 // 9. +0
83 // 10. -0
85 // nextUp(+inf) = +inf.
86 test = APFloat::getInf(APFloat::IEEEquad(), false);
87 expected = APFloat::getInf(APFloat::IEEEquad(), false);
88 EXPECT_EQ(test.next(false), APFloat::opOK);
89 EXPECT_TRUE(test.isInfinity());
90 EXPECT_TRUE(!test.isNegative());
91 EXPECT_TRUE(test.bitwiseIsEqual(expected));
93 // nextDown(+inf) = -nextUp(-inf) = -(-getLargest()) = getLargest()
94 test = APFloat::getInf(APFloat::IEEEquad(), false);
95 expected = APFloat::getLargest(APFloat::IEEEquad(), false);
96 EXPECT_EQ(test.next(true), APFloat::opOK);
97 EXPECT_TRUE(!test.isNegative());
98 EXPECT_TRUE(test.bitwiseIsEqual(expected));
100 // nextUp(-inf) = -getLargest()
101 test = APFloat::getInf(APFloat::IEEEquad(), true);
102 expected = APFloat::getLargest(APFloat::IEEEquad(), true);
103 EXPECT_EQ(test.next(false), APFloat::opOK);
104 EXPECT_TRUE(test.isNegative());
105 EXPECT_TRUE(test.bitwiseIsEqual(expected));
107 // nextDown(-inf) = -nextUp(+inf) = -(+inf) = -inf.
108 test = APFloat::getInf(APFloat::IEEEquad(), true);
109 expected = APFloat::getInf(APFloat::IEEEquad(), true);
110 EXPECT_EQ(test.next(true), APFloat::opOK);
111 EXPECT_TRUE(test.isInfinity() && test.isNegative());
112 EXPECT_TRUE(test.bitwiseIsEqual(expected));
114 // nextUp(getLargest()) = +inf
115 test = APFloat::getLargest(APFloat::IEEEquad(), false);
116 expected = APFloat::getInf(APFloat::IEEEquad(), false);
117 EXPECT_EQ(test.next(false), APFloat::opOK);
118 EXPECT_TRUE(test.isInfinity() && !test.isNegative());
119 EXPECT_TRUE(test.bitwiseIsEqual(expected));
121 // nextDown(getLargest()) = -nextUp(-getLargest())
122 // = -(-getLargest() + inc)
123 // = getLargest() - inc.
124 test = APFloat::getLargest(APFloat::IEEEquad(), false);
125 expected = APFloat(APFloat::IEEEquad(),
126 "0x1.fffffffffffffffffffffffffffep+16383");
127 EXPECT_EQ(test.next(true), APFloat::opOK);
128 EXPECT_TRUE(!test.isInfinity() && !test.isNegative());
129 EXPECT_TRUE(test.bitwiseIsEqual(expected));
131 // nextUp(-getLargest()) = -getLargest() + inc.
132 test = APFloat::getLargest(APFloat::IEEEquad(), true);
133 expected = APFloat(APFloat::IEEEquad(),
134 "-0x1.fffffffffffffffffffffffffffep+16383");
135 EXPECT_EQ(test.next(false), APFloat::opOK);
136 EXPECT_TRUE(test.bitwiseIsEqual(expected));
138 // nextDown(-getLargest()) = -nextUp(getLargest()) = -(inf) = -inf.
139 test = APFloat::getLargest(APFloat::IEEEquad(), true);
140 expected = APFloat::getInf(APFloat::IEEEquad(), true);
141 EXPECT_EQ(test.next(true), APFloat::opOK);
142 EXPECT_TRUE(test.isInfinity() && test.isNegative());
143 EXPECT_TRUE(test.bitwiseIsEqual(expected));
145 // nextUp(getSmallest()) = getSmallest() + inc.
146 test = APFloat(APFloat::IEEEquad(), "0x0.0000000000000000000000000001p-16382");
147 expected = APFloat(APFloat::IEEEquad(),
148 "0x0.0000000000000000000000000002p-16382");
149 EXPECT_EQ(test.next(false), APFloat::opOK);
150 EXPECT_TRUE(test.bitwiseIsEqual(expected));
152 // nextDown(getSmallest()) = -nextUp(-getSmallest()) = -(-0) = +0.
153 test = APFloat(APFloat::IEEEquad(), "0x0.0000000000000000000000000001p-16382");
154 expected = APFloat::getZero(APFloat::IEEEquad(), false);
155 EXPECT_EQ(test.next(true), APFloat::opOK);
156 EXPECT_TRUE(test.isPosZero());
157 EXPECT_TRUE(test.bitwiseIsEqual(expected));
159 // nextUp(-getSmallest()) = -0.
160 test = APFloat(APFloat::IEEEquad(), "-0x0.0000000000000000000000000001p-16382");
161 expected = APFloat::getZero(APFloat::IEEEquad(), true);
162 EXPECT_EQ(test.next(false), APFloat::opOK);
163 EXPECT_TRUE(test.isNegZero());
164 EXPECT_TRUE(test.bitwiseIsEqual(expected));
166 // nextDown(-getSmallest()) = -nextUp(getSmallest()) = -getSmallest() - inc.
167 test = APFloat(APFloat::IEEEquad(), "-0x0.0000000000000000000000000001p-16382");
168 expected = APFloat(APFloat::IEEEquad(),
169 "-0x0.0000000000000000000000000002p-16382");
170 EXPECT_EQ(test.next(true), APFloat::opOK);
171 EXPECT_TRUE(test.bitwiseIsEqual(expected));
173 // nextUp(qNaN) = qNaN
174 test = APFloat::getQNaN(APFloat::IEEEquad(), false);
175 expected = APFloat::getQNaN(APFloat::IEEEquad(), false);
176 EXPECT_EQ(test.next(false), APFloat::opOK);
177 EXPECT_TRUE(test.bitwiseIsEqual(expected));
179 // nextDown(qNaN) = qNaN
180 test = APFloat::getQNaN(APFloat::IEEEquad(), false);
181 expected = APFloat::getQNaN(APFloat::IEEEquad(), false);
182 EXPECT_EQ(test.next(true), APFloat::opOK);
183 EXPECT_TRUE(test.bitwiseIsEqual(expected));
185 // nextUp(sNaN) = qNaN
186 test = APFloat::getSNaN(APFloat::IEEEquad(), false);
187 expected = APFloat::getQNaN(APFloat::IEEEquad(), false);
188 EXPECT_EQ(test.next(false), APFloat::opInvalidOp);
189 EXPECT_TRUE(test.bitwiseIsEqual(expected));
191 // nextDown(sNaN) = qNaN
192 test = APFloat::getSNaN(APFloat::IEEEquad(), false);
193 expected = APFloat::getQNaN(APFloat::IEEEquad(), false);
194 EXPECT_EQ(test.next(true), APFloat::opInvalidOp);
195 EXPECT_TRUE(test.bitwiseIsEqual(expected));
197 // nextUp(+0) = +getSmallest()
198 test = APFloat::getZero(APFloat::IEEEquad(), false);
199 expected = APFloat::getSmallest(APFloat::IEEEquad(), false);
200 EXPECT_EQ(test.next(false), APFloat::opOK);
201 EXPECT_TRUE(test.bitwiseIsEqual(expected));
203 // nextDown(+0) = -nextUp(-0) = -getSmallest()
204 test = APFloat::getZero(APFloat::IEEEquad(), false);
205 expected = APFloat::getSmallest(APFloat::IEEEquad(), true);
206 EXPECT_EQ(test.next(true), APFloat::opOK);
207 EXPECT_TRUE(test.bitwiseIsEqual(expected));
209 // nextUp(-0) = +getSmallest()
210 test = APFloat::getZero(APFloat::IEEEquad(), true);
211 expected = APFloat::getSmallest(APFloat::IEEEquad(), false);
212 EXPECT_EQ(test.next(false), APFloat::opOK);
213 EXPECT_TRUE(test.bitwiseIsEqual(expected));
215 // nextDown(-0) = -nextUp(0) = -getSmallest()
216 test = APFloat::getZero(APFloat::IEEEquad(), true);
217 expected = APFloat::getSmallest(APFloat::IEEEquad(), true);
218 EXPECT_EQ(test.next(true), APFloat::opOK);
219 EXPECT_TRUE(test.bitwiseIsEqual(expected));
221 // 2. Binade Boundary Tests.
223 // 2a. Test denormal <-> normal binade boundaries.
224 // * nextUp(+Largest Denormal) -> +Smallest Normal.
225 // * nextDown(-Largest Denormal) -> -Smallest Normal.
226 // * nextUp(-Smallest Normal) -> -Largest Denormal.
227 // * nextDown(+Smallest Normal) -> +Largest Denormal.
229 // nextUp(+Largest Denormal) -> +Smallest Normal.
230 test = APFloat(APFloat::IEEEquad(), "0x0.ffffffffffffffffffffffffffffp-16382");
231 expected = APFloat(APFloat::IEEEquad(),
232 "0x1.0000000000000000000000000000p-16382");
233 EXPECT_EQ(test.next(false), APFloat::opOK);
234 EXPECT_FALSE(test.isDenormal());
235 EXPECT_TRUE(test.bitwiseIsEqual(expected));
237 // nextDown(-Largest Denormal) -> -Smallest Normal.
238 test = APFloat(APFloat::IEEEquad(),
239 "-0x0.ffffffffffffffffffffffffffffp-16382");
240 expected = APFloat(APFloat::IEEEquad(),
241 "-0x1.0000000000000000000000000000p-16382");
242 EXPECT_EQ(test.next(true), APFloat::opOK);
243 EXPECT_FALSE(test.isDenormal());
244 EXPECT_TRUE(test.bitwiseIsEqual(expected));
246 // nextUp(-Smallest Normal) -> -LargestDenormal.
247 test = APFloat(APFloat::IEEEquad(),
248 "-0x1.0000000000000000000000000000p-16382");
249 expected = APFloat(APFloat::IEEEquad(),
250 "-0x0.ffffffffffffffffffffffffffffp-16382");
251 EXPECT_EQ(test.next(false), APFloat::opOK);
252 EXPECT_TRUE(test.isDenormal());
253 EXPECT_TRUE(test.bitwiseIsEqual(expected));
255 // nextDown(+Smallest Normal) -> +Largest Denormal.
256 test = APFloat(APFloat::IEEEquad(),
257 "+0x1.0000000000000000000000000000p-16382");
258 expected = APFloat(APFloat::IEEEquad(),
259 "+0x0.ffffffffffffffffffffffffffffp-16382");
260 EXPECT_EQ(test.next(true), APFloat::opOK);
261 EXPECT_TRUE(test.isDenormal());
262 EXPECT_TRUE(test.bitwiseIsEqual(expected));
264 // 2b. Test normal <-> normal binade boundaries.
265 // * nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
266 // * nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
267 // * nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
268 // * nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
270 // nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
271 test = APFloat(APFloat::IEEEquad(), "-0x1p+1");
272 expected = APFloat(APFloat::IEEEquad(),
273 "-0x1.ffffffffffffffffffffffffffffp+0");
274 EXPECT_EQ(test.next(false), APFloat::opOK);
275 EXPECT_TRUE(test.bitwiseIsEqual(expected));
277 // nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
278 test = APFloat(APFloat::IEEEquad(), "0x1p+1");
279 expected = APFloat(APFloat::IEEEquad(), "0x1.ffffffffffffffffffffffffffffp+0");
280 EXPECT_EQ(test.next(true), APFloat::opOK);
281 EXPECT_TRUE(test.bitwiseIsEqual(expected));
283 // nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
284 test = APFloat(APFloat::IEEEquad(), "0x1.ffffffffffffffffffffffffffffp+0");
285 expected = APFloat(APFloat::IEEEquad(), "0x1p+1");
286 EXPECT_EQ(test.next(false), APFloat::opOK);
287 EXPECT_TRUE(test.bitwiseIsEqual(expected));
289 // nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
290 test = APFloat(APFloat::IEEEquad(), "-0x1.ffffffffffffffffffffffffffffp+0");
291 expected = APFloat(APFloat::IEEEquad(), "-0x1p+1");
292 EXPECT_EQ(test.next(true), APFloat::opOK);
293 EXPECT_TRUE(test.bitwiseIsEqual(expected));
295 // 2c. Test using next at binade boundaries with a direction away from the
296 // binade boundary. Away from denormal <-> normal boundaries.
298 // This is to make sure that even though we are at a binade boundary, since
299 // we are rounding away, we do not trigger the binade boundary code. Thus we
300 // test:
301 // * nextUp(-Largest Denormal) -> -Largest Denormal + inc.
302 // * nextDown(+Largest Denormal) -> +Largest Denormal - inc.
303 // * nextUp(+Smallest Normal) -> +Smallest Normal + inc.
304 // * nextDown(-Smallest Normal) -> -Smallest Normal - inc.
306 // nextUp(-Largest Denormal) -> -Largest Denormal + inc.
307 test = APFloat(APFloat::IEEEquad(), "-0x0.ffffffffffffffffffffffffffffp-16382");
308 expected = APFloat(APFloat::IEEEquad(),
309 "-0x0.fffffffffffffffffffffffffffep-16382");
310 EXPECT_EQ(test.next(false), APFloat::opOK);
311 EXPECT_TRUE(test.isDenormal());
312 EXPECT_TRUE(test.isNegative());
313 EXPECT_TRUE(test.bitwiseIsEqual(expected));
315 // nextDown(+Largest Denormal) -> +Largest Denormal - inc.
316 test = APFloat(APFloat::IEEEquad(), "0x0.ffffffffffffffffffffffffffffp-16382");
317 expected = APFloat(APFloat::IEEEquad(),
318 "0x0.fffffffffffffffffffffffffffep-16382");
319 EXPECT_EQ(test.next(true), APFloat::opOK);
320 EXPECT_TRUE(test.isDenormal());
321 EXPECT_TRUE(!test.isNegative());
322 EXPECT_TRUE(test.bitwiseIsEqual(expected));
324 // nextUp(+Smallest Normal) -> +Smallest Normal + inc.
325 test = APFloat(APFloat::IEEEquad(), "0x1.0000000000000000000000000000p-16382");
326 expected = APFloat(APFloat::IEEEquad(),
327 "0x1.0000000000000000000000000001p-16382");
328 EXPECT_EQ(test.next(false), APFloat::opOK);
329 EXPECT_TRUE(!test.isDenormal());
330 EXPECT_TRUE(!test.isNegative());
331 EXPECT_TRUE(test.bitwiseIsEqual(expected));
333 // nextDown(-Smallest Normal) -> -Smallest Normal - inc.
334 test = APFloat(APFloat::IEEEquad(), "-0x1.0000000000000000000000000000p-16382");
335 expected = APFloat(APFloat::IEEEquad(),
336 "-0x1.0000000000000000000000000001p-16382");
337 EXPECT_EQ(test.next(true), APFloat::opOK);
338 EXPECT_TRUE(!test.isDenormal());
339 EXPECT_TRUE(test.isNegative());
340 EXPECT_TRUE(test.bitwiseIsEqual(expected));
342 // 2d. Test values which cause our exponent to go to min exponent. This
343 // is to ensure that guards in the code to check for min exponent
344 // trigger properly.
345 // * nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
346 // * nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
347 // -0x1p-16381
348 // * nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16382
349 // * nextDown(0x1p-16382) -> 0x1.ffffffffffffffffffffffffffffp-16382
351 // nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
352 test = APFloat(APFloat::IEEEquad(), "-0x1p-16381");
353 expected = APFloat(APFloat::IEEEquad(),
354 "-0x1.ffffffffffffffffffffffffffffp-16382");
355 EXPECT_EQ(test.next(false), APFloat::opOK);
356 EXPECT_TRUE(test.bitwiseIsEqual(expected));
358 // nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
359 // -0x1p-16381
360 test = APFloat(APFloat::IEEEquad(), "-0x1.ffffffffffffffffffffffffffffp-16382");
361 expected = APFloat(APFloat::IEEEquad(), "-0x1p-16381");
362 EXPECT_EQ(test.next(true), APFloat::opOK);
363 EXPECT_TRUE(test.bitwiseIsEqual(expected));
365 // nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16381
366 test = APFloat(APFloat::IEEEquad(), "0x1.ffffffffffffffffffffffffffffp-16382");
367 expected = APFloat(APFloat::IEEEquad(), "0x1p-16381");
368 EXPECT_EQ(test.next(false), APFloat::opOK);
369 EXPECT_TRUE(test.bitwiseIsEqual(expected));
371 // nextDown(0x1p-16381) -> 0x1.ffffffffffffffffffffffffffffp-16382
372 test = APFloat(APFloat::IEEEquad(), "0x1p-16381");
373 expected = APFloat(APFloat::IEEEquad(),
374 "0x1.ffffffffffffffffffffffffffffp-16382");
375 EXPECT_EQ(test.next(true), APFloat::opOK);
376 EXPECT_TRUE(test.bitwiseIsEqual(expected));
378 // 3. Now we test both denormal/normal computation which will not cause us
379 // to go across binade boundaries. Specifically we test:
380 // * nextUp(+Denormal) -> +Denormal.
381 // * nextDown(+Denormal) -> +Denormal.
382 // * nextUp(-Denormal) -> -Denormal.
383 // * nextDown(-Denormal) -> -Denormal.
384 // * nextUp(+Normal) -> +Normal.
385 // * nextDown(+Normal) -> +Normal.
386 // * nextUp(-Normal) -> -Normal.
387 // * nextDown(-Normal) -> -Normal.
389 // nextUp(+Denormal) -> +Denormal.
390 test = APFloat(APFloat::IEEEquad(),
391 "0x0.ffffffffffffffffffffffff000cp-16382");
392 expected = APFloat(APFloat::IEEEquad(),
393 "0x0.ffffffffffffffffffffffff000dp-16382");
394 EXPECT_EQ(test.next(false), APFloat::opOK);
395 EXPECT_TRUE(test.isDenormal());
396 EXPECT_TRUE(!test.isNegative());
397 EXPECT_TRUE(test.bitwiseIsEqual(expected));
399 // nextDown(+Denormal) -> +Denormal.
400 test = APFloat(APFloat::IEEEquad(),
401 "0x0.ffffffffffffffffffffffff000cp-16382");
402 expected = APFloat(APFloat::IEEEquad(),
403 "0x0.ffffffffffffffffffffffff000bp-16382");
404 EXPECT_EQ(test.next(true), APFloat::opOK);
405 EXPECT_TRUE(test.isDenormal());
406 EXPECT_TRUE(!test.isNegative());
407 EXPECT_TRUE(test.bitwiseIsEqual(expected));
409 // nextUp(-Denormal) -> -Denormal.
410 test = APFloat(APFloat::IEEEquad(),
411 "-0x0.ffffffffffffffffffffffff000cp-16382");
412 expected = APFloat(APFloat::IEEEquad(),
413 "-0x0.ffffffffffffffffffffffff000bp-16382");
414 EXPECT_EQ(test.next(false), APFloat::opOK);
415 EXPECT_TRUE(test.isDenormal());
416 EXPECT_TRUE(test.isNegative());
417 EXPECT_TRUE(test.bitwiseIsEqual(expected));
419 // nextDown(-Denormal) -> -Denormal
420 test = APFloat(APFloat::IEEEquad(),
421 "-0x0.ffffffffffffffffffffffff000cp-16382");
422 expected = APFloat(APFloat::IEEEquad(),
423 "-0x0.ffffffffffffffffffffffff000dp-16382");
424 EXPECT_EQ(test.next(true), APFloat::opOK);
425 EXPECT_TRUE(test.isDenormal());
426 EXPECT_TRUE(test.isNegative());
427 EXPECT_TRUE(test.bitwiseIsEqual(expected));
429 // nextUp(+Normal) -> +Normal.
430 test = APFloat(APFloat::IEEEquad(),
431 "0x1.ffffffffffffffffffffffff000cp-16000");
432 expected = APFloat(APFloat::IEEEquad(),
433 "0x1.ffffffffffffffffffffffff000dp-16000");
434 EXPECT_EQ(test.next(false), APFloat::opOK);
435 EXPECT_TRUE(!test.isDenormal());
436 EXPECT_TRUE(!test.isNegative());
437 EXPECT_TRUE(test.bitwiseIsEqual(expected));
439 // nextDown(+Normal) -> +Normal.
440 test = APFloat(APFloat::IEEEquad(),
441 "0x1.ffffffffffffffffffffffff000cp-16000");
442 expected = APFloat(APFloat::IEEEquad(),
443 "0x1.ffffffffffffffffffffffff000bp-16000");
444 EXPECT_EQ(test.next(true), APFloat::opOK);
445 EXPECT_TRUE(!test.isDenormal());
446 EXPECT_TRUE(!test.isNegative());
447 EXPECT_TRUE(test.bitwiseIsEqual(expected));
449 // nextUp(-Normal) -> -Normal.
450 test = APFloat(APFloat::IEEEquad(),
451 "-0x1.ffffffffffffffffffffffff000cp-16000");
452 expected = APFloat(APFloat::IEEEquad(),
453 "-0x1.ffffffffffffffffffffffff000bp-16000");
454 EXPECT_EQ(test.next(false), APFloat::opOK);
455 EXPECT_TRUE(!test.isDenormal());
456 EXPECT_TRUE(test.isNegative());
457 EXPECT_TRUE(test.bitwiseIsEqual(expected));
459 // nextDown(-Normal) -> -Normal.
460 test = APFloat(APFloat::IEEEquad(),
461 "-0x1.ffffffffffffffffffffffff000cp-16000");
462 expected = APFloat(APFloat::IEEEquad(),
463 "-0x1.ffffffffffffffffffffffff000dp-16000");
464 EXPECT_EQ(test.next(true), APFloat::opOK);
465 EXPECT_TRUE(!test.isDenormal());
466 EXPECT_TRUE(test.isNegative());
467 EXPECT_TRUE(test.bitwiseIsEqual(expected));
470 TEST(APFloatTest, FMA) {
471 APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;
474 APFloat f1(14.5f);
475 APFloat f2(-14.5f);
476 APFloat f3(225.0f);
477 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
478 EXPECT_EQ(14.75f, f1.convertToFloat());
482 APFloat Val2(2.0f);
483 APFloat f1((float)1.17549435e-38F);
484 APFloat f2((float)1.17549435e-38F);
485 f1.divide(Val2, rdmd);
486 f2.divide(Val2, rdmd);
487 APFloat f3(12.0f);
488 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
489 EXPECT_EQ(12.0f, f1.convertToFloat());
492 // Test for correct zero sign when answer is exactly zero.
493 // fma(1.0, -1.0, 1.0) -> +ve 0.
495 APFloat f1(1.0);
496 APFloat f2(-1.0);
497 APFloat f3(1.0);
498 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
499 EXPECT_TRUE(!f1.isNegative() && f1.isZero());
502 // Test for correct zero sign when answer is exactly zero and rounding towards
503 // negative.
504 // fma(1.0, -1.0, 1.0) -> +ve 0.
506 APFloat f1(1.0);
507 APFloat f2(-1.0);
508 APFloat f3(1.0);
509 f1.fusedMultiplyAdd(f2, f3, APFloat::rmTowardNegative);
510 EXPECT_TRUE(f1.isNegative() && f1.isZero());
513 // Test for correct (in this case -ve) sign when adding like signed zeros.
514 // Test fma(0.0, -0.0, -0.0) -> -ve 0.
516 APFloat f1(0.0);
517 APFloat f2(-0.0);
518 APFloat f3(-0.0);
519 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
520 EXPECT_TRUE(f1.isNegative() && f1.isZero());
523 // Test -ve sign preservation when small negative results underflow.
525 APFloat f1(APFloat::IEEEdouble(), "-0x1p-1074");
526 APFloat f2(APFloat::IEEEdouble(), "+0x1p-1074");
527 APFloat f3(0.0);
528 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
529 EXPECT_TRUE(f1.isNegative() && f1.isZero());
532 // Test x87 extended precision case from http://llvm.org/PR20728.
534 APFloat M1(APFloat::x87DoubleExtended(), 1);
535 APFloat M2(APFloat::x87DoubleExtended(), 1);
536 APFloat A(APFloat::x87DoubleExtended(), 3);
538 bool losesInfo = false;
539 M1.fusedMultiplyAdd(M1, A, APFloat::rmNearestTiesToEven);
540 M1.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
541 EXPECT_FALSE(losesInfo);
542 EXPECT_EQ(4.0f, M1.convertToFloat());
545 // Regression test that failed an assertion.
547 APFloat f1(-8.85242279E-41f);
548 APFloat f2(2.0f);
549 APFloat f3(8.85242279E-41f);
550 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
551 EXPECT_EQ(-8.85242279E-41f, f1.convertToFloat());
554 // Test using only a single instance of APFloat.
556 APFloat F(1.5);
558 F.fusedMultiplyAdd(F, F, APFloat::rmNearestTiesToEven);
559 EXPECT_EQ(3.75, F.convertToDouble());
563 TEST(APFloatTest, MinNum) {
564 APFloat f1(1.0);
565 APFloat f2(2.0);
566 APFloat nan = APFloat::getNaN(APFloat::IEEEdouble());
568 EXPECT_EQ(1.0, minnum(f1, f2).convertToDouble());
569 EXPECT_EQ(1.0, minnum(f2, f1).convertToDouble());
570 EXPECT_EQ(1.0, minnum(f1, nan).convertToDouble());
571 EXPECT_EQ(1.0, minnum(nan, f1).convertToDouble());
574 TEST(APFloatTest, MaxNum) {
575 APFloat f1(1.0);
576 APFloat f2(2.0);
577 APFloat nan = APFloat::getNaN(APFloat::IEEEdouble());
579 EXPECT_EQ(2.0, maxnum(f1, f2).convertToDouble());
580 EXPECT_EQ(2.0, maxnum(f2, f1).convertToDouble());
581 EXPECT_EQ(1.0, maxnum(f1, nan).convertToDouble());
582 EXPECT_EQ(1.0, maxnum(nan, f1).convertToDouble());
585 TEST(APFloatTest, Minimum) {
586 APFloat f1(1.0);
587 APFloat f2(2.0);
588 APFloat zp(0.0);
589 APFloat zn(-0.0);
590 APFloat nan = APFloat::getNaN(APFloat::IEEEdouble());
592 EXPECT_EQ(1.0, minimum(f1, f2).convertToDouble());
593 EXPECT_EQ(1.0, minimum(f2, f1).convertToDouble());
594 EXPECT_EQ(-0.0, minimum(zp, zn).convertToDouble());
595 EXPECT_EQ(-0.0, minimum(zn, zp).convertToDouble());
596 EXPECT_TRUE(std::isnan(minimum(f1, nan).convertToDouble()));
597 EXPECT_TRUE(std::isnan(minimum(nan, f1).convertToDouble()));
600 TEST(APFloatTest, Maximum) {
601 APFloat f1(1.0);
602 APFloat f2(2.0);
603 APFloat zp(0.0);
604 APFloat zn(-0.0);
605 APFloat nan = APFloat::getNaN(APFloat::IEEEdouble());
607 EXPECT_EQ(2.0, maximum(f1, f2).convertToDouble());
608 EXPECT_EQ(2.0, maximum(f2, f1).convertToDouble());
609 EXPECT_EQ(0.0, maximum(zp, zn).convertToDouble());
610 EXPECT_EQ(0.0, maximum(zn, zp).convertToDouble());
611 EXPECT_TRUE(std::isnan(maximum(f1, nan).convertToDouble()));
612 EXPECT_TRUE(std::isnan(maximum(nan, f1).convertToDouble()));
615 TEST(APFloatTest, Denormal) {
616 APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;
618 // Test single precision
620 const char *MinNormalStr = "1.17549435082228750797e-38";
621 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), MinNormalStr).isDenormal());
622 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), 0).isDenormal());
624 APFloat Val2(APFloat::IEEEsingle(), 2);
625 APFloat T(APFloat::IEEEsingle(), MinNormalStr);
626 T.divide(Val2, rdmd);
627 EXPECT_TRUE(T.isDenormal());
630 // Test double precision
632 const char *MinNormalStr = "2.22507385850720138309e-308";
633 EXPECT_FALSE(APFloat(APFloat::IEEEdouble(), MinNormalStr).isDenormal());
634 EXPECT_FALSE(APFloat(APFloat::IEEEdouble(), 0).isDenormal());
636 APFloat Val2(APFloat::IEEEdouble(), 2);
637 APFloat T(APFloat::IEEEdouble(), MinNormalStr);
638 T.divide(Val2, rdmd);
639 EXPECT_TRUE(T.isDenormal());
642 // Test Intel double-ext
644 const char *MinNormalStr = "3.36210314311209350626e-4932";
645 EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended(), MinNormalStr).isDenormal());
646 EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended(), 0).isDenormal());
648 APFloat Val2(APFloat::x87DoubleExtended(), 2);
649 APFloat T(APFloat::x87DoubleExtended(), MinNormalStr);
650 T.divide(Val2, rdmd);
651 EXPECT_TRUE(T.isDenormal());
654 // Test quadruple precision
656 const char *MinNormalStr = "3.36210314311209350626267781732175260e-4932";
657 EXPECT_FALSE(APFloat(APFloat::IEEEquad(), MinNormalStr).isDenormal());
658 EXPECT_FALSE(APFloat(APFloat::IEEEquad(), 0).isDenormal());
660 APFloat Val2(APFloat::IEEEquad(), 2);
661 APFloat T(APFloat::IEEEquad(), MinNormalStr);
662 T.divide(Val2, rdmd);
663 EXPECT_TRUE(T.isDenormal());
667 TEST(APFloatTest, Zero) {
668 EXPECT_EQ(0.0f, APFloat(0.0f).convertToFloat());
669 EXPECT_EQ(-0.0f, APFloat(-0.0f).convertToFloat());
670 EXPECT_TRUE(APFloat(-0.0f).isNegative());
672 EXPECT_EQ(0.0, APFloat(0.0).convertToDouble());
673 EXPECT_EQ(-0.0, APFloat(-0.0).convertToDouble());
674 EXPECT_TRUE(APFloat(-0.0).isNegative());
677 TEST(APFloatTest, DecimalStringsWithoutNullTerminators) {
678 // Make sure that we can parse strings without null terminators.
679 // rdar://14323230.
680 EXPECT_EQ(convertToDoubleFromString(StringRef("0.00", 3)), 0.0);
681 EXPECT_EQ(convertToDoubleFromString(StringRef("0.01", 3)), 0.0);
682 EXPECT_EQ(convertToDoubleFromString(StringRef("0.09", 3)), 0.0);
683 EXPECT_EQ(convertToDoubleFromString(StringRef("0.095", 4)), 0.09);
684 EXPECT_EQ(convertToDoubleFromString(StringRef("0.00e+3", 7)), 0.00);
685 EXPECT_EQ(convertToDoubleFromString(StringRef("0e+3", 4)), 0.00);
688 TEST(APFloatTest, fromZeroDecimalString) {
689 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0").convertToDouble());
690 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0").convertToDouble());
691 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0").convertToDouble());
693 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.").convertToDouble());
694 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.").convertToDouble());
695 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.").convertToDouble());
697 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), ".0").convertToDouble());
698 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+.0").convertToDouble());
699 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-.0").convertToDouble());
701 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.0").convertToDouble());
702 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.0").convertToDouble());
703 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.0").convertToDouble());
705 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "00000.").convertToDouble());
706 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+00000.").convertToDouble());
707 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-00000.").convertToDouble());
709 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble(), ".00000").convertToDouble());
710 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+.00000").convertToDouble());
711 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-.00000").convertToDouble());
713 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0000.00000").convertToDouble());
714 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0000.00000").convertToDouble());
715 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0000.00000").convertToDouble());
718 TEST(APFloatTest, fromZeroDecimalSingleExponentString) {
719 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e1").convertToDouble());
720 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e1").convertToDouble());
721 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e1").convertToDouble());
723 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e+1").convertToDouble());
724 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e+1").convertToDouble());
725 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e+1").convertToDouble());
727 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e-1").convertToDouble());
728 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e-1").convertToDouble());
729 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e-1").convertToDouble());
732 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.e1").convertToDouble());
733 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.e1").convertToDouble());
734 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.e1").convertToDouble());
736 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.e+1").convertToDouble());
737 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.e+1").convertToDouble());
738 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.e+1").convertToDouble());
740 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.e-1").convertToDouble());
741 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.e-1").convertToDouble());
742 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.e-1").convertToDouble());
744 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), ".0e1").convertToDouble());
745 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+.0e1").convertToDouble());
746 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-.0e1").convertToDouble());
748 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), ".0e+1").convertToDouble());
749 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+.0e+1").convertToDouble());
750 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-.0e+1").convertToDouble());
752 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), ".0e-1").convertToDouble());
753 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+.0e-1").convertToDouble());
754 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-.0e-1").convertToDouble());
757 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.0e1").convertToDouble());
758 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.0e1").convertToDouble());
759 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.0e1").convertToDouble());
761 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.0e+1").convertToDouble());
762 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.0e+1").convertToDouble());
763 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.0e+1").convertToDouble());
765 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.0e-1").convertToDouble());
766 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.0e-1").convertToDouble());
767 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.0e-1").convertToDouble());
770 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "000.0000e1").convertToDouble());
771 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+000.0000e+1").convertToDouble());
772 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-000.0000e+1").convertToDouble());
775 TEST(APFloatTest, fromZeroDecimalLargeExponentString) {
776 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e1234").convertToDouble());
777 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e1234").convertToDouble());
778 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e1234").convertToDouble());
780 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e+1234").convertToDouble());
781 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e+1234").convertToDouble());
782 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e+1234").convertToDouble());
784 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e-1234").convertToDouble());
785 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e-1234").convertToDouble());
786 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e-1234").convertToDouble());
788 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble(), "000.0000e1234").convertToDouble());
789 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble(), "000.0000e-1234").convertToDouble());
791 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble(), StringRef("0e1234" "\0" "2", 6)).convertToDouble());
794 TEST(APFloatTest, fromZeroHexadecimalString) {
795 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0p1").convertToDouble());
796 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0p1").convertToDouble());
797 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0p1").convertToDouble());
799 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0p+1").convertToDouble());
800 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0p+1").convertToDouble());
801 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0p+1").convertToDouble());
803 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0p-1").convertToDouble());
804 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0p-1").convertToDouble());
805 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0p-1").convertToDouble());
808 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.p1").convertToDouble());
809 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.p1").convertToDouble());
810 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.p1").convertToDouble());
812 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.p+1").convertToDouble());
813 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.p+1").convertToDouble());
814 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.p+1").convertToDouble());
816 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.p-1").convertToDouble());
817 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.p-1").convertToDouble());
818 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.p-1").convertToDouble());
821 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x.0p1").convertToDouble());
822 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x.0p1").convertToDouble());
823 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x.0p1").convertToDouble());
825 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x.0p+1").convertToDouble());
826 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x.0p+1").convertToDouble());
827 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x.0p+1").convertToDouble());
829 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x.0p-1").convertToDouble());
830 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x.0p-1").convertToDouble());
831 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x.0p-1").convertToDouble());
834 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.0p1").convertToDouble());
835 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.0p1").convertToDouble());
836 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.0p1").convertToDouble());
838 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.0p+1").convertToDouble());
839 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.0p+1").convertToDouble());
840 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.0p+1").convertToDouble());
842 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.0p-1").convertToDouble());
843 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.0p-1").convertToDouble());
844 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.0p-1").convertToDouble());
847 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x00000.p1").convertToDouble());
848 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0000.00000p1").convertToDouble());
849 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x.00000p1").convertToDouble());
850 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.p1").convertToDouble());
851 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0p1234").convertToDouble());
852 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0p1234").convertToDouble());
853 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x00000.p1234").convertToDouble());
854 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0000.00000p1234").convertToDouble());
855 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x.00000p1234").convertToDouble());
856 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.p1234").convertToDouble());
859 TEST(APFloatTest, fromDecimalString) {
860 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1").convertToDouble());
861 EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble(), "2.").convertToDouble());
862 EXPECT_EQ(0.5, APFloat(APFloat::IEEEdouble(), ".5").convertToDouble());
863 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0").convertToDouble());
864 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble(), "-2").convertToDouble());
865 EXPECT_EQ(-4.0, APFloat(APFloat::IEEEdouble(), "-4.").convertToDouble());
866 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble(), "-.5").convertToDouble());
867 EXPECT_EQ(-1.5, APFloat(APFloat::IEEEdouble(), "-1.5").convertToDouble());
868 EXPECT_EQ(1.25e12, APFloat(APFloat::IEEEdouble(), "1.25e12").convertToDouble());
869 EXPECT_EQ(1.25e+12, APFloat(APFloat::IEEEdouble(), "1.25e+12").convertToDouble());
870 EXPECT_EQ(1.25e-12, APFloat(APFloat::IEEEdouble(), "1.25e-12").convertToDouble());
871 EXPECT_EQ(1024.0, APFloat(APFloat::IEEEdouble(), "1024.").convertToDouble());
872 EXPECT_EQ(1024.05, APFloat(APFloat::IEEEdouble(), "1024.05000").convertToDouble());
873 EXPECT_EQ(0.05, APFloat(APFloat::IEEEdouble(), ".05000").convertToDouble());
874 EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble(), "2.").convertToDouble());
875 EXPECT_EQ(2.0e2, APFloat(APFloat::IEEEdouble(), "2.e2").convertToDouble());
876 EXPECT_EQ(2.0e+2, APFloat(APFloat::IEEEdouble(), "2.e+2").convertToDouble());
877 EXPECT_EQ(2.0e-2, APFloat(APFloat::IEEEdouble(), "2.e-2").convertToDouble());
878 EXPECT_EQ(2.05e2, APFloat(APFloat::IEEEdouble(), "002.05000e2").convertToDouble());
879 EXPECT_EQ(2.05e+2, APFloat(APFloat::IEEEdouble(), "002.05000e+2").convertToDouble());
880 EXPECT_EQ(2.05e-2, APFloat(APFloat::IEEEdouble(), "002.05000e-2").convertToDouble());
881 EXPECT_EQ(2.05e12, APFloat(APFloat::IEEEdouble(), "002.05000e12").convertToDouble());
882 EXPECT_EQ(2.05e+12, APFloat(APFloat::IEEEdouble(), "002.05000e+12").convertToDouble());
883 EXPECT_EQ(2.05e-12, APFloat(APFloat::IEEEdouble(), "002.05000e-12").convertToDouble());
885 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1e").convertToDouble());
886 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "+1e").convertToDouble());
887 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-1e").convertToDouble());
889 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.e").convertToDouble());
890 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "+1.e").convertToDouble());
891 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-1.e").convertToDouble());
893 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e").convertToDouble());
894 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), "+.1e").convertToDouble());
895 EXPECT_EQ(-0.1, APFloat(APFloat::IEEEdouble(), "-.1e").convertToDouble());
897 EXPECT_EQ(1.1, APFloat(APFloat::IEEEdouble(), "1.1e").convertToDouble());
898 EXPECT_EQ(1.1, APFloat(APFloat::IEEEdouble(), "+1.1e").convertToDouble());
899 EXPECT_EQ(-1.1, APFloat(APFloat::IEEEdouble(), "-1.1e").convertToDouble());
901 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1e+").convertToDouble());
902 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1e-").convertToDouble());
904 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e").convertToDouble());
905 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e+").convertToDouble());
906 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e-").convertToDouble());
908 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0e").convertToDouble());
909 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0e+").convertToDouble());
910 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0e-").convertToDouble());
912 // These are "carefully selected" to overflow the fast log-base
913 // calculations in APFloat.cpp
914 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "99e99999").isInfinity());
915 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-99e99999").isInfinity());
916 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "1e-99999").isPosZero());
917 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-1e-99999").isNegZero());
919 EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
922 TEST(APFloatTest, fromStringSpecials) {
923 const fltSemantics &Sem = APFloat::IEEEdouble();
924 const unsigned Precision = 53;
925 const unsigned PayloadBits = Precision - 2;
926 uint64_t PayloadMask = (uint64_t(1) << PayloadBits) - uint64_t(1);
928 uint64_t NaNPayloads[] = {
931 123,
932 0xDEADBEEF,
933 uint64_t(-2),
934 uint64_t(1) << PayloadBits, // overflow bit
935 uint64_t(1) << (PayloadBits - 1), // signaling bit
936 uint64_t(1) << (PayloadBits - 2) // highest possible bit
939 // Convert payload integer to decimal string representation.
940 std::string NaNPayloadDecStrings[std::size(NaNPayloads)];
941 for (size_t I = 0; I < std::size(NaNPayloads); ++I)
942 NaNPayloadDecStrings[I] = utostr(NaNPayloads[I]);
944 // Convert payload integer to hexadecimal string representation.
945 std::string NaNPayloadHexStrings[std::size(NaNPayloads)];
946 for (size_t I = 0; I < std::size(NaNPayloads); ++I)
947 NaNPayloadHexStrings[I] = "0x" + utohexstr(NaNPayloads[I]);
949 // Fix payloads to expected result.
950 for (uint64_t &Payload : NaNPayloads)
951 Payload &= PayloadMask;
953 // Signaling NaN must have a non-zero payload. In case a zero payload is
954 // requested, a default arbitrary payload is set instead. Save this payload
955 // for testing.
956 const uint64_t SNaNDefaultPayload =
957 APFloat::getSNaN(Sem).bitcastToAPInt().getZExtValue() & PayloadMask;
959 // Negative sign prefix (or none - for positive).
960 const char Signs[] = {0, '-'};
962 // "Signaling" prefix (or none - for "Quiet").
963 const char NaNTypes[] = {0, 's', 'S'};
965 const StringRef NaNStrings[] = {"nan", "NaN"};
966 for (StringRef NaNStr : NaNStrings)
967 for (char TypeChar : NaNTypes) {
968 bool Signaling = (TypeChar == 's' || TypeChar == 'S');
970 for (size_t J = 0; J < std::size(NaNPayloads); ++J) {
971 uint64_t Payload = (Signaling && !NaNPayloads[J]) ? SNaNDefaultPayload
972 : NaNPayloads[J];
973 std::string &PayloadDec = NaNPayloadDecStrings[J];
974 std::string &PayloadHex = NaNPayloadHexStrings[J];
976 for (char SignChar : Signs) {
977 bool Negative = (SignChar == '-');
979 std::string TestStrings[5];
980 size_t NumTestStrings = 0;
982 std::string Prefix;
983 if (SignChar)
984 Prefix += SignChar;
985 if (TypeChar)
986 Prefix += TypeChar;
987 Prefix += NaNStr;
989 // Test without any paylod.
990 if (!Payload)
991 TestStrings[NumTestStrings++] = Prefix;
993 // Test with the payload as a suffix.
994 TestStrings[NumTestStrings++] = Prefix + PayloadDec;
995 TestStrings[NumTestStrings++] = Prefix + PayloadHex;
997 // Test with the payload inside parentheses.
998 TestStrings[NumTestStrings++] = Prefix + '(' + PayloadDec + ')';
999 TestStrings[NumTestStrings++] = Prefix + '(' + PayloadHex + ')';
1001 for (size_t K = 0; K < NumTestStrings; ++K) {
1002 StringRef TestStr = TestStrings[K];
1004 APFloat F(Sem);
1005 bool HasError = !F.convertFromString(
1006 TestStr, llvm::APFloat::rmNearestTiesToEven);
1007 EXPECT_FALSE(HasError);
1008 EXPECT_TRUE(F.isNaN());
1009 EXPECT_EQ(Signaling, F.isSignaling());
1010 EXPECT_EQ(Negative, F.isNegative());
1011 uint64_t PayloadResult =
1012 F.bitcastToAPInt().getZExtValue() & PayloadMask;
1013 EXPECT_EQ(Payload, PayloadResult);
1019 const StringRef InfStrings[] = {"inf", "INFINITY", "+Inf",
1020 "-inf", "-INFINITY", "-Inf"};
1021 for (StringRef InfStr : InfStrings) {
1022 bool Negative = InfStr.front() == '-';
1024 APFloat F(Sem);
1025 bool HasError =
1026 !F.convertFromString(InfStr, llvm::APFloat::rmNearestTiesToEven);
1027 EXPECT_FALSE(HasError);
1028 EXPECT_TRUE(F.isInfinity());
1029 EXPECT_EQ(Negative, F.isNegative());
1030 uint64_t PayloadResult = F.bitcastToAPInt().getZExtValue() & PayloadMask;
1031 EXPECT_EQ(UINT64_C(0), PayloadResult);
1035 TEST(APFloatTest, fromToStringSpecials) {
1036 auto expects = [] (const char *first, const char *second) {
1037 std::string roundtrip = convertToString(convertToDoubleFromString(second), 0, 3);
1038 EXPECT_STREQ(first, roundtrip.c_str());
1040 expects("+Inf", "+Inf");
1041 expects("+Inf", "INFINITY");
1042 expects("+Inf", "inf");
1043 expects("-Inf", "-Inf");
1044 expects("-Inf", "-INFINITY");
1045 expects("-Inf", "-inf");
1046 expects("NaN", "NaN");
1047 expects("NaN", "nan");
1048 expects("NaN", "-NaN");
1049 expects("NaN", "-nan");
1052 TEST(APFloatTest, fromHexadecimalString) {
1053 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(), "0x1p0").convertToDouble());
1054 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p0").convertToDouble());
1055 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-0x1p0").convertToDouble());
1057 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(), "0x1p+0").convertToDouble());
1058 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p+0").convertToDouble());
1059 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-0x1p+0").convertToDouble());
1061 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(), "0x1p-0").convertToDouble());
1062 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p-0").convertToDouble());
1063 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-0x1p-0").convertToDouble());
1066 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble(), "0x1p1").convertToDouble());
1067 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble(), "+0x1p1").convertToDouble());
1068 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble(), "-0x1p1").convertToDouble());
1070 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble(), "0x1p+1").convertToDouble());
1071 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble(), "+0x1p+1").convertToDouble());
1072 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble(), "-0x1p+1").convertToDouble());
1074 EXPECT_EQ( 0.5, APFloat(APFloat::IEEEdouble(), "0x1p-1").convertToDouble());
1075 EXPECT_EQ(+0.5, APFloat(APFloat::IEEEdouble(), "+0x1p-1").convertToDouble());
1076 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble(), "-0x1p-1").convertToDouble());
1079 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble(), "0x1.8p1").convertToDouble());
1080 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble(), "+0x1.8p1").convertToDouble());
1081 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble(), "-0x1.8p1").convertToDouble());
1083 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble(), "0x1.8p+1").convertToDouble());
1084 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble(), "+0x1.8p+1").convertToDouble());
1085 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble(), "-0x1.8p+1").convertToDouble());
1087 EXPECT_EQ( 0.75, APFloat(APFloat::IEEEdouble(), "0x1.8p-1").convertToDouble());
1088 EXPECT_EQ(+0.75, APFloat(APFloat::IEEEdouble(), "+0x1.8p-1").convertToDouble());
1089 EXPECT_EQ(-0.75, APFloat(APFloat::IEEEdouble(), "-0x1.8p-1").convertToDouble());
1092 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000.000p1").convertToDouble());
1093 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000.000p1").convertToDouble());
1094 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000.000p1").convertToDouble());
1096 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000.000p+1").convertToDouble());
1097 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000.000p+1").convertToDouble());
1098 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000.000p+1").convertToDouble());
1100 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble(), "0x1000.000p-1").convertToDouble());
1101 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble(), "+0x1000.000p-1").convertToDouble());
1102 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble(), "-0x1000.000p-1").convertToDouble());
1105 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000p1").convertToDouble());
1106 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000p1").convertToDouble());
1107 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000p1").convertToDouble());
1109 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000p+1").convertToDouble());
1110 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000p+1").convertToDouble());
1111 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000p+1").convertToDouble());
1113 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble(), "0x1000p-1").convertToDouble());
1114 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble(), "+0x1000p-1").convertToDouble());
1115 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble(), "-0x1000p-1").convertToDouble());
1118 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble(), "0x10p10").convertToDouble());
1119 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble(), "+0x10p10").convertToDouble());
1120 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble(), "-0x10p10").convertToDouble());
1122 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble(), "0x10p+10").convertToDouble());
1123 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble(), "+0x10p+10").convertToDouble());
1124 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble(), "-0x10p+10").convertToDouble());
1126 EXPECT_EQ( 0.015625, APFloat(APFloat::IEEEdouble(), "0x10p-10").convertToDouble());
1127 EXPECT_EQ(+0.015625, APFloat(APFloat::IEEEdouble(), "+0x10p-10").convertToDouble());
1128 EXPECT_EQ(-0.015625, APFloat(APFloat::IEEEdouble(), "-0x10p-10").convertToDouble());
1130 EXPECT_EQ(1.0625, APFloat(APFloat::IEEEdouble(), "0x1.1p0").convertToDouble());
1131 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "0x1p0").convertToDouble());
1133 EXPECT_EQ(convertToDoubleFromString("0x1p-150"),
1134 convertToDoubleFromString("+0x800000000000000001.p-221"));
1135 EXPECT_EQ(2251799813685248.5,
1136 convertToDoubleFromString("0x80000000000004000000.010p-28"));
1139 TEST(APFloatTest, toString) {
1140 ASSERT_EQ("10", convertToString(10.0, 6, 3));
1141 ASSERT_EQ("1.0E+1", convertToString(10.0, 6, 0));
1142 ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2));
1143 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 4, 2));
1144 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 5, 1));
1145 ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2));
1146 ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2));
1147 ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 5, 1));
1148 ASSERT_EQ("0.78539816339744828", convertToString(0.78539816339744830961, 0, 3));
1149 ASSERT_EQ("4.9406564584124654E-324", convertToString(4.9406564584124654e-324, 0, 3));
1150 ASSERT_EQ("873.18340000000001", convertToString(873.1834, 0, 1));
1151 ASSERT_EQ("8.7318340000000001E+2", convertToString(873.1834, 0, 0));
1152 ASSERT_EQ("1.7976931348623157E+308", convertToString(1.7976931348623157E+308, 0, 0));
1153 ASSERT_EQ("10", convertToString(10.0, 6, 3, false));
1154 ASSERT_EQ("1.000000e+01", convertToString(10.0, 6, 0, false));
1155 ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2, false));
1156 ASSERT_EQ("1.0100e+04", convertToString(1.01E+4, 4, 2, false));
1157 ASSERT_EQ("1.01000e+04", convertToString(1.01E+4, 5, 1, false));
1158 ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2, false));
1159 ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2, false));
1160 ASSERT_EQ("1.01000e-02", convertToString(1.01E-2, 5, 1, false));
1161 ASSERT_EQ("0.78539816339744828",
1162 convertToString(0.78539816339744830961, 0, 3, false));
1163 ASSERT_EQ("4.94065645841246540e-324",
1164 convertToString(4.9406564584124654e-324, 0, 3, false));
1165 ASSERT_EQ("873.18340000000001", convertToString(873.1834, 0, 1, false));
1166 ASSERT_EQ("8.73183400000000010e+02", convertToString(873.1834, 0, 0, false));
1167 ASSERT_EQ("1.79769313486231570e+308",
1168 convertToString(1.7976931348623157E+308, 0, 0, false));
1171 SmallString<64> Str;
1172 APFloat UnnormalZero(APFloat::x87DoubleExtended(), APInt(80, {0, 1}));
1173 UnnormalZero.toString(Str);
1174 ASSERT_EQ("NaN", Str);
1178 TEST(APFloatTest, toInteger) {
1179 bool isExact = false;
1180 APSInt result(5, /*isUnsigned=*/true);
1182 EXPECT_EQ(APFloat::opOK,
1183 APFloat(APFloat::IEEEdouble(), "10")
1184 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1185 EXPECT_TRUE(isExact);
1186 EXPECT_EQ(APSInt(APInt(5, 10), true), result);
1188 EXPECT_EQ(APFloat::opInvalidOp,
1189 APFloat(APFloat::IEEEdouble(), "-10")
1190 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1191 EXPECT_FALSE(isExact);
1192 EXPECT_EQ(APSInt::getMinValue(5, true), result);
1194 EXPECT_EQ(APFloat::opInvalidOp,
1195 APFloat(APFloat::IEEEdouble(), "32")
1196 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1197 EXPECT_FALSE(isExact);
1198 EXPECT_EQ(APSInt::getMaxValue(5, true), result);
1200 EXPECT_EQ(APFloat::opInexact,
1201 APFloat(APFloat::IEEEdouble(), "7.9")
1202 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1203 EXPECT_FALSE(isExact);
1204 EXPECT_EQ(APSInt(APInt(5, 7), true), result);
1206 result.setIsUnsigned(false);
1207 EXPECT_EQ(APFloat::opOK,
1208 APFloat(APFloat::IEEEdouble(), "-10")
1209 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1210 EXPECT_TRUE(isExact);
1211 EXPECT_EQ(APSInt(APInt(5, -10, true), false), result);
1213 EXPECT_EQ(APFloat::opInvalidOp,
1214 APFloat(APFloat::IEEEdouble(), "-17")
1215 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1216 EXPECT_FALSE(isExact);
1217 EXPECT_EQ(APSInt::getMinValue(5, false), result);
1219 EXPECT_EQ(APFloat::opInvalidOp,
1220 APFloat(APFloat::IEEEdouble(), "16")
1221 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1222 EXPECT_FALSE(isExact);
1223 EXPECT_EQ(APSInt::getMaxValue(5, false), result);
1226 static APInt nanbitsFromAPInt(const fltSemantics &Sem, bool SNaN, bool Negative,
1227 uint64_t payload) {
1228 APInt appayload(64, payload);
1229 if (SNaN)
1230 return APFloat::getSNaN(Sem, Negative, &appayload).bitcastToAPInt();
1231 else
1232 return APFloat::getQNaN(Sem, Negative, &appayload).bitcastToAPInt();
1235 TEST(APFloatTest, makeNaN) {
1236 const struct {
1237 uint64_t expected;
1238 const fltSemantics &semantics;
1239 bool SNaN;
1240 bool Negative;
1241 uint64_t payload;
1242 } tests[] = {
1243 /* expected semantics SNaN Neg payload */
1244 { 0x7fc00000ULL, APFloat::IEEEsingle(), false, false, 0x00000000ULL },
1245 { 0xffc00000ULL, APFloat::IEEEsingle(), false, true, 0x00000000ULL },
1246 { 0x7fc0ae72ULL, APFloat::IEEEsingle(), false, false, 0x0000ae72ULL },
1247 { 0x7fffae72ULL, APFloat::IEEEsingle(), false, false, 0xffffae72ULL },
1248 { 0x7fdaae72ULL, APFloat::IEEEsingle(), false, false, 0x00daae72ULL },
1249 { 0x7fa00000ULL, APFloat::IEEEsingle(), true, false, 0x00000000ULL },
1250 { 0xffa00000ULL, APFloat::IEEEsingle(), true, true, 0x00000000ULL },
1251 { 0x7f80ae72ULL, APFloat::IEEEsingle(), true, false, 0x0000ae72ULL },
1252 { 0x7fbfae72ULL, APFloat::IEEEsingle(), true, false, 0xffffae72ULL },
1253 { 0x7f9aae72ULL, APFloat::IEEEsingle(), true, false, 0x001aae72ULL },
1254 { 0x7ff8000000000000ULL, APFloat::IEEEdouble(), false, false, 0x0000000000000000ULL },
1255 { 0xfff8000000000000ULL, APFloat::IEEEdouble(), false, true, 0x0000000000000000ULL },
1256 { 0x7ff800000000ae72ULL, APFloat::IEEEdouble(), false, false, 0x000000000000ae72ULL },
1257 { 0x7fffffffffffae72ULL, APFloat::IEEEdouble(), false, false, 0xffffffffffffae72ULL },
1258 { 0x7ffdaaaaaaaaae72ULL, APFloat::IEEEdouble(), false, false, 0x000daaaaaaaaae72ULL },
1259 { 0x7ff4000000000000ULL, APFloat::IEEEdouble(), true, false, 0x0000000000000000ULL },
1260 { 0xfff4000000000000ULL, APFloat::IEEEdouble(), true, true, 0x0000000000000000ULL },
1261 { 0x7ff000000000ae72ULL, APFloat::IEEEdouble(), true, false, 0x000000000000ae72ULL },
1262 { 0x7ff7ffffffffae72ULL, APFloat::IEEEdouble(), true, false, 0xffffffffffffae72ULL },
1263 { 0x7ff1aaaaaaaaae72ULL, APFloat::IEEEdouble(), true, false, 0x0001aaaaaaaaae72ULL },
1266 for (const auto &t : tests) {
1267 ASSERT_EQ(t.expected, nanbitsFromAPInt(t.semantics, t.SNaN, t.Negative, t.payload));
1271 #ifdef GTEST_HAS_DEATH_TEST
1272 #ifndef NDEBUG
1273 TEST(APFloatTest, SemanticsDeath) {
1274 EXPECT_DEATH(APFloat(APFloat::IEEEquad(), 0).convertToDouble(),
1275 "Float semantics is not representable by IEEEdouble");
1276 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), 0).convertToFloat(),
1277 "Float semantics is not representable by IEEEsingle");
1279 #endif
1280 #endif
1282 TEST(APFloatTest, StringDecimalError) {
1283 EXPECT_EQ("Invalid string length", convertToErrorFromString(""));
1284 EXPECT_EQ("String has no digits", convertToErrorFromString("+"));
1285 EXPECT_EQ("String has no digits", convertToErrorFromString("-"));
1287 EXPECT_EQ("Invalid character in significand", convertToErrorFromString(StringRef("\0", 1)));
1288 EXPECT_EQ("Invalid character in significand", convertToErrorFromString(StringRef("1\0", 2)));
1289 EXPECT_EQ("Invalid character in significand", convertToErrorFromString(StringRef("1" "\0" "2", 3)));
1290 EXPECT_EQ("Invalid character in significand", convertToErrorFromString(StringRef("1" "\0" "2e1", 5)));
1291 EXPECT_EQ("Invalid character in exponent", convertToErrorFromString(StringRef("1e\0", 3)));
1292 EXPECT_EQ("Invalid character in exponent", convertToErrorFromString(StringRef("1e1\0", 4)));
1293 EXPECT_EQ("Invalid character in exponent", convertToErrorFromString(StringRef("1e1" "\0" "2", 5)));
1295 EXPECT_EQ("Invalid character in significand", convertToErrorFromString("1.0f"));
1297 EXPECT_EQ("String contains multiple dots", convertToErrorFromString(".."));
1298 EXPECT_EQ("String contains multiple dots", convertToErrorFromString("..0"));
1299 EXPECT_EQ("String contains multiple dots", convertToErrorFromString("1.0.0"));
1302 TEST(APFloatTest, StringDecimalSignificandError) {
1303 EXPECT_EQ("Significand has no digits", convertToErrorFromString( "."));
1304 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+."));
1305 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-."));
1308 EXPECT_EQ("Significand has no digits", convertToErrorFromString( "e"));
1309 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+e"));
1310 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-e"));
1312 EXPECT_EQ("Significand has no digits", convertToErrorFromString( "e1"));
1313 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+e1"));
1314 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-e1"));
1316 EXPECT_EQ("Significand has no digits", convertToErrorFromString( ".e1"));
1317 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+.e1"));
1318 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-.e1"));
1321 EXPECT_EQ("Significand has no digits", convertToErrorFromString( ".e"));
1322 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+.e"));
1323 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-.e"));
1326 TEST(APFloatTest, StringHexadecimalError) {
1327 EXPECT_EQ("Invalid string", convertToErrorFromString( "0x"));
1328 EXPECT_EQ("Invalid string", convertToErrorFromString("+0x"));
1329 EXPECT_EQ("Invalid string", convertToErrorFromString("-0x"));
1331 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString( "0x0"));
1332 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString("+0x0"));
1333 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString("-0x0"));
1335 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString( "0x0."));
1336 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString("+0x0."));
1337 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString("-0x0."));
1339 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString( "0x.0"));
1340 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString("+0x.0"));
1341 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString("-0x.0"));
1343 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString( "0x0.0"));
1344 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString("+0x0.0"));
1345 EXPECT_EQ("Hex strings require an exponent", convertToErrorFromString("-0x0.0"));
1347 EXPECT_EQ("Invalid character in significand", convertToErrorFromString(StringRef("0x\0", 3)));
1348 EXPECT_EQ("Invalid character in significand", convertToErrorFromString(StringRef("0x1\0", 4)));
1349 EXPECT_EQ("Invalid character in significand", convertToErrorFromString(StringRef("0x1" "\0" "2", 5)));
1350 EXPECT_EQ("Invalid character in significand", convertToErrorFromString(StringRef("0x1" "\0" "2p1", 7)));
1351 EXPECT_EQ("Invalid character in exponent", convertToErrorFromString(StringRef("0x1p\0", 5)));
1352 EXPECT_EQ("Invalid character in exponent", convertToErrorFromString(StringRef("0x1p1\0", 6)));
1353 EXPECT_EQ("Invalid character in exponent", convertToErrorFromString(StringRef("0x1p1" "\0" "2", 7)));
1355 EXPECT_EQ("Invalid character in exponent", convertToErrorFromString("0x1p0f"));
1357 EXPECT_EQ("String contains multiple dots", convertToErrorFromString("0x..p1"));
1358 EXPECT_EQ("String contains multiple dots", convertToErrorFromString("0x..0p1"));
1359 EXPECT_EQ("String contains multiple dots", convertToErrorFromString("0x1.0.0p1"));
1362 TEST(APFloatTest, StringHexadecimalSignificandError) {
1363 EXPECT_EQ("Significand has no digits", convertToErrorFromString( "0x."));
1364 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+0x."));
1365 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-0x."));
1367 EXPECT_EQ("Significand has no digits", convertToErrorFromString( "0xp"));
1368 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+0xp"));
1369 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-0xp"));
1371 EXPECT_EQ("Significand has no digits", convertToErrorFromString( "0xp+"));
1372 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+0xp+"));
1373 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-0xp+"));
1375 EXPECT_EQ("Significand has no digits", convertToErrorFromString( "0xp-"));
1376 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+0xp-"));
1377 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-0xp-"));
1380 EXPECT_EQ("Significand has no digits", convertToErrorFromString( "0x.p"));
1381 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+0x.p"));
1382 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-0x.p"));
1384 EXPECT_EQ("Significand has no digits", convertToErrorFromString( "0x.p+"));
1385 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+0x.p+"));
1386 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-0x.p+"));
1388 EXPECT_EQ("Significand has no digits", convertToErrorFromString( "0x.p-"));
1389 EXPECT_EQ("Significand has no digits", convertToErrorFromString("+0x.p-"));
1390 EXPECT_EQ("Significand has no digits", convertToErrorFromString("-0x.p-"));
1393 TEST(APFloatTest, StringHexadecimalExponentError) {
1394 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x1p"));
1395 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x1p"));
1396 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x1p"));
1398 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x1p+"));
1399 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x1p+"));
1400 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x1p+"));
1402 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x1p-"));
1403 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x1p-"));
1404 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x1p-"));
1407 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x1.p"));
1408 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x1.p"));
1409 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x1.p"));
1411 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x1.p+"));
1412 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x1.p+"));
1413 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x1.p+"));
1415 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x1.p-"));
1416 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x1.p-"));
1417 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x1.p-"));
1420 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x.1p"));
1421 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x.1p"));
1422 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x.1p"));
1424 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x.1p+"));
1425 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x.1p+"));
1426 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x.1p+"));
1428 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x.1p-"));
1429 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x.1p-"));
1430 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x.1p-"));
1433 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x1.1p"));
1434 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x1.1p"));
1435 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x1.1p"));
1437 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x1.1p+"));
1438 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x1.1p+"));
1439 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x1.1p+"));
1441 EXPECT_EQ("Exponent has no digits", convertToErrorFromString( "0x1.1p-"));
1442 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("+0x1.1p-"));
1443 EXPECT_EQ("Exponent has no digits", convertToErrorFromString("-0x1.1p-"));
1446 TEST(APFloatTest, exactInverse) {
1447 APFloat inv(0.0f);
1449 // Trivial operation.
1450 EXPECT_TRUE(APFloat(2.0).getExactInverse(&inv));
1451 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5)));
1452 EXPECT_TRUE(APFloat(2.0f).getExactInverse(&inv));
1453 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5f)));
1454 EXPECT_TRUE(APFloat(APFloat::IEEEquad(), "2.0").getExactInverse(&inv));
1455 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::IEEEquad(), "0.5")));
1456 EXPECT_TRUE(APFloat(APFloat::PPCDoubleDouble(), "2.0").getExactInverse(&inv));
1457 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::PPCDoubleDouble(), "0.5")));
1458 EXPECT_TRUE(APFloat(APFloat::x87DoubleExtended(), "2.0").getExactInverse(&inv));
1459 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::x87DoubleExtended(), "0.5")));
1461 // FLT_MIN
1462 EXPECT_TRUE(APFloat(1.17549435e-38f).getExactInverse(&inv));
1463 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(8.5070592e+37f)));
1465 // Large float, inverse is a denormal.
1466 EXPECT_FALSE(APFloat(1.7014118e38f).getExactInverse(nullptr));
1467 // Zero
1468 EXPECT_FALSE(APFloat(0.0).getExactInverse(nullptr));
1469 // Denormalized float
1470 EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(nullptr));
1473 TEST(APFloatTest, roundToIntegral) {
1474 APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble())), P(0.0);
1476 P = T;
1477 P.roundToIntegral(APFloat::rmTowardZero);
1478 EXPECT_EQ(-0.0, P.convertToDouble());
1479 P = T;
1480 P.roundToIntegral(APFloat::rmTowardNegative);
1481 EXPECT_EQ(-1.0, P.convertToDouble());
1482 P = T;
1483 P.roundToIntegral(APFloat::rmTowardPositive);
1484 EXPECT_EQ(-0.0, P.convertToDouble());
1485 P = T;
1486 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1487 EXPECT_EQ(-0.0, P.convertToDouble());
1489 P = S;
1490 P.roundToIntegral(APFloat::rmTowardZero);
1491 EXPECT_EQ(3.0, P.convertToDouble());
1492 P = S;
1493 P.roundToIntegral(APFloat::rmTowardNegative);
1494 EXPECT_EQ(3.0, P.convertToDouble());
1495 P = S;
1496 P.roundToIntegral(APFloat::rmTowardPositive);
1497 EXPECT_EQ(4.0, P.convertToDouble());
1498 P = S;
1499 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1500 EXPECT_EQ(3.0, P.convertToDouble());
1502 P = R;
1503 P.roundToIntegral(APFloat::rmTowardZero);
1504 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1505 P = R;
1506 P.roundToIntegral(APFloat::rmTowardNegative);
1507 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1508 P = R;
1509 P.roundToIntegral(APFloat::rmTowardPositive);
1510 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1511 P = R;
1512 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1513 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1515 P = APFloat::getZero(APFloat::IEEEdouble());
1516 P.roundToIntegral(APFloat::rmTowardZero);
1517 EXPECT_EQ(0.0, P.convertToDouble());
1518 P = APFloat::getZero(APFloat::IEEEdouble(), true);
1519 P.roundToIntegral(APFloat::rmTowardZero);
1520 EXPECT_EQ(-0.0, P.convertToDouble());
1521 P = APFloat::getNaN(APFloat::IEEEdouble());
1522 P.roundToIntegral(APFloat::rmTowardZero);
1523 EXPECT_TRUE(std::isnan(P.convertToDouble()));
1524 P = APFloat::getInf(APFloat::IEEEdouble());
1525 P.roundToIntegral(APFloat::rmTowardZero);
1526 EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() > 0.0);
1527 P = APFloat::getInf(APFloat::IEEEdouble(), true);
1528 P.roundToIntegral(APFloat::rmTowardZero);
1529 EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() < 0.0);
1531 APFloat::opStatus St;
1533 P = APFloat::getNaN(APFloat::IEEEdouble());
1534 St = P.roundToIntegral(APFloat::rmTowardZero);
1535 EXPECT_TRUE(P.isNaN());
1536 EXPECT_FALSE(P.isNegative());
1537 EXPECT_EQ(APFloat::opOK, St);
1539 P = APFloat::getNaN(APFloat::IEEEdouble(), true);
1540 St = P.roundToIntegral(APFloat::rmTowardZero);
1541 EXPECT_TRUE(P.isNaN());
1542 EXPECT_TRUE(P.isNegative());
1543 EXPECT_EQ(APFloat::opOK, St);
1545 P = APFloat::getSNaN(APFloat::IEEEdouble());
1546 St = P.roundToIntegral(APFloat::rmTowardZero);
1547 EXPECT_TRUE(P.isNaN());
1548 EXPECT_FALSE(P.isSignaling());
1549 EXPECT_FALSE(P.isNegative());
1550 EXPECT_EQ(APFloat::opInvalidOp, St);
1552 P = APFloat::getSNaN(APFloat::IEEEdouble(), true);
1553 St = P.roundToIntegral(APFloat::rmTowardZero);
1554 EXPECT_TRUE(P.isNaN());
1555 EXPECT_FALSE(P.isSignaling());
1556 EXPECT_TRUE(P.isNegative());
1557 EXPECT_EQ(APFloat::opInvalidOp, St);
1559 P = APFloat::getInf(APFloat::IEEEdouble());
1560 St = P.roundToIntegral(APFloat::rmTowardZero);
1561 EXPECT_TRUE(P.isInfinity());
1562 EXPECT_FALSE(P.isNegative());
1563 EXPECT_EQ(APFloat::opOK, St);
1565 P = APFloat::getInf(APFloat::IEEEdouble(), true);
1566 St = P.roundToIntegral(APFloat::rmTowardZero);
1567 EXPECT_TRUE(P.isInfinity());
1568 EXPECT_TRUE(P.isNegative());
1569 EXPECT_EQ(APFloat::opOK, St);
1571 P = APFloat::getZero(APFloat::IEEEdouble(), false);
1572 St = P.roundToIntegral(APFloat::rmTowardZero);
1573 EXPECT_TRUE(P.isZero());
1574 EXPECT_FALSE(P.isNegative());
1575 EXPECT_EQ(APFloat::opOK, St);
1577 P = APFloat::getZero(APFloat::IEEEdouble(), false);
1578 St = P.roundToIntegral(APFloat::rmTowardNegative);
1579 EXPECT_TRUE(P.isZero());
1580 EXPECT_FALSE(P.isNegative());
1581 EXPECT_EQ(APFloat::opOK, St);
1583 P = APFloat::getZero(APFloat::IEEEdouble(), true);
1584 St = P.roundToIntegral(APFloat::rmTowardZero);
1585 EXPECT_TRUE(P.isZero());
1586 EXPECT_TRUE(P.isNegative());
1587 EXPECT_EQ(APFloat::opOK, St);
1589 P = APFloat::getZero(APFloat::IEEEdouble(), true);
1590 St = P.roundToIntegral(APFloat::rmTowardNegative);
1591 EXPECT_TRUE(P.isZero());
1592 EXPECT_TRUE(P.isNegative());
1593 EXPECT_EQ(APFloat::opOK, St);
1595 P = APFloat(1E-100);
1596 St = P.roundToIntegral(APFloat::rmTowardNegative);
1597 EXPECT_TRUE(P.isZero());
1598 EXPECT_FALSE(P.isNegative());
1599 EXPECT_EQ(APFloat::opInexact, St);
1601 P = APFloat(1E-100);
1602 St = P.roundToIntegral(APFloat::rmTowardPositive);
1603 EXPECT_EQ(1.0, P.convertToDouble());
1604 EXPECT_FALSE(P.isNegative());
1605 EXPECT_EQ(APFloat::opInexact, St);
1607 P = APFloat(-1E-100);
1608 St = P.roundToIntegral(APFloat::rmTowardNegative);
1609 EXPECT_TRUE(P.isNegative());
1610 EXPECT_EQ(-1.0, P.convertToDouble());
1611 EXPECT_EQ(APFloat::opInexact, St);
1613 P = APFloat(-1E-100);
1614 St = P.roundToIntegral(APFloat::rmTowardPositive);
1615 EXPECT_TRUE(P.isZero());
1616 EXPECT_TRUE(P.isNegative());
1617 EXPECT_EQ(APFloat::opInexact, St);
1619 P = APFloat(10.0);
1620 St = P.roundToIntegral(APFloat::rmTowardZero);
1621 EXPECT_EQ(10.0, P.convertToDouble());
1622 EXPECT_EQ(APFloat::opOK, St);
1624 P = APFloat(10.5);
1625 St = P.roundToIntegral(APFloat::rmTowardZero);
1626 EXPECT_EQ(10.0, P.convertToDouble());
1627 EXPECT_EQ(APFloat::opInexact, St);
1629 P = APFloat(10.5);
1630 St = P.roundToIntegral(APFloat::rmTowardPositive);
1631 EXPECT_EQ(11.0, P.convertToDouble());
1632 EXPECT_EQ(APFloat::opInexact, St);
1634 P = APFloat(10.5);
1635 St = P.roundToIntegral(APFloat::rmTowardNegative);
1636 EXPECT_EQ(10.0, P.convertToDouble());
1637 EXPECT_EQ(APFloat::opInexact, St);
1639 P = APFloat(10.5);
1640 St = P.roundToIntegral(APFloat::rmNearestTiesToAway);
1641 EXPECT_EQ(11.0, P.convertToDouble());
1642 EXPECT_EQ(APFloat::opInexact, St);
1644 P = APFloat(10.5);
1645 St = P.roundToIntegral(APFloat::rmNearestTiesToEven);
1646 EXPECT_EQ(10.0, P.convertToDouble());
1647 EXPECT_EQ(APFloat::opInexact, St);
1650 TEST(APFloatTest, isInteger) {
1651 APFloat T(-0.0);
1652 EXPECT_TRUE(T.isInteger());
1653 T = APFloat(3.14159);
1654 EXPECT_FALSE(T.isInteger());
1655 T = APFloat::getNaN(APFloat::IEEEdouble());
1656 EXPECT_FALSE(T.isInteger());
1657 T = APFloat::getInf(APFloat::IEEEdouble());
1658 EXPECT_FALSE(T.isInteger());
1659 T = APFloat::getInf(APFloat::IEEEdouble(), true);
1660 EXPECT_FALSE(T.isInteger());
1661 T = APFloat::getLargest(APFloat::IEEEdouble());
1662 EXPECT_TRUE(T.isInteger());
1665 TEST(DoubleAPFloatTest, isInteger) {
1666 APFloat F1(-0.0);
1667 APFloat F2(-0.0);
1668 llvm::detail::DoubleAPFloat T(APFloat::PPCDoubleDouble(), std::move(F1),
1669 std::move(F2));
1670 EXPECT_TRUE(T.isInteger());
1671 APFloat F3(3.14159);
1672 APFloat F4(-0.0);
1673 llvm::detail::DoubleAPFloat T2(APFloat::PPCDoubleDouble(), std::move(F3),
1674 std::move(F4));
1675 EXPECT_FALSE(T2.isInteger());
1676 APFloat F5(-0.0);
1677 APFloat F6(3.14159);
1678 llvm::detail::DoubleAPFloat T3(APFloat::PPCDoubleDouble(), std::move(F5),
1679 std::move(F6));
1680 EXPECT_FALSE(T3.isInteger());
1683 TEST(APFloatTest, getLargest) {
1684 EXPECT_EQ(3.402823466e+38f, APFloat::getLargest(APFloat::IEEEsingle()).convertToFloat());
1685 EXPECT_EQ(1.7976931348623158e+308, APFloat::getLargest(APFloat::IEEEdouble()).convertToDouble());
1688 TEST(APFloatTest, getSmallest) {
1689 APFloat test = APFloat::getSmallest(APFloat::IEEEsingle(), false);
1690 APFloat expected = APFloat(APFloat::IEEEsingle(), "0x0.000002p-126");
1691 EXPECT_FALSE(test.isNegative());
1692 EXPECT_TRUE(test.isFiniteNonZero());
1693 EXPECT_TRUE(test.isDenormal());
1694 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1696 test = APFloat::getSmallest(APFloat::IEEEsingle(), true);
1697 expected = APFloat(APFloat::IEEEsingle(), "-0x0.000002p-126");
1698 EXPECT_TRUE(test.isNegative());
1699 EXPECT_TRUE(test.isFiniteNonZero());
1700 EXPECT_TRUE(test.isDenormal());
1701 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1703 test = APFloat::getSmallest(APFloat::IEEEquad(), false);
1704 expected = APFloat(APFloat::IEEEquad(), "0x0.0000000000000000000000000001p-16382");
1705 EXPECT_FALSE(test.isNegative());
1706 EXPECT_TRUE(test.isFiniteNonZero());
1707 EXPECT_TRUE(test.isDenormal());
1708 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1710 test = APFloat::getSmallest(APFloat::IEEEquad(), true);
1711 expected = APFloat(APFloat::IEEEquad(), "-0x0.0000000000000000000000000001p-16382");
1712 EXPECT_TRUE(test.isNegative());
1713 EXPECT_TRUE(test.isFiniteNonZero());
1714 EXPECT_TRUE(test.isDenormal());
1715 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1718 TEST(APFloatTest, getSmallestNormalized) {
1719 APFloat test = APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
1720 APFloat expected = APFloat(APFloat::IEEEsingle(), "0x1p-126");
1721 EXPECT_FALSE(test.isNegative());
1722 EXPECT_TRUE(test.isFiniteNonZero());
1723 EXPECT_FALSE(test.isDenormal());
1724 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1726 test = APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
1727 expected = APFloat(APFloat::IEEEsingle(), "-0x1p-126");
1728 EXPECT_TRUE(test.isNegative());
1729 EXPECT_TRUE(test.isFiniteNonZero());
1730 EXPECT_FALSE(test.isDenormal());
1731 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1733 test = APFloat::getSmallestNormalized(APFloat::IEEEquad(), false);
1734 expected = APFloat(APFloat::IEEEquad(), "0x1p-16382");
1735 EXPECT_FALSE(test.isNegative());
1736 EXPECT_TRUE(test.isFiniteNonZero());
1737 EXPECT_FALSE(test.isDenormal());
1738 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1740 test = APFloat::getSmallestNormalized(APFloat::IEEEquad(), true);
1741 expected = APFloat(APFloat::IEEEquad(), "-0x1p-16382");
1742 EXPECT_TRUE(test.isNegative());
1743 EXPECT_TRUE(test.isFiniteNonZero());
1744 EXPECT_FALSE(test.isDenormal());
1745 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1748 TEST(APFloatTest, getZero) {
1749 struct {
1750 const fltSemantics *semantics;
1751 const bool sign;
1752 const unsigned long long bitPattern[2];
1753 const unsigned bitPatternLength;
1754 } const GetZeroTest[] = {
1755 { &APFloat::IEEEhalf(), false, {0, 0}, 1},
1756 { &APFloat::IEEEhalf(), true, {0x8000ULL, 0}, 1},
1757 { &APFloat::IEEEsingle(), false, {0, 0}, 1},
1758 { &APFloat::IEEEsingle(), true, {0x80000000ULL, 0}, 1},
1759 { &APFloat::IEEEdouble(), false, {0, 0}, 1},
1760 { &APFloat::IEEEdouble(), true, {0x8000000000000000ULL, 0}, 1},
1761 { &APFloat::IEEEquad(), false, {0, 0}, 2},
1762 { &APFloat::IEEEquad(), true, {0, 0x8000000000000000ULL}, 2},
1763 { &APFloat::PPCDoubleDouble(), false, {0, 0}, 2},
1764 { &APFloat::PPCDoubleDouble(), true, {0x8000000000000000ULL, 0}, 2},
1765 { &APFloat::x87DoubleExtended(), false, {0, 0}, 2},
1766 { &APFloat::x87DoubleExtended(), true, {0, 0x8000ULL}, 2},
1768 const unsigned NumGetZeroTests = 12;
1769 for (unsigned i = 0; i < NumGetZeroTests; ++i) {
1770 APFloat test = APFloat::getZero(*GetZeroTest[i].semantics,
1771 GetZeroTest[i].sign);
1772 const char *pattern = GetZeroTest[i].sign? "-0x0p+0" : "0x0p+0";
1773 APFloat expected = APFloat(*GetZeroTest[i].semantics,
1774 pattern);
1775 EXPECT_TRUE(test.isZero());
1776 EXPECT_TRUE(GetZeroTest[i].sign? test.isNegative() : !test.isNegative());
1777 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1778 for (unsigned j = 0, je = GetZeroTest[i].bitPatternLength; j < je; ++j) {
1779 EXPECT_EQ(GetZeroTest[i].bitPattern[j],
1780 test.bitcastToAPInt().getRawData()[j]);
1785 TEST(APFloatTest, copySign) {
1786 EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
1787 APFloat::copySign(APFloat(42.0), APFloat(-1.0))));
1788 EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
1789 APFloat::copySign(APFloat(-42.0), APFloat(1.0))));
1790 EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
1791 APFloat::copySign(APFloat(-42.0), APFloat(-1.0))));
1792 EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
1793 APFloat::copySign(APFloat(42.0), APFloat(1.0))));
1796 TEST(APFloatTest, convert) {
1797 bool losesInfo;
1798 APFloat test(APFloat::IEEEdouble(), "1.0");
1799 test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1800 EXPECT_EQ(1.0f, test.convertToFloat());
1801 EXPECT_FALSE(losesInfo);
1803 test = APFloat(APFloat::x87DoubleExtended(), "0x1p-53");
1804 test.add(APFloat(APFloat::x87DoubleExtended(), "1.0"), APFloat::rmNearestTiesToEven);
1805 test.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1806 EXPECT_EQ(1.0, test.convertToDouble());
1807 EXPECT_TRUE(losesInfo);
1809 test = APFloat(APFloat::IEEEquad(), "0x1p-53");
1810 test.add(APFloat(APFloat::IEEEquad(), "1.0"), APFloat::rmNearestTiesToEven);
1811 test.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1812 EXPECT_EQ(1.0, test.convertToDouble());
1813 EXPECT_TRUE(losesInfo);
1815 test = APFloat(APFloat::x87DoubleExtended(), "0xf.fffffffp+28");
1816 test.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1817 EXPECT_EQ(4294967295.0, test.convertToDouble());
1818 EXPECT_FALSE(losesInfo);
1820 test = APFloat::getSNaN(APFloat::IEEEsingle());
1821 APFloat::opStatus status = test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven, &losesInfo);
1822 // Conversion quiets the SNAN, so now 2 bits of the 64-bit significand should be set.
1823 APInt topTwoBits(64, 0x6000000000000000);
1824 EXPECT_TRUE(test.bitwiseIsEqual(APFloat::getQNaN(APFloat::x87DoubleExtended(), false, &topTwoBits)));
1825 EXPECT_FALSE(losesInfo);
1826 EXPECT_EQ(status, APFloat::opInvalidOp);
1828 test = APFloat::getQNaN(APFloat::IEEEsingle());
1829 APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended());
1830 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1831 &losesInfo);
1832 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1833 EXPECT_FALSE(losesInfo);
1835 test = APFloat::getSNaN(APFloat::x87DoubleExtended());
1836 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1837 &losesInfo);
1838 APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended());
1839 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1840 EXPECT_FALSE(losesInfo);
1842 test = APFloat::getQNaN(APFloat::x87DoubleExtended());
1843 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1844 &losesInfo);
1845 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1846 EXPECT_FALSE(losesInfo);
1848 // The payload is lost in truncation, but we retain NaN by setting the quiet bit.
1849 APInt payload(52, 1);
1850 test = APFloat::getSNaN(APFloat::IEEEdouble(), false, &payload);
1851 status = test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1852 EXPECT_EQ(0x7fc00000, test.bitcastToAPInt());
1853 EXPECT_TRUE(losesInfo);
1854 EXPECT_EQ(status, APFloat::opInvalidOp);
1856 // The payload is lost in truncation. QNaN remains QNaN.
1857 test = APFloat::getQNaN(APFloat::IEEEdouble(), false, &payload);
1858 status = test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1859 EXPECT_EQ(0x7fc00000, test.bitcastToAPInt());
1860 EXPECT_TRUE(losesInfo);
1861 EXPECT_EQ(status, APFloat::opOK);
1863 // Test that subnormals are handled correctly in double to float conversion
1864 test = APFloat(APFloat::IEEEdouble(), "0x0.0000010000000p-1022");
1865 test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1866 EXPECT_EQ(0.0f, test.convertToFloat());
1867 EXPECT_TRUE(losesInfo);
1869 test = APFloat(APFloat::IEEEdouble(), "0x0.0000010000001p-1022");
1870 test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1871 EXPECT_EQ(0.0f, test.convertToFloat());
1872 EXPECT_TRUE(losesInfo);
1874 test = APFloat(APFloat::IEEEdouble(), "-0x0.0000010000001p-1022");
1875 test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1876 EXPECT_EQ(0.0f, test.convertToFloat());
1877 EXPECT_TRUE(losesInfo);
1879 test = APFloat(APFloat::IEEEdouble(), "0x0.0000020000000p-1022");
1880 test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1881 EXPECT_EQ(0.0f, test.convertToFloat());
1882 EXPECT_TRUE(losesInfo);
1884 test = APFloat(APFloat::IEEEdouble(), "0x0.0000020000001p-1022");
1885 test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1886 EXPECT_EQ(0.0f, test.convertToFloat());
1887 EXPECT_TRUE(losesInfo);
1889 // Test subnormal conversion to bfloat
1890 test = APFloat(APFloat::IEEEsingle(), "0x0.01p-126");
1891 test.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven, &losesInfo);
1892 EXPECT_EQ(0.0f, test.convertToFloat());
1893 EXPECT_TRUE(losesInfo);
1895 test = APFloat(APFloat::IEEEsingle(), "0x0.02p-126");
1896 test.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven, &losesInfo);
1897 EXPECT_EQ(0x01, test.bitcastToAPInt());
1898 EXPECT_FALSE(losesInfo);
1900 test = APFloat(APFloat::IEEEsingle(), "0x0.01p-126");
1901 test.convert(APFloat::BFloat(), APFloat::rmNearestTiesToAway, &losesInfo);
1902 EXPECT_EQ(0x01, test.bitcastToAPInt());
1903 EXPECT_TRUE(losesInfo);
1906 TEST(APFloatTest, PPCDoubleDouble) {
1907 APFloat test(APFloat::PPCDoubleDouble(), "1.0");
1908 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1909 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1911 // LDBL_MAX
1912 test = APFloat(APFloat::PPCDoubleDouble(), "1.79769313486231580793728971405301e+308");
1913 EXPECT_EQ(0x7fefffffffffffffull, test.bitcastToAPInt().getRawData()[0]);
1914 EXPECT_EQ(0x7c8ffffffffffffeull, test.bitcastToAPInt().getRawData()[1]);
1916 // LDBL_MIN
1917 test = APFloat(APFloat::PPCDoubleDouble(), "2.00416836000897277799610805135016e-292");
1918 EXPECT_EQ(0x0360000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1919 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1921 // PR30869
1923 auto Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") +
1924 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1925 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1927 Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") -
1928 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1929 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1931 Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") *
1932 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1933 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1935 Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") /
1936 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1937 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1939 int Exp;
1940 Result = frexp(APFloat(APFloat::PPCDoubleDouble(), "1.0"), Exp,
1941 APFloat::rmNearestTiesToEven);
1942 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1944 Result = scalbn(APFloat(APFloat::PPCDoubleDouble(), "1.0"), 1,
1945 APFloat::rmNearestTiesToEven);
1946 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1950 TEST(APFloatTest, isNegative) {
1951 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1952 EXPECT_FALSE(t.isNegative());
1953 t = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
1954 EXPECT_TRUE(t.isNegative());
1956 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isNegative());
1957 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle(), true).isNegative());
1959 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isNegative());
1960 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle(), true).isNegative());
1962 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNegative());
1963 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle(), true).isNegative());
1965 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNegative());
1966 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), true).isNegative());
1969 TEST(APFloatTest, isNormal) {
1970 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1971 EXPECT_TRUE(t.isNormal());
1973 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isNormal());
1974 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isNormal());
1975 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNormal());
1976 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNormal());
1977 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isNormal());
1980 TEST(APFloatTest, isFinite) {
1981 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1982 EXPECT_TRUE(t.isFinite());
1983 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isFinite());
1984 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle(), false).isFinite());
1985 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isFinite());
1986 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isFinite());
1987 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isFinite());
1990 TEST(APFloatTest, isInfinity) {
1991 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1992 EXPECT_FALSE(t.isInfinity());
1993 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle(), false).isInfinity());
1994 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isInfinity());
1995 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isInfinity());
1996 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isInfinity());
1997 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isInfinity());
2000 TEST(APFloatTest, isNaN) {
2001 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
2002 EXPECT_FALSE(t.isNaN());
2003 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isNaN());
2004 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isNaN());
2005 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNaN());
2006 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNaN());
2007 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isNaN());
2010 TEST(APFloatTest, isFiniteNonZero) {
2011 // Test positive/negative normal value.
2012 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "0x1p+0").isFiniteNonZero());
2013 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "-0x1p+0").isFiniteNonZero());
2015 // Test positive/negative denormal value.
2016 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isFiniteNonZero());
2017 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "-0x1p-149").isFiniteNonZero());
2019 // Test +/- Infinity.
2020 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isFiniteNonZero());
2021 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), true).isFiniteNonZero());
2023 // Test +/- Zero.
2024 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isFiniteNonZero());
2025 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), true).isFiniteNonZero());
2027 // Test +/- qNaN. +/- dont mean anything with qNaN but paranoia can't hurt in
2028 // this instance.
2029 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isFiniteNonZero());
2030 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), true).isFiniteNonZero());
2032 // Test +/- sNaN. +/- dont mean anything with sNaN but paranoia can't hurt in
2033 // this instance.
2034 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isFiniteNonZero());
2035 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), true).isFiniteNonZero());
2038 TEST(APFloatTest, add) {
2039 // Test Special Cases against each other and normal values.
2041 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2042 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2043 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2044 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2045 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2046 APFloat SNaN = APFloat(APFloat::IEEEsingle(), "snan123");
2047 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2048 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2049 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2050 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2051 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2052 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2053 APFloat PSmallestNormalized =
2054 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2055 APFloat MSmallestNormalized =
2056 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2058 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2060 struct {
2061 APFloat x;
2062 APFloat y;
2063 const char *result;
2064 int status;
2065 int category;
2066 } SpecialCaseTests[] = {
2067 { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2068 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2069 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2070 { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2071 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2072 { PInf, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2073 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2074 { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2075 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2076 { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2077 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2078 { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2079 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2080 { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2081 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2082 { MInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2083 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2084 { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2085 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2086 { MInf, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2087 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2088 { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2089 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2090 { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2091 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2092 { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2093 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2094 { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2095 { PZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2096 { PZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2097 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2098 { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2099 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2100 { PZero, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2101 { PZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2102 { PZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2103 { PZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2104 { PZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2105 { PZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2106 { PZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2107 { PZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2108 { PZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2109 { MZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2110 { MZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2111 { MZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2112 { MZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2113 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2114 { MZero, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2115 { MZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2116 { MZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2117 { MZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2118 { MZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2119 { MZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2120 { MZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2121 { MZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2122 { MZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2123 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2124 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2125 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2126 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2127 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2128 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2129 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2130 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2131 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2132 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2133 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2134 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2135 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2136 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2137 { SNaN, PInf, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2138 { SNaN, MInf, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2139 { SNaN, PZero, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2140 { SNaN, MZero, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2141 { SNaN, QNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2142 { SNaN, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2143 { SNaN, PNormalValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2144 { SNaN, MNormalValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2145 { SNaN, PLargestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2146 { SNaN, MLargestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2147 { SNaN, PSmallestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2148 { SNaN, MSmallestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2149 { SNaN, PSmallestNormalized, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2150 { SNaN, MSmallestNormalized, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2151 { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2152 { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2153 { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2154 { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2155 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2156 { PNormalValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2157 { PNormalValue, PNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal },
2158 { PNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2159 { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2160 { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2161 { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2162 { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2163 { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2164 { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2165 { MNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2166 { MNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2167 { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2168 { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2169 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2170 { MNormalValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2171 { MNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2172 { MNormalValue, MNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal },
2173 { MNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2174 { MNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2175 { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2176 { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2177 { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2178 { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2179 { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2180 { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2181 { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2182 { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2183 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2184 { PLargestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2185 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2186 { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2187 { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2188 { PLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2189 { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2190 { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2191 { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2192 { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2193 { MLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2194 { MLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2195 { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2196 { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2197 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2198 { MLargestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2199 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2200 { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2201 { MLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2202 { MLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2203 { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2204 { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2205 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2206 { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2207 { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2208 { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2209 { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2210 { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2211 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2212 { PSmallestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2213 { PSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2214 { PSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2215 { PSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2216 { PSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2217 { PSmallestValue, PSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal },
2218 { PSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2219 { PSmallestValue, PSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2220 { PSmallestValue, MSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2221 { MSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2222 { MSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2223 { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2224 { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2225 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2226 { MSmallestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2227 { MSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2228 { MSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2229 { MSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2230 { MSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2231 { MSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2232 { MSmallestValue, MSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal },
2233 { MSmallestValue, PSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2234 { MSmallestValue, MSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2235 { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2236 { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2237 { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2238 { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2239 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2240 { PSmallestNormalized, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2241 { PSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2242 { PSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2243 { PSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2244 { PSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2245 { PSmallestNormalized, PSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2246 { PSmallestNormalized, MSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2247 { PSmallestNormalized, PSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
2248 { PSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2249 { MSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2250 { MSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2251 { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2252 { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2253 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2254 { MSmallestNormalized, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2255 { MSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2256 { MSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2257 { MSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2258 { MSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2259 { MSmallestNormalized, PSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2260 { MSmallestNormalized, MSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2261 { MSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2262 { MSmallestNormalized, MSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal }
2265 for (size_t i = 0; i < std::size(SpecialCaseTests); ++i) {
2266 APFloat x(SpecialCaseTests[i].x);
2267 APFloat y(SpecialCaseTests[i].y);
2268 APFloat::opStatus status = x.add(y, APFloat::rmNearestTiesToEven);
2270 APFloat result(APFloat::IEEEsingle(), SpecialCaseTests[i].result);
2272 EXPECT_TRUE(result.bitwiseIsEqual(x));
2273 EXPECT_EQ(SpecialCaseTests[i].status, (int)status);
2274 EXPECT_EQ(SpecialCaseTests[i].category, (int)x.getCategory());
2278 TEST(APFloatTest, subtract) {
2279 // Test Special Cases against each other and normal values.
2281 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2282 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2283 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2284 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2285 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2286 APFloat SNaN = APFloat(APFloat::IEEEsingle(), "snan123");
2287 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2288 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2289 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2290 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2291 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2292 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2293 APFloat PSmallestNormalized =
2294 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2295 APFloat MSmallestNormalized =
2296 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2298 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2300 struct {
2301 APFloat x;
2302 APFloat y;
2303 const char *result;
2304 int status;
2305 int category;
2306 } SpecialCaseTests[] = {
2307 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2308 { PInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2309 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2310 { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2311 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2312 { PInf, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2313 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2314 { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2315 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2316 { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2317 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2318 { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2319 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2320 { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2321 { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2322 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2323 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2324 { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2325 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2326 { MInf, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2327 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2328 { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2329 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2330 { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2331 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2332 { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2333 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2334 { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2335 { PZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2336 { PZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2337 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2338 { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2339 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2340 { PZero, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2341 { PZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2342 { PZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2343 { PZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2344 { PZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2345 { PZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2346 { PZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2347 { PZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2348 { PZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2349 { MZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2350 { MZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2351 { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2352 { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2353 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2354 { MZero, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2355 { MZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2356 { MZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2357 { MZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2358 { MZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2359 { MZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2360 { MZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2361 { MZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2362 { MZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2363 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2364 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2365 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2366 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2367 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2368 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2369 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2370 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2371 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2372 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2373 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2374 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2375 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2376 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2377 { SNaN, PInf, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2378 { SNaN, MInf, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2379 { SNaN, PZero, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2380 { SNaN, MZero, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2381 { SNaN, QNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2382 { SNaN, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2383 { SNaN, PNormalValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2384 { SNaN, MNormalValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2385 { SNaN, PLargestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2386 { SNaN, MLargestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2387 { SNaN, PSmallestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2388 { SNaN, MSmallestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2389 { SNaN, PSmallestNormalized, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2390 { SNaN, MSmallestNormalized, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2391 { PNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2392 { PNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2393 { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2394 { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2395 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2396 { PNormalValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2397 { PNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2398 { PNormalValue, MNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal },
2399 { PNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2400 { PNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2401 { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2402 { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2403 { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2404 { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2405 { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2406 { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2407 { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2408 { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2409 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2410 { MNormalValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2411 { MNormalValue, PNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal },
2412 { MNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2413 { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2414 { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2415 { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2416 { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2417 { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2418 { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2419 { PLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2420 { PLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2421 { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2422 { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2423 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2424 { PLargestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2425 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2426 { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2427 { PLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2428 { PLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2429 { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2430 { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2431 { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2432 { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2433 { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2434 { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2435 { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2436 { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2437 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2438 { MLargestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2439 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2440 { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2441 { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2442 { MLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2443 { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2444 { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2445 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2446 { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2447 { PSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2448 { PSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2449 { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2450 { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2451 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2452 { PSmallestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2453 { PSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2454 { PSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2455 { PSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2456 { PSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2457 { PSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2458 { PSmallestValue, MSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal },
2459 { PSmallestValue, PSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2460 { PSmallestValue, MSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2461 { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2462 { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2463 { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2464 { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2465 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2466 { MSmallestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2467 { MSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2468 { MSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2469 { MSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2470 { MSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2471 { MSmallestValue, PSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal },
2472 { MSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2473 { MSmallestValue, PSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2474 { MSmallestValue, MSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2475 { PSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2476 { PSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2477 { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2478 { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2479 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2480 { PSmallestNormalized, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2481 { PSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2482 { PSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2483 { PSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2484 { PSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2485 { PSmallestNormalized, PSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2486 { PSmallestNormalized, MSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2487 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2488 { PSmallestNormalized, MSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
2489 { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2490 { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2491 { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2492 { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2493 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2494 { MSmallestNormalized, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2495 { MSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2496 { MSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2497 { MSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2498 { MSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2499 { MSmallestNormalized, PSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2500 { MSmallestNormalized, MSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2501 { MSmallestNormalized, PSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
2502 { MSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }
2505 for (size_t i = 0; i < std::size(SpecialCaseTests); ++i) {
2506 APFloat x(SpecialCaseTests[i].x);
2507 APFloat y(SpecialCaseTests[i].y);
2508 APFloat::opStatus status = x.subtract(y, APFloat::rmNearestTiesToEven);
2510 APFloat result(APFloat::IEEEsingle(), SpecialCaseTests[i].result);
2512 EXPECT_TRUE(result.bitwiseIsEqual(x));
2513 EXPECT_EQ(SpecialCaseTests[i].status, (int)status);
2514 EXPECT_EQ(SpecialCaseTests[i].category, (int)x.getCategory());
2518 TEST(APFloatTest, multiply) {
2519 // Test Special Cases against each other and normal values.
2521 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2522 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2523 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2524 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2525 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2526 APFloat SNaN = APFloat(APFloat::IEEEsingle(), "snan123");
2527 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2528 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2529 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2530 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2531 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2532 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2533 APFloat PSmallestNormalized =
2534 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2535 APFloat MSmallestNormalized =
2536 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2538 APFloat MaxQuad(APFloat::IEEEquad(),
2539 "0x1.ffffffffffffffffffffffffffffp+16383");
2540 APFloat MinQuad(APFloat::IEEEquad(),
2541 "0x0.0000000000000000000000000001p-16382");
2542 APFloat NMinQuad(APFloat::IEEEquad(),
2543 "-0x0.0000000000000000000000000001p-16382");
2545 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2546 const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact;
2548 struct {
2549 APFloat x;
2550 APFloat y;
2551 const char *result;
2552 int status;
2553 int category;
2554 APFloat::roundingMode roundingMode = APFloat::rmNearestTiesToEven;
2555 } SpecialCaseTests[] = {
2556 { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2557 { PInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2558 { PInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2559 { PInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2560 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2561 { PInf, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2562 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2563 { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2564 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2565 { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2566 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2567 { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2568 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2569 { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2570 { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2571 { MInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2572 { MInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2573 { MInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2574 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2575 { MInf, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2576 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2577 { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2578 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2579 { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2580 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2581 { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2582 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2583 { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2584 { PZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2585 { PZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2586 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2587 { PZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2588 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2589 { PZero, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2590 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2591 { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2592 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2593 { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2594 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2595 { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2596 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2597 { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2598 { MZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2599 { MZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2600 { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2601 { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2602 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2603 { MZero, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2604 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2605 { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2606 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2607 { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2608 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2609 { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2610 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2611 { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2612 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2613 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2614 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2615 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2616 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2617 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2618 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2619 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2620 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2621 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2622 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2623 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2624 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2625 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2626 { SNaN, PInf, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2627 { SNaN, MInf, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2628 { SNaN, PZero, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2629 { SNaN, MZero, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2630 { SNaN, QNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2631 { SNaN, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2632 { SNaN, PNormalValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2633 { SNaN, MNormalValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2634 { SNaN, PLargestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2635 { SNaN, MLargestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2636 { SNaN, PSmallestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2637 { SNaN, MSmallestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2638 { SNaN, PSmallestNormalized, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2639 { SNaN, MSmallestNormalized, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2640 { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2641 { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2642 { PNormalValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2643 { PNormalValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2644 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2645 { PNormalValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2646 { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2647 { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2648 { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2649 { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2650 { PNormalValue, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2651 { PNormalValue, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2652 { PNormalValue, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2653 { PNormalValue, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2654 { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2655 { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2656 { MNormalValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2657 { MNormalValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2658 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2659 { MNormalValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2660 { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2661 { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2662 { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2663 { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2664 { MNormalValue, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2665 { MNormalValue, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2666 { MNormalValue, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2667 { MNormalValue, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2668 { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2669 { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2670 { PLargestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2671 { PLargestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2672 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2673 { PLargestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2674 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2675 { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2676 { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2677 { PLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2678 { PLargestValue, PSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2679 { PLargestValue, MSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2680 { PLargestValue, PSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2681 { PLargestValue, MSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2682 { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2683 { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2684 { MLargestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2685 { MLargestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2686 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2687 { MLargestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2688 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2689 { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2690 { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2691 { MLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2692 { MLargestValue, PSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2693 { MLargestValue, MSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2694 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2695 { MLargestValue, MSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2696 { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2697 { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2698 { PSmallestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2699 { PSmallestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2700 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2701 { PSmallestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2702 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2703 { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2704 { PSmallestValue, PLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2705 { PSmallestValue, MLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2706 { PSmallestValue, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2707 { PSmallestValue, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2708 { PSmallestValue, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2709 { PSmallestValue, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2710 { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2711 { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2712 { MSmallestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2713 { MSmallestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2714 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2715 { MSmallestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2716 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2717 { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2718 { MSmallestValue, PLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2719 { MSmallestValue, MLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2720 { MSmallestValue, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2721 { MSmallestValue, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2722 { MSmallestValue, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2723 { MSmallestValue, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2724 { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2725 { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2726 { PSmallestNormalized, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2727 { PSmallestNormalized, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2728 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2729 { PSmallestNormalized, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2730 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2731 { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2732 { PSmallestNormalized, PLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2733 { PSmallestNormalized, MLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2734 { PSmallestNormalized, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2735 { PSmallestNormalized, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2736 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2737 { PSmallestNormalized, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2738 { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2739 { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2740 { MSmallestNormalized, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2741 { MSmallestNormalized, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2742 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2743 { MSmallestNormalized, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2744 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2745 { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2746 { MSmallestNormalized, PLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2747 { MSmallestNormalized, MLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2748 { MSmallestNormalized, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2749 { MSmallestNormalized, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2750 { MSmallestNormalized, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2751 { MSmallestNormalized, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2753 {MaxQuad, MinQuad, "0x1.ffffffffffffffffffffffffffffp-111", APFloat::opOK,
2754 APFloat::fcNormal, APFloat::rmNearestTiesToEven},
2755 {MaxQuad, MinQuad, "0x1.ffffffffffffffffffffffffffffp-111", APFloat::opOK,
2756 APFloat::fcNormal, APFloat::rmTowardPositive},
2757 {MaxQuad, MinQuad, "0x1.ffffffffffffffffffffffffffffp-111", APFloat::opOK,
2758 APFloat::fcNormal, APFloat::rmTowardNegative},
2759 {MaxQuad, MinQuad, "0x1.ffffffffffffffffffffffffffffp-111", APFloat::opOK,
2760 APFloat::fcNormal, APFloat::rmTowardZero},
2761 {MaxQuad, MinQuad, "0x1.ffffffffffffffffffffffffffffp-111", APFloat::opOK,
2762 APFloat::fcNormal, APFloat::rmNearestTiesToAway},
2764 {MaxQuad, NMinQuad, "-0x1.ffffffffffffffffffffffffffffp-111", APFloat::opOK,
2765 APFloat::fcNormal, APFloat::rmNearestTiesToEven},
2766 {MaxQuad, NMinQuad, "-0x1.ffffffffffffffffffffffffffffp-111", APFloat::opOK,
2767 APFloat::fcNormal, APFloat::rmTowardPositive},
2768 {MaxQuad, NMinQuad, "-0x1.ffffffffffffffffffffffffffffp-111", APFloat::opOK,
2769 APFloat::fcNormal, APFloat::rmTowardNegative},
2770 {MaxQuad, NMinQuad, "-0x1.ffffffffffffffffffffffffffffp-111", APFloat::opOK,
2771 APFloat::fcNormal, APFloat::rmTowardZero},
2772 {MaxQuad, NMinQuad, "-0x1.ffffffffffffffffffffffffffffp-111", APFloat::opOK,
2773 APFloat::fcNormal, APFloat::rmNearestTiesToAway},
2775 {MaxQuad, MaxQuad, "inf", OverflowStatus, APFloat::fcInfinity,
2776 APFloat::rmNearestTiesToEven},
2777 {MaxQuad, MaxQuad, "inf", OverflowStatus, APFloat::fcInfinity,
2778 APFloat::rmTowardPositive},
2779 {MaxQuad, MaxQuad, "0x1.ffffffffffffffffffffffffffffp+16383",
2780 APFloat::opInexact, APFloat::fcNormal, APFloat::rmTowardNegative},
2781 {MaxQuad, MaxQuad, "0x1.ffffffffffffffffffffffffffffp+16383",
2782 APFloat::opInexact, APFloat::fcNormal, APFloat::rmTowardZero},
2783 {MaxQuad, MaxQuad, "inf", OverflowStatus, APFloat::fcInfinity,
2784 APFloat::rmNearestTiesToAway},
2786 {MinQuad, MinQuad, "0", UnderflowStatus, APFloat::fcZero,
2787 APFloat::rmNearestTiesToEven},
2788 {MinQuad, MinQuad, "0x0.0000000000000000000000000001p-16382",
2789 UnderflowStatus, APFloat::fcNormal, APFloat::rmTowardPositive},
2790 {MinQuad, MinQuad, "0", UnderflowStatus, APFloat::fcZero,
2791 APFloat::rmTowardNegative},
2792 {MinQuad, MinQuad, "0", UnderflowStatus, APFloat::fcZero,
2793 APFloat::rmTowardZero},
2794 {MinQuad, MinQuad, "0", UnderflowStatus, APFloat::fcZero,
2795 APFloat::rmNearestTiesToAway},
2797 {MinQuad, NMinQuad, "-0", UnderflowStatus, APFloat::fcZero,
2798 APFloat::rmNearestTiesToEven},
2799 {MinQuad, NMinQuad, "-0", UnderflowStatus, APFloat::fcZero,
2800 APFloat::rmTowardPositive},
2801 {MinQuad, NMinQuad, "-0x0.0000000000000000000000000001p-16382",
2802 UnderflowStatus, APFloat::fcNormal, APFloat::rmTowardNegative},
2803 {MinQuad, NMinQuad, "-0", UnderflowStatus, APFloat::fcZero,
2804 APFloat::rmTowardZero},
2805 {MinQuad, NMinQuad, "-0", UnderflowStatus, APFloat::fcZero,
2806 APFloat::rmNearestTiesToAway},
2809 for (size_t i = 0; i < std::size(SpecialCaseTests); ++i) {
2810 APFloat x(SpecialCaseTests[i].x);
2811 APFloat y(SpecialCaseTests[i].y);
2812 APFloat::opStatus status = x.multiply(y, SpecialCaseTests[i].roundingMode);
2814 APFloat result(x.getSemantics(), SpecialCaseTests[i].result);
2816 EXPECT_TRUE(result.bitwiseIsEqual(x));
2817 EXPECT_EQ(SpecialCaseTests[i].status, (int)status);
2818 EXPECT_EQ(SpecialCaseTests[i].category, (int)x.getCategory());
2822 TEST(APFloatTest, divide) {
2823 // Test Special Cases against each other and normal values.
2825 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2826 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2827 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2828 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2829 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2830 APFloat SNaN = APFloat(APFloat::IEEEsingle(), "snan123");
2831 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2832 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2833 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2834 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2835 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2836 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2837 APFloat PSmallestNormalized =
2838 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2839 APFloat MSmallestNormalized =
2840 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2842 APFloat MaxQuad(APFloat::IEEEquad(),
2843 "0x1.ffffffffffffffffffffffffffffp+16383");
2844 APFloat MinQuad(APFloat::IEEEquad(),
2845 "0x0.0000000000000000000000000001p-16382");
2846 APFloat NMinQuad(APFloat::IEEEquad(),
2847 "-0x0.0000000000000000000000000001p-16382");
2849 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2850 const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact;
2852 struct {
2853 APFloat x;
2854 APFloat y;
2855 const char *result;
2856 int status;
2857 int category;
2858 APFloat::roundingMode roundingMode = APFloat::rmNearestTiesToEven;
2859 } SpecialCaseTests[] = {
2860 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2861 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2862 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2863 { PInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2864 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2865 { PInf, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2866 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2867 { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2868 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2869 { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2870 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2871 { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2872 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2873 { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2874 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2875 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2876 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2877 { MInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2878 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2879 { MInf, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2880 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2881 { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2882 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2883 { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2884 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2885 { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2886 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2887 { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2888 { PZero, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2889 { PZero, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2890 { PZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2891 { PZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2892 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2893 { PZero, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2894 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2895 { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2896 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2897 { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2898 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2899 { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2900 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2901 { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2902 { MZero, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2903 { MZero, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2904 { MZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2905 { MZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2906 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2907 { MZero, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2908 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2909 { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2910 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2911 { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2912 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2913 { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2914 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2915 { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2916 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2917 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2918 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2919 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2920 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2921 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2922 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2923 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2924 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2925 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2926 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2927 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2928 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2929 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2930 { SNaN, PInf, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2931 { SNaN, MInf, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2932 { SNaN, PZero, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2933 { SNaN, MZero, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2934 { SNaN, QNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2935 { SNaN, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2936 { SNaN, PNormalValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2937 { SNaN, MNormalValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2938 { SNaN, PLargestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2939 { SNaN, MLargestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2940 { SNaN, PSmallestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2941 { SNaN, MSmallestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2942 { SNaN, PSmallestNormalized, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2943 { SNaN, MSmallestNormalized, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2944 { PNormalValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2945 { PNormalValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2946 { PNormalValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2947 { PNormalValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2948 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2949 { PNormalValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2950 { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2951 { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2952 { PNormalValue, PLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal },
2953 { PNormalValue, MLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal },
2954 { PNormalValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2955 { PNormalValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2956 { PNormalValue, PSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal },
2957 { PNormalValue, MSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal },
2958 { MNormalValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2959 { MNormalValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2960 { MNormalValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2961 { MNormalValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2962 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2963 { MNormalValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2964 { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2965 { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2966 { MNormalValue, PLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal },
2967 { MNormalValue, MLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal },
2968 { MNormalValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2969 { MNormalValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2970 { MNormalValue, PSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal },
2971 { MNormalValue, MSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal },
2972 { PLargestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2973 { PLargestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2974 { PLargestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2975 { PLargestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2976 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2977 { PLargestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2978 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2979 { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2980 { PLargestValue, PLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2981 { PLargestValue, MLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2982 { PLargestValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2983 { PLargestValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2984 { PLargestValue, PSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity },
2985 { PLargestValue, MSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity },
2986 { MLargestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2987 { MLargestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2988 { MLargestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2989 { MLargestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2990 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2991 { MLargestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
2992 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2993 { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2994 { MLargestValue, PLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2995 { MLargestValue, MLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2996 { MLargestValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2997 { MLargestValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2998 { MLargestValue, PSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity },
2999 { MLargestValue, MSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity },
3000 { PSmallestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3001 { PSmallestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3002 { PSmallestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
3003 { PSmallestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
3004 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3005 { PSmallestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3006 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
3007 { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
3008 { PSmallestValue, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
3009 { PSmallestValue, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
3010 { PSmallestValue, PSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
3011 { PSmallestValue, MSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
3012 { PSmallestValue, PSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal },
3013 { PSmallestValue, MSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal },
3014 { MSmallestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3015 { MSmallestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3016 { MSmallestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
3017 { MSmallestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
3018 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3019 { MSmallestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3020 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
3021 { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
3022 { MSmallestValue, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
3023 { MSmallestValue, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
3024 { MSmallestValue, PSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
3025 { MSmallestValue, MSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
3026 { MSmallestValue, PSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal },
3027 { MSmallestValue, MSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal },
3028 { PSmallestNormalized, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3029 { PSmallestNormalized, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3030 { PSmallestNormalized, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
3031 { PSmallestNormalized, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
3032 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3033 { PSmallestNormalized, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3034 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
3035 { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
3036 { PSmallestNormalized, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
3037 { PSmallestNormalized, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
3038 { PSmallestNormalized, PSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal },
3039 { PSmallestNormalized, MSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal },
3040 { PSmallestNormalized, PSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
3041 { PSmallestNormalized, MSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
3042 { MSmallestNormalized, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3043 { MSmallestNormalized, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3044 { MSmallestNormalized, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
3045 { MSmallestNormalized, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
3046 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3047 { MSmallestNormalized, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3048 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
3049 { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
3050 { MSmallestNormalized, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
3051 { MSmallestNormalized, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
3052 { MSmallestNormalized, PSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal },
3053 { MSmallestNormalized, MSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal },
3054 { MSmallestNormalized, PSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
3055 { MSmallestNormalized, MSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
3057 {MaxQuad, NMinQuad, "-inf", OverflowStatus, APFloat::fcInfinity,
3058 APFloat::rmNearestTiesToEven},
3059 {MaxQuad, NMinQuad, "-0x1.ffffffffffffffffffffffffffffp+16383",
3060 APFloat::opInexact, APFloat::fcNormal, APFloat::rmTowardPositive},
3061 {MaxQuad, NMinQuad, "-inf", OverflowStatus, APFloat::fcInfinity,
3062 APFloat::rmTowardNegative},
3063 {MaxQuad, NMinQuad, "-0x1.ffffffffffffffffffffffffffffp+16383",
3064 APFloat::opInexact, APFloat::fcNormal, APFloat::rmTowardZero},
3065 {MaxQuad, NMinQuad, "-inf", OverflowStatus, APFloat::fcInfinity,
3066 APFloat::rmNearestTiesToAway},
3068 {MinQuad, MaxQuad, "0", UnderflowStatus, APFloat::fcZero,
3069 APFloat::rmNearestTiesToEven},
3070 {MinQuad, MaxQuad, "0x0.0000000000000000000000000001p-16382",
3071 UnderflowStatus, APFloat::fcNormal, APFloat::rmTowardPositive},
3072 {MinQuad, MaxQuad, "0", UnderflowStatus, APFloat::fcZero,
3073 APFloat::rmTowardNegative},
3074 {MinQuad, MaxQuad, "0", UnderflowStatus, APFloat::fcZero,
3075 APFloat::rmTowardZero},
3076 {MinQuad, MaxQuad, "0", UnderflowStatus, APFloat::fcZero,
3077 APFloat::rmNearestTiesToAway},
3079 {NMinQuad, MaxQuad, "-0", UnderflowStatus, APFloat::fcZero,
3080 APFloat::rmNearestTiesToEven},
3081 {NMinQuad, MaxQuad, "-0", UnderflowStatus, APFloat::fcZero,
3082 APFloat::rmTowardPositive},
3083 {NMinQuad, MaxQuad, "-0x0.0000000000000000000000000001p-16382",
3084 UnderflowStatus, APFloat::fcNormal, APFloat::rmTowardNegative},
3085 {NMinQuad, MaxQuad, "-0", UnderflowStatus, APFloat::fcZero,
3086 APFloat::rmTowardZero},
3087 {NMinQuad, MaxQuad, "-0", UnderflowStatus, APFloat::fcZero,
3088 APFloat::rmNearestTiesToAway},
3091 for (size_t i = 0; i < std::size(SpecialCaseTests); ++i) {
3092 APFloat x(SpecialCaseTests[i].x);
3093 APFloat y(SpecialCaseTests[i].y);
3094 APFloat::opStatus status = x.divide(y, SpecialCaseTests[i].roundingMode);
3096 APFloat result(x.getSemantics(), SpecialCaseTests[i].result);
3098 EXPECT_TRUE(result.bitwiseIsEqual(x));
3099 EXPECT_EQ(SpecialCaseTests[i].status, (int)status);
3100 EXPECT_EQ(SpecialCaseTests[i].category, (int)x.getCategory());
3104 TEST(APFloatTest, operatorOverloads) {
3105 // This is mostly testing that these operator overloads compile.
3106 APFloat One = APFloat(APFloat::IEEEsingle(), "0x1p+0");
3107 APFloat Two = APFloat(APFloat::IEEEsingle(), "0x2p+0");
3108 EXPECT_TRUE(Two.bitwiseIsEqual(One + One));
3109 EXPECT_TRUE(One.bitwiseIsEqual(Two - One));
3110 EXPECT_TRUE(Two.bitwiseIsEqual(One * Two));
3111 EXPECT_TRUE(One.bitwiseIsEqual(Two / Two));
3114 TEST(APFloatTest, Comparisons) {
3115 enum {MNan, MInf, MBig, MOne, MZer, PZer, POne, PBig, PInf, PNan, NumVals};
3116 APFloat Vals[NumVals] = {
3117 APFloat::getNaN(APFloat::IEEEsingle(), true),
3118 APFloat::getInf(APFloat::IEEEsingle(), true),
3119 APFloat::getLargest(APFloat::IEEEsingle(), true),
3120 APFloat(APFloat::IEEEsingle(), "-0x1p+0"),
3121 APFloat::getZero(APFloat::IEEEsingle(), true),
3122 APFloat::getZero(APFloat::IEEEsingle(), false),
3123 APFloat(APFloat::IEEEsingle(), "0x1p+0"),
3124 APFloat::getLargest(APFloat::IEEEsingle(), false),
3125 APFloat::getInf(APFloat::IEEEsingle(), false),
3126 APFloat::getNaN(APFloat::IEEEsingle(), false),
3128 using Relation = void (*)(const APFloat &, const APFloat &);
3129 Relation LT = [](const APFloat &LHS, const APFloat &RHS) {
3130 EXPECT_FALSE(LHS == RHS);
3131 EXPECT_TRUE(LHS != RHS);
3132 EXPECT_TRUE(LHS < RHS);
3133 EXPECT_FALSE(LHS > RHS);
3134 EXPECT_TRUE(LHS <= RHS);
3135 EXPECT_FALSE(LHS >= RHS);
3137 Relation EQ = [](const APFloat &LHS, const APFloat &RHS) {
3138 EXPECT_TRUE(LHS == RHS);
3139 EXPECT_FALSE(LHS != RHS);
3140 EXPECT_FALSE(LHS < RHS);
3141 EXPECT_FALSE(LHS > RHS);
3142 EXPECT_TRUE(LHS <= RHS);
3143 EXPECT_TRUE(LHS >= RHS);
3145 Relation GT = [](const APFloat &LHS, const APFloat &RHS) {
3146 EXPECT_FALSE(LHS == RHS);
3147 EXPECT_TRUE(LHS != RHS);
3148 EXPECT_FALSE(LHS < RHS);
3149 EXPECT_TRUE(LHS > RHS);
3150 EXPECT_FALSE(LHS <= RHS);
3151 EXPECT_TRUE(LHS >= RHS);
3153 Relation UN = [](const APFloat &LHS, const APFloat &RHS) {
3154 EXPECT_FALSE(LHS == RHS);
3155 EXPECT_TRUE(LHS != RHS);
3156 EXPECT_FALSE(LHS < RHS);
3157 EXPECT_FALSE(LHS > RHS);
3158 EXPECT_FALSE(LHS <= RHS);
3159 EXPECT_FALSE(LHS >= RHS);
3161 Relation Relations[NumVals][NumVals] = {
3162 // -N -I -B -1 -0 +0 +1 +B +I +N
3163 /* MNan */ {UN, UN, UN, UN, UN, UN, UN, UN, UN, UN},
3164 /* MInf */ {UN, EQ, LT, LT, LT, LT, LT, LT, LT, UN},
3165 /* MBig */ {UN, GT, EQ, LT, LT, LT, LT, LT, LT, UN},
3166 /* MOne */ {UN, GT, GT, EQ, LT, LT, LT, LT, LT, UN},
3167 /* MZer */ {UN, GT, GT, GT, EQ, EQ, LT, LT, LT, UN},
3168 /* PZer */ {UN, GT, GT, GT, EQ, EQ, LT, LT, LT, UN},
3169 /* POne */ {UN, GT, GT, GT, GT, GT, EQ, LT, LT, UN},
3170 /* PBig */ {UN, GT, GT, GT, GT, GT, GT, EQ, LT, UN},
3171 /* PInf */ {UN, GT, GT, GT, GT, GT, GT, GT, EQ, UN},
3172 /* PNan */ {UN, UN, UN, UN, UN, UN, UN, UN, UN, UN},
3174 for (unsigned I = 0; I < NumVals; ++I)
3175 for (unsigned J = 0; J < NumVals; ++J)
3176 Relations[I][J](Vals[I], Vals[J]);
3179 TEST(APFloatTest, abs) {
3180 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
3181 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
3182 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
3183 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
3184 APFloat PQNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
3185 APFloat MQNaN = APFloat::getNaN(APFloat::IEEEsingle(), true);
3186 APFloat PSNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
3187 APFloat MSNaN = APFloat::getSNaN(APFloat::IEEEsingle(), true);
3188 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
3189 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
3190 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
3191 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
3192 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
3193 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
3194 APFloat PSmallestNormalized =
3195 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
3196 APFloat MSmallestNormalized =
3197 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
3199 EXPECT_TRUE(PInf.bitwiseIsEqual(abs(PInf)));
3200 EXPECT_TRUE(PInf.bitwiseIsEqual(abs(MInf)));
3201 EXPECT_TRUE(PZero.bitwiseIsEqual(abs(PZero)));
3202 EXPECT_TRUE(PZero.bitwiseIsEqual(abs(MZero)));
3203 EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(PQNaN)));
3204 EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(MQNaN)));
3205 EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(PSNaN)));
3206 EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(MSNaN)));
3207 EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(PNormalValue)));
3208 EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(MNormalValue)));
3209 EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(PLargestValue)));
3210 EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(MLargestValue)));
3211 EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(PSmallestValue)));
3212 EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(MSmallestValue)));
3213 EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(PSmallestNormalized)));
3214 EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(MSmallestNormalized)));
3217 TEST(APFloatTest, neg) {
3218 APFloat One = APFloat(APFloat::IEEEsingle(), "1.0");
3219 APFloat NegOne = APFloat(APFloat::IEEEsingle(), "-1.0");
3220 APFloat Zero = APFloat::getZero(APFloat::IEEEsingle(), false);
3221 APFloat NegZero = APFloat::getZero(APFloat::IEEEsingle(), true);
3222 APFloat Inf = APFloat::getInf(APFloat::IEEEsingle(), false);
3223 APFloat NegInf = APFloat::getInf(APFloat::IEEEsingle(), true);
3224 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
3225 APFloat NegQNaN = APFloat::getNaN(APFloat::IEEEsingle(), true);
3227 EXPECT_TRUE(NegOne.bitwiseIsEqual(neg(One)));
3228 EXPECT_TRUE(One.bitwiseIsEqual(neg(NegOne)));
3229 EXPECT_TRUE(NegZero.bitwiseIsEqual(neg(Zero)));
3230 EXPECT_TRUE(Zero.bitwiseIsEqual(neg(NegZero)));
3231 EXPECT_TRUE(NegInf.bitwiseIsEqual(neg(Inf)));
3232 EXPECT_TRUE(Inf.bitwiseIsEqual(neg(NegInf)));
3233 EXPECT_TRUE(NegInf.bitwiseIsEqual(neg(Inf)));
3234 EXPECT_TRUE(Inf.bitwiseIsEqual(neg(NegInf)));
3235 EXPECT_TRUE(NegQNaN.bitwiseIsEqual(neg(QNaN)));
3236 EXPECT_TRUE(QNaN.bitwiseIsEqual(neg(NegQNaN)));
3238 EXPECT_TRUE(NegOne.bitwiseIsEqual(-One));
3239 EXPECT_TRUE(One.bitwiseIsEqual(-NegOne));
3240 EXPECT_TRUE(NegZero.bitwiseIsEqual(-Zero));
3241 EXPECT_TRUE(Zero.bitwiseIsEqual(-NegZero));
3242 EXPECT_TRUE(NegInf.bitwiseIsEqual(-Inf));
3243 EXPECT_TRUE(Inf.bitwiseIsEqual(-NegInf));
3244 EXPECT_TRUE(NegInf.bitwiseIsEqual(-Inf));
3245 EXPECT_TRUE(Inf.bitwiseIsEqual(-NegInf));
3246 EXPECT_TRUE(NegQNaN.bitwiseIsEqual(-QNaN));
3247 EXPECT_TRUE(QNaN.bitwiseIsEqual(-NegQNaN));
3250 TEST(APFloatTest, ilogb) {
3251 EXPECT_EQ(-1074, ilogb(APFloat::getSmallest(APFloat::IEEEdouble(), false)));
3252 EXPECT_EQ(-1074, ilogb(APFloat::getSmallest(APFloat::IEEEdouble(), true)));
3253 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1024")));
3254 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1023")));
3255 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1023")));
3256 EXPECT_EQ(-51, ilogb(APFloat(APFloat::IEEEdouble(), "0x1p-51")));
3257 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-1023")));
3258 EXPECT_EQ(-2, ilogb(APFloat(APFloat::IEEEdouble(), "0x0.ffffp-1")));
3259 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.fffep-1023")));
3260 EXPECT_EQ(1023, ilogb(APFloat::getLargest(APFloat::IEEEdouble(), false)));
3261 EXPECT_EQ(1023, ilogb(APFloat::getLargest(APFloat::IEEEdouble(), true)));
3264 EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle(), "0x1p+0")));
3265 EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle(), "-0x1p+0")));
3266 EXPECT_EQ(42, ilogb(APFloat(APFloat::IEEEsingle(), "0x1p+42")));
3267 EXPECT_EQ(-42, ilogb(APFloat(APFloat::IEEEsingle(), "0x1p-42")));
3269 EXPECT_EQ(APFloat::IEK_Inf,
3270 ilogb(APFloat::getInf(APFloat::IEEEsingle(), false)));
3271 EXPECT_EQ(APFloat::IEK_Inf,
3272 ilogb(APFloat::getInf(APFloat::IEEEsingle(), true)));
3273 EXPECT_EQ(APFloat::IEK_Zero,
3274 ilogb(APFloat::getZero(APFloat::IEEEsingle(), false)));
3275 EXPECT_EQ(APFloat::IEK_Zero,
3276 ilogb(APFloat::getZero(APFloat::IEEEsingle(), true)));
3277 EXPECT_EQ(APFloat::IEK_NaN,
3278 ilogb(APFloat::getNaN(APFloat::IEEEsingle(), false)));
3279 EXPECT_EQ(APFloat::IEK_NaN,
3280 ilogb(APFloat::getSNaN(APFloat::IEEEsingle(), false)));
3282 EXPECT_EQ(127, ilogb(APFloat::getLargest(APFloat::IEEEsingle(), false)));
3283 EXPECT_EQ(127, ilogb(APFloat::getLargest(APFloat::IEEEsingle(), true)));
3285 EXPECT_EQ(-149, ilogb(APFloat::getSmallest(APFloat::IEEEsingle(), false)));
3286 EXPECT_EQ(-149, ilogb(APFloat::getSmallest(APFloat::IEEEsingle(), true)));
3287 EXPECT_EQ(-126,
3288 ilogb(APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false)));
3289 EXPECT_EQ(-126,
3290 ilogb(APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true)));
3293 TEST(APFloatTest, scalbn) {
3295 const APFloat::roundingMode RM = APFloat::rmNearestTiesToEven;
3296 EXPECT_TRUE(
3297 APFloat(APFloat::IEEEsingle(), "0x1p+0")
3298 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), 0, RM)));
3299 EXPECT_TRUE(
3300 APFloat(APFloat::IEEEsingle(), "0x1p+42")
3301 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), 42, RM)));
3302 EXPECT_TRUE(
3303 APFloat(APFloat::IEEEsingle(), "0x1p-42")
3304 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), -42, RM)));
3306 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
3307 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
3308 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
3309 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
3310 APFloat QPNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
3311 APFloat QMNaN = APFloat::getNaN(APFloat::IEEEsingle(), true);
3312 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
3314 EXPECT_TRUE(PInf.bitwiseIsEqual(scalbn(PInf, 0, RM)));
3315 EXPECT_TRUE(MInf.bitwiseIsEqual(scalbn(MInf, 0, RM)));
3316 EXPECT_TRUE(PZero.bitwiseIsEqual(scalbn(PZero, 0, RM)));
3317 EXPECT_TRUE(MZero.bitwiseIsEqual(scalbn(MZero, 0, RM)));
3318 EXPECT_TRUE(QPNaN.bitwiseIsEqual(scalbn(QPNaN, 0, RM)));
3319 EXPECT_TRUE(QMNaN.bitwiseIsEqual(scalbn(QMNaN, 0, RM)));
3320 EXPECT_FALSE(scalbn(SNaN, 0, RM).isSignaling());
3322 APFloat ScalbnSNaN = scalbn(SNaN, 1, RM);
3323 EXPECT_TRUE(ScalbnSNaN.isNaN() && !ScalbnSNaN.isSignaling());
3325 // Make sure highest bit of payload is preserved.
3326 const APInt Payload(64, (UINT64_C(1) << 50) |
3327 (UINT64_C(1) << 49) |
3328 (UINT64_C(1234) << 32) |
3331 APFloat SNaNWithPayload = APFloat::getSNaN(APFloat::IEEEdouble(), false,
3332 &Payload);
3333 APFloat QuietPayload = scalbn(SNaNWithPayload, 1, RM);
3334 EXPECT_TRUE(QuietPayload.isNaN() && !QuietPayload.isSignaling());
3335 EXPECT_EQ(Payload, QuietPayload.bitcastToAPInt().getLoBits(51));
3337 EXPECT_TRUE(PInf.bitwiseIsEqual(
3338 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), 128, RM)));
3339 EXPECT_TRUE(MInf.bitwiseIsEqual(
3340 scalbn(APFloat(APFloat::IEEEsingle(), "-0x1p+0"), 128, RM)));
3341 EXPECT_TRUE(PInf.bitwiseIsEqual(
3342 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+127"), 1, RM)));
3343 EXPECT_TRUE(PZero.bitwiseIsEqual(
3344 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p-127"), -127, RM)));
3345 EXPECT_TRUE(MZero.bitwiseIsEqual(
3346 scalbn(APFloat(APFloat::IEEEsingle(), "-0x1p-127"), -127, RM)));
3347 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "-0x1p-149").bitwiseIsEqual(
3348 scalbn(APFloat(APFloat::IEEEsingle(), "-0x1p-127"), -22, RM)));
3349 EXPECT_TRUE(PZero.bitwiseIsEqual(
3350 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p-126"), -24, RM)));
3353 APFloat SmallestF64 = APFloat::getSmallest(APFloat::IEEEdouble(), false);
3354 APFloat NegSmallestF64 = APFloat::getSmallest(APFloat::IEEEdouble(), true);
3356 APFloat LargestF64 = APFloat::getLargest(APFloat::IEEEdouble(), false);
3357 APFloat NegLargestF64 = APFloat::getLargest(APFloat::IEEEdouble(), true);
3359 APFloat SmallestNormalizedF64
3360 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), false);
3361 APFloat NegSmallestNormalizedF64
3362 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), true);
3364 APFloat LargestDenormalF64(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1023");
3365 APFloat NegLargestDenormalF64(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1023");
3368 EXPECT_TRUE(SmallestF64.bitwiseIsEqual(
3369 scalbn(APFloat(APFloat::IEEEdouble(), "0x1p-1074"), 0, RM)));
3370 EXPECT_TRUE(NegSmallestF64.bitwiseIsEqual(
3371 scalbn(APFloat(APFloat::IEEEdouble(), "-0x1p-1074"), 0, RM)));
3373 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+1023")
3374 .bitwiseIsEqual(scalbn(SmallestF64, 2097, RM)));
3376 EXPECT_TRUE(scalbn(SmallestF64, -2097, RM).isPosZero());
3377 EXPECT_TRUE(scalbn(SmallestF64, -2098, RM).isPosZero());
3378 EXPECT_TRUE(scalbn(SmallestF64, -2099, RM).isPosZero());
3379 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+1022")
3380 .bitwiseIsEqual(scalbn(SmallestF64, 2096, RM)));
3381 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+1023")
3382 .bitwiseIsEqual(scalbn(SmallestF64, 2097, RM)));
3383 EXPECT_TRUE(scalbn(SmallestF64, 2098, RM).isInfinity());
3384 EXPECT_TRUE(scalbn(SmallestF64, 2099, RM).isInfinity());
3386 // Test for integer overflows when adding to exponent.
3387 EXPECT_TRUE(scalbn(SmallestF64, -INT_MAX, RM).isPosZero());
3388 EXPECT_TRUE(scalbn(LargestF64, INT_MAX, RM).isInfinity());
3390 EXPECT_TRUE(LargestDenormalF64
3391 .bitwiseIsEqual(scalbn(LargestDenormalF64, 0, RM)));
3392 EXPECT_TRUE(NegLargestDenormalF64
3393 .bitwiseIsEqual(scalbn(NegLargestDenormalF64, 0, RM)));
3395 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1022")
3396 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1, RM)));
3397 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1021")
3398 .bitwiseIsEqual(scalbn(NegLargestDenormalF64, 2, RM)));
3400 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep+1")
3401 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1024, RM)));
3402 EXPECT_TRUE(scalbn(LargestDenormalF64, -1023, RM).isPosZero());
3403 EXPECT_TRUE(scalbn(LargestDenormalF64, -1024, RM).isPosZero());
3404 EXPECT_TRUE(scalbn(LargestDenormalF64, -2048, RM).isPosZero());
3405 EXPECT_TRUE(scalbn(LargestDenormalF64, 2047, RM).isInfinity());
3406 EXPECT_TRUE(scalbn(LargestDenormalF64, 2098, RM).isInfinity());
3407 EXPECT_TRUE(scalbn(LargestDenormalF64, 2099, RM).isInfinity());
3409 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-2")
3410 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1021, RM)));
3411 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1")
3412 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1022, RM)));
3413 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep+0")
3414 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1023, RM)));
3415 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep+1023")
3416 .bitwiseIsEqual(scalbn(LargestDenormalF64, 2046, RM)));
3417 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+974")
3418 .bitwiseIsEqual(scalbn(SmallestF64, 2048, RM)));
3420 APFloat RandomDenormalF64(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp+51");
3421 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-972")
3422 .bitwiseIsEqual(scalbn(RandomDenormalF64, -1023, RM)));
3423 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-1")
3424 .bitwiseIsEqual(scalbn(RandomDenormalF64, -52, RM)));
3425 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-2")
3426 .bitwiseIsEqual(scalbn(RandomDenormalF64, -53, RM)));
3427 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp+0")
3428 .bitwiseIsEqual(scalbn(RandomDenormalF64, -51, RM)));
3430 EXPECT_TRUE(scalbn(RandomDenormalF64, -2097, RM).isPosZero());
3431 EXPECT_TRUE(scalbn(RandomDenormalF64, -2090, RM).isPosZero());
3434 EXPECT_TRUE(
3435 APFloat(APFloat::IEEEdouble(), "-0x1p-1073")
3436 .bitwiseIsEqual(scalbn(NegLargestF64, -2097, RM)));
3438 EXPECT_TRUE(
3439 APFloat(APFloat::IEEEdouble(), "-0x1p-1024")
3440 .bitwiseIsEqual(scalbn(NegLargestF64, -2048, RM)));
3442 EXPECT_TRUE(
3443 APFloat(APFloat::IEEEdouble(), "0x1p-1073")
3444 .bitwiseIsEqual(scalbn(LargestF64, -2097, RM)));
3446 EXPECT_TRUE(
3447 APFloat(APFloat::IEEEdouble(), "0x1p-1074")
3448 .bitwiseIsEqual(scalbn(LargestF64, -2098, RM)));
3449 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1p-1074")
3450 .bitwiseIsEqual(scalbn(NegLargestF64, -2098, RM)));
3451 EXPECT_TRUE(scalbn(NegLargestF64, -2099, RM).isNegZero());
3452 EXPECT_TRUE(scalbn(LargestF64, 1, RM).isInfinity());
3455 EXPECT_TRUE(
3456 APFloat(APFloat::IEEEdouble(), "0x1p+0")
3457 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEdouble(), "0x1p+52"), -52, RM)));
3459 EXPECT_TRUE(
3460 APFloat(APFloat::IEEEdouble(), "0x1p-103")
3461 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEdouble(), "0x1p-51"), -52, RM)));
3464 TEST(APFloatTest, frexp) {
3465 const APFloat::roundingMode RM = APFloat::rmNearestTiesToEven;
3467 APFloat PZero = APFloat::getZero(APFloat::IEEEdouble(), false);
3468 APFloat MZero = APFloat::getZero(APFloat::IEEEdouble(), true);
3469 APFloat One(1.0);
3470 APFloat MOne(-1.0);
3471 APFloat Two(2.0);
3472 APFloat MTwo(-2.0);
3474 APFloat LargestDenormal(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1023");
3475 APFloat NegLargestDenormal(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1023");
3477 APFloat Smallest = APFloat::getSmallest(APFloat::IEEEdouble(), false);
3478 APFloat NegSmallest = APFloat::getSmallest(APFloat::IEEEdouble(), true);
3480 APFloat Largest = APFloat::getLargest(APFloat::IEEEdouble(), false);
3481 APFloat NegLargest = APFloat::getLargest(APFloat::IEEEdouble(), true);
3483 APFloat PInf = APFloat::getInf(APFloat::IEEEdouble(), false);
3484 APFloat MInf = APFloat::getInf(APFloat::IEEEdouble(), true);
3486 APFloat QPNaN = APFloat::getNaN(APFloat::IEEEdouble(), false);
3487 APFloat QMNaN = APFloat::getNaN(APFloat::IEEEdouble(), true);
3488 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEdouble(), false);
3490 // Make sure highest bit of payload is preserved.
3491 const APInt Payload(64, (UINT64_C(1) << 50) |
3492 (UINT64_C(1) << 49) |
3493 (UINT64_C(1234) << 32) |
3496 APFloat SNaNWithPayload = APFloat::getSNaN(APFloat::IEEEdouble(), false,
3497 &Payload);
3499 APFloat SmallestNormalized
3500 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), false);
3501 APFloat NegSmallestNormalized
3502 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), true);
3504 int Exp;
3505 APFloat Frac(APFloat::IEEEdouble());
3508 Frac = frexp(PZero, Exp, RM);
3509 EXPECT_EQ(0, Exp);
3510 EXPECT_TRUE(Frac.isPosZero());
3512 Frac = frexp(MZero, Exp, RM);
3513 EXPECT_EQ(0, Exp);
3514 EXPECT_TRUE(Frac.isNegZero());
3517 Frac = frexp(One, Exp, RM);
3518 EXPECT_EQ(1, Exp);
3519 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p-1").bitwiseIsEqual(Frac));
3521 Frac = frexp(MOne, Exp, RM);
3522 EXPECT_EQ(1, Exp);
3523 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1p-1").bitwiseIsEqual(Frac));
3525 Frac = frexp(LargestDenormal, Exp, RM);
3526 EXPECT_EQ(-1022, Exp);
3527 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1").bitwiseIsEqual(Frac));
3529 Frac = frexp(NegLargestDenormal, Exp, RM);
3530 EXPECT_EQ(-1022, Exp);
3531 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1").bitwiseIsEqual(Frac));
3534 Frac = frexp(Smallest, Exp, RM);
3535 EXPECT_EQ(-1073, Exp);
3536 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p-1").bitwiseIsEqual(Frac));
3538 Frac = frexp(NegSmallest, Exp, RM);
3539 EXPECT_EQ(-1073, Exp);
3540 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1p-1").bitwiseIsEqual(Frac));
3543 Frac = frexp(Largest, Exp, RM);
3544 EXPECT_EQ(1024, Exp);
3545 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.fffffffffffffp-1").bitwiseIsEqual(Frac));
3547 Frac = frexp(NegLargest, Exp, RM);
3548 EXPECT_EQ(1024, Exp);
3549 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1.fffffffffffffp-1").bitwiseIsEqual(Frac));
3552 Frac = frexp(PInf, Exp, RM);
3553 EXPECT_EQ(INT_MAX, Exp);
3554 EXPECT_TRUE(Frac.isInfinity() && !Frac.isNegative());
3556 Frac = frexp(MInf, Exp, RM);
3557 EXPECT_EQ(INT_MAX, Exp);
3558 EXPECT_TRUE(Frac.isInfinity() && Frac.isNegative());
3560 Frac = frexp(QPNaN, Exp, RM);
3561 EXPECT_EQ(INT_MIN, Exp);
3562 EXPECT_TRUE(Frac.isNaN());
3564 Frac = frexp(QMNaN, Exp, RM);
3565 EXPECT_EQ(INT_MIN, Exp);
3566 EXPECT_TRUE(Frac.isNaN());
3568 Frac = frexp(SNaN, Exp, RM);
3569 EXPECT_EQ(INT_MIN, Exp);
3570 EXPECT_TRUE(Frac.isNaN() && !Frac.isSignaling());
3572 Frac = frexp(SNaNWithPayload, Exp, RM);
3573 EXPECT_EQ(INT_MIN, Exp);
3574 EXPECT_TRUE(Frac.isNaN() && !Frac.isSignaling());
3575 EXPECT_EQ(Payload, Frac.bitcastToAPInt().getLoBits(51));
3577 Frac = frexp(APFloat(APFloat::IEEEdouble(), "0x0.ffffp-1"), Exp, RM);
3578 EXPECT_EQ(-1, Exp);
3579 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.fffep-1").bitwiseIsEqual(Frac));
3581 Frac = frexp(APFloat(APFloat::IEEEdouble(), "0x1p-51"), Exp, RM);
3582 EXPECT_EQ(-50, Exp);
3583 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p-1").bitwiseIsEqual(Frac));
3585 Frac = frexp(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp+51"), Exp, RM);
3586 EXPECT_EQ(52, Exp);
3587 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-1").bitwiseIsEqual(Frac));
3590 TEST(APFloatTest, mod) {
3592 APFloat f1(APFloat::IEEEdouble(), "1.5");
3593 APFloat f2(APFloat::IEEEdouble(), "1.0");
3594 APFloat expected(APFloat::IEEEdouble(), "0.5");
3595 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3596 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3599 APFloat f1(APFloat::IEEEdouble(), "0.5");
3600 APFloat f2(APFloat::IEEEdouble(), "1.0");
3601 APFloat expected(APFloat::IEEEdouble(), "0.5");
3602 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3603 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3606 APFloat f1(APFloat::IEEEdouble(), "0x1.3333333333333p-2"); // 0.3
3607 APFloat f2(APFloat::IEEEdouble(), "0x1.47ae147ae147bp-7"); // 0.01
3608 APFloat expected(APFloat::IEEEdouble(),
3609 "0x1.47ae147ae1471p-7"); // 0.009999999999999983
3610 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3611 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3614 APFloat f1(APFloat::IEEEdouble(), "0x1p64"); // 1.8446744073709552e19
3615 APFloat f2(APFloat::IEEEdouble(), "1.5");
3616 APFloat expected(APFloat::IEEEdouble(), "1.0");
3617 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3618 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3621 APFloat f1(APFloat::IEEEdouble(), "0x1p1000");
3622 APFloat f2(APFloat::IEEEdouble(), "0x1p-1000");
3623 APFloat expected(APFloat::IEEEdouble(), "0.0");
3624 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3625 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3628 APFloat f1(APFloat::IEEEdouble(), "0.0");
3629 APFloat f2(APFloat::IEEEdouble(), "1.0");
3630 APFloat expected(APFloat::IEEEdouble(), "0.0");
3631 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3632 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3635 APFloat f1(APFloat::IEEEdouble(), "1.0");
3636 APFloat f2(APFloat::IEEEdouble(), "0.0");
3637 EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp);
3638 EXPECT_TRUE(f1.isNaN());
3641 APFloat f1(APFloat::IEEEdouble(), "0.0");
3642 APFloat f2(APFloat::IEEEdouble(), "0.0");
3643 EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp);
3644 EXPECT_TRUE(f1.isNaN());
3647 APFloat f1 = APFloat::getInf(APFloat::IEEEdouble(), false);
3648 APFloat f2(APFloat::IEEEdouble(), "1.0");
3649 EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp);
3650 EXPECT_TRUE(f1.isNaN());
3653 APFloat f1(APFloat::IEEEdouble(), "-4.0");
3654 APFloat f2(APFloat::IEEEdouble(), "-2.0");
3655 APFloat expected(APFloat::IEEEdouble(), "-0.0");
3656 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3657 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3660 APFloat f1(APFloat::IEEEdouble(), "-4.0");
3661 APFloat f2(APFloat::IEEEdouble(), "2.0");
3662 APFloat expected(APFloat::IEEEdouble(), "-0.0");
3663 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3664 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3668 TEST(APFloatTest, remainder) {
3669 // Test Special Cases against each other and normal values.
3671 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
3672 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
3673 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
3674 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
3675 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
3676 APFloat SNaN = APFloat(APFloat::IEEEsingle(), "snan123");
3677 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
3678 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
3679 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
3680 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
3681 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
3682 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
3683 APFloat PSmallestNormalized =
3684 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
3685 APFloat MSmallestNormalized =
3686 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
3688 APFloat PVal1(APFloat::IEEEsingle(), "0x1.fffffep+126");
3689 APFloat MVal1(APFloat::IEEEsingle(), "-0x1.fffffep+126");
3690 APFloat PVal2(APFloat::IEEEsingle(), "0x1.fffffep-126");
3691 APFloat MVal2(APFloat::IEEEsingle(), "-0x1.fffffep-126");
3692 APFloat PVal3(APFloat::IEEEsingle(), "0x1p-125");
3693 APFloat MVal3(APFloat::IEEEsingle(), "-0x1p-125");
3694 APFloat PVal4(APFloat::IEEEsingle(), "0x1p+127");
3695 APFloat MVal4(APFloat::IEEEsingle(), "-0x1p+127");
3696 APFloat PVal5(APFloat::IEEEsingle(), "1.5");
3697 APFloat MVal5(APFloat::IEEEsingle(), "-1.5");
3698 APFloat PVal6(APFloat::IEEEsingle(), "1");
3699 APFloat MVal6(APFloat::IEEEsingle(), "-1");
3701 struct {
3702 APFloat x;
3703 APFloat y;
3704 const char *result;
3705 int status;
3706 int category;
3707 } SpecialCaseTests[] = {
3708 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3709 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3710 { PInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3711 { PInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3712 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3713 { PInf, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3714 { PInf, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3715 { PInf, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3716 { PInf, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3717 { PInf, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3718 { PInf, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3719 { PInf, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3720 { PInf, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3721 { PInf, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3722 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3723 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3724 { MInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3725 { MInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3726 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3727 { MInf, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3728 { MInf, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3729 { MInf, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3730 { MInf, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3731 { MInf, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3732 { MInf, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3733 { MInf, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3734 { MInf, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3735 { MInf, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3736 { PZero, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3737 { PZero, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3738 { PZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3739 { PZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3740 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3741 { PZero, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3742 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3743 { PZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3744 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3745 { PZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3746 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3747 { PZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3748 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3749 { PZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3750 { MZero, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3751 { MZero, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3752 { MZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3753 { MZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3754 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3755 { MZero, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3756 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3757 { MZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3758 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3759 { MZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3760 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3761 { MZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3762 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3763 { MZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3764 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
3765 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
3766 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
3767 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
3768 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3769 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3770 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
3771 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
3772 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
3773 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
3774 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
3775 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
3776 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
3777 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
3778 { SNaN, PInf, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3779 { SNaN, MInf, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3780 { SNaN, PZero, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3781 { SNaN, MZero, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3782 { SNaN, QNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3783 { SNaN, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3784 { SNaN, PNormalValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3785 { SNaN, MNormalValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3786 { SNaN, PLargestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3787 { SNaN, MLargestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3788 { SNaN, PSmallestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3789 { SNaN, MSmallestValue, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3790 { SNaN, PSmallestNormalized, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3791 { SNaN, MSmallestNormalized, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3792 { PNormalValue, PInf, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
3793 { PNormalValue, MInf, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
3794 { PNormalValue, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3795 { PNormalValue, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3796 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3797 { PNormalValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3798 { PNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3799 { PNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3800 { PNormalValue, PLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
3801 { PNormalValue, MLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
3802 { PNormalValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3803 { PNormalValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3804 { PNormalValue, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3805 { PNormalValue, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3806 { MNormalValue, PInf, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
3807 { MNormalValue, MInf, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
3808 { MNormalValue, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3809 { MNormalValue, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3810 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3811 { MNormalValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3812 { MNormalValue, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3813 { MNormalValue, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3814 { MNormalValue, PLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
3815 { MNormalValue, MLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
3816 { MNormalValue, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3817 { MNormalValue, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3818 { MNormalValue, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3819 { MNormalValue, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3820 { PLargestValue, PInf, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
3821 { PLargestValue, MInf, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
3822 { PLargestValue, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3823 { PLargestValue, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3824 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3825 { PLargestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3826 { PLargestValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3827 { PLargestValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3828 { PLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3829 { PLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3830 { PLargestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3831 { PLargestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3832 { PLargestValue, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3833 { PLargestValue, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3834 { MLargestValue, PInf, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
3835 { MLargestValue, MInf, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
3836 { MLargestValue, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3837 { MLargestValue, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3838 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3839 { MLargestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3840 { MLargestValue, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3841 { MLargestValue, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3842 { MLargestValue, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3843 { MLargestValue, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3844 { MLargestValue, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3845 { MLargestValue, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3846 { MLargestValue, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3847 { MLargestValue, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3848 { PSmallestValue, PInf, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
3849 { PSmallestValue, MInf, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
3850 { PSmallestValue, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3851 { PSmallestValue, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3852 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3853 { PSmallestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3854 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
3855 { PSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
3856 { PSmallestValue, PLargestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
3857 { PSmallestValue, MLargestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
3858 { PSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3859 { PSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3860 { PSmallestValue, PSmallestNormalized, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
3861 { PSmallestValue, MSmallestNormalized, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
3862 { MSmallestValue, PInf, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
3863 { MSmallestValue, MInf, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
3864 { MSmallestValue, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3865 { MSmallestValue, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3866 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3867 { MSmallestValue, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3868 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
3869 { MSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
3870 { MSmallestValue, PLargestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
3871 { MSmallestValue, MLargestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
3872 { MSmallestValue, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3873 { MSmallestValue, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3874 { MSmallestValue, PSmallestNormalized, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
3875 { MSmallestValue, MSmallestNormalized, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
3876 { PSmallestNormalized, PInf, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
3877 { PSmallestNormalized, MInf, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
3878 { PSmallestNormalized, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3879 { PSmallestNormalized, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3880 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3881 { PSmallestNormalized, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3882 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
3883 { PSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
3884 { PSmallestNormalized, PLargestValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
3885 { PSmallestNormalized, MLargestValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
3886 { PSmallestNormalized, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3887 { PSmallestNormalized, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3888 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3889 { PSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3890 { MSmallestNormalized, PInf, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
3891 { MSmallestNormalized, MInf, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
3892 { MSmallestNormalized, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3893 { MSmallestNormalized, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
3894 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
3895 { MSmallestNormalized, SNaN, "nan123", APFloat::opInvalidOp, APFloat::fcNaN },
3896 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
3897 { MSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
3898 { MSmallestNormalized, PLargestValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
3899 { MSmallestNormalized, MLargestValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
3900 { MSmallestNormalized, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3901 { MSmallestNormalized, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3902 { MSmallestNormalized, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3903 { MSmallestNormalized, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3905 { PVal1, PVal1, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3906 { PVal1, MVal1, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3907 { PVal1, PVal2, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3908 { PVal1, MVal2, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3909 { PVal1, PVal3, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3910 { PVal1, MVal3, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3911 { PVal1, PVal4, "-0x1p+103", APFloat::opOK, APFloat::fcNormal },
3912 { PVal1, MVal4, "-0x1p+103", APFloat::opOK, APFloat::fcNormal },
3913 { PVal1, PVal5, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3914 { PVal1, MVal5, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3915 { PVal1, PVal6, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3916 { PVal1, MVal6, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3917 { MVal1, PVal1, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3918 { MVal1, MVal1, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3919 { MVal1, PVal2, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3920 { MVal1, MVal2, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3921 { MVal1, PVal3, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3922 { MVal1, MVal3, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3923 { MVal1, PVal4, "0x1p+103", APFloat::opOK, APFloat::fcNormal },
3924 { MVal1, MVal4, "0x1p+103", APFloat::opOK, APFloat::fcNormal },
3925 { MVal1, PVal5, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3926 { MVal1, MVal5, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3927 { MVal1, PVal6, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3928 { MVal1, MVal6, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3929 { PVal2, PVal1, "0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3930 { PVal2, MVal1, "0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3931 { PVal2, PVal2, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3932 { PVal2, MVal2, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3933 { PVal2, PVal3, "-0x0.000002p-126", APFloat::opOK, APFloat::fcNormal },
3934 { PVal2, MVal3, "-0x0.000002p-126", APFloat::opOK, APFloat::fcNormal },
3935 { PVal2, PVal4, "0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3936 { PVal2, MVal4, "0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3937 { PVal2, PVal5, "0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3938 { PVal2, MVal5, "0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3939 { PVal2, PVal6, "0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3940 { PVal2, MVal6, "0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3941 { MVal2, PVal1, "-0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3942 { MVal2, MVal1, "-0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3943 { MVal2, PVal2, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3944 { MVal2, MVal2, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3945 { MVal2, PVal3, "0x0.000002p-126", APFloat::opOK, APFloat::fcNormal },
3946 { MVal2, MVal3, "0x0.000002p-126", APFloat::opOK, APFloat::fcNormal },
3947 { MVal2, PVal4, "-0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3948 { MVal2, MVal4, "-0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3949 { MVal2, PVal5, "-0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3950 { MVal2, MVal5, "-0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3951 { MVal2, PVal6, "-0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3952 { MVal2, MVal6, "-0x1.fffffep-126", APFloat::opOK, APFloat::fcNormal },
3953 { PVal3, PVal1, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
3954 { PVal3, MVal1, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
3955 { PVal3, PVal2, "0x0.000002p-126", APFloat::opOK, APFloat::fcNormal },
3956 { PVal3, MVal2, "0x0.000002p-126", APFloat::opOK, APFloat::fcNormal },
3957 { PVal3, PVal3, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3958 { PVal3, MVal3, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3959 { PVal3, PVal4, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
3960 { PVal3, MVal4, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
3961 { PVal3, PVal5, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
3962 { PVal3, MVal5, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
3963 { PVal3, PVal6, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
3964 { PVal3, MVal6, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
3965 { MVal3, PVal1, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
3966 { MVal3, MVal1, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
3967 { MVal3, PVal2, "-0x0.000002p-126", APFloat::opOK, APFloat::fcNormal },
3968 { MVal3, MVal2, "-0x0.000002p-126", APFloat::opOK, APFloat::fcNormal },
3969 { MVal3, PVal3, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3970 { MVal3, MVal3, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3971 { MVal3, PVal4, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
3972 { MVal3, MVal4, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
3973 { MVal3, PVal5, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
3974 { MVal3, MVal5, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
3975 { MVal3, PVal6, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
3976 { MVal3, MVal6, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
3977 { PVal4, PVal1, "0x1p+103", APFloat::opOK, APFloat::fcNormal },
3978 { PVal4, MVal1, "0x1p+103", APFloat::opOK, APFloat::fcNormal },
3979 { PVal4, PVal2, "0x0.002p-126", APFloat::opOK, APFloat::fcNormal },
3980 { PVal4, MVal2, "0x0.002p-126", APFloat::opOK, APFloat::fcNormal },
3981 { PVal4, PVal3, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3982 { PVal4, MVal3, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3983 { PVal4, PVal4, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3984 { PVal4, MVal4, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3985 { PVal4, PVal5, "0.5", APFloat::opOK, APFloat::fcNormal },
3986 { PVal4, MVal5, "0.5", APFloat::opOK, APFloat::fcNormal },
3987 { PVal4, PVal6, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3988 { PVal4, MVal6, "0x0p+0", APFloat::opOK, APFloat::fcZero },
3989 { MVal4, PVal1, "-0x1p+103", APFloat::opOK, APFloat::fcNormal },
3990 { MVal4, MVal1, "-0x1p+103", APFloat::opOK, APFloat::fcNormal },
3991 { MVal4, PVal2, "-0x0.002p-126", APFloat::opOK, APFloat::fcNormal },
3992 { MVal4, MVal2, "-0x0.002p-126", APFloat::opOK, APFloat::fcNormal },
3993 { MVal4, PVal3, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3994 { MVal4, MVal3, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3995 { MVal4, PVal4, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3996 { MVal4, MVal4, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
3997 { MVal4, PVal5, "-0.5", APFloat::opOK, APFloat::fcNormal },
3998 { MVal4, MVal5, "-0.5", APFloat::opOK, APFloat::fcNormal },
3999 { MVal4, PVal6, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
4000 { MVal4, MVal6, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
4001 { PVal5, PVal1, "1.5", APFloat::opOK, APFloat::fcNormal },
4002 { PVal5, MVal1, "1.5", APFloat::opOK, APFloat::fcNormal },
4003 { PVal5, PVal2, "0x0.00006p-126", APFloat::opOK, APFloat::fcNormal },
4004 { PVal5, MVal2, "0x0.00006p-126", APFloat::opOK, APFloat::fcNormal },
4005 { PVal5, PVal3, "0x0p+0", APFloat::opOK, APFloat::fcZero },
4006 { PVal5, MVal3, "0x0p+0", APFloat::opOK, APFloat::fcZero },
4007 { PVal5, PVal4, "1.5", APFloat::opOK, APFloat::fcNormal },
4008 { PVal5, MVal4, "1.5", APFloat::opOK, APFloat::fcNormal },
4009 { PVal5, PVal5, "0x0p+0", APFloat::opOK, APFloat::fcZero },
4010 { PVal5, MVal5, "0x0p+0", APFloat::opOK, APFloat::fcZero },
4011 { PVal5, PVal6, "-0.5", APFloat::opOK, APFloat::fcNormal },
4012 { PVal5, MVal6, "-0.5", APFloat::opOK, APFloat::fcNormal },
4013 { MVal5, PVal1, "-1.5", APFloat::opOK, APFloat::fcNormal },
4014 { MVal5, MVal1, "-1.5", APFloat::opOK, APFloat::fcNormal },
4015 { MVal5, PVal2, "-0x0.00006p-126", APFloat::opOK, APFloat::fcNormal },
4016 { MVal5, MVal2, "-0x0.00006p-126", APFloat::opOK, APFloat::fcNormal },
4017 { MVal5, PVal3, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
4018 { MVal5, MVal3, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
4019 { MVal5, PVal4, "-1.5", APFloat::opOK, APFloat::fcNormal },
4020 { MVal5, MVal4, "-1.5", APFloat::opOK, APFloat::fcNormal },
4021 { MVal5, PVal5, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
4022 { MVal5, MVal5, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
4023 { MVal5, PVal6, "0.5", APFloat::opOK, APFloat::fcNormal },
4024 { MVal5, MVal6, "0.5", APFloat::opOK, APFloat::fcNormal },
4025 { PVal6, PVal1, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
4026 { PVal6, MVal1, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
4027 { PVal6, PVal2, "0x0.00004p-126", APFloat::opOK, APFloat::fcNormal },
4028 { PVal6, MVal2, "0x0.00004p-126", APFloat::opOK, APFloat::fcNormal },
4029 { PVal6, PVal3, "0x0p+0", APFloat::opOK, APFloat::fcZero },
4030 { PVal6, MVal3, "0x0p+0", APFloat::opOK, APFloat::fcZero },
4031 { PVal6, PVal4, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
4032 { PVal6, MVal4, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
4033 { PVal6, PVal5, "-0.5", APFloat::opOK, APFloat::fcNormal },
4034 { PVal6, MVal5, "-0.5", APFloat::opOK, APFloat::fcNormal },
4035 { PVal6, PVal6, "0x0p+0", APFloat::opOK, APFloat::fcZero },
4036 { PVal6, MVal6, "0x0p+0", APFloat::opOK, APFloat::fcZero },
4037 { MVal6, PVal1, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
4038 { MVal6, MVal1, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
4039 { MVal6, PVal2, "-0x0.00004p-126", APFloat::opOK, APFloat::fcNormal },
4040 { MVal6, MVal2, "-0x0.00004p-126", APFloat::opOK, APFloat::fcNormal },
4041 { MVal6, PVal3, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
4042 { MVal6, MVal3, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
4043 { MVal6, PVal4, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
4044 { MVal6, MVal4, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
4045 { MVal6, PVal5, "0.5", APFloat::opOK, APFloat::fcNormal },
4046 { MVal6, MVal5, "0.5", APFloat::opOK, APFloat::fcNormal },
4047 { MVal6, PVal6, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
4048 { MVal6, MVal6, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
4051 for (size_t i = 0; i < std::size(SpecialCaseTests); ++i) {
4052 APFloat x(SpecialCaseTests[i].x);
4053 APFloat y(SpecialCaseTests[i].y);
4054 APFloat::opStatus status = x.remainder(y);
4056 APFloat result(x.getSemantics(), SpecialCaseTests[i].result);
4058 EXPECT_TRUE(result.bitwiseIsEqual(x));
4059 EXPECT_EQ(SpecialCaseTests[i].status, (int)status);
4060 EXPECT_EQ(SpecialCaseTests[i].category, (int)x.getCategory());
4064 APFloat f1(APFloat::IEEEdouble(), "0x1.3333333333333p-2"); // 0.3
4065 APFloat f2(APFloat::IEEEdouble(), "0x1.47ae147ae147bp-7"); // 0.01
4066 APFloat expected(APFloat::IEEEdouble(), "-0x1.4p-56");
4067 EXPECT_EQ(APFloat::opOK, f1.remainder(f2));
4068 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
4071 APFloat f1(APFloat::IEEEdouble(), "0x1p64"); // 1.8446744073709552e19
4072 APFloat f2(APFloat::IEEEdouble(), "1.5");
4073 APFloat expected(APFloat::IEEEdouble(), "-0.5");
4074 EXPECT_EQ(APFloat::opOK, f1.remainder(f2));
4075 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
4078 APFloat f1(APFloat::IEEEdouble(), "0x1p1000");
4079 APFloat f2(APFloat::IEEEdouble(), "0x1p-1000");
4080 APFloat expected(APFloat::IEEEdouble(), "0.0");
4081 EXPECT_EQ(APFloat::opOK, f1.remainder(f2));
4082 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
4085 APFloat f1 = APFloat::getInf(APFloat::IEEEdouble(), false);
4086 APFloat f2(APFloat::IEEEdouble(), "1.0");
4087 EXPECT_EQ(f1.remainder(f2), APFloat::opInvalidOp);
4088 EXPECT_TRUE(f1.isNaN());
4091 APFloat f1(APFloat::IEEEdouble(), "-4.0");
4092 APFloat f2(APFloat::IEEEdouble(), "-2.0");
4093 APFloat expected(APFloat::IEEEdouble(), "-0.0");
4094 EXPECT_EQ(APFloat::opOK, f1.remainder(f2));
4095 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
4098 APFloat f1(APFloat::IEEEdouble(), "-4.0");
4099 APFloat f2(APFloat::IEEEdouble(), "2.0");
4100 APFloat expected(APFloat::IEEEdouble(), "-0.0");
4101 EXPECT_EQ(APFloat::opOK, f1.remainder(f2));
4102 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
4106 TEST(APFloatTest, PPCDoubleDoubleAddSpecial) {
4107 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t,
4108 APFloat::fltCategory, APFloat::roundingMode>;
4109 DataType Data[] = {
4110 // (1 + 0) + (-1 + 0) = fcZero
4111 std::make_tuple(0x3ff0000000000000ull, 0, 0xbff0000000000000ull, 0,
4112 APFloat::fcZero, APFloat::rmNearestTiesToEven),
4113 // LDBL_MAX + (1.1 >> (1023 - 106) + 0)) = fcInfinity
4114 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
4115 0x7948000000000000ull, 0ull, APFloat::fcInfinity,
4116 APFloat::rmNearestTiesToEven),
4117 // TODO: change the 4th 0x75effffffffffffe to 0x75efffffffffffff when
4118 // semPPCDoubleDoubleLegacy is gone.
4119 // LDBL_MAX + (1.011111... >> (1023 - 106) + (1.1111111...0 >> (1023 -
4120 // 160))) = fcNormal
4121 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
4122 0x7947ffffffffffffull, 0x75effffffffffffeull,
4123 APFloat::fcNormal, APFloat::rmNearestTiesToEven),
4124 // LDBL_MAX + (1.1 >> (1023 - 106) + 0)) = fcInfinity
4125 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
4126 0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
4127 APFloat::fcInfinity, APFloat::rmNearestTiesToEven),
4128 // NaN + (1 + 0) = fcNaN
4129 std::make_tuple(0x7ff8000000000000ull, 0, 0x3ff0000000000000ull, 0,
4130 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
4133 for (auto Tp : Data) {
4134 uint64_t Op1[2], Op2[2];
4135 APFloat::fltCategory Expected;
4136 APFloat::roundingMode RM;
4137 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected, RM) = Tp;
4140 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4141 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4142 A1.add(A2, RM);
4144 EXPECT_EQ(Expected, A1.getCategory())
4145 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op1[0], Op1[1],
4146 Op2[0], Op2[1])
4147 .str();
4150 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4151 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4152 A2.add(A1, RM);
4154 EXPECT_EQ(Expected, A2.getCategory())
4155 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op2[0], Op2[1],
4156 Op1[0], Op1[1])
4157 .str();
4162 TEST(APFloatTest, PPCDoubleDoubleAdd) {
4163 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
4164 uint64_t, APFloat::roundingMode>;
4165 DataType Data[] = {
4166 // (1 + 0) + (1e-105 + 0) = (1 + 1e-105)
4167 std::make_tuple(0x3ff0000000000000ull, 0, 0x3960000000000000ull, 0,
4168 0x3ff0000000000000ull, 0x3960000000000000ull,
4169 APFloat::rmNearestTiesToEven),
4170 // (1 + 0) + (1e-106 + 0) = (1 + 1e-106)
4171 std::make_tuple(0x3ff0000000000000ull, 0, 0x3950000000000000ull, 0,
4172 0x3ff0000000000000ull, 0x3950000000000000ull,
4173 APFloat::rmNearestTiesToEven),
4174 // (1 + 1e-106) + (1e-106 + 0) = (1 + 1e-105)
4175 std::make_tuple(0x3ff0000000000000ull, 0x3950000000000000ull,
4176 0x3950000000000000ull, 0, 0x3ff0000000000000ull,
4177 0x3960000000000000ull, APFloat::rmNearestTiesToEven),
4178 // (1 + 0) + (epsilon + 0) = (1 + epsilon)
4179 std::make_tuple(0x3ff0000000000000ull, 0, 0x0000000000000001ull, 0,
4180 0x3ff0000000000000ull, 0x0000000000000001ull,
4181 APFloat::rmNearestTiesToEven),
4182 // TODO: change 0xf950000000000000 to 0xf940000000000000, when
4183 // semPPCDoubleDoubleLegacy is gone.
4184 // (DBL_MAX - 1 << (1023 - 105)) + (1 << (1023 - 53) + 0) = DBL_MAX +
4185 // 1.11111... << (1023 - 52)
4186 std::make_tuple(0x7fefffffffffffffull, 0xf950000000000000ull,
4187 0x7c90000000000000ull, 0, 0x7fefffffffffffffull,
4188 0x7c8ffffffffffffeull, APFloat::rmNearestTiesToEven),
4189 // TODO: change 0xf950000000000000 to 0xf940000000000000, when
4190 // semPPCDoubleDoubleLegacy is gone.
4191 // (1 << (1023 - 53) + 0) + (DBL_MAX - 1 << (1023 - 105)) = DBL_MAX +
4192 // 1.11111... << (1023 - 52)
4193 std::make_tuple(0x7c90000000000000ull, 0, 0x7fefffffffffffffull,
4194 0xf950000000000000ull, 0x7fefffffffffffffull,
4195 0x7c8ffffffffffffeull, APFloat::rmNearestTiesToEven),
4198 for (auto Tp : Data) {
4199 uint64_t Op1[2], Op2[2], Expected[2];
4200 APFloat::roundingMode RM;
4201 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
4204 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4205 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4206 A1.add(A2, RM);
4208 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
4209 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op1[0], Op1[1],
4210 Op2[0], Op2[1])
4211 .str();
4212 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
4213 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op1[0], Op1[1],
4214 Op2[0], Op2[1])
4215 .str();
4218 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4219 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4220 A2.add(A1, RM);
4222 EXPECT_EQ(Expected[0], A2.bitcastToAPInt().getRawData()[0])
4223 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op2[0], Op2[1],
4224 Op1[0], Op1[1])
4225 .str();
4226 EXPECT_EQ(Expected[1], A2.bitcastToAPInt().getRawData()[1])
4227 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op2[0], Op2[1],
4228 Op1[0], Op1[1])
4229 .str();
4234 TEST(APFloatTest, PPCDoubleDoubleSubtract) {
4235 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
4236 uint64_t, APFloat::roundingMode>;
4237 DataType Data[] = {
4238 // (1 + 0) - (-1e-105 + 0) = (1 + 1e-105)
4239 std::make_tuple(0x3ff0000000000000ull, 0, 0xb960000000000000ull, 0,
4240 0x3ff0000000000000ull, 0x3960000000000000ull,
4241 APFloat::rmNearestTiesToEven),
4242 // (1 + 0) - (-1e-106 + 0) = (1 + 1e-106)
4243 std::make_tuple(0x3ff0000000000000ull, 0, 0xb950000000000000ull, 0,
4244 0x3ff0000000000000ull, 0x3950000000000000ull,
4245 APFloat::rmNearestTiesToEven),
4248 for (auto Tp : Data) {
4249 uint64_t Op1[2], Op2[2], Expected[2];
4250 APFloat::roundingMode RM;
4251 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
4253 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4254 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4255 A1.subtract(A2, RM);
4257 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
4258 << formatv("({0:x} + {1:x}) - ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
4259 Op2[1])
4260 .str();
4261 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
4262 << formatv("({0:x} + {1:x}) - ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
4263 Op2[1])
4264 .str();
4268 TEST(APFloatTest, PPCDoubleDoubleMultiplySpecial) {
4269 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t,
4270 APFloat::fltCategory, APFloat::roundingMode>;
4271 DataType Data[] = {
4272 // fcNaN * fcNaN = fcNaN
4273 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull, 0,
4274 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
4275 // fcNaN * fcZero = fcNaN
4276 std::make_tuple(0x7ff8000000000000ull, 0, 0, 0, APFloat::fcNaN,
4277 APFloat::rmNearestTiesToEven),
4278 // fcNaN * fcInfinity = fcNaN
4279 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff0000000000000ull, 0,
4280 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
4281 // fcNaN * fcNormal = fcNaN
4282 std::make_tuple(0x7ff8000000000000ull, 0, 0x3ff0000000000000ull, 0,
4283 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
4284 // fcInfinity * fcInfinity = fcInfinity
4285 std::make_tuple(0x7ff0000000000000ull, 0, 0x7ff0000000000000ull, 0,
4286 APFloat::fcInfinity, APFloat::rmNearestTiesToEven),
4287 // fcInfinity * fcZero = fcNaN
4288 std::make_tuple(0x7ff0000000000000ull, 0, 0, 0, APFloat::fcNaN,
4289 APFloat::rmNearestTiesToEven),
4290 // fcInfinity * fcNormal = fcInfinity
4291 std::make_tuple(0x7ff0000000000000ull, 0, 0x3ff0000000000000ull, 0,
4292 APFloat::fcInfinity, APFloat::rmNearestTiesToEven),
4293 // fcZero * fcZero = fcZero
4294 std::make_tuple(0, 0, 0, 0, APFloat::fcZero,
4295 APFloat::rmNearestTiesToEven),
4296 // fcZero * fcNormal = fcZero
4297 std::make_tuple(0, 0, 0x3ff0000000000000ull, 0, APFloat::fcZero,
4298 APFloat::rmNearestTiesToEven),
4301 for (auto Tp : Data) {
4302 uint64_t Op1[2], Op2[2];
4303 APFloat::fltCategory Expected;
4304 APFloat::roundingMode RM;
4305 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected, RM) = Tp;
4308 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4309 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4310 A1.multiply(A2, RM);
4312 EXPECT_EQ(Expected, A1.getCategory())
4313 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op1[0], Op1[1],
4314 Op2[0], Op2[1])
4315 .str();
4318 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4319 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4320 A2.multiply(A1, RM);
4322 EXPECT_EQ(Expected, A2.getCategory())
4323 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op2[0], Op2[1],
4324 Op1[0], Op1[1])
4325 .str();
4330 TEST(APFloatTest, PPCDoubleDoubleMultiply) {
4331 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
4332 uint64_t, APFloat::roundingMode>;
4333 DataType Data[] = {
4334 // 1/3 * 3 = 1.0
4335 std::make_tuple(0x3fd5555555555555ull, 0x3c75555555555556ull,
4336 0x4008000000000000ull, 0, 0x3ff0000000000000ull, 0,
4337 APFloat::rmNearestTiesToEven),
4338 // (1 + epsilon) * (1 + 0) = fcZero
4339 std::make_tuple(0x3ff0000000000000ull, 0x0000000000000001ull,
4340 0x3ff0000000000000ull, 0, 0x3ff0000000000000ull,
4341 0x0000000000000001ull, APFloat::rmNearestTiesToEven),
4342 // (1 + epsilon) * (1 + epsilon) = 1 + 2 * epsilon
4343 std::make_tuple(0x3ff0000000000000ull, 0x0000000000000001ull,
4344 0x3ff0000000000000ull, 0x0000000000000001ull,
4345 0x3ff0000000000000ull, 0x0000000000000002ull,
4346 APFloat::rmNearestTiesToEven),
4347 // -(1 + epsilon) * (1 + epsilon) = -1
4348 std::make_tuple(0xbff0000000000000ull, 0x0000000000000001ull,
4349 0x3ff0000000000000ull, 0x0000000000000001ull,
4350 0xbff0000000000000ull, 0, APFloat::rmNearestTiesToEven),
4351 // (0.5 + 0) * (1 + 2 * epsilon) = 0.5 + epsilon
4352 std::make_tuple(0x3fe0000000000000ull, 0, 0x3ff0000000000000ull,
4353 0x0000000000000002ull, 0x3fe0000000000000ull,
4354 0x0000000000000001ull, APFloat::rmNearestTiesToEven),
4355 // (0.5 + 0) * (1 + epsilon) = 0.5
4356 std::make_tuple(0x3fe0000000000000ull, 0, 0x3ff0000000000000ull,
4357 0x0000000000000001ull, 0x3fe0000000000000ull, 0,
4358 APFloat::rmNearestTiesToEven),
4359 // __LDBL_MAX__ * (1 + 1 << 106) = inf
4360 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
4361 0x3ff0000000000000ull, 0x3950000000000000ull,
4362 0x7ff0000000000000ull, 0, APFloat::rmNearestTiesToEven),
4363 // __LDBL_MAX__ * (1 + 1 << 107) > __LDBL_MAX__, but not inf, yes =_=|||
4364 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
4365 0x3ff0000000000000ull, 0x3940000000000000ull,
4366 0x7fefffffffffffffull, 0x7c8fffffffffffffull,
4367 APFloat::rmNearestTiesToEven),
4368 // __LDBL_MAX__ * (1 + 1 << 108) = __LDBL_MAX__
4369 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
4370 0x3ff0000000000000ull, 0x3930000000000000ull,
4371 0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
4372 APFloat::rmNearestTiesToEven),
4375 for (auto Tp : Data) {
4376 uint64_t Op1[2], Op2[2], Expected[2];
4377 APFloat::roundingMode RM;
4378 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
4381 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4382 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4383 A1.multiply(A2, RM);
4385 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
4386 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op1[0], Op1[1],
4387 Op2[0], Op2[1])
4388 .str();
4389 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
4390 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op1[0], Op1[1],
4391 Op2[0], Op2[1])
4392 .str();
4395 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4396 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4397 A2.multiply(A1, RM);
4399 EXPECT_EQ(Expected[0], A2.bitcastToAPInt().getRawData()[0])
4400 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op2[0], Op2[1],
4401 Op1[0], Op1[1])
4402 .str();
4403 EXPECT_EQ(Expected[1], A2.bitcastToAPInt().getRawData()[1])
4404 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op2[0], Op2[1],
4405 Op1[0], Op1[1])
4406 .str();
4411 TEST(APFloatTest, PPCDoubleDoubleDivide) {
4412 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
4413 uint64_t, APFloat::roundingMode>;
4414 // TODO: Only a sanity check for now. Add more edge cases when the
4415 // double-double algorithm is implemented.
4416 DataType Data[] = {
4417 // 1 / 3 = 1/3
4418 std::make_tuple(0x3ff0000000000000ull, 0, 0x4008000000000000ull, 0,
4419 0x3fd5555555555555ull, 0x3c75555555555556ull,
4420 APFloat::rmNearestTiesToEven),
4423 for (auto Tp : Data) {
4424 uint64_t Op1[2], Op2[2], Expected[2];
4425 APFloat::roundingMode RM;
4426 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
4428 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4429 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4430 A1.divide(A2, RM);
4432 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
4433 << formatv("({0:x} + {1:x}) / ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
4434 Op2[1])
4435 .str();
4436 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
4437 << formatv("({0:x} + {1:x}) / ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
4438 Op2[1])
4439 .str();
4443 TEST(APFloatTest, PPCDoubleDoubleRemainder) {
4444 using DataType =
4445 std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>;
4446 DataType Data[] = {
4447 // remainder(3.0 + 3.0 << 53, 1.25 + 1.25 << 53) = (0.5 + 0.5 << 53)
4448 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
4449 0x3ff4000000000000ull, 0x3ca4000000000000ull,
4450 0x3fe0000000000000ull, 0x3c90000000000000ull),
4451 // remainder(3.0 + 3.0 << 53, 1.75 + 1.75 << 53) = (-0.5 - 0.5 << 53)
4452 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
4453 0x3ffc000000000000ull, 0x3cac000000000000ull,
4454 0xbfe0000000000000ull, 0xbc90000000000000ull),
4457 for (auto Tp : Data) {
4458 uint64_t Op1[2], Op2[2], Expected[2];
4459 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1]) = Tp;
4461 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4462 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4463 A1.remainder(A2);
4465 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
4466 << formatv("remainder({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
4467 Op2[0], Op2[1])
4468 .str();
4469 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
4470 << formatv("remainder(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0],
4471 Op1[1], Op2[0], Op2[1])
4472 .str();
4476 TEST(APFloatTest, PPCDoubleDoubleMod) {
4477 using DataType =
4478 std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>;
4479 DataType Data[] = {
4480 // mod(3.0 + 3.0 << 53, 1.25 + 1.25 << 53) = (0.5 + 0.5 << 53)
4481 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
4482 0x3ff4000000000000ull, 0x3ca4000000000000ull,
4483 0x3fe0000000000000ull, 0x3c90000000000000ull),
4484 // mod(3.0 + 3.0 << 53, 1.75 + 1.75 << 53) = (1.25 + 1.25 << 53)
4485 // 0xbc98000000000000 doesn't seem right, but it's what we currently have.
4486 // TODO: investigate
4487 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
4488 0x3ffc000000000000ull, 0x3cac000000000000ull,
4489 0x3ff4000000000001ull, 0xbc98000000000000ull),
4492 for (auto Tp : Data) {
4493 uint64_t Op1[2], Op2[2], Expected[2];
4494 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1]) = Tp;
4496 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4497 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4498 A1.mod(A2);
4500 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
4501 << formatv("fmod(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
4502 Op2[0], Op2[1])
4503 .str();
4504 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
4505 << formatv("fmod(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
4506 Op2[0], Op2[1])
4507 .str();
4511 TEST(APFloatTest, PPCDoubleDoubleFMA) {
4512 // Sanity check for now.
4513 APFloat A(APFloat::PPCDoubleDouble(), "2");
4514 A.fusedMultiplyAdd(APFloat(APFloat::PPCDoubleDouble(), "3"),
4515 APFloat(APFloat::PPCDoubleDouble(), "4"),
4516 APFloat::rmNearestTiesToEven);
4517 EXPECT_EQ(APFloat::cmpEqual,
4518 APFloat(APFloat::PPCDoubleDouble(), "10").compare(A));
4521 TEST(APFloatTest, PPCDoubleDoubleRoundToIntegral) {
4523 APFloat A(APFloat::PPCDoubleDouble(), "1.5");
4524 A.roundToIntegral(APFloat::rmNearestTiesToEven);
4525 EXPECT_EQ(APFloat::cmpEqual,
4526 APFloat(APFloat::PPCDoubleDouble(), "2").compare(A));
4529 APFloat A(APFloat::PPCDoubleDouble(), "2.5");
4530 A.roundToIntegral(APFloat::rmNearestTiesToEven);
4531 EXPECT_EQ(APFloat::cmpEqual,
4532 APFloat(APFloat::PPCDoubleDouble(), "2").compare(A));
4536 TEST(APFloatTest, PPCDoubleDoubleCompare) {
4537 using DataType =
4538 std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, APFloat::cmpResult>;
4540 DataType Data[] = {
4541 // (1 + 0) = (1 + 0)
4542 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000000ull, 0,
4543 APFloat::cmpEqual),
4544 // (1 + 0) < (1.00...1 + 0)
4545 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000001ull, 0,
4546 APFloat::cmpLessThan),
4547 // (1.00...1 + 0) > (1 + 0)
4548 std::make_tuple(0x3ff0000000000001ull, 0, 0x3ff0000000000000ull, 0,
4549 APFloat::cmpGreaterThan),
4550 // (1 + 0) < (1 + epsilon)
4551 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000001ull,
4552 0x0000000000000001ull, APFloat::cmpLessThan),
4553 // NaN != NaN
4554 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull, 0,
4555 APFloat::cmpUnordered),
4556 // (1 + 0) != NaN
4557 std::make_tuple(0x3ff0000000000000ull, 0, 0x7ff8000000000000ull, 0,
4558 APFloat::cmpUnordered),
4559 // Inf = Inf
4560 std::make_tuple(0x7ff0000000000000ull, 0, 0x7ff0000000000000ull, 0,
4561 APFloat::cmpEqual),
4564 for (auto Tp : Data) {
4565 uint64_t Op1[2], Op2[2];
4566 APFloat::cmpResult Expected;
4567 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected) = Tp;
4569 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4570 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4571 EXPECT_EQ(Expected, A1.compare(A2))
4572 << formatv("compare(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
4573 Op2[0], Op2[1])
4574 .str();
4578 TEST(APFloatTest, PPCDoubleDoubleBitwiseIsEqual) {
4579 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, bool>;
4581 DataType Data[] = {
4582 // (1 + 0) = (1 + 0)
4583 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000000ull, 0, true),
4584 // (1 + 0) != (1.00...1 + 0)
4585 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000001ull, 0,
4586 false),
4587 // NaN = NaN
4588 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull, 0, true),
4589 // NaN != NaN with a different bit pattern
4590 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull,
4591 0x3ff0000000000000ull, false),
4592 // Inf = Inf
4593 std::make_tuple(0x7ff0000000000000ull, 0, 0x7ff0000000000000ull, 0, true),
4596 for (auto Tp : Data) {
4597 uint64_t Op1[2], Op2[2];
4598 bool Expected;
4599 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected) = Tp;
4601 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
4602 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
4603 EXPECT_EQ(Expected, A1.bitwiseIsEqual(A2))
4604 << formatv("({0:x} + {1:x}) = ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
4605 Op2[1])
4606 .str();
4610 TEST(APFloatTest, PPCDoubleDoubleHashValue) {
4611 uint64_t Data1[] = {0x3ff0000000000001ull, 0x0000000000000001ull};
4612 uint64_t Data2[] = {0x3ff0000000000001ull, 0};
4613 // The hash values are *hopefully* different.
4614 EXPECT_NE(
4615 hash_value(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Data1))),
4616 hash_value(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Data2))));
4619 TEST(APFloatTest, PPCDoubleDoubleChangeSign) {
4620 uint64_t Data[] = {
4621 0x400f000000000000ull, 0xbcb0000000000000ull,
4623 APFloat Float(APFloat::PPCDoubleDouble(), APInt(128, 2, Data));
4625 APFloat Actual =
4626 APFloat::copySign(Float, APFloat(APFloat::IEEEdouble(), "1"));
4627 EXPECT_EQ(0x400f000000000000ull, Actual.bitcastToAPInt().getRawData()[0]);
4628 EXPECT_EQ(0xbcb0000000000000ull, Actual.bitcastToAPInt().getRawData()[1]);
4631 APFloat Actual =
4632 APFloat::copySign(Float, APFloat(APFloat::IEEEdouble(), "-1"));
4633 EXPECT_EQ(0xc00f000000000000ull, Actual.bitcastToAPInt().getRawData()[0]);
4634 EXPECT_EQ(0x3cb0000000000000ull, Actual.bitcastToAPInt().getRawData()[1]);
4638 TEST(APFloatTest, PPCDoubleDoubleFactories) {
4640 uint64_t Data[] = {
4641 0, 0,
4643 EXPECT_EQ(APInt(128, 2, Data),
4644 APFloat::getZero(APFloat::PPCDoubleDouble()).bitcastToAPInt());
4647 uint64_t Data[] = {
4648 0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
4650 EXPECT_EQ(APInt(128, 2, Data),
4651 APFloat::getLargest(APFloat::PPCDoubleDouble()).bitcastToAPInt());
4654 uint64_t Data[] = {
4655 0x0000000000000001ull, 0,
4657 EXPECT_EQ(
4658 APInt(128, 2, Data),
4659 APFloat::getSmallest(APFloat::PPCDoubleDouble()).bitcastToAPInt());
4662 uint64_t Data[] = {0x0360000000000000ull, 0};
4663 EXPECT_EQ(APInt(128, 2, Data),
4664 APFloat::getSmallestNormalized(APFloat::PPCDoubleDouble())
4665 .bitcastToAPInt());
4668 uint64_t Data[] = {
4669 0x8000000000000000ull, 0x0000000000000000ull,
4671 EXPECT_EQ(
4672 APInt(128, 2, Data),
4673 APFloat::getZero(APFloat::PPCDoubleDouble(), true).bitcastToAPInt());
4676 uint64_t Data[] = {
4677 0xffefffffffffffffull, 0xfc8ffffffffffffeull,
4679 EXPECT_EQ(
4680 APInt(128, 2, Data),
4681 APFloat::getLargest(APFloat::PPCDoubleDouble(), true).bitcastToAPInt());
4684 uint64_t Data[] = {
4685 0x8000000000000001ull, 0x0000000000000000ull,
4687 EXPECT_EQ(APInt(128, 2, Data),
4688 APFloat::getSmallest(APFloat::PPCDoubleDouble(), true)
4689 .bitcastToAPInt());
4692 uint64_t Data[] = {
4693 0x8360000000000000ull, 0x0000000000000000ull,
4695 EXPECT_EQ(APInt(128, 2, Data),
4696 APFloat::getSmallestNormalized(APFloat::PPCDoubleDouble(), true)
4697 .bitcastToAPInt());
4699 EXPECT_TRUE(APFloat::getSmallest(APFloat::PPCDoubleDouble()).isSmallest());
4700 EXPECT_TRUE(APFloat::getLargest(APFloat::PPCDoubleDouble()).isLargest());
4703 TEST(APFloatTest, PPCDoubleDoubleIsDenormal) {
4704 EXPECT_TRUE(APFloat::getSmallest(APFloat::PPCDoubleDouble()).isDenormal());
4705 EXPECT_FALSE(APFloat::getLargest(APFloat::PPCDoubleDouble()).isDenormal());
4706 EXPECT_FALSE(
4707 APFloat::getSmallestNormalized(APFloat::PPCDoubleDouble()).isDenormal());
4709 // (4 + 3) is not normalized
4710 uint64_t Data[] = {
4711 0x4010000000000000ull, 0x4008000000000000ull,
4713 EXPECT_TRUE(
4714 APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Data)).isDenormal());
4718 TEST(APFloatTest, PPCDoubleDoubleScalbn) {
4719 // 3.0 + 3.0 << 53
4720 uint64_t Input[] = {
4721 0x4008000000000000ull, 0x3cb8000000000000ull,
4723 APFloat Result =
4724 scalbn(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Input)), 1,
4725 APFloat::rmNearestTiesToEven);
4726 // 6.0 + 6.0 << 53
4727 EXPECT_EQ(0x4018000000000000ull, Result.bitcastToAPInt().getRawData()[0]);
4728 EXPECT_EQ(0x3cc8000000000000ull, Result.bitcastToAPInt().getRawData()[1]);
4731 TEST(APFloatTest, PPCDoubleDoubleFrexp) {
4732 // 3.0 + 3.0 << 53
4733 uint64_t Input[] = {
4734 0x4008000000000000ull, 0x3cb8000000000000ull,
4736 int Exp;
4737 // 0.75 + 0.75 << 53
4738 APFloat Result =
4739 frexp(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Input)), Exp,
4740 APFloat::rmNearestTiesToEven);
4741 EXPECT_EQ(2, Exp);
4742 EXPECT_EQ(0x3fe8000000000000ull, Result.bitcastToAPInt().getRawData()[0]);
4743 EXPECT_EQ(0x3c98000000000000ull, Result.bitcastToAPInt().getRawData()[1]);
4746 TEST(APFloatTest, x87Largest) {
4747 APFloat MaxX87Val = APFloat::getLargest(APFloat::x87DoubleExtended());
4748 EXPECT_TRUE(MaxX87Val.isLargest());
4751 TEST(APFloatTest, x87Next) {
4752 APFloat F(APFloat::x87DoubleExtended(), "-1.0");
4753 F.next(false);
4754 EXPECT_TRUE(ilogb(F) == -1);
4757 TEST(APFloatTest, ToDouble) {
4758 APFloat DPosZero(0.0);
4759 APFloat DPosZeroToDouble(DPosZero.convertToDouble());
4760 EXPECT_TRUE(DPosZeroToDouble.isPosZero());
4761 APFloat DNegZero(-0.0);
4762 APFloat DNegZeroToDouble(DNegZero.convertToDouble());
4763 EXPECT_TRUE(DNegZeroToDouble.isNegZero());
4765 APFloat DOne(1.0);
4766 EXPECT_EQ(1.0, DOne.convertToDouble());
4767 APFloat DPosLargest = APFloat::getLargest(APFloat::IEEEdouble(), false);
4768 EXPECT_EQ(std::numeric_limits<double>::max(), DPosLargest.convertToDouble());
4769 APFloat DNegLargest = APFloat::getLargest(APFloat::IEEEdouble(), true);
4770 EXPECT_EQ(-std::numeric_limits<double>::max(), DNegLargest.convertToDouble());
4771 APFloat DPosSmallest =
4772 APFloat::getSmallestNormalized(APFloat::IEEEdouble(), false);
4773 EXPECT_EQ(std::numeric_limits<double>::min(), DPosSmallest.convertToDouble());
4774 APFloat DNegSmallest =
4775 APFloat::getSmallestNormalized(APFloat::IEEEdouble(), true);
4776 EXPECT_EQ(-std::numeric_limits<double>::min(),
4777 DNegSmallest.convertToDouble());
4779 APFloat DSmallestDenorm = APFloat::getSmallest(APFloat::IEEEdouble(), false);
4780 EXPECT_EQ(std::numeric_limits<double>::denorm_min(),
4781 DSmallestDenorm.convertToDouble());
4782 APFloat DLargestDenorm(APFloat::IEEEdouble(), "0x0.FFFFFFFFFFFFFp-1022");
4783 EXPECT_EQ(/*0x0.FFFFFFFFFFFFFp-1022*/ 2.225073858507201e-308,
4784 DLargestDenorm.convertToDouble());
4786 APFloat DPosInf = APFloat::getInf(APFloat::IEEEdouble());
4787 EXPECT_EQ(std::numeric_limits<double>::infinity(), DPosInf.convertToDouble());
4788 APFloat DNegInf = APFloat::getInf(APFloat::IEEEdouble(), true);
4789 EXPECT_EQ(-std::numeric_limits<double>::infinity(),
4790 DNegInf.convertToDouble());
4791 APFloat DQNaN = APFloat::getQNaN(APFloat::IEEEdouble());
4792 EXPECT_TRUE(std::isnan(DQNaN.convertToDouble()));
4794 APFloat FPosZero(0.0F);
4795 APFloat FPosZeroToDouble(FPosZero.convertToDouble());
4796 EXPECT_TRUE(FPosZeroToDouble.isPosZero());
4797 APFloat FNegZero(-0.0F);
4798 APFloat FNegZeroToDouble(FNegZero.convertToDouble());
4799 EXPECT_TRUE(FNegZeroToDouble.isNegZero());
4801 APFloat FOne(1.0F);
4802 EXPECT_EQ(1.0, FOne.convertToDouble());
4803 APFloat FPosLargest = APFloat::getLargest(APFloat::IEEEsingle(), false);
4804 EXPECT_EQ(std::numeric_limits<float>::max(), FPosLargest.convertToDouble());
4805 APFloat FNegLargest = APFloat::getLargest(APFloat::IEEEsingle(), true);
4806 EXPECT_EQ(-std::numeric_limits<float>::max(), FNegLargest.convertToDouble());
4807 APFloat FPosSmallest =
4808 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
4809 EXPECT_EQ(std::numeric_limits<float>::min(), FPosSmallest.convertToDouble());
4810 APFloat FNegSmallest =
4811 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
4812 EXPECT_EQ(-std::numeric_limits<float>::min(), FNegSmallest.convertToDouble());
4814 APFloat FSmallestDenorm = APFloat::getSmallest(APFloat::IEEEsingle(), false);
4815 EXPECT_EQ(std::numeric_limits<float>::denorm_min(),
4816 FSmallestDenorm.convertToDouble());
4817 APFloat FLargestDenorm(APFloat::IEEEdouble(), "0x0.FFFFFEp-126");
4818 EXPECT_EQ(/*0x0.FFFFFEp-126*/ 1.1754942106924411e-38,
4819 FLargestDenorm.convertToDouble());
4821 APFloat FPosInf = APFloat::getInf(APFloat::IEEEsingle());
4822 EXPECT_EQ(std::numeric_limits<double>::infinity(), FPosInf.convertToDouble());
4823 APFloat FNegInf = APFloat::getInf(APFloat::IEEEsingle(), true);
4824 EXPECT_EQ(-std::numeric_limits<double>::infinity(),
4825 FNegInf.convertToDouble());
4826 APFloat FQNaN = APFloat::getQNaN(APFloat::IEEEsingle());
4827 EXPECT_TRUE(std::isnan(FQNaN.convertToDouble()));
4829 APFloat HPosZero = APFloat::getZero(APFloat::IEEEhalf());
4830 APFloat HPosZeroToDouble(HPosZero.convertToDouble());
4831 EXPECT_TRUE(HPosZeroToDouble.isPosZero());
4832 APFloat HNegZero = APFloat::getZero(APFloat::IEEEhalf(), true);
4833 APFloat HNegZeroToDouble(HNegZero.convertToDouble());
4834 EXPECT_TRUE(HNegZeroToDouble.isNegZero());
4836 APFloat HOne(APFloat::IEEEhalf(), "1.0");
4837 EXPECT_EQ(1.0, HOne.convertToDouble());
4838 APFloat HPosLargest = APFloat::getLargest(APFloat::IEEEhalf(), false);
4839 EXPECT_EQ(65504.0, HPosLargest.convertToDouble());
4840 APFloat HNegLargest = APFloat::getLargest(APFloat::IEEEhalf(), true);
4841 EXPECT_EQ(-65504.0, HNegLargest.convertToDouble());
4842 APFloat HPosSmallest =
4843 APFloat::getSmallestNormalized(APFloat::IEEEhalf(), false);
4844 EXPECT_EQ(/*0x1.p-14*/ 6.103515625e-05, HPosSmallest.convertToDouble());
4845 APFloat HNegSmallest =
4846 APFloat::getSmallestNormalized(APFloat::IEEEhalf(), true);
4847 EXPECT_EQ(/*-0x1.p-14*/ -6.103515625e-05, HNegSmallest.convertToDouble());
4849 APFloat HSmallestDenorm = APFloat::getSmallest(APFloat::IEEEhalf(), false);
4850 EXPECT_EQ(/*0x1.p-24*/ 5.960464477539063e-08,
4851 HSmallestDenorm.convertToDouble());
4852 APFloat HLargestDenorm(APFloat::IEEEhalf(), "0x1.FFCp-14");
4853 EXPECT_EQ(/*0x1.FFCp-14*/ 0.00012201070785522461,
4854 HLargestDenorm.convertToDouble());
4856 APFloat HPosInf = APFloat::getInf(APFloat::IEEEhalf());
4857 EXPECT_EQ(std::numeric_limits<double>::infinity(), HPosInf.convertToDouble());
4858 APFloat HNegInf = APFloat::getInf(APFloat::IEEEhalf(), true);
4859 EXPECT_EQ(-std::numeric_limits<double>::infinity(),
4860 HNegInf.convertToDouble());
4861 APFloat HQNaN = APFloat::getQNaN(APFloat::IEEEhalf());
4862 EXPECT_TRUE(std::isnan(HQNaN.convertToDouble()));
4864 APFloat BPosZero = APFloat::getZero(APFloat::IEEEhalf());
4865 APFloat BPosZeroToDouble(BPosZero.convertToDouble());
4866 EXPECT_TRUE(BPosZeroToDouble.isPosZero());
4867 APFloat BNegZero = APFloat::getZero(APFloat::IEEEhalf(), true);
4868 APFloat BNegZeroToDouble(BNegZero.convertToDouble());
4869 EXPECT_TRUE(BNegZeroToDouble.isNegZero());
4871 APFloat BOne(APFloat::BFloat(), "1.0");
4872 EXPECT_EQ(1.0, BOne.convertToDouble());
4873 APFloat BPosLargest = APFloat::getLargest(APFloat::BFloat(), false);
4874 EXPECT_EQ(/*0x1.FEp127*/ 3.3895313892515355e+38,
4875 BPosLargest.convertToDouble());
4876 APFloat BNegLargest = APFloat::getLargest(APFloat::BFloat(), true);
4877 EXPECT_EQ(/*-0x1.FEp127*/ -3.3895313892515355e+38,
4878 BNegLargest.convertToDouble());
4879 APFloat BPosSmallest =
4880 APFloat::getSmallestNormalized(APFloat::BFloat(), false);
4881 EXPECT_EQ(/*0x1.p-126*/ 1.1754943508222875e-38,
4882 BPosSmallest.convertToDouble());
4883 APFloat BNegSmallest =
4884 APFloat::getSmallestNormalized(APFloat::BFloat(), true);
4885 EXPECT_EQ(/*-0x1.p-126*/ -1.1754943508222875e-38,
4886 BNegSmallest.convertToDouble());
4888 APFloat BSmallestDenorm = APFloat::getSmallest(APFloat::BFloat(), false);
4889 EXPECT_EQ(/*0x1.p-133*/ 9.183549615799121e-41,
4890 BSmallestDenorm.convertToDouble());
4891 APFloat BLargestDenorm(APFloat::BFloat(), "0x1.FCp-127");
4892 EXPECT_EQ(/*0x1.FCp-127*/ 1.1663108012064884e-38,
4893 BLargestDenorm.convertToDouble());
4895 APFloat BPosInf = APFloat::getInf(APFloat::BFloat());
4896 EXPECT_EQ(std::numeric_limits<double>::infinity(), BPosInf.convertToDouble());
4897 APFloat BNegInf = APFloat::getInf(APFloat::BFloat(), true);
4898 EXPECT_EQ(-std::numeric_limits<double>::infinity(),
4899 BNegInf.convertToDouble());
4900 APFloat BQNaN = APFloat::getQNaN(APFloat::BFloat());
4901 EXPECT_TRUE(std::isnan(BQNaN.convertToDouble()));
4904 TEST(APFloatTest, ToFloat) {
4905 APFloat FPosZero(0.0F);
4906 APFloat FPosZeroToFloat(FPosZero.convertToFloat());
4907 EXPECT_TRUE(FPosZeroToFloat.isPosZero());
4908 APFloat FNegZero(-0.0F);
4909 APFloat FNegZeroToFloat(FNegZero.convertToFloat());
4910 EXPECT_TRUE(FNegZeroToFloat.isNegZero());
4912 APFloat FOne(1.0F);
4913 EXPECT_EQ(1.0F, FOne.convertToFloat());
4914 APFloat FPosLargest = APFloat::getLargest(APFloat::IEEEsingle(), false);
4915 EXPECT_EQ(std::numeric_limits<float>::max(), FPosLargest.convertToFloat());
4916 APFloat FNegLargest = APFloat::getLargest(APFloat::IEEEsingle(), true);
4917 EXPECT_EQ(-std::numeric_limits<float>::max(), FNegLargest.convertToFloat());
4918 APFloat FPosSmallest =
4919 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
4920 EXPECT_EQ(std::numeric_limits<float>::min(), FPosSmallest.convertToFloat());
4921 APFloat FNegSmallest =
4922 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
4923 EXPECT_EQ(-std::numeric_limits<float>::min(), FNegSmallest.convertToFloat());
4925 APFloat FSmallestDenorm = APFloat::getSmallest(APFloat::IEEEsingle(), false);
4926 EXPECT_EQ(std::numeric_limits<float>::denorm_min(),
4927 FSmallestDenorm.convertToFloat());
4928 APFloat FLargestDenorm(APFloat::IEEEsingle(), "0x1.FFFFFEp-126");
4929 EXPECT_EQ(/*0x1.FFFFFEp-126*/ 2.3509885615147286e-38F,
4930 FLargestDenorm.convertToFloat());
4932 APFloat FPosInf = APFloat::getInf(APFloat::IEEEsingle());
4933 EXPECT_EQ(std::numeric_limits<float>::infinity(), FPosInf.convertToFloat());
4934 APFloat FNegInf = APFloat::getInf(APFloat::IEEEsingle(), true);
4935 EXPECT_EQ(-std::numeric_limits<float>::infinity(), FNegInf.convertToFloat());
4936 APFloat FQNaN = APFloat::getQNaN(APFloat::IEEEsingle());
4937 EXPECT_TRUE(std::isnan(FQNaN.convertToFloat()));
4939 APFloat HPosZero = APFloat::getZero(APFloat::IEEEhalf());
4940 APFloat HPosZeroToFloat(HPosZero.convertToFloat());
4941 EXPECT_TRUE(HPosZeroToFloat.isPosZero());
4942 APFloat HNegZero = APFloat::getZero(APFloat::IEEEhalf(), true);
4943 APFloat HNegZeroToFloat(HNegZero.convertToFloat());
4944 EXPECT_TRUE(HNegZeroToFloat.isNegZero());
4946 APFloat HOne(APFloat::IEEEhalf(), "1.0");
4947 EXPECT_EQ(1.0F, HOne.convertToFloat());
4948 APFloat HPosLargest = APFloat::getLargest(APFloat::IEEEhalf(), false);
4949 EXPECT_EQ(/*0x1.FFCp15*/ 65504.0F, HPosLargest.convertToFloat());
4950 APFloat HNegLargest = APFloat::getLargest(APFloat::IEEEhalf(), true);
4951 EXPECT_EQ(/*-0x1.FFCp15*/ -65504.0F, HNegLargest.convertToFloat());
4952 APFloat HPosSmallest =
4953 APFloat::getSmallestNormalized(APFloat::IEEEhalf(), false);
4954 EXPECT_EQ(/*0x1.p-14*/ 6.103515625e-05F, HPosSmallest.convertToFloat());
4955 APFloat HNegSmallest =
4956 APFloat::getSmallestNormalized(APFloat::IEEEhalf(), true);
4957 EXPECT_EQ(/*-0x1.p-14*/ -6.103515625e-05F, HNegSmallest.convertToFloat());
4959 APFloat HSmallestDenorm = APFloat::getSmallest(APFloat::IEEEhalf(), false);
4960 EXPECT_EQ(/*0x1.p-24*/ 5.960464477539063e-08F,
4961 HSmallestDenorm.convertToFloat());
4962 APFloat HLargestDenorm(APFloat::IEEEhalf(), "0x1.FFCp-14");
4963 EXPECT_EQ(/*0x1.FFCp-14*/ 0.00012201070785522461F,
4964 HLargestDenorm.convertToFloat());
4966 APFloat HPosInf = APFloat::getInf(APFloat::IEEEhalf());
4967 EXPECT_EQ(std::numeric_limits<float>::infinity(), HPosInf.convertToFloat());
4968 APFloat HNegInf = APFloat::getInf(APFloat::IEEEhalf(), true);
4969 EXPECT_EQ(-std::numeric_limits<float>::infinity(), HNegInf.convertToFloat());
4970 APFloat HQNaN = APFloat::getQNaN(APFloat::IEEEhalf());
4971 EXPECT_TRUE(std::isnan(HQNaN.convertToFloat()));
4973 APFloat BPosZero = APFloat::getZero(APFloat::BFloat());
4974 APFloat BPosZeroToDouble(BPosZero.convertToFloat());
4975 EXPECT_TRUE(BPosZeroToDouble.isPosZero());
4976 APFloat BNegZero = APFloat::getZero(APFloat::BFloat(), true);
4977 APFloat BNegZeroToDouble(BNegZero.convertToFloat());
4978 EXPECT_TRUE(BNegZeroToDouble.isNegZero());
4980 APFloat BOne(APFloat::BFloat(), "1.0");
4981 EXPECT_EQ(1.0F, BOne.convertToFloat());
4982 APFloat BPosLargest = APFloat::getLargest(APFloat::BFloat(), false);
4983 EXPECT_EQ(/*0x1.FEp127*/ 3.3895313892515355e+38F,
4984 BPosLargest.convertToFloat());
4985 APFloat BNegLargest = APFloat::getLargest(APFloat::BFloat(), true);
4986 EXPECT_EQ(/*-0x1.FEp127*/ -3.3895313892515355e+38F,
4987 BNegLargest.convertToFloat());
4988 APFloat BPosSmallest =
4989 APFloat::getSmallestNormalized(APFloat::BFloat(), false);
4990 EXPECT_EQ(/*0x1.p-126*/ 1.1754943508222875e-38F,
4991 BPosSmallest.convertToFloat());
4992 APFloat BNegSmallest =
4993 APFloat::getSmallestNormalized(APFloat::BFloat(), true);
4994 EXPECT_EQ(/*-0x1.p-126*/ -1.1754943508222875e-38F,
4995 BNegSmallest.convertToFloat());
4997 APFloat BSmallestDenorm = APFloat::getSmallest(APFloat::BFloat(), false);
4998 EXPECT_EQ(/*0x1.p-133*/ 9.183549615799121e-41F,
4999 BSmallestDenorm.convertToFloat());
5000 APFloat BLargestDenorm(APFloat::BFloat(), "0x1.FCp-127");
5001 EXPECT_EQ(/*0x1.FCp-127*/ 1.1663108012064884e-38F,
5002 BLargestDenorm.convertToFloat());
5004 APFloat BPosInf = APFloat::getInf(APFloat::BFloat());
5005 EXPECT_EQ(std::numeric_limits<float>::infinity(), BPosInf.convertToFloat());
5006 APFloat BNegInf = APFloat::getInf(APFloat::BFloat(), true);
5007 EXPECT_EQ(-std::numeric_limits<float>::infinity(), BNegInf.convertToFloat());
5008 APFloat BQNaN = APFloat::getQNaN(APFloat::BFloat());
5009 EXPECT_TRUE(std::isnan(BQNaN.convertToFloat()));