Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / unittests / ADT / APFloatTest.cpp
blobfb3607772f0e64983ebe6957d2392cba10cffbc5
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/FormatVariadic.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "gtest/gtest.h"
16 #include <cmath>
17 #include <ostream>
18 #include <string>
19 #include <tuple>
21 using namespace llvm;
23 static double convertToDoubleFromString(const char *Str) {
24 llvm::APFloat F(0.0);
25 F.convertFromString(Str, llvm::APFloat::rmNearestTiesToEven);
26 return F.convertToDouble();
29 static std::string convertToString(double d, unsigned Prec, unsigned Pad,
30 bool Tr = true) {
31 llvm::SmallVector<char, 100> Buffer;
32 llvm::APFloat F(d);
33 F.toString(Buffer, Prec, Pad, Tr);
34 return std::string(Buffer.data(), Buffer.size());
37 namespace {
39 TEST(APFloatTest, isSignaling) {
40 // We test qNaN, -qNaN, +sNaN, -sNaN with and without payloads. *NOTE* The
41 // positive/negative distinction is included only since the getQNaN/getSNaN
42 // API provides the option.
43 APInt payload = APInt::getOneBitSet(4, 2);
44 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle(), false).isSignaling());
45 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle(), true).isSignaling());
46 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle(), false, &payload).isSignaling());
47 EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle(), true, &payload).isSignaling());
48 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isSignaling());
49 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), true).isSignaling());
50 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), false, &payload).isSignaling());
51 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), true, &payload).isSignaling());
54 TEST(APFloatTest, next) {
56 APFloat test(APFloat::IEEEquad(), APFloat::uninitialized);
57 APFloat expected(APFloat::IEEEquad(), APFloat::uninitialized);
59 // 1. Test Special Cases Values.
61 // Test all special values for nextUp and nextDown perscribed by IEEE-754R
62 // 2008. These are:
63 // 1. +inf
64 // 2. -inf
65 // 3. getLargest()
66 // 4. -getLargest()
67 // 5. getSmallest()
68 // 6. -getSmallest()
69 // 7. qNaN
70 // 8. sNaN
71 // 9. +0
72 // 10. -0
74 // nextUp(+inf) = +inf.
75 test = APFloat::getInf(APFloat::IEEEquad(), false);
76 expected = APFloat::getInf(APFloat::IEEEquad(), false);
77 EXPECT_EQ(test.next(false), APFloat::opOK);
78 EXPECT_TRUE(test.isInfinity());
79 EXPECT_TRUE(!test.isNegative());
80 EXPECT_TRUE(test.bitwiseIsEqual(expected));
82 // nextDown(+inf) = -nextUp(-inf) = -(-getLargest()) = getLargest()
83 test = APFloat::getInf(APFloat::IEEEquad(), false);
84 expected = APFloat::getLargest(APFloat::IEEEquad(), false);
85 EXPECT_EQ(test.next(true), APFloat::opOK);
86 EXPECT_TRUE(!test.isNegative());
87 EXPECT_TRUE(test.bitwiseIsEqual(expected));
89 // nextUp(-inf) = -getLargest()
90 test = APFloat::getInf(APFloat::IEEEquad(), true);
91 expected = APFloat::getLargest(APFloat::IEEEquad(), true);
92 EXPECT_EQ(test.next(false), APFloat::opOK);
93 EXPECT_TRUE(test.isNegative());
94 EXPECT_TRUE(test.bitwiseIsEqual(expected));
96 // nextDown(-inf) = -nextUp(+inf) = -(+inf) = -inf.
97 test = APFloat::getInf(APFloat::IEEEquad(), true);
98 expected = APFloat::getInf(APFloat::IEEEquad(), true);
99 EXPECT_EQ(test.next(true), APFloat::opOK);
100 EXPECT_TRUE(test.isInfinity() && test.isNegative());
101 EXPECT_TRUE(test.bitwiseIsEqual(expected));
103 // nextUp(getLargest()) = +inf
104 test = APFloat::getLargest(APFloat::IEEEquad(), false);
105 expected = APFloat::getInf(APFloat::IEEEquad(), false);
106 EXPECT_EQ(test.next(false), APFloat::opOK);
107 EXPECT_TRUE(test.isInfinity() && !test.isNegative());
108 EXPECT_TRUE(test.bitwiseIsEqual(expected));
110 // nextDown(getLargest()) = -nextUp(-getLargest())
111 // = -(-getLargest() + inc)
112 // = getLargest() - inc.
113 test = APFloat::getLargest(APFloat::IEEEquad(), false);
114 expected = APFloat(APFloat::IEEEquad(),
115 "0x1.fffffffffffffffffffffffffffep+16383");
116 EXPECT_EQ(test.next(true), APFloat::opOK);
117 EXPECT_TRUE(!test.isInfinity() && !test.isNegative());
118 EXPECT_TRUE(test.bitwiseIsEqual(expected));
120 // nextUp(-getLargest()) = -getLargest() + inc.
121 test = APFloat::getLargest(APFloat::IEEEquad(), true);
122 expected = APFloat(APFloat::IEEEquad(),
123 "-0x1.fffffffffffffffffffffffffffep+16383");
124 EXPECT_EQ(test.next(false), APFloat::opOK);
125 EXPECT_TRUE(test.bitwiseIsEqual(expected));
127 // nextDown(-getLargest()) = -nextUp(getLargest()) = -(inf) = -inf.
128 test = APFloat::getLargest(APFloat::IEEEquad(), true);
129 expected = APFloat::getInf(APFloat::IEEEquad(), true);
130 EXPECT_EQ(test.next(true), APFloat::opOK);
131 EXPECT_TRUE(test.isInfinity() && test.isNegative());
132 EXPECT_TRUE(test.bitwiseIsEqual(expected));
134 // nextUp(getSmallest()) = getSmallest() + inc.
135 test = APFloat(APFloat::IEEEquad(), "0x0.0000000000000000000000000001p-16382");
136 expected = APFloat(APFloat::IEEEquad(),
137 "0x0.0000000000000000000000000002p-16382");
138 EXPECT_EQ(test.next(false), APFloat::opOK);
139 EXPECT_TRUE(test.bitwiseIsEqual(expected));
141 // nextDown(getSmallest()) = -nextUp(-getSmallest()) = -(-0) = +0.
142 test = APFloat(APFloat::IEEEquad(), "0x0.0000000000000000000000000001p-16382");
143 expected = APFloat::getZero(APFloat::IEEEquad(), false);
144 EXPECT_EQ(test.next(true), APFloat::opOK);
145 EXPECT_TRUE(test.isPosZero());
146 EXPECT_TRUE(test.bitwiseIsEqual(expected));
148 // nextUp(-getSmallest()) = -0.
149 test = APFloat(APFloat::IEEEquad(), "-0x0.0000000000000000000000000001p-16382");
150 expected = APFloat::getZero(APFloat::IEEEquad(), true);
151 EXPECT_EQ(test.next(false), APFloat::opOK);
152 EXPECT_TRUE(test.isNegZero());
153 EXPECT_TRUE(test.bitwiseIsEqual(expected));
155 // nextDown(-getSmallest()) = -nextUp(getSmallest()) = -getSmallest() - inc.
156 test = APFloat(APFloat::IEEEquad(), "-0x0.0000000000000000000000000001p-16382");
157 expected = APFloat(APFloat::IEEEquad(),
158 "-0x0.0000000000000000000000000002p-16382");
159 EXPECT_EQ(test.next(true), APFloat::opOK);
160 EXPECT_TRUE(test.bitwiseIsEqual(expected));
162 // nextUp(qNaN) = qNaN
163 test = APFloat::getQNaN(APFloat::IEEEquad(), false);
164 expected = APFloat::getQNaN(APFloat::IEEEquad(), false);
165 EXPECT_EQ(test.next(false), APFloat::opOK);
166 EXPECT_TRUE(test.bitwiseIsEqual(expected));
168 // nextDown(qNaN) = qNaN
169 test = APFloat::getQNaN(APFloat::IEEEquad(), false);
170 expected = APFloat::getQNaN(APFloat::IEEEquad(), false);
171 EXPECT_EQ(test.next(true), APFloat::opOK);
172 EXPECT_TRUE(test.bitwiseIsEqual(expected));
174 // nextUp(sNaN) = qNaN
175 test = APFloat::getSNaN(APFloat::IEEEquad(), false);
176 expected = APFloat::getQNaN(APFloat::IEEEquad(), false);
177 EXPECT_EQ(test.next(false), APFloat::opInvalidOp);
178 EXPECT_TRUE(test.bitwiseIsEqual(expected));
180 // nextDown(sNaN) = qNaN
181 test = APFloat::getSNaN(APFloat::IEEEquad(), false);
182 expected = APFloat::getQNaN(APFloat::IEEEquad(), false);
183 EXPECT_EQ(test.next(true), APFloat::opInvalidOp);
184 EXPECT_TRUE(test.bitwiseIsEqual(expected));
186 // nextUp(+0) = +getSmallest()
187 test = APFloat::getZero(APFloat::IEEEquad(), false);
188 expected = APFloat::getSmallest(APFloat::IEEEquad(), false);
189 EXPECT_EQ(test.next(false), APFloat::opOK);
190 EXPECT_TRUE(test.bitwiseIsEqual(expected));
192 // nextDown(+0) = -nextUp(-0) = -getSmallest()
193 test = APFloat::getZero(APFloat::IEEEquad(), false);
194 expected = APFloat::getSmallest(APFloat::IEEEquad(), true);
195 EXPECT_EQ(test.next(true), APFloat::opOK);
196 EXPECT_TRUE(test.bitwiseIsEqual(expected));
198 // nextUp(-0) = +getSmallest()
199 test = APFloat::getZero(APFloat::IEEEquad(), true);
200 expected = APFloat::getSmallest(APFloat::IEEEquad(), false);
201 EXPECT_EQ(test.next(false), APFloat::opOK);
202 EXPECT_TRUE(test.bitwiseIsEqual(expected));
204 // nextDown(-0) = -nextUp(0) = -getSmallest()
205 test = APFloat::getZero(APFloat::IEEEquad(), true);
206 expected = APFloat::getSmallest(APFloat::IEEEquad(), true);
207 EXPECT_EQ(test.next(true), APFloat::opOK);
208 EXPECT_TRUE(test.bitwiseIsEqual(expected));
210 // 2. Binade Boundary Tests.
212 // 2a. Test denormal <-> normal binade boundaries.
213 // * nextUp(+Largest Denormal) -> +Smallest Normal.
214 // * nextDown(-Largest Denormal) -> -Smallest Normal.
215 // * nextUp(-Smallest Normal) -> -Largest Denormal.
216 // * nextDown(+Smallest Normal) -> +Largest Denormal.
218 // nextUp(+Largest Denormal) -> +Smallest Normal.
219 test = APFloat(APFloat::IEEEquad(), "0x0.ffffffffffffffffffffffffffffp-16382");
220 expected = APFloat(APFloat::IEEEquad(),
221 "0x1.0000000000000000000000000000p-16382");
222 EXPECT_EQ(test.next(false), APFloat::opOK);
223 EXPECT_FALSE(test.isDenormal());
224 EXPECT_TRUE(test.bitwiseIsEqual(expected));
226 // nextDown(-Largest Denormal) -> -Smallest Normal.
227 test = APFloat(APFloat::IEEEquad(),
228 "-0x0.ffffffffffffffffffffffffffffp-16382");
229 expected = APFloat(APFloat::IEEEquad(),
230 "-0x1.0000000000000000000000000000p-16382");
231 EXPECT_EQ(test.next(true), APFloat::opOK);
232 EXPECT_FALSE(test.isDenormal());
233 EXPECT_TRUE(test.bitwiseIsEqual(expected));
235 // nextUp(-Smallest Normal) -> -LargestDenormal.
236 test = APFloat(APFloat::IEEEquad(),
237 "-0x1.0000000000000000000000000000p-16382");
238 expected = APFloat(APFloat::IEEEquad(),
239 "-0x0.ffffffffffffffffffffffffffffp-16382");
240 EXPECT_EQ(test.next(false), APFloat::opOK);
241 EXPECT_TRUE(test.isDenormal());
242 EXPECT_TRUE(test.bitwiseIsEqual(expected));
244 // nextDown(+Smallest Normal) -> +Largest Denormal.
245 test = APFloat(APFloat::IEEEquad(),
246 "+0x1.0000000000000000000000000000p-16382");
247 expected = APFloat(APFloat::IEEEquad(),
248 "+0x0.ffffffffffffffffffffffffffffp-16382");
249 EXPECT_EQ(test.next(true), APFloat::opOK);
250 EXPECT_TRUE(test.isDenormal());
251 EXPECT_TRUE(test.bitwiseIsEqual(expected));
253 // 2b. Test normal <-> normal binade boundaries.
254 // * nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
255 // * nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
256 // * nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
257 // * nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
259 // nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
260 test = APFloat(APFloat::IEEEquad(), "-0x1p+1");
261 expected = APFloat(APFloat::IEEEquad(),
262 "-0x1.ffffffffffffffffffffffffffffp+0");
263 EXPECT_EQ(test.next(false), APFloat::opOK);
264 EXPECT_TRUE(test.bitwiseIsEqual(expected));
266 // nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
267 test = APFloat(APFloat::IEEEquad(), "0x1p+1");
268 expected = APFloat(APFloat::IEEEquad(), "0x1.ffffffffffffffffffffffffffffp+0");
269 EXPECT_EQ(test.next(true), APFloat::opOK);
270 EXPECT_TRUE(test.bitwiseIsEqual(expected));
272 // nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
273 test = APFloat(APFloat::IEEEquad(), "0x1.ffffffffffffffffffffffffffffp+0");
274 expected = APFloat(APFloat::IEEEquad(), "0x1p+1");
275 EXPECT_EQ(test.next(false), APFloat::opOK);
276 EXPECT_TRUE(test.bitwiseIsEqual(expected));
278 // nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
279 test = APFloat(APFloat::IEEEquad(), "-0x1.ffffffffffffffffffffffffffffp+0");
280 expected = APFloat(APFloat::IEEEquad(), "-0x1p+1");
281 EXPECT_EQ(test.next(true), APFloat::opOK);
282 EXPECT_TRUE(test.bitwiseIsEqual(expected));
284 // 2c. Test using next at binade boundaries with a direction away from the
285 // binade boundary. Away from denormal <-> normal boundaries.
287 // This is to make sure that even though we are at a binade boundary, since
288 // we are rounding away, we do not trigger the binade boundary code. Thus we
289 // test:
290 // * nextUp(-Largest Denormal) -> -Largest Denormal + inc.
291 // * nextDown(+Largest Denormal) -> +Largest Denormal - inc.
292 // * nextUp(+Smallest Normal) -> +Smallest Normal + inc.
293 // * nextDown(-Smallest Normal) -> -Smallest Normal - inc.
295 // nextUp(-Largest Denormal) -> -Largest Denormal + inc.
296 test = APFloat(APFloat::IEEEquad(), "-0x0.ffffffffffffffffffffffffffffp-16382");
297 expected = APFloat(APFloat::IEEEquad(),
298 "-0x0.fffffffffffffffffffffffffffep-16382");
299 EXPECT_EQ(test.next(false), APFloat::opOK);
300 EXPECT_TRUE(test.isDenormal());
301 EXPECT_TRUE(test.isNegative());
302 EXPECT_TRUE(test.bitwiseIsEqual(expected));
304 // nextDown(+Largest Denormal) -> +Largest Denormal - inc.
305 test = APFloat(APFloat::IEEEquad(), "0x0.ffffffffffffffffffffffffffffp-16382");
306 expected = APFloat(APFloat::IEEEquad(),
307 "0x0.fffffffffffffffffffffffffffep-16382");
308 EXPECT_EQ(test.next(true), APFloat::opOK);
309 EXPECT_TRUE(test.isDenormal());
310 EXPECT_TRUE(!test.isNegative());
311 EXPECT_TRUE(test.bitwiseIsEqual(expected));
313 // nextUp(+Smallest Normal) -> +Smallest Normal + inc.
314 test = APFloat(APFloat::IEEEquad(), "0x1.0000000000000000000000000000p-16382");
315 expected = APFloat(APFloat::IEEEquad(),
316 "0x1.0000000000000000000000000001p-16382");
317 EXPECT_EQ(test.next(false), APFloat::opOK);
318 EXPECT_TRUE(!test.isDenormal());
319 EXPECT_TRUE(!test.isNegative());
320 EXPECT_TRUE(test.bitwiseIsEqual(expected));
322 // nextDown(-Smallest Normal) -> -Smallest Normal - inc.
323 test = APFloat(APFloat::IEEEquad(), "-0x1.0000000000000000000000000000p-16382");
324 expected = APFloat(APFloat::IEEEquad(),
325 "-0x1.0000000000000000000000000001p-16382");
326 EXPECT_EQ(test.next(true), APFloat::opOK);
327 EXPECT_TRUE(!test.isDenormal());
328 EXPECT_TRUE(test.isNegative());
329 EXPECT_TRUE(test.bitwiseIsEqual(expected));
331 // 2d. Test values which cause our exponent to go to min exponent. This
332 // is to ensure that guards in the code to check for min exponent
333 // trigger properly.
334 // * nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
335 // * nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
336 // -0x1p-16381
337 // * nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16382
338 // * nextDown(0x1p-16382) -> 0x1.ffffffffffffffffffffffffffffp-16382
340 // nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
341 test = APFloat(APFloat::IEEEquad(), "-0x1p-16381");
342 expected = APFloat(APFloat::IEEEquad(),
343 "-0x1.ffffffffffffffffffffffffffffp-16382");
344 EXPECT_EQ(test.next(false), APFloat::opOK);
345 EXPECT_TRUE(test.bitwiseIsEqual(expected));
347 // nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
348 // -0x1p-16381
349 test = APFloat(APFloat::IEEEquad(), "-0x1.ffffffffffffffffffffffffffffp-16382");
350 expected = APFloat(APFloat::IEEEquad(), "-0x1p-16381");
351 EXPECT_EQ(test.next(true), APFloat::opOK);
352 EXPECT_TRUE(test.bitwiseIsEqual(expected));
354 // nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16381
355 test = APFloat(APFloat::IEEEquad(), "0x1.ffffffffffffffffffffffffffffp-16382");
356 expected = APFloat(APFloat::IEEEquad(), "0x1p-16381");
357 EXPECT_EQ(test.next(false), APFloat::opOK);
358 EXPECT_TRUE(test.bitwiseIsEqual(expected));
360 // nextDown(0x1p-16381) -> 0x1.ffffffffffffffffffffffffffffp-16382
361 test = APFloat(APFloat::IEEEquad(), "0x1p-16381");
362 expected = APFloat(APFloat::IEEEquad(),
363 "0x1.ffffffffffffffffffffffffffffp-16382");
364 EXPECT_EQ(test.next(true), APFloat::opOK);
365 EXPECT_TRUE(test.bitwiseIsEqual(expected));
367 // 3. Now we test both denormal/normal computation which will not cause us
368 // to go across binade boundaries. Specifically we test:
369 // * nextUp(+Denormal) -> +Denormal.
370 // * nextDown(+Denormal) -> +Denormal.
371 // * nextUp(-Denormal) -> -Denormal.
372 // * nextDown(-Denormal) -> -Denormal.
373 // * nextUp(+Normal) -> +Normal.
374 // * nextDown(+Normal) -> +Normal.
375 // * nextUp(-Normal) -> -Normal.
376 // * nextDown(-Normal) -> -Normal.
378 // nextUp(+Denormal) -> +Denormal.
379 test = APFloat(APFloat::IEEEquad(),
380 "0x0.ffffffffffffffffffffffff000cp-16382");
381 expected = APFloat(APFloat::IEEEquad(),
382 "0x0.ffffffffffffffffffffffff000dp-16382");
383 EXPECT_EQ(test.next(false), APFloat::opOK);
384 EXPECT_TRUE(test.isDenormal());
385 EXPECT_TRUE(!test.isNegative());
386 EXPECT_TRUE(test.bitwiseIsEqual(expected));
388 // nextDown(+Denormal) -> +Denormal.
389 test = APFloat(APFloat::IEEEquad(),
390 "0x0.ffffffffffffffffffffffff000cp-16382");
391 expected = APFloat(APFloat::IEEEquad(),
392 "0x0.ffffffffffffffffffffffff000bp-16382");
393 EXPECT_EQ(test.next(true), APFloat::opOK);
394 EXPECT_TRUE(test.isDenormal());
395 EXPECT_TRUE(!test.isNegative());
396 EXPECT_TRUE(test.bitwiseIsEqual(expected));
398 // nextUp(-Denormal) -> -Denormal.
399 test = APFloat(APFloat::IEEEquad(),
400 "-0x0.ffffffffffffffffffffffff000cp-16382");
401 expected = APFloat(APFloat::IEEEquad(),
402 "-0x0.ffffffffffffffffffffffff000bp-16382");
403 EXPECT_EQ(test.next(false), APFloat::opOK);
404 EXPECT_TRUE(test.isDenormal());
405 EXPECT_TRUE(test.isNegative());
406 EXPECT_TRUE(test.bitwiseIsEqual(expected));
408 // nextDown(-Denormal) -> -Denormal
409 test = APFloat(APFloat::IEEEquad(),
410 "-0x0.ffffffffffffffffffffffff000cp-16382");
411 expected = APFloat(APFloat::IEEEquad(),
412 "-0x0.ffffffffffffffffffffffff000dp-16382");
413 EXPECT_EQ(test.next(true), APFloat::opOK);
414 EXPECT_TRUE(test.isDenormal());
415 EXPECT_TRUE(test.isNegative());
416 EXPECT_TRUE(test.bitwiseIsEqual(expected));
418 // nextUp(+Normal) -> +Normal.
419 test = APFloat(APFloat::IEEEquad(),
420 "0x1.ffffffffffffffffffffffff000cp-16000");
421 expected = APFloat(APFloat::IEEEquad(),
422 "0x1.ffffffffffffffffffffffff000dp-16000");
423 EXPECT_EQ(test.next(false), APFloat::opOK);
424 EXPECT_TRUE(!test.isDenormal());
425 EXPECT_TRUE(!test.isNegative());
426 EXPECT_TRUE(test.bitwiseIsEqual(expected));
428 // nextDown(+Normal) -> +Normal.
429 test = APFloat(APFloat::IEEEquad(),
430 "0x1.ffffffffffffffffffffffff000cp-16000");
431 expected = APFloat(APFloat::IEEEquad(),
432 "0x1.ffffffffffffffffffffffff000bp-16000");
433 EXPECT_EQ(test.next(true), APFloat::opOK);
434 EXPECT_TRUE(!test.isDenormal());
435 EXPECT_TRUE(!test.isNegative());
436 EXPECT_TRUE(test.bitwiseIsEqual(expected));
438 // nextUp(-Normal) -> -Normal.
439 test = APFloat(APFloat::IEEEquad(),
440 "-0x1.ffffffffffffffffffffffff000cp-16000");
441 expected = APFloat(APFloat::IEEEquad(),
442 "-0x1.ffffffffffffffffffffffff000bp-16000");
443 EXPECT_EQ(test.next(false), APFloat::opOK);
444 EXPECT_TRUE(!test.isDenormal());
445 EXPECT_TRUE(test.isNegative());
446 EXPECT_TRUE(test.bitwiseIsEqual(expected));
448 // nextDown(-Normal) -> -Normal.
449 test = APFloat(APFloat::IEEEquad(),
450 "-0x1.ffffffffffffffffffffffff000cp-16000");
451 expected = APFloat(APFloat::IEEEquad(),
452 "-0x1.ffffffffffffffffffffffff000dp-16000");
453 EXPECT_EQ(test.next(true), APFloat::opOK);
454 EXPECT_TRUE(!test.isDenormal());
455 EXPECT_TRUE(test.isNegative());
456 EXPECT_TRUE(test.bitwiseIsEqual(expected));
459 TEST(APFloatTest, FMA) {
460 APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;
463 APFloat f1(14.5f);
464 APFloat f2(-14.5f);
465 APFloat f3(225.0f);
466 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
467 EXPECT_EQ(14.75f, f1.convertToFloat());
471 APFloat Val2(2.0f);
472 APFloat f1((float)1.17549435e-38F);
473 APFloat f2((float)1.17549435e-38F);
474 f1.divide(Val2, rdmd);
475 f2.divide(Val2, rdmd);
476 APFloat f3(12.0f);
477 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
478 EXPECT_EQ(12.0f, f1.convertToFloat());
481 // Test for correct zero sign when answer is exactly zero.
482 // fma(1.0, -1.0, 1.0) -> +ve 0.
484 APFloat f1(1.0);
485 APFloat f2(-1.0);
486 APFloat f3(1.0);
487 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
488 EXPECT_TRUE(!f1.isNegative() && f1.isZero());
491 // Test for correct zero sign when answer is exactly zero and rounding towards
492 // negative.
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::rmTowardNegative);
499 EXPECT_TRUE(f1.isNegative() && f1.isZero());
502 // Test for correct (in this case -ve) sign when adding like signed zeros.
503 // Test fma(0.0, -0.0, -0.0) -> -ve 0.
505 APFloat f1(0.0);
506 APFloat f2(-0.0);
507 APFloat f3(-0.0);
508 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
509 EXPECT_TRUE(f1.isNegative() && f1.isZero());
512 // Test -ve sign preservation when small negative results underflow.
514 APFloat f1(APFloat::IEEEdouble(), "-0x1p-1074");
515 APFloat f2(APFloat::IEEEdouble(), "+0x1p-1074");
516 APFloat f3(0.0);
517 f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
518 EXPECT_TRUE(f1.isNegative() && f1.isZero());
521 // Test x87 extended precision case from http://llvm.org/PR20728.
523 APFloat M1(APFloat::x87DoubleExtended(), 1.0);
524 APFloat M2(APFloat::x87DoubleExtended(), 1.0);
525 APFloat A(APFloat::x87DoubleExtended(), 3.0);
527 bool losesInfo = false;
528 M1.fusedMultiplyAdd(M1, A, APFloat::rmNearestTiesToEven);
529 M1.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
530 EXPECT_FALSE(losesInfo);
531 EXPECT_EQ(4.0f, M1.convertToFloat());
535 TEST(APFloatTest, MinNum) {
536 APFloat f1(1.0);
537 APFloat f2(2.0);
538 APFloat nan = APFloat::getNaN(APFloat::IEEEdouble());
540 EXPECT_EQ(1.0, minnum(f1, f2).convertToDouble());
541 EXPECT_EQ(1.0, minnum(f2, f1).convertToDouble());
542 EXPECT_EQ(1.0, minnum(f1, nan).convertToDouble());
543 EXPECT_EQ(1.0, minnum(nan, f1).convertToDouble());
546 TEST(APFloatTest, MaxNum) {
547 APFloat f1(1.0);
548 APFloat f2(2.0);
549 APFloat nan = APFloat::getNaN(APFloat::IEEEdouble());
551 EXPECT_EQ(2.0, maxnum(f1, f2).convertToDouble());
552 EXPECT_EQ(2.0, maxnum(f2, f1).convertToDouble());
553 EXPECT_EQ(1.0, maxnum(f1, nan).convertToDouble());
554 EXPECT_EQ(1.0, maxnum(nan, f1).convertToDouble());
557 TEST(APFloatTest, Minimum) {
558 APFloat f1(1.0);
559 APFloat f2(2.0);
560 APFloat zp(0.0);
561 APFloat zn(-0.0);
562 APFloat nan = APFloat::getNaN(APFloat::IEEEdouble());
564 EXPECT_EQ(1.0, minimum(f1, f2).convertToDouble());
565 EXPECT_EQ(1.0, minimum(f2, f1).convertToDouble());
566 EXPECT_EQ(-0.0, minimum(zp, zn).convertToDouble());
567 EXPECT_EQ(-0.0, minimum(zn, zp).convertToDouble());
568 EXPECT_TRUE(std::isnan(minimum(f1, nan).convertToDouble()));
569 EXPECT_TRUE(std::isnan(minimum(nan, f1).convertToDouble()));
572 TEST(APFloatTest, Maximum) {
573 APFloat f1(1.0);
574 APFloat f2(2.0);
575 APFloat zp(0.0);
576 APFloat zn(-0.0);
577 APFloat nan = APFloat::getNaN(APFloat::IEEEdouble());
579 EXPECT_EQ(2.0, maximum(f1, f2).convertToDouble());
580 EXPECT_EQ(2.0, maximum(f2, f1).convertToDouble());
581 EXPECT_EQ(0.0, maximum(zp, zn).convertToDouble());
582 EXPECT_EQ(0.0, maximum(zn, zp).convertToDouble());
583 EXPECT_TRUE(std::isnan(maximum(f1, nan).convertToDouble()));
584 EXPECT_TRUE(std::isnan(maximum(nan, f1).convertToDouble()));
587 TEST(APFloatTest, Denormal) {
588 APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;
590 // Test single precision
592 const char *MinNormalStr = "1.17549435082228750797e-38";
593 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), MinNormalStr).isDenormal());
594 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), 0.0).isDenormal());
596 APFloat Val2(APFloat::IEEEsingle(), 2.0e0);
597 APFloat T(APFloat::IEEEsingle(), MinNormalStr);
598 T.divide(Val2, rdmd);
599 EXPECT_TRUE(T.isDenormal());
602 // Test double precision
604 const char *MinNormalStr = "2.22507385850720138309e-308";
605 EXPECT_FALSE(APFloat(APFloat::IEEEdouble(), MinNormalStr).isDenormal());
606 EXPECT_FALSE(APFloat(APFloat::IEEEdouble(), 0.0).isDenormal());
608 APFloat Val2(APFloat::IEEEdouble(), 2.0e0);
609 APFloat T(APFloat::IEEEdouble(), MinNormalStr);
610 T.divide(Val2, rdmd);
611 EXPECT_TRUE(T.isDenormal());
614 // Test Intel double-ext
616 const char *MinNormalStr = "3.36210314311209350626e-4932";
617 EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended(), MinNormalStr).isDenormal());
618 EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended(), 0.0).isDenormal());
620 APFloat Val2(APFloat::x87DoubleExtended(), 2.0e0);
621 APFloat T(APFloat::x87DoubleExtended(), MinNormalStr);
622 T.divide(Val2, rdmd);
623 EXPECT_TRUE(T.isDenormal());
626 // Test quadruple precision
628 const char *MinNormalStr = "3.36210314311209350626267781732175260e-4932";
629 EXPECT_FALSE(APFloat(APFloat::IEEEquad(), MinNormalStr).isDenormal());
630 EXPECT_FALSE(APFloat(APFloat::IEEEquad(), 0.0).isDenormal());
632 APFloat Val2(APFloat::IEEEquad(), 2.0e0);
633 APFloat T(APFloat::IEEEquad(), MinNormalStr);
634 T.divide(Val2, rdmd);
635 EXPECT_TRUE(T.isDenormal());
639 TEST(APFloatTest, Zero) {
640 EXPECT_EQ(0.0f, APFloat(0.0f).convertToFloat());
641 EXPECT_EQ(-0.0f, APFloat(-0.0f).convertToFloat());
642 EXPECT_TRUE(APFloat(-0.0f).isNegative());
644 EXPECT_EQ(0.0, APFloat(0.0).convertToDouble());
645 EXPECT_EQ(-0.0, APFloat(-0.0).convertToDouble());
646 EXPECT_TRUE(APFloat(-0.0).isNegative());
649 TEST(APFloatTest, DecimalStringsWithoutNullTerminators) {
650 // Make sure that we can parse strings without null terminators.
651 // rdar://14323230.
652 APFloat Val(APFloat::IEEEdouble());
653 Val.convertFromString(StringRef("0.00", 3),
654 llvm::APFloat::rmNearestTiesToEven);
655 EXPECT_EQ(Val.convertToDouble(), 0.0);
656 Val.convertFromString(StringRef("0.01", 3),
657 llvm::APFloat::rmNearestTiesToEven);
658 EXPECT_EQ(Val.convertToDouble(), 0.0);
659 Val.convertFromString(StringRef("0.09", 3),
660 llvm::APFloat::rmNearestTiesToEven);
661 EXPECT_EQ(Val.convertToDouble(), 0.0);
662 Val.convertFromString(StringRef("0.095", 4),
663 llvm::APFloat::rmNearestTiesToEven);
664 EXPECT_EQ(Val.convertToDouble(), 0.09);
665 Val.convertFromString(StringRef("0.00e+3", 7),
666 llvm::APFloat::rmNearestTiesToEven);
667 EXPECT_EQ(Val.convertToDouble(), 0.00);
668 Val.convertFromString(StringRef("0e+3", 4),
669 llvm::APFloat::rmNearestTiesToEven);
670 EXPECT_EQ(Val.convertToDouble(), 0.00);
674 TEST(APFloatTest, fromZeroDecimalString) {
675 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0").convertToDouble());
676 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0").convertToDouble());
677 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0").convertToDouble());
679 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.").convertToDouble());
680 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.").convertToDouble());
681 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.").convertToDouble());
683 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), ".0").convertToDouble());
684 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+.0").convertToDouble());
685 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-.0").convertToDouble());
687 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.0").convertToDouble());
688 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.0").convertToDouble());
689 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.0").convertToDouble());
691 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "00000.").convertToDouble());
692 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+00000.").convertToDouble());
693 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-00000.").convertToDouble());
695 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble(), ".00000").convertToDouble());
696 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+.00000").convertToDouble());
697 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-.00000").convertToDouble());
699 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0000.00000").convertToDouble());
700 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0000.00000").convertToDouble());
701 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0000.00000").convertToDouble());
704 TEST(APFloatTest, fromZeroDecimalSingleExponentString) {
705 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e1").convertToDouble());
706 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e1").convertToDouble());
707 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e1").convertToDouble());
709 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e+1").convertToDouble());
710 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e+1").convertToDouble());
711 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e+1").convertToDouble());
713 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e-1").convertToDouble());
714 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e-1").convertToDouble());
715 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e-1").convertToDouble());
718 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.e1").convertToDouble());
719 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.e1").convertToDouble());
720 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.e1").convertToDouble());
722 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.e+1").convertToDouble());
723 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.e+1").convertToDouble());
724 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.e+1").convertToDouble());
726 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.e-1").convertToDouble());
727 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.e-1").convertToDouble());
728 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.e-1").convertToDouble());
730 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), ".0e1").convertToDouble());
731 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+.0e1").convertToDouble());
732 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-.0e1").convertToDouble());
734 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), ".0e+1").convertToDouble());
735 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+.0e+1").convertToDouble());
736 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-.0e+1").convertToDouble());
738 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), ".0e-1").convertToDouble());
739 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+.0e-1").convertToDouble());
740 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-.0e-1").convertToDouble());
743 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.0e1").convertToDouble());
744 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.0e1").convertToDouble());
745 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.0e1").convertToDouble());
747 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.0e+1").convertToDouble());
748 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.0e+1").convertToDouble());
749 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.0e+1").convertToDouble());
751 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0.0e-1").convertToDouble());
752 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0.0e-1").convertToDouble());
753 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0.0e-1").convertToDouble());
756 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "000.0000e1").convertToDouble());
757 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+000.0000e+1").convertToDouble());
758 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-000.0000e+1").convertToDouble());
761 TEST(APFloatTest, fromZeroDecimalLargeExponentString) {
762 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e1234").convertToDouble());
763 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e1234").convertToDouble());
764 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e1234").convertToDouble());
766 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e+1234").convertToDouble());
767 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e+1234").convertToDouble());
768 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e+1234").convertToDouble());
770 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0e-1234").convertToDouble());
771 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0e-1234").convertToDouble());
772 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0e-1234").convertToDouble());
774 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble(), "000.0000e1234").convertToDouble());
775 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble(), "000.0000e-1234").convertToDouble());
777 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble(), StringRef("0e1234" "\0" "2", 6)).convertToDouble());
780 TEST(APFloatTest, fromZeroHexadecimalString) {
781 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0p1").convertToDouble());
782 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0p1").convertToDouble());
783 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0p1").convertToDouble());
785 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0p+1").convertToDouble());
786 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0p+1").convertToDouble());
787 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0p+1").convertToDouble());
789 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0p-1").convertToDouble());
790 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0p-1").convertToDouble());
791 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0p-1").convertToDouble());
794 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.p1").convertToDouble());
795 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.p1").convertToDouble());
796 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.p1").convertToDouble());
798 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.p+1").convertToDouble());
799 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.p+1").convertToDouble());
800 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.p+1").convertToDouble());
802 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.p-1").convertToDouble());
803 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.p-1").convertToDouble());
804 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.p-1").convertToDouble());
807 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x.0p1").convertToDouble());
808 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x.0p1").convertToDouble());
809 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x.0p1").convertToDouble());
811 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x.0p+1").convertToDouble());
812 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x.0p+1").convertToDouble());
813 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x.0p+1").convertToDouble());
815 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x.0p-1").convertToDouble());
816 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x.0p-1").convertToDouble());
817 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x.0p-1").convertToDouble());
820 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.0p1").convertToDouble());
821 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.0p1").convertToDouble());
822 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.0p1").convertToDouble());
824 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.0p+1").convertToDouble());
825 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.0p+1").convertToDouble());
826 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.0p+1").convertToDouble());
828 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.0p-1").convertToDouble());
829 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble(), "+0x0.0p-1").convertToDouble());
830 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0.0p-1").convertToDouble());
833 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x00000.p1").convertToDouble());
834 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0000.00000p1").convertToDouble());
835 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x.00000p1").convertToDouble());
836 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.p1").convertToDouble());
837 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0p1234").convertToDouble());
838 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble(), "-0x0p1234").convertToDouble());
839 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x00000.p1234").convertToDouble());
840 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0000.00000p1234").convertToDouble());
841 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x.00000p1234").convertToDouble());
842 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble(), "0x0.p1234").convertToDouble());
845 TEST(APFloatTest, fromDecimalString) {
846 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1").convertToDouble());
847 EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble(), "2.").convertToDouble());
848 EXPECT_EQ(0.5, APFloat(APFloat::IEEEdouble(), ".5").convertToDouble());
849 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0").convertToDouble());
850 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble(), "-2").convertToDouble());
851 EXPECT_EQ(-4.0, APFloat(APFloat::IEEEdouble(), "-4.").convertToDouble());
852 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble(), "-.5").convertToDouble());
853 EXPECT_EQ(-1.5, APFloat(APFloat::IEEEdouble(), "-1.5").convertToDouble());
854 EXPECT_EQ(1.25e12, APFloat(APFloat::IEEEdouble(), "1.25e12").convertToDouble());
855 EXPECT_EQ(1.25e+12, APFloat(APFloat::IEEEdouble(), "1.25e+12").convertToDouble());
856 EXPECT_EQ(1.25e-12, APFloat(APFloat::IEEEdouble(), "1.25e-12").convertToDouble());
857 EXPECT_EQ(1024.0, APFloat(APFloat::IEEEdouble(), "1024.").convertToDouble());
858 EXPECT_EQ(1024.05, APFloat(APFloat::IEEEdouble(), "1024.05000").convertToDouble());
859 EXPECT_EQ(0.05, APFloat(APFloat::IEEEdouble(), ".05000").convertToDouble());
860 EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble(), "2.").convertToDouble());
861 EXPECT_EQ(2.0e2, APFloat(APFloat::IEEEdouble(), "2.e2").convertToDouble());
862 EXPECT_EQ(2.0e+2, APFloat(APFloat::IEEEdouble(), "2.e+2").convertToDouble());
863 EXPECT_EQ(2.0e-2, APFloat(APFloat::IEEEdouble(), "2.e-2").convertToDouble());
864 EXPECT_EQ(2.05e2, APFloat(APFloat::IEEEdouble(), "002.05000e2").convertToDouble());
865 EXPECT_EQ(2.05e+2, APFloat(APFloat::IEEEdouble(), "002.05000e+2").convertToDouble());
866 EXPECT_EQ(2.05e-2, APFloat(APFloat::IEEEdouble(), "002.05000e-2").convertToDouble());
867 EXPECT_EQ(2.05e12, APFloat(APFloat::IEEEdouble(), "002.05000e12").convertToDouble());
868 EXPECT_EQ(2.05e+12, APFloat(APFloat::IEEEdouble(), "002.05000e+12").convertToDouble());
869 EXPECT_EQ(2.05e-12, APFloat(APFloat::IEEEdouble(), "002.05000e-12").convertToDouble());
871 // These are "carefully selected" to overflow the fast log-base
872 // calculations in APFloat.cpp
873 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "99e99999").isInfinity());
874 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-99e99999").isInfinity());
875 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "1e-99999").isPosZero());
876 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-1e-99999").isNegZero());
878 EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
881 TEST(APFloatTest, fromToStringSpecials) {
882 auto expects = [] (const char *first, const char *second) {
883 std::string roundtrip = convertToString(convertToDoubleFromString(second), 0, 3);
884 EXPECT_STREQ(first, roundtrip.c_str());
886 expects("+Inf", "+Inf");
887 expects("+Inf", "INFINITY");
888 expects("+Inf", "inf");
889 expects("-Inf", "-Inf");
890 expects("-Inf", "-INFINITY");
891 expects("-Inf", "-inf");
892 expects("NaN", "NaN");
893 expects("NaN", "nan");
894 expects("NaN", "-NaN");
895 expects("NaN", "-nan");
898 TEST(APFloatTest, fromHexadecimalString) {
899 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(), "0x1p0").convertToDouble());
900 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p0").convertToDouble());
901 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-0x1p0").convertToDouble());
903 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(), "0x1p+0").convertToDouble());
904 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p+0").convertToDouble());
905 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-0x1p+0").convertToDouble());
907 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(), "0x1p-0").convertToDouble());
908 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p-0").convertToDouble());
909 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-0x1p-0").convertToDouble());
912 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble(), "0x1p1").convertToDouble());
913 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble(), "+0x1p1").convertToDouble());
914 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble(), "-0x1p1").convertToDouble());
916 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble(), "0x1p+1").convertToDouble());
917 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble(), "+0x1p+1").convertToDouble());
918 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble(), "-0x1p+1").convertToDouble());
920 EXPECT_EQ( 0.5, APFloat(APFloat::IEEEdouble(), "0x1p-1").convertToDouble());
921 EXPECT_EQ(+0.5, APFloat(APFloat::IEEEdouble(), "+0x1p-1").convertToDouble());
922 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble(), "-0x1p-1").convertToDouble());
925 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble(), "0x1.8p1").convertToDouble());
926 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble(), "+0x1.8p1").convertToDouble());
927 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble(), "-0x1.8p1").convertToDouble());
929 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble(), "0x1.8p+1").convertToDouble());
930 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble(), "+0x1.8p+1").convertToDouble());
931 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble(), "-0x1.8p+1").convertToDouble());
933 EXPECT_EQ( 0.75, APFloat(APFloat::IEEEdouble(), "0x1.8p-1").convertToDouble());
934 EXPECT_EQ(+0.75, APFloat(APFloat::IEEEdouble(), "+0x1.8p-1").convertToDouble());
935 EXPECT_EQ(-0.75, APFloat(APFloat::IEEEdouble(), "-0x1.8p-1").convertToDouble());
938 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000.000p1").convertToDouble());
939 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000.000p1").convertToDouble());
940 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000.000p1").convertToDouble());
942 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000.000p+1").convertToDouble());
943 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000.000p+1").convertToDouble());
944 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000.000p+1").convertToDouble());
946 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble(), "0x1000.000p-1").convertToDouble());
947 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble(), "+0x1000.000p-1").convertToDouble());
948 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble(), "-0x1000.000p-1").convertToDouble());
951 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000p1").convertToDouble());
952 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000p1").convertToDouble());
953 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000p1").convertToDouble());
955 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000p+1").convertToDouble());
956 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000p+1").convertToDouble());
957 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000p+1").convertToDouble());
959 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble(), "0x1000p-1").convertToDouble());
960 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble(), "+0x1000p-1").convertToDouble());
961 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble(), "-0x1000p-1").convertToDouble());
964 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble(), "0x10p10").convertToDouble());
965 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble(), "+0x10p10").convertToDouble());
966 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble(), "-0x10p10").convertToDouble());
968 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble(), "0x10p+10").convertToDouble());
969 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble(), "+0x10p+10").convertToDouble());
970 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble(), "-0x10p+10").convertToDouble());
972 EXPECT_EQ( 0.015625, APFloat(APFloat::IEEEdouble(), "0x10p-10").convertToDouble());
973 EXPECT_EQ(+0.015625, APFloat(APFloat::IEEEdouble(), "+0x10p-10").convertToDouble());
974 EXPECT_EQ(-0.015625, APFloat(APFloat::IEEEdouble(), "-0x10p-10").convertToDouble());
976 EXPECT_EQ(1.0625, APFloat(APFloat::IEEEdouble(), "0x1.1p0").convertToDouble());
977 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "0x1p0").convertToDouble());
979 EXPECT_EQ(convertToDoubleFromString("0x1p-150"),
980 convertToDoubleFromString("+0x800000000000000001.p-221"));
981 EXPECT_EQ(2251799813685248.5,
982 convertToDoubleFromString("0x80000000000004000000.010p-28"));
985 TEST(APFloatTest, toString) {
986 ASSERT_EQ("10", convertToString(10.0, 6, 3));
987 ASSERT_EQ("1.0E+1", convertToString(10.0, 6, 0));
988 ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2));
989 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 4, 2));
990 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 5, 1));
991 ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2));
992 ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2));
993 ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 5, 1));
994 ASSERT_EQ("0.78539816339744828", convertToString(0.78539816339744830961, 0, 3));
995 ASSERT_EQ("4.9406564584124654E-324", convertToString(4.9406564584124654e-324, 0, 3));
996 ASSERT_EQ("873.18340000000001", convertToString(873.1834, 0, 1));
997 ASSERT_EQ("8.7318340000000001E+2", convertToString(873.1834, 0, 0));
998 ASSERT_EQ("1.7976931348623157E+308", convertToString(1.7976931348623157E+308, 0, 0));
999 ASSERT_EQ("10", convertToString(10.0, 6, 3, false));
1000 ASSERT_EQ("1.000000e+01", convertToString(10.0, 6, 0, false));
1001 ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2, false));
1002 ASSERT_EQ("1.0100e+04", convertToString(1.01E+4, 4, 2, false));
1003 ASSERT_EQ("1.01000e+04", convertToString(1.01E+4, 5, 1, false));
1004 ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2, false));
1005 ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2, false));
1006 ASSERT_EQ("1.01000e-02", convertToString(1.01E-2, 5, 1, false));
1007 ASSERT_EQ("0.78539816339744828",
1008 convertToString(0.78539816339744830961, 0, 3, false));
1009 ASSERT_EQ("4.94065645841246540e-324",
1010 convertToString(4.9406564584124654e-324, 0, 3, false));
1011 ASSERT_EQ("873.18340000000001", convertToString(873.1834, 0, 1, false));
1012 ASSERT_EQ("8.73183400000000010e+02", convertToString(873.1834, 0, 0, false));
1013 ASSERT_EQ("1.79769313486231570e+308",
1014 convertToString(1.7976931348623157E+308, 0, 0, false));
1017 SmallString<64> Str;
1018 APFloat UnnormalZero(APFloat::x87DoubleExtended(), APInt(80, {0, 1}));
1019 UnnormalZero.toString(Str);
1020 ASSERT_EQ("NaN", Str);
1024 TEST(APFloatTest, toInteger) {
1025 bool isExact = false;
1026 APSInt result(5, /*isUnsigned=*/true);
1028 EXPECT_EQ(APFloat::opOK,
1029 APFloat(APFloat::IEEEdouble(), "10")
1030 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1031 EXPECT_TRUE(isExact);
1032 EXPECT_EQ(APSInt(APInt(5, 10), true), result);
1034 EXPECT_EQ(APFloat::opInvalidOp,
1035 APFloat(APFloat::IEEEdouble(), "-10")
1036 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1037 EXPECT_FALSE(isExact);
1038 EXPECT_EQ(APSInt::getMinValue(5, true), result);
1040 EXPECT_EQ(APFloat::opInvalidOp,
1041 APFloat(APFloat::IEEEdouble(), "32")
1042 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1043 EXPECT_FALSE(isExact);
1044 EXPECT_EQ(APSInt::getMaxValue(5, true), result);
1046 EXPECT_EQ(APFloat::opInexact,
1047 APFloat(APFloat::IEEEdouble(), "7.9")
1048 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1049 EXPECT_FALSE(isExact);
1050 EXPECT_EQ(APSInt(APInt(5, 7), true), result);
1052 result.setIsUnsigned(false);
1053 EXPECT_EQ(APFloat::opOK,
1054 APFloat(APFloat::IEEEdouble(), "-10")
1055 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1056 EXPECT_TRUE(isExact);
1057 EXPECT_EQ(APSInt(APInt(5, -10, true), false), result);
1059 EXPECT_EQ(APFloat::opInvalidOp,
1060 APFloat(APFloat::IEEEdouble(), "-17")
1061 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1062 EXPECT_FALSE(isExact);
1063 EXPECT_EQ(APSInt::getMinValue(5, false), result);
1065 EXPECT_EQ(APFloat::opInvalidOp,
1066 APFloat(APFloat::IEEEdouble(), "16")
1067 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1068 EXPECT_FALSE(isExact);
1069 EXPECT_EQ(APSInt::getMaxValue(5, false), result);
1072 static APInt nanbitsFromAPInt(const fltSemantics &Sem, bool SNaN, bool Negative,
1073 uint64_t payload) {
1074 APInt appayload(64, payload);
1075 if (SNaN)
1076 return APFloat::getSNaN(Sem, Negative, &appayload).bitcastToAPInt();
1077 else
1078 return APFloat::getQNaN(Sem, Negative, &appayload).bitcastToAPInt();
1081 TEST(APFloatTest, makeNaN) {
1082 const struct {
1083 uint64_t expected;
1084 const fltSemantics &semantics;
1085 bool SNaN;
1086 bool Negative;
1087 uint64_t payload;
1088 } tests[] = {
1089 /* expected semantics SNaN Neg payload */
1090 { 0x7fc00000ULL, APFloat::IEEEsingle(), false, false, 0x00000000ULL },
1091 { 0xffc00000ULL, APFloat::IEEEsingle(), false, true, 0x00000000ULL },
1092 { 0x7fc0ae72ULL, APFloat::IEEEsingle(), false, false, 0x0000ae72ULL },
1093 { 0x7fffae72ULL, APFloat::IEEEsingle(), false, false, 0xffffae72ULL },
1094 { 0x7fdaae72ULL, APFloat::IEEEsingle(), false, false, 0x00daae72ULL },
1095 { 0x7fa00000ULL, APFloat::IEEEsingle(), true, false, 0x00000000ULL },
1096 { 0xffa00000ULL, APFloat::IEEEsingle(), true, true, 0x00000000ULL },
1097 { 0x7f80ae72ULL, APFloat::IEEEsingle(), true, false, 0x0000ae72ULL },
1098 { 0x7fbfae72ULL, APFloat::IEEEsingle(), true, false, 0xffffae72ULL },
1099 { 0x7f9aae72ULL, APFloat::IEEEsingle(), true, false, 0x001aae72ULL },
1100 { 0x7ff8000000000000ULL, APFloat::IEEEdouble(), false, false, 0x0000000000000000ULL },
1101 { 0xfff8000000000000ULL, APFloat::IEEEdouble(), false, true, 0x0000000000000000ULL },
1102 { 0x7ff800000000ae72ULL, APFloat::IEEEdouble(), false, false, 0x000000000000ae72ULL },
1103 { 0x7fffffffffffae72ULL, APFloat::IEEEdouble(), false, false, 0xffffffffffffae72ULL },
1104 { 0x7ffdaaaaaaaaae72ULL, APFloat::IEEEdouble(), false, false, 0x000daaaaaaaaae72ULL },
1105 { 0x7ff4000000000000ULL, APFloat::IEEEdouble(), true, false, 0x0000000000000000ULL },
1106 { 0xfff4000000000000ULL, APFloat::IEEEdouble(), true, true, 0x0000000000000000ULL },
1107 { 0x7ff000000000ae72ULL, APFloat::IEEEdouble(), true, false, 0x000000000000ae72ULL },
1108 { 0x7ff7ffffffffae72ULL, APFloat::IEEEdouble(), true, false, 0xffffffffffffae72ULL },
1109 { 0x7ff1aaaaaaaaae72ULL, APFloat::IEEEdouble(), true, false, 0x0001aaaaaaaaae72ULL },
1112 for (const auto &t : tests) {
1113 ASSERT_EQ(t.expected, nanbitsFromAPInt(t.semantics, t.SNaN, t.Negative, t.payload));
1117 #ifdef GTEST_HAS_DEATH_TEST
1118 #ifndef NDEBUG
1119 TEST(APFloatTest, SemanticsDeath) {
1120 EXPECT_DEATH(APFloat(APFloat::IEEEsingle(), 0.0f).convertToDouble(), "Float semantics are not IEEEdouble");
1121 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), 0.0 ).convertToFloat(), "Float semantics are not IEEEsingle");
1124 TEST(APFloatTest, StringDecimalDeath) {
1125 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ""), "Invalid string length");
1126 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+"), "String has no digits");
1127 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-"), "String has no digits");
1129 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("\0", 1)), "Invalid character in significand");
1130 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1\0", 2)), "Invalid character in significand");
1131 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1" "\0" "2", 3)), "Invalid character in significand");
1132 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1" "\0" "2e1", 5)), "Invalid character in significand");
1133 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1e\0", 3)), "Invalid character in exponent");
1134 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1e1\0", 4)), "Invalid character in exponent");
1135 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1e1" "\0" "2", 5)), "Invalid character in exponent");
1137 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.0f"), "Invalid character in significand");
1139 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".."), "String contains multiple dots");
1140 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "..0"), "String contains multiple dots");
1141 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.0.0"), "String contains multiple dots");
1144 TEST(APFloatTest, StringDecimalSignificandDeath) {
1145 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "."), "Significand has no digits");
1146 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+."), "Significand has no digits");
1147 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-."), "Significand has no digits");
1150 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "e"), "Significand has no digits");
1151 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+e"), "Significand has no digits");
1152 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-e"), "Significand has no digits");
1154 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "e1"), "Significand has no digits");
1155 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+e1"), "Significand has no digits");
1156 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-e1"), "Significand has no digits");
1158 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".e1"), "Significand has no digits");
1159 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+.e1"), "Significand has no digits");
1160 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-.e1"), "Significand has no digits");
1163 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".e"), "Significand has no digits");
1164 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+.e"), "Significand has no digits");
1165 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-.e"), "Significand has no digits");
1168 TEST(APFloatTest, StringDecimalExponentDeath) {
1169 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1e"), "Exponent has no digits");
1170 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+1e"), "Exponent has no digits");
1171 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-1e"), "Exponent has no digits");
1173 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.e"), "Exponent has no digits");
1174 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+1.e"), "Exponent has no digits");
1175 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-1.e"), "Exponent has no digits");
1177 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".1e"), "Exponent has no digits");
1178 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+.1e"), "Exponent has no digits");
1179 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-.1e"), "Exponent has no digits");
1181 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.1e"), "Exponent has no digits");
1182 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+1.1e"), "Exponent has no digits");
1183 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-1.1e"), "Exponent has no digits");
1186 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1e+"), "Exponent has no digits");
1187 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1e-"), "Exponent has no digits");
1189 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".1e"), "Exponent has no digits");
1190 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".1e+"), "Exponent has no digits");
1191 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".1e-"), "Exponent has no digits");
1193 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.0e"), "Exponent has no digits");
1194 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.0e+"), "Exponent has no digits");
1195 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.0e-"), "Exponent has no digits");
1198 TEST(APFloatTest, StringHexadecimalDeath) {
1199 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x"), "Invalid string");
1200 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x"), "Invalid string");
1201 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x"), "Invalid string");
1203 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x0"), "Hex strings require an exponent");
1204 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x0"), "Hex strings require an exponent");
1205 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x0"), "Hex strings require an exponent");
1207 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x0."), "Hex strings require an exponent");
1208 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x0."), "Hex strings require an exponent");
1209 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x0."), "Hex strings require an exponent");
1211 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x.0"), "Hex strings require an exponent");
1212 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x.0"), "Hex strings require an exponent");
1213 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x.0"), "Hex strings require an exponent");
1215 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x0.0"), "Hex strings require an exponent");
1216 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x0.0"), "Hex strings require an exponent");
1217 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x0.0"), "Hex strings require an exponent");
1219 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x\0", 3)), "Invalid character in significand");
1220 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1\0", 4)), "Invalid character in significand");
1221 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1" "\0" "2", 5)), "Invalid character in significand");
1222 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1" "\0" "2p1", 7)), "Invalid character in significand");
1223 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1p\0", 5)), "Invalid character in exponent");
1224 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1p1\0", 6)), "Invalid character in exponent");
1225 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1p1" "\0" "2", 7)), "Invalid character in exponent");
1227 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1p0f"), "Invalid character in exponent");
1229 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x..p1"), "String contains multiple dots");
1230 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x..0p1"), "String contains multiple dots");
1231 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1.0.0p1"), "String contains multiple dots");
1234 TEST(APFloatTest, StringHexadecimalSignificandDeath) {
1235 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x."), "Significand has no digits");
1236 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x."), "Significand has no digits");
1237 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x."), "Significand has no digits");
1239 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0xp"), "Significand has no digits");
1240 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0xp"), "Significand has no digits");
1241 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0xp"), "Significand has no digits");
1243 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0xp+"), "Significand has no digits");
1244 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0xp+"), "Significand has no digits");
1245 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0xp+"), "Significand has no digits");
1247 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0xp-"), "Significand has no digits");
1248 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0xp-"), "Significand has no digits");
1249 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0xp-"), "Significand has no digits");
1252 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x.p"), "Significand has no digits");
1253 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x.p"), "Significand has no digits");
1254 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x.p"), "Significand has no digits");
1256 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x.p+"), "Significand has no digits");
1257 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x.p+"), "Significand has no digits");
1258 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x.p+"), "Significand has no digits");
1260 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x.p-"), "Significand has no digits");
1261 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x.p-"), "Significand has no digits");
1262 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x.p-"), "Significand has no digits");
1265 TEST(APFloatTest, StringHexadecimalExponentDeath) {
1266 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1p"), "Exponent has no digits");
1267 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1p"), "Exponent has no digits");
1268 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1p"), "Exponent has no digits");
1270 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1p+"), "Exponent has no digits");
1271 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1p+"), "Exponent has no digits");
1272 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1p+"), "Exponent has no digits");
1274 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1p-"), "Exponent has no digits");
1275 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1p-"), "Exponent has no digits");
1276 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1p-"), "Exponent has no digits");
1279 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1.p"), "Exponent has no digits");
1280 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1.p"), "Exponent has no digits");
1281 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1.p"), "Exponent has no digits");
1283 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1.p+"), "Exponent has no digits");
1284 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1.p+"), "Exponent has no digits");
1285 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1.p+"), "Exponent has no digits");
1287 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1.p-"), "Exponent has no digits");
1288 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1.p-"), "Exponent has no digits");
1289 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1.p-"), "Exponent has no digits");
1292 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x.1p"), "Exponent has no digits");
1293 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x.1p"), "Exponent has no digits");
1294 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x.1p"), "Exponent has no digits");
1296 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x.1p+"), "Exponent has no digits");
1297 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x.1p+"), "Exponent has no digits");
1298 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x.1p+"), "Exponent has no digits");
1300 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x.1p-"), "Exponent has no digits");
1301 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x.1p-"), "Exponent has no digits");
1302 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x.1p-"), "Exponent has no digits");
1305 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1.1p"), "Exponent has no digits");
1306 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1.1p"), "Exponent has no digits");
1307 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1.1p"), "Exponent has no digits");
1309 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1.1p+"), "Exponent has no digits");
1310 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1.1p+"), "Exponent has no digits");
1311 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1.1p+"), "Exponent has no digits");
1313 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1.1p-"), "Exponent has no digits");
1314 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1.1p-"), "Exponent has no digits");
1315 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1.1p-"), "Exponent has no digits");
1317 #endif
1318 #endif
1320 TEST(APFloatTest, exactInverse) {
1321 APFloat inv(0.0f);
1323 // Trivial operation.
1324 EXPECT_TRUE(APFloat(2.0).getExactInverse(&inv));
1325 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5)));
1326 EXPECT_TRUE(APFloat(2.0f).getExactInverse(&inv));
1327 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5f)));
1328 EXPECT_TRUE(APFloat(APFloat::IEEEquad(), "2.0").getExactInverse(&inv));
1329 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::IEEEquad(), "0.5")));
1330 EXPECT_TRUE(APFloat(APFloat::PPCDoubleDouble(), "2.0").getExactInverse(&inv));
1331 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::PPCDoubleDouble(), "0.5")));
1332 EXPECT_TRUE(APFloat(APFloat::x87DoubleExtended(), "2.0").getExactInverse(&inv));
1333 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::x87DoubleExtended(), "0.5")));
1335 // FLT_MIN
1336 EXPECT_TRUE(APFloat(1.17549435e-38f).getExactInverse(&inv));
1337 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(8.5070592e+37f)));
1339 // Large float, inverse is a denormal.
1340 EXPECT_FALSE(APFloat(1.7014118e38f).getExactInverse(nullptr));
1341 // Zero
1342 EXPECT_FALSE(APFloat(0.0).getExactInverse(nullptr));
1343 // Denormalized float
1344 EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(nullptr));
1347 TEST(APFloatTest, roundToIntegral) {
1348 APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble())), P(0.0);
1350 P = T;
1351 P.roundToIntegral(APFloat::rmTowardZero);
1352 EXPECT_EQ(-0.0, P.convertToDouble());
1353 P = T;
1354 P.roundToIntegral(APFloat::rmTowardNegative);
1355 EXPECT_EQ(-1.0, P.convertToDouble());
1356 P = T;
1357 P.roundToIntegral(APFloat::rmTowardPositive);
1358 EXPECT_EQ(-0.0, P.convertToDouble());
1359 P = T;
1360 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1361 EXPECT_EQ(-0.0, P.convertToDouble());
1363 P = S;
1364 P.roundToIntegral(APFloat::rmTowardZero);
1365 EXPECT_EQ(3.0, P.convertToDouble());
1366 P = S;
1367 P.roundToIntegral(APFloat::rmTowardNegative);
1368 EXPECT_EQ(3.0, P.convertToDouble());
1369 P = S;
1370 P.roundToIntegral(APFloat::rmTowardPositive);
1371 EXPECT_EQ(4.0, P.convertToDouble());
1372 P = S;
1373 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1374 EXPECT_EQ(3.0, P.convertToDouble());
1376 P = R;
1377 P.roundToIntegral(APFloat::rmTowardZero);
1378 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1379 P = R;
1380 P.roundToIntegral(APFloat::rmTowardNegative);
1381 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1382 P = R;
1383 P.roundToIntegral(APFloat::rmTowardPositive);
1384 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1385 P = R;
1386 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1387 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1389 P = APFloat::getZero(APFloat::IEEEdouble());
1390 P.roundToIntegral(APFloat::rmTowardZero);
1391 EXPECT_EQ(0.0, P.convertToDouble());
1392 P = APFloat::getZero(APFloat::IEEEdouble(), true);
1393 P.roundToIntegral(APFloat::rmTowardZero);
1394 EXPECT_EQ(-0.0, P.convertToDouble());
1395 P = APFloat::getNaN(APFloat::IEEEdouble());
1396 P.roundToIntegral(APFloat::rmTowardZero);
1397 EXPECT_TRUE(std::isnan(P.convertToDouble()));
1398 P = APFloat::getInf(APFloat::IEEEdouble());
1399 P.roundToIntegral(APFloat::rmTowardZero);
1400 EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() > 0.0);
1401 P = APFloat::getInf(APFloat::IEEEdouble(), true);
1402 P.roundToIntegral(APFloat::rmTowardZero);
1403 EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() < 0.0);
1406 TEST(APFloatTest, isInteger) {
1407 APFloat T(-0.0);
1408 EXPECT_TRUE(T.isInteger());
1409 T = APFloat(3.14159);
1410 EXPECT_FALSE(T.isInteger());
1411 T = APFloat::getNaN(APFloat::IEEEdouble());
1412 EXPECT_FALSE(T.isInteger());
1413 T = APFloat::getInf(APFloat::IEEEdouble());
1414 EXPECT_FALSE(T.isInteger());
1415 T = APFloat::getInf(APFloat::IEEEdouble(), true);
1416 EXPECT_FALSE(T.isInteger());
1417 T = APFloat::getLargest(APFloat::IEEEdouble());
1418 EXPECT_TRUE(T.isInteger());
1421 TEST(DoubleAPFloatTest, isInteger) {
1422 APFloat F1(-0.0);
1423 APFloat F2(-0.0);
1424 llvm::detail::DoubleAPFloat T(APFloat::PPCDoubleDouble(), std::move(F1),
1425 std::move(F2));
1426 EXPECT_TRUE(T.isInteger());
1427 APFloat F3(3.14159);
1428 APFloat F4(-0.0);
1429 llvm::detail::DoubleAPFloat T2(APFloat::PPCDoubleDouble(), std::move(F3),
1430 std::move(F4));
1431 EXPECT_FALSE(T2.isInteger());
1432 APFloat F5(-0.0);
1433 APFloat F6(3.14159);
1434 llvm::detail::DoubleAPFloat T3(APFloat::PPCDoubleDouble(), std::move(F5),
1435 std::move(F6));
1436 EXPECT_FALSE(T3.isInteger());
1439 TEST(APFloatTest, getLargest) {
1440 EXPECT_EQ(3.402823466e+38f, APFloat::getLargest(APFloat::IEEEsingle()).convertToFloat());
1441 EXPECT_EQ(1.7976931348623158e+308, APFloat::getLargest(APFloat::IEEEdouble()).convertToDouble());
1444 TEST(APFloatTest, getSmallest) {
1445 APFloat test = APFloat::getSmallest(APFloat::IEEEsingle(), false);
1446 APFloat expected = APFloat(APFloat::IEEEsingle(), "0x0.000002p-126");
1447 EXPECT_FALSE(test.isNegative());
1448 EXPECT_TRUE(test.isFiniteNonZero());
1449 EXPECT_TRUE(test.isDenormal());
1450 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1452 test = APFloat::getSmallest(APFloat::IEEEsingle(), true);
1453 expected = APFloat(APFloat::IEEEsingle(), "-0x0.000002p-126");
1454 EXPECT_TRUE(test.isNegative());
1455 EXPECT_TRUE(test.isFiniteNonZero());
1456 EXPECT_TRUE(test.isDenormal());
1457 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1459 test = APFloat::getSmallest(APFloat::IEEEquad(), false);
1460 expected = APFloat(APFloat::IEEEquad(), "0x0.0000000000000000000000000001p-16382");
1461 EXPECT_FALSE(test.isNegative());
1462 EXPECT_TRUE(test.isFiniteNonZero());
1463 EXPECT_TRUE(test.isDenormal());
1464 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1466 test = APFloat::getSmallest(APFloat::IEEEquad(), true);
1467 expected = APFloat(APFloat::IEEEquad(), "-0x0.0000000000000000000000000001p-16382");
1468 EXPECT_TRUE(test.isNegative());
1469 EXPECT_TRUE(test.isFiniteNonZero());
1470 EXPECT_TRUE(test.isDenormal());
1471 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1474 TEST(APFloatTest, getSmallestNormalized) {
1475 APFloat test = APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
1476 APFloat expected = APFloat(APFloat::IEEEsingle(), "0x1p-126");
1477 EXPECT_FALSE(test.isNegative());
1478 EXPECT_TRUE(test.isFiniteNonZero());
1479 EXPECT_FALSE(test.isDenormal());
1480 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1482 test = APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
1483 expected = APFloat(APFloat::IEEEsingle(), "-0x1p-126");
1484 EXPECT_TRUE(test.isNegative());
1485 EXPECT_TRUE(test.isFiniteNonZero());
1486 EXPECT_FALSE(test.isDenormal());
1487 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1489 test = APFloat::getSmallestNormalized(APFloat::IEEEquad(), false);
1490 expected = APFloat(APFloat::IEEEquad(), "0x1p-16382");
1491 EXPECT_FALSE(test.isNegative());
1492 EXPECT_TRUE(test.isFiniteNonZero());
1493 EXPECT_FALSE(test.isDenormal());
1494 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1496 test = APFloat::getSmallestNormalized(APFloat::IEEEquad(), true);
1497 expected = APFloat(APFloat::IEEEquad(), "-0x1p-16382");
1498 EXPECT_TRUE(test.isNegative());
1499 EXPECT_TRUE(test.isFiniteNonZero());
1500 EXPECT_FALSE(test.isDenormal());
1501 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1504 TEST(APFloatTest, getZero) {
1505 struct {
1506 const fltSemantics *semantics;
1507 const bool sign;
1508 const unsigned long long bitPattern[2];
1509 const unsigned bitPatternLength;
1510 } const GetZeroTest[] = {
1511 { &APFloat::IEEEhalf(), false, {0, 0}, 1},
1512 { &APFloat::IEEEhalf(), true, {0x8000ULL, 0}, 1},
1513 { &APFloat::IEEEsingle(), false, {0, 0}, 1},
1514 { &APFloat::IEEEsingle(), true, {0x80000000ULL, 0}, 1},
1515 { &APFloat::IEEEdouble(), false, {0, 0}, 1},
1516 { &APFloat::IEEEdouble(), true, {0x8000000000000000ULL, 0}, 1},
1517 { &APFloat::IEEEquad(), false, {0, 0}, 2},
1518 { &APFloat::IEEEquad(), true, {0, 0x8000000000000000ULL}, 2},
1519 { &APFloat::PPCDoubleDouble(), false, {0, 0}, 2},
1520 { &APFloat::PPCDoubleDouble(), true, {0x8000000000000000ULL, 0}, 2},
1521 { &APFloat::x87DoubleExtended(), false, {0, 0}, 2},
1522 { &APFloat::x87DoubleExtended(), true, {0, 0x8000ULL}, 2},
1524 const unsigned NumGetZeroTests = 12;
1525 for (unsigned i = 0; i < NumGetZeroTests; ++i) {
1526 APFloat test = APFloat::getZero(*GetZeroTest[i].semantics,
1527 GetZeroTest[i].sign);
1528 const char *pattern = GetZeroTest[i].sign? "-0x0p+0" : "0x0p+0";
1529 APFloat expected = APFloat(*GetZeroTest[i].semantics,
1530 pattern);
1531 EXPECT_TRUE(test.isZero());
1532 EXPECT_TRUE(GetZeroTest[i].sign? test.isNegative() : !test.isNegative());
1533 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1534 for (unsigned j = 0, je = GetZeroTest[i].bitPatternLength; j < je; ++j) {
1535 EXPECT_EQ(GetZeroTest[i].bitPattern[j],
1536 test.bitcastToAPInt().getRawData()[j]);
1541 TEST(APFloatTest, copySign) {
1542 EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
1543 APFloat::copySign(APFloat(42.0), APFloat(-1.0))));
1544 EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
1545 APFloat::copySign(APFloat(-42.0), APFloat(1.0))));
1546 EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
1547 APFloat::copySign(APFloat(-42.0), APFloat(-1.0))));
1548 EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
1549 APFloat::copySign(APFloat(42.0), APFloat(1.0))));
1552 TEST(APFloatTest, convert) {
1553 bool losesInfo;
1554 APFloat test(APFloat::IEEEdouble(), "1.0");
1555 test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1556 EXPECT_EQ(1.0f, test.convertToFloat());
1557 EXPECT_FALSE(losesInfo);
1559 test = APFloat(APFloat::x87DoubleExtended(), "0x1p-53");
1560 test.add(APFloat(APFloat::x87DoubleExtended(), "1.0"), APFloat::rmNearestTiesToEven);
1561 test.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1562 EXPECT_EQ(1.0, test.convertToDouble());
1563 EXPECT_TRUE(losesInfo);
1565 test = APFloat(APFloat::IEEEquad(), "0x1p-53");
1566 test.add(APFloat(APFloat::IEEEquad(), "1.0"), APFloat::rmNearestTiesToEven);
1567 test.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1568 EXPECT_EQ(1.0, test.convertToDouble());
1569 EXPECT_TRUE(losesInfo);
1571 test = APFloat(APFloat::x87DoubleExtended(), "0xf.fffffffp+28");
1572 test.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1573 EXPECT_EQ(4294967295.0, test.convertToDouble());
1574 EXPECT_FALSE(losesInfo);
1576 test = APFloat::getSNaN(APFloat::IEEEsingle());
1577 APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended());
1578 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1579 &losesInfo);
1580 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1581 EXPECT_FALSE(losesInfo);
1583 test = APFloat::getQNaN(APFloat::IEEEsingle());
1584 APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended());
1585 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1586 &losesInfo);
1587 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1588 EXPECT_FALSE(losesInfo);
1590 test = APFloat::getSNaN(APFloat::x87DoubleExtended());
1591 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1592 &losesInfo);
1593 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1594 EXPECT_FALSE(losesInfo);
1596 test = APFloat::getQNaN(APFloat::x87DoubleExtended());
1597 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1598 &losesInfo);
1599 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1600 EXPECT_FALSE(losesInfo);
1603 TEST(APFloatTest, PPCDoubleDouble) {
1604 APFloat test(APFloat::PPCDoubleDouble(), "1.0");
1605 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1606 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1608 // LDBL_MAX
1609 test = APFloat(APFloat::PPCDoubleDouble(), "1.79769313486231580793728971405301e+308");
1610 EXPECT_EQ(0x7fefffffffffffffull, test.bitcastToAPInt().getRawData()[0]);
1611 EXPECT_EQ(0x7c8ffffffffffffeull, test.bitcastToAPInt().getRawData()[1]);
1613 // LDBL_MIN
1614 test = APFloat(APFloat::PPCDoubleDouble(), "2.00416836000897277799610805135016e-292");
1615 EXPECT_EQ(0x0360000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1616 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1618 // PR30869
1620 auto Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") +
1621 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1622 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1624 Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") -
1625 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1626 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1628 Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") *
1629 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1630 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1632 Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") /
1633 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1634 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1636 int Exp;
1637 Result = frexp(APFloat(APFloat::PPCDoubleDouble(), "1.0"), Exp,
1638 APFloat::rmNearestTiesToEven);
1639 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1641 Result = scalbn(APFloat(APFloat::PPCDoubleDouble(), "1.0"), 1,
1642 APFloat::rmNearestTiesToEven);
1643 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1647 TEST(APFloatTest, isNegative) {
1648 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1649 EXPECT_FALSE(t.isNegative());
1650 t = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
1651 EXPECT_TRUE(t.isNegative());
1653 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isNegative());
1654 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle(), true).isNegative());
1656 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isNegative());
1657 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle(), true).isNegative());
1659 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNegative());
1660 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle(), true).isNegative());
1662 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNegative());
1663 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), true).isNegative());
1666 TEST(APFloatTest, isNormal) {
1667 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1668 EXPECT_TRUE(t.isNormal());
1670 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isNormal());
1671 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isNormal());
1672 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNormal());
1673 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNormal());
1674 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isNormal());
1677 TEST(APFloatTest, isFinite) {
1678 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1679 EXPECT_TRUE(t.isFinite());
1680 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isFinite());
1681 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle(), false).isFinite());
1682 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isFinite());
1683 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isFinite());
1684 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isFinite());
1687 TEST(APFloatTest, isInfinity) {
1688 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1689 EXPECT_FALSE(t.isInfinity());
1690 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle(), false).isInfinity());
1691 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isInfinity());
1692 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isInfinity());
1693 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isInfinity());
1694 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isInfinity());
1697 TEST(APFloatTest, isNaN) {
1698 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1699 EXPECT_FALSE(t.isNaN());
1700 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isNaN());
1701 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isNaN());
1702 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNaN());
1703 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNaN());
1704 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isNaN());
1707 TEST(APFloatTest, isFiniteNonZero) {
1708 // Test positive/negative normal value.
1709 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "0x1p+0").isFiniteNonZero());
1710 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "-0x1p+0").isFiniteNonZero());
1712 // Test positive/negative denormal value.
1713 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isFiniteNonZero());
1714 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "-0x1p-149").isFiniteNonZero());
1716 // Test +/- Infinity.
1717 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isFiniteNonZero());
1718 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), true).isFiniteNonZero());
1720 // Test +/- Zero.
1721 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isFiniteNonZero());
1722 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), true).isFiniteNonZero());
1724 // Test +/- qNaN. +/- dont mean anything with qNaN but paranoia can't hurt in
1725 // this instance.
1726 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isFiniteNonZero());
1727 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), true).isFiniteNonZero());
1729 // Test +/- sNaN. +/- dont mean anything with sNaN but paranoia can't hurt in
1730 // this instance.
1731 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isFiniteNonZero());
1732 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), true).isFiniteNonZero());
1735 TEST(APFloatTest, add) {
1736 // Test Special Cases against each other and normal values.
1738 // TODOS/NOTES:
1739 // 1. Since we perform only default exception handling all operations with
1740 // signaling NaNs should have a result that is a quiet NaN. Currently they
1741 // return sNaN.
1743 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
1744 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
1745 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
1746 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
1747 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
1748 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
1749 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
1750 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
1751 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
1752 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
1753 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
1754 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
1755 APFloat PSmallestNormalized =
1756 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
1757 APFloat MSmallestNormalized =
1758 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
1760 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
1762 const unsigned NumTests = 169;
1763 struct {
1764 APFloat x;
1765 APFloat y;
1766 const char *result;
1767 int status;
1768 int category;
1769 } SpecialCaseTests[NumTests] = {
1770 { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1771 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1772 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1773 { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1774 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1775 #if 0
1776 // See Note 1.
1777 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1778 #endif
1779 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1780 { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1781 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1782 { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1783 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1784 { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1785 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1786 { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1787 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1788 { MInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1789 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1790 { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1791 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1792 #if 0
1793 // See Note 1.
1794 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1795 #endif
1796 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1797 { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1798 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1799 { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1800 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1801 { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1802 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1803 { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1804 { PZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1805 { PZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1806 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1807 { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1808 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1809 #if 0
1810 // See Note 1.
1811 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1812 #endif
1813 { PZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1814 { PZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1815 { PZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1816 { PZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1817 { PZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1818 { PZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1819 { PZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1820 { PZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1821 { MZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1822 { MZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1823 { MZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1824 { MZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
1825 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1826 #if 0
1827 // See Note 1.
1828 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1829 #endif
1830 { MZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1831 { MZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1832 { MZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1833 { MZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1834 { MZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1835 { MZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1836 { MZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1837 { MZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1838 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
1839 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
1840 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
1841 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
1842 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1843 #if 0
1844 // See Note 1.
1845 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1846 #endif
1847 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
1848 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
1849 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1850 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1851 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1852 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1853 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
1854 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
1855 #if 0
1856 // See Note 1.
1857 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1858 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1859 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1860 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1861 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1862 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1863 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1864 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1865 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1866 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1867 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1868 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1869 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1870 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1871 #endif
1872 { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1873 { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1874 { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1875 { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1876 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1877 #if 0
1878 // See Note 1.
1879 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1880 #endif
1881 { PNormalValue, PNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal },
1882 { PNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1883 { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1884 { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1885 { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1886 { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1887 { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1888 { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1889 { MNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1890 { MNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1891 { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1892 { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1893 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1894 #if 0
1895 // See Note 1.
1896 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1897 #endif
1898 { MNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1899 { MNormalValue, MNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal },
1900 { MNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1901 { MNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1902 { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1903 { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1904 { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1905 { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1906 { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1907 { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1908 { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1909 { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1910 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1911 #if 0
1912 // See Note 1.
1913 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1914 #endif
1915 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1916 { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1917 { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
1918 { PLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1919 { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1920 { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1921 { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1922 { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1923 { MLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1924 { MLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1925 { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1926 { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1927 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1928 #if 0
1929 // See Note 1.
1930 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1931 #endif
1932 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1933 { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1934 { MLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1935 { MLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
1936 { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1937 { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1938 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1939 { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1940 { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1941 { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1942 { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1943 { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1944 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1945 #if 0
1946 // See Note 1.
1947 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1948 #endif
1949 { PSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1950 { PSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1951 { PSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1952 { PSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1953 { PSmallestValue, PSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal },
1954 { PSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1955 { PSmallestValue, PSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1956 { PSmallestValue, MSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1957 { MSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1958 { MSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1959 { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1960 { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1961 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1962 #if 0
1963 // See Note 1.
1964 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1965 #endif
1966 { MSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1967 { MSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1968 { MSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1969 { MSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1970 { MSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1971 { MSmallestValue, MSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal },
1972 { MSmallestValue, PSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1973 { MSmallestValue, MSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1974 { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1975 { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1976 { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1977 { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1978 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1979 #if 0
1980 // See Note 1.
1981 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1982 #endif
1983 { PSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1984 { PSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1985 { PSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1986 { PSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1987 { PSmallestNormalized, PSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1988 { PSmallestNormalized, MSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1989 { PSmallestNormalized, PSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
1990 { PSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1991 { MSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1992 { MSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1993 { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1994 { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1995 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1996 #if 0
1997 // See Note 1.
1998 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1999 #endif
2000 { MSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2001 { MSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2002 { MSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2003 { MSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2004 { MSmallestNormalized, PSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2005 { MSmallestNormalized, MSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2006 { MSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2007 { MSmallestNormalized, MSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal }
2010 for (size_t i = 0; i < NumTests; ++i) {
2011 APFloat x(SpecialCaseTests[i].x);
2012 APFloat y(SpecialCaseTests[i].y);
2013 APFloat::opStatus status = x.add(y, APFloat::rmNearestTiesToEven);
2015 APFloat result(APFloat::IEEEsingle(), SpecialCaseTests[i].result);
2017 EXPECT_TRUE(result.bitwiseIsEqual(x));
2018 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2019 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2023 TEST(APFloatTest, subtract) {
2024 // Test Special Cases against each other and normal values.
2026 // TODOS/NOTES:
2027 // 1. Since we perform only default exception handling all operations with
2028 // signaling NaNs should have a result that is a quiet NaN. Currently they
2029 // return sNaN.
2031 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2032 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2033 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2034 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2035 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2036 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
2037 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2038 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2039 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2040 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2041 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2042 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2043 APFloat PSmallestNormalized =
2044 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2045 APFloat MSmallestNormalized =
2046 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2048 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2050 const unsigned NumTests = 169;
2051 struct {
2052 APFloat x;
2053 APFloat y;
2054 const char *result;
2055 int status;
2056 int category;
2057 } SpecialCaseTests[NumTests] = {
2058 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2059 { PInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2060 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2061 { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2062 { PInf, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2063 #if 0
2064 // See Note 1.
2065 { PInf, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2066 #endif
2067 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2068 { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2069 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2070 { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2071 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2072 { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2073 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2074 { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2075 { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2076 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2077 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2078 { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2079 { MInf, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2080 #if 0
2081 // See Note 1.
2082 { MInf, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2083 #endif
2084 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2085 { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2086 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2087 { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2088 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2089 { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2090 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2091 { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2092 { PZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2093 { PZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2094 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2095 { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2096 { PZero, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2097 #if 0
2098 // See Note 1.
2099 { PZero, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2100 #endif
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 #if 0
2115 // See Note 1.
2116 { MZero, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2117 #endif
2118 { MZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2119 { MZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2120 { MZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2121 { MZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2122 { MZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2123 { MZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2124 { MZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2125 { MZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2126 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2127 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2128 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2129 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2130 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2131 #if 0
2132 // See Note 1.
2133 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2134 #endif
2135 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2136 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2137 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2138 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2139 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2140 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2141 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2142 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2143 #if 0
2144 // See Note 1.
2145 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2146 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2147 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2148 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2149 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2150 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2151 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2152 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2153 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2154 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2155 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2156 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2157 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2158 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2159 #endif
2160 { PNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2161 { PNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2162 { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2163 { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2164 { PNormalValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2165 #if 0
2166 // See Note 1.
2167 { PNormalValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2168 #endif
2169 { PNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2170 { PNormalValue, MNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal },
2171 { PNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2172 { PNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2173 { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2174 { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2175 { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2176 { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2177 { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2178 { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2179 { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2180 { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2181 { MNormalValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2182 #if 0
2183 // See Note 1.
2184 { MNormalValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2185 #endif
2186 { MNormalValue, PNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal },
2187 { MNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2188 { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2189 { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2190 { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2191 { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2192 { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2193 { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2194 { PLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2195 { PLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2196 { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2197 { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2198 { PLargestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2199 #if 0
2200 // See Note 1.
2201 { PLargestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2202 #endif
2203 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2204 { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2205 { PLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2206 { PLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2207 { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2208 { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2209 { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2210 { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2211 { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2212 { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2213 { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2214 { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2215 { MLargestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2216 #if 0
2217 // See Note 1.
2218 { MLargestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2219 #endif
2220 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2221 { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2222 { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2223 { MLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2224 { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2225 { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2226 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2227 { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2228 { PSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2229 { PSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2230 { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2231 { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2232 { PSmallestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2233 #if 0
2234 // See Note 1.
2235 { PSmallestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2236 #endif
2237 { PSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2238 { PSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2239 { PSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2240 { PSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2241 { PSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2242 { PSmallestValue, MSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal },
2243 { PSmallestValue, PSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2244 { PSmallestValue, MSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2245 { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2246 { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2247 { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2248 { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2249 { MSmallestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2250 #if 0
2251 // See Note 1.
2252 { MSmallestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2253 #endif
2254 { MSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2255 { MSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2256 { MSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2257 { MSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2258 { MSmallestValue, PSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal },
2259 { MSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2260 { MSmallestValue, PSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2261 { MSmallestValue, MSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2262 { PSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2263 { PSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2264 { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2265 { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2266 { PSmallestNormalized, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2267 #if 0
2268 // See Note 1.
2269 { PSmallestNormalized, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2270 #endif
2271 { PSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2272 { PSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2273 { PSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2274 { PSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2275 { PSmallestNormalized, PSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2276 { PSmallestNormalized, MSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2277 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2278 { PSmallestNormalized, MSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
2279 { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2280 { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2281 { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2282 { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2283 { MSmallestNormalized, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2284 #if 0
2285 // See Note 1.
2286 { MSmallestNormalized, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2287 #endif
2288 { MSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2289 { MSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2290 { MSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2291 { MSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2292 { MSmallestNormalized, PSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2293 { MSmallestNormalized, MSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2294 { MSmallestNormalized, PSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
2295 { MSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }
2298 for (size_t i = 0; i < NumTests; ++i) {
2299 APFloat x(SpecialCaseTests[i].x);
2300 APFloat y(SpecialCaseTests[i].y);
2301 APFloat::opStatus status = x.subtract(y, APFloat::rmNearestTiesToEven);
2303 APFloat result(APFloat::IEEEsingle(), SpecialCaseTests[i].result);
2305 EXPECT_TRUE(result.bitwiseIsEqual(x));
2306 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2307 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2311 TEST(APFloatTest, multiply) {
2312 // Test Special Cases against each other and normal values.
2314 // TODOS/NOTES:
2315 // 1. Since we perform only default exception handling all operations with
2316 // signaling NaNs should have a result that is a quiet NaN. Currently they
2317 // return sNaN.
2319 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2320 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2321 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2322 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2323 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2324 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
2325 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2326 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2327 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2328 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2329 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2330 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2331 APFloat PSmallestNormalized =
2332 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2333 APFloat MSmallestNormalized =
2334 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2336 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2337 const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact;
2339 const unsigned NumTests = 169;
2340 struct {
2341 APFloat x;
2342 APFloat y;
2343 const char *result;
2344 int status;
2345 int category;
2346 } SpecialCaseTests[NumTests] = {
2347 { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2348 { PInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2349 { PInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2350 { PInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2351 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2352 #if 0
2353 // See Note 1.
2354 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2355 #endif
2356 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2357 { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2358 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2359 { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2360 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2361 { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2362 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2363 { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2364 { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2365 { MInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2366 { MInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2367 { MInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2368 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2369 #if 0
2370 // See Note 1.
2371 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2372 #endif
2373 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2374 { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2375 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2376 { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2377 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2378 { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2379 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2380 { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2381 { PZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2382 { PZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2383 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2384 { PZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2385 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2386 #if 0
2387 // See Note 1.
2388 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2389 #endif
2390 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2391 { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2392 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2393 { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2394 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2395 { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2396 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2397 { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2398 { MZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2399 { MZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2400 { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2401 { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2402 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2403 #if 0
2404 // See Note 1.
2405 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2406 #endif
2407 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2408 { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2409 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2410 { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2411 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2412 { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2413 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2414 { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2415 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2416 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2417 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2418 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2419 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2420 #if 0
2421 // See Note 1.
2422 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2423 #endif
2424 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2425 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2426 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2427 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2428 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2429 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2430 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2431 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2432 #if 0
2433 // See Note 1.
2434 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2435 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2436 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2437 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2438 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2439 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2440 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2441 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2442 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2443 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2444 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2445 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2446 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2447 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2448 #endif
2449 { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2450 { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2451 { PNormalValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2452 { PNormalValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2453 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2454 #if 0
2455 // See Note 1.
2456 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2457 #endif
2458 { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2459 { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2460 { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2461 { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2462 { PNormalValue, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2463 { PNormalValue, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2464 { PNormalValue, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2465 { PNormalValue, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2466 { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2467 { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2468 { MNormalValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2469 { MNormalValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2470 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2471 #if 0
2472 // See Note 1.
2473 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2474 #endif
2475 { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2476 { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2477 { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2478 { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2479 { MNormalValue, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2480 { MNormalValue, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2481 { MNormalValue, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2482 { MNormalValue, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2483 { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2484 { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2485 { PLargestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2486 { PLargestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2487 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2488 #if 0
2489 // See Note 1.
2490 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2491 #endif
2492 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2493 { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2494 { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2495 { PLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2496 { PLargestValue, PSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2497 { PLargestValue, MSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2498 { PLargestValue, PSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2499 { PLargestValue, MSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2500 { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2501 { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2502 { MLargestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2503 { MLargestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2504 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2505 #if 0
2506 // See Note 1.
2507 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2508 #endif
2509 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2510 { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2511 { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2512 { MLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2513 { MLargestValue, PSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2514 { MLargestValue, MSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2515 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2516 { MLargestValue, MSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2517 { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2518 { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2519 { PSmallestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2520 { PSmallestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2521 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2522 #if 0
2523 // See Note 1.
2524 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2525 #endif
2526 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2527 { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2528 { PSmallestValue, PLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2529 { PSmallestValue, MLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2530 { PSmallestValue, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2531 { PSmallestValue, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2532 { PSmallestValue, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2533 { PSmallestValue, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2534 { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2535 { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2536 { MSmallestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2537 { MSmallestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2538 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2539 #if 0
2540 // See Note 1.
2541 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2542 #endif
2543 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2544 { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2545 { MSmallestValue, PLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2546 { MSmallestValue, MLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2547 { MSmallestValue, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2548 { MSmallestValue, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2549 { MSmallestValue, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2550 { MSmallestValue, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2551 { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2552 { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2553 { PSmallestNormalized, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2554 { PSmallestNormalized, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2555 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2556 #if 0
2557 // See Note 1.
2558 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2559 #endif
2560 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2561 { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2562 { PSmallestNormalized, PLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2563 { PSmallestNormalized, MLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2564 { PSmallestNormalized, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2565 { PSmallestNormalized, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2566 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2567 { PSmallestNormalized, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2568 { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2569 { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2570 { MSmallestNormalized, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2571 { MSmallestNormalized, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2572 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2573 #if 0
2574 // See Note 1.
2575 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2576 #endif
2577 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2578 { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2579 { MSmallestNormalized, PLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2580 { MSmallestNormalized, MLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2581 { MSmallestNormalized, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2582 { MSmallestNormalized, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2583 { MSmallestNormalized, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2584 { MSmallestNormalized, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero }
2587 for (size_t i = 0; i < NumTests; ++i) {
2588 APFloat x(SpecialCaseTests[i].x);
2589 APFloat y(SpecialCaseTests[i].y);
2590 APFloat::opStatus status = x.multiply(y, APFloat::rmNearestTiesToEven);
2592 APFloat result(APFloat::IEEEsingle(), SpecialCaseTests[i].result);
2594 EXPECT_TRUE(result.bitwiseIsEqual(x));
2595 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2596 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2600 TEST(APFloatTest, divide) {
2601 // Test Special Cases against each other and normal values.
2603 // TODOS/NOTES:
2604 // 1. Since we perform only default exception handling all operations with
2605 // signaling NaNs should have a result that is a quiet NaN. Currently they
2606 // return sNaN.
2608 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2609 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2610 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2611 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2612 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2613 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
2614 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2615 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2616 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2617 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2618 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2619 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2620 APFloat PSmallestNormalized =
2621 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2622 APFloat MSmallestNormalized =
2623 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2625 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2626 const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact;
2628 const unsigned NumTests = 169;
2629 struct {
2630 APFloat x;
2631 APFloat y;
2632 const char *result;
2633 int status;
2634 int category;
2635 } SpecialCaseTests[NumTests] = {
2636 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2637 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2638 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2639 { PInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2640 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2641 #if 0
2642 // See Note 1.
2643 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2644 #endif
2645 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2646 { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2647 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2648 { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2649 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2650 { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2651 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2652 { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2653 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2654 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2655 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2656 { MInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2657 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2658 #if 0
2659 // See Note 1.
2660 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2661 #endif
2662 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2663 { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2664 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2665 { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2666 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2667 { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2668 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2669 { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2670 { PZero, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2671 { PZero, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2672 { PZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2673 { PZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2674 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2675 #if 0
2676 // See Note 1.
2677 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2678 #endif
2679 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2680 { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2681 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2682 { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2683 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2684 { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2685 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2686 { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2687 { MZero, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2688 { MZero, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2689 { MZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2690 { MZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2691 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2692 #if 0
2693 // See Note 1.
2694 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2695 #endif
2696 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2697 { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2698 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2699 { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2700 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2701 { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2702 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2703 { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2704 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2705 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2706 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2707 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2708 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2709 #if 0
2710 // See Note 1.
2711 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2712 #endif
2713 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2714 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2715 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2716 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2717 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2718 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2719 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2720 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2721 #if 0
2722 // See Note 1.
2723 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2724 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2725 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2726 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2727 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2728 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2729 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2730 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2731 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2732 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2733 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2734 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2735 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2736 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2737 #endif
2738 { PNormalValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2739 { PNormalValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2740 { PNormalValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2741 { PNormalValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2742 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2743 #if 0
2744 // See Note 1.
2745 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2746 #endif
2747 { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2748 { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2749 { PNormalValue, PLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal },
2750 { PNormalValue, MLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal },
2751 { PNormalValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2752 { PNormalValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2753 { PNormalValue, PSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal },
2754 { PNormalValue, MSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal },
2755 { MNormalValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2756 { MNormalValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2757 { MNormalValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2758 { MNormalValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2759 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2760 #if 0
2761 // See Note 1.
2762 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2763 #endif
2764 { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2765 { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2766 { MNormalValue, PLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal },
2767 { MNormalValue, MLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal },
2768 { MNormalValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2769 { MNormalValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2770 { MNormalValue, PSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal },
2771 { MNormalValue, MSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal },
2772 { PLargestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2773 { PLargestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2774 { PLargestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2775 { PLargestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2776 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2777 #if 0
2778 // See Note 1.
2779 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2780 #endif
2781 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2782 { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2783 { PLargestValue, PLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2784 { PLargestValue, MLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2785 { PLargestValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2786 { PLargestValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2787 { PLargestValue, PSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity },
2788 { PLargestValue, MSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity },
2789 { MLargestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2790 { MLargestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2791 { MLargestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2792 { MLargestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2793 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2794 #if 0
2795 // See Note 1.
2796 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2797 #endif
2798 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2799 { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2800 { MLargestValue, PLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2801 { MLargestValue, MLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2802 { MLargestValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2803 { MLargestValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2804 { MLargestValue, PSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity },
2805 { MLargestValue, MSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity },
2806 { PSmallestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2807 { PSmallestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2808 { PSmallestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2809 { PSmallestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2810 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2811 #if 0
2812 // See Note 1.
2813 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2814 #endif
2815 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2816 { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2817 { PSmallestValue, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2818 { PSmallestValue, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2819 { PSmallestValue, PSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2820 { PSmallestValue, MSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2821 { PSmallestValue, PSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal },
2822 { PSmallestValue, MSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal },
2823 { MSmallestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2824 { MSmallestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2825 { MSmallestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2826 { MSmallestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2827 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2828 #if 0
2829 // See Note 1.
2830 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2831 #endif
2832 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2833 { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2834 { MSmallestValue, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2835 { MSmallestValue, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2836 { MSmallestValue, PSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2837 { MSmallestValue, MSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2838 { MSmallestValue, PSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal },
2839 { MSmallestValue, MSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal },
2840 { PSmallestNormalized, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2841 { PSmallestNormalized, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2842 { PSmallestNormalized, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2843 { PSmallestNormalized, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2844 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2845 #if 0
2846 // See Note 1.
2847 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2848 #endif
2849 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2850 { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2851 { PSmallestNormalized, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2852 { PSmallestNormalized, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2853 { PSmallestNormalized, PSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal },
2854 { PSmallestNormalized, MSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal },
2855 { PSmallestNormalized, PSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2856 { PSmallestNormalized, MSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2857 { MSmallestNormalized, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2858 { MSmallestNormalized, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2859 { MSmallestNormalized, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2860 { MSmallestNormalized, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2861 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2862 #if 0
2863 // See Note 1.
2864 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2865 #endif
2866 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2867 { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2868 { MSmallestNormalized, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2869 { MSmallestNormalized, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2870 { MSmallestNormalized, PSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal },
2871 { MSmallestNormalized, MSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal },
2872 { MSmallestNormalized, PSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2873 { MSmallestNormalized, MSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2876 for (size_t i = 0; i < NumTests; ++i) {
2877 APFloat x(SpecialCaseTests[i].x);
2878 APFloat y(SpecialCaseTests[i].y);
2879 APFloat::opStatus status = x.divide(y, APFloat::rmNearestTiesToEven);
2881 APFloat result(APFloat::IEEEsingle(), SpecialCaseTests[i].result);
2883 EXPECT_TRUE(result.bitwiseIsEqual(x));
2884 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2885 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2889 TEST(APFloatTest, operatorOverloads) {
2890 // This is mostly testing that these operator overloads compile.
2891 APFloat One = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2892 APFloat Two = APFloat(APFloat::IEEEsingle(), "0x2p+0");
2893 EXPECT_TRUE(Two.bitwiseIsEqual(One + One));
2894 EXPECT_TRUE(One.bitwiseIsEqual(Two - One));
2895 EXPECT_TRUE(Two.bitwiseIsEqual(One * Two));
2896 EXPECT_TRUE(One.bitwiseIsEqual(Two / Two));
2899 TEST(APFloatTest, abs) {
2900 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2901 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2902 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2903 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2904 APFloat PQNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2905 APFloat MQNaN = APFloat::getNaN(APFloat::IEEEsingle(), true);
2906 APFloat PSNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
2907 APFloat MSNaN = APFloat::getSNaN(APFloat::IEEEsingle(), true);
2908 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2909 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2910 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2911 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2912 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2913 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2914 APFloat PSmallestNormalized =
2915 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2916 APFloat MSmallestNormalized =
2917 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2919 EXPECT_TRUE(PInf.bitwiseIsEqual(abs(PInf)));
2920 EXPECT_TRUE(PInf.bitwiseIsEqual(abs(MInf)));
2921 EXPECT_TRUE(PZero.bitwiseIsEqual(abs(PZero)));
2922 EXPECT_TRUE(PZero.bitwiseIsEqual(abs(MZero)));
2923 EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(PQNaN)));
2924 EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(MQNaN)));
2925 EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(PSNaN)));
2926 EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(MSNaN)));
2927 EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(PNormalValue)));
2928 EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(MNormalValue)));
2929 EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(PLargestValue)));
2930 EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(MLargestValue)));
2931 EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(PSmallestValue)));
2932 EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(MSmallestValue)));
2933 EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(PSmallestNormalized)));
2934 EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(MSmallestNormalized)));
2937 TEST(APFloatTest, neg) {
2938 APFloat One = APFloat(APFloat::IEEEsingle(), "1.0");
2939 APFloat NegOne = APFloat(APFloat::IEEEsingle(), "-1.0");
2940 APFloat Zero = APFloat::getZero(APFloat::IEEEsingle(), false);
2941 APFloat NegZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2942 APFloat Inf = APFloat::getInf(APFloat::IEEEsingle(), false);
2943 APFloat NegInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2944 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2945 APFloat NegQNaN = APFloat::getNaN(APFloat::IEEEsingle(), true);
2947 EXPECT_TRUE(NegOne.bitwiseIsEqual(neg(One)));
2948 EXPECT_TRUE(One.bitwiseIsEqual(neg(NegOne)));
2949 EXPECT_TRUE(NegZero.bitwiseIsEqual(neg(Zero)));
2950 EXPECT_TRUE(Zero.bitwiseIsEqual(neg(NegZero)));
2951 EXPECT_TRUE(NegInf.bitwiseIsEqual(neg(Inf)));
2952 EXPECT_TRUE(Inf.bitwiseIsEqual(neg(NegInf)));
2953 EXPECT_TRUE(NegInf.bitwiseIsEqual(neg(Inf)));
2954 EXPECT_TRUE(Inf.bitwiseIsEqual(neg(NegInf)));
2955 EXPECT_TRUE(NegQNaN.bitwiseIsEqual(neg(QNaN)));
2956 EXPECT_TRUE(QNaN.bitwiseIsEqual(neg(NegQNaN)));
2959 TEST(APFloatTest, ilogb) {
2960 EXPECT_EQ(-1074, ilogb(APFloat::getSmallest(APFloat::IEEEdouble(), false)));
2961 EXPECT_EQ(-1074, ilogb(APFloat::getSmallest(APFloat::IEEEdouble(), true)));
2962 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1024")));
2963 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1023")));
2964 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1023")));
2965 EXPECT_EQ(-51, ilogb(APFloat(APFloat::IEEEdouble(), "0x1p-51")));
2966 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-1023")));
2967 EXPECT_EQ(-2, ilogb(APFloat(APFloat::IEEEdouble(), "0x0.ffffp-1")));
2968 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.fffep-1023")));
2969 EXPECT_EQ(1023, ilogb(APFloat::getLargest(APFloat::IEEEdouble(), false)));
2970 EXPECT_EQ(1023, ilogb(APFloat::getLargest(APFloat::IEEEdouble(), true)));
2973 EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle(), "0x1p+0")));
2974 EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle(), "-0x1p+0")));
2975 EXPECT_EQ(42, ilogb(APFloat(APFloat::IEEEsingle(), "0x1p+42")));
2976 EXPECT_EQ(-42, ilogb(APFloat(APFloat::IEEEsingle(), "0x1p-42")));
2978 EXPECT_EQ(APFloat::IEK_Inf,
2979 ilogb(APFloat::getInf(APFloat::IEEEsingle(), false)));
2980 EXPECT_EQ(APFloat::IEK_Inf,
2981 ilogb(APFloat::getInf(APFloat::IEEEsingle(), true)));
2982 EXPECT_EQ(APFloat::IEK_Zero,
2983 ilogb(APFloat::getZero(APFloat::IEEEsingle(), false)));
2984 EXPECT_EQ(APFloat::IEK_Zero,
2985 ilogb(APFloat::getZero(APFloat::IEEEsingle(), true)));
2986 EXPECT_EQ(APFloat::IEK_NaN,
2987 ilogb(APFloat::getNaN(APFloat::IEEEsingle(), false)));
2988 EXPECT_EQ(APFloat::IEK_NaN,
2989 ilogb(APFloat::getSNaN(APFloat::IEEEsingle(), false)));
2991 EXPECT_EQ(127, ilogb(APFloat::getLargest(APFloat::IEEEsingle(), false)));
2992 EXPECT_EQ(127, ilogb(APFloat::getLargest(APFloat::IEEEsingle(), true)));
2994 EXPECT_EQ(-149, ilogb(APFloat::getSmallest(APFloat::IEEEsingle(), false)));
2995 EXPECT_EQ(-149, ilogb(APFloat::getSmallest(APFloat::IEEEsingle(), true)));
2996 EXPECT_EQ(-126,
2997 ilogb(APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false)));
2998 EXPECT_EQ(-126,
2999 ilogb(APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true)));
3002 TEST(APFloatTest, scalbn) {
3004 const APFloat::roundingMode RM = APFloat::rmNearestTiesToEven;
3005 EXPECT_TRUE(
3006 APFloat(APFloat::IEEEsingle(), "0x1p+0")
3007 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), 0, RM)));
3008 EXPECT_TRUE(
3009 APFloat(APFloat::IEEEsingle(), "0x1p+42")
3010 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), 42, RM)));
3011 EXPECT_TRUE(
3012 APFloat(APFloat::IEEEsingle(), "0x1p-42")
3013 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), -42, RM)));
3015 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
3016 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
3017 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
3018 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
3019 APFloat QPNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
3020 APFloat QMNaN = APFloat::getNaN(APFloat::IEEEsingle(), true);
3021 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
3023 EXPECT_TRUE(PInf.bitwiseIsEqual(scalbn(PInf, 0, RM)));
3024 EXPECT_TRUE(MInf.bitwiseIsEqual(scalbn(MInf, 0, RM)));
3025 EXPECT_TRUE(PZero.bitwiseIsEqual(scalbn(PZero, 0, RM)));
3026 EXPECT_TRUE(MZero.bitwiseIsEqual(scalbn(MZero, 0, RM)));
3027 EXPECT_TRUE(QPNaN.bitwiseIsEqual(scalbn(QPNaN, 0, RM)));
3028 EXPECT_TRUE(QMNaN.bitwiseIsEqual(scalbn(QMNaN, 0, RM)));
3029 EXPECT_FALSE(scalbn(SNaN, 0, RM).isSignaling());
3031 APFloat ScalbnSNaN = scalbn(SNaN, 1, RM);
3032 EXPECT_TRUE(ScalbnSNaN.isNaN() && !ScalbnSNaN.isSignaling());
3034 // Make sure highest bit of payload is preserved.
3035 const APInt Payload(64, (UINT64_C(1) << 50) |
3036 (UINT64_C(1) << 49) |
3037 (UINT64_C(1234) << 32) |
3040 APFloat SNaNWithPayload = APFloat::getSNaN(APFloat::IEEEdouble(), false,
3041 &Payload);
3042 APFloat QuietPayload = scalbn(SNaNWithPayload, 1, RM);
3043 EXPECT_TRUE(QuietPayload.isNaN() && !QuietPayload.isSignaling());
3044 EXPECT_EQ(Payload, QuietPayload.bitcastToAPInt().getLoBits(51));
3046 EXPECT_TRUE(PInf.bitwiseIsEqual(
3047 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), 128, RM)));
3048 EXPECT_TRUE(MInf.bitwiseIsEqual(
3049 scalbn(APFloat(APFloat::IEEEsingle(), "-0x1p+0"), 128, RM)));
3050 EXPECT_TRUE(PInf.bitwiseIsEqual(
3051 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+127"), 1, RM)));
3052 EXPECT_TRUE(PZero.bitwiseIsEqual(
3053 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p-127"), -127, RM)));
3054 EXPECT_TRUE(MZero.bitwiseIsEqual(
3055 scalbn(APFloat(APFloat::IEEEsingle(), "-0x1p-127"), -127, RM)));
3056 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "-0x1p-149").bitwiseIsEqual(
3057 scalbn(APFloat(APFloat::IEEEsingle(), "-0x1p-127"), -22, RM)));
3058 EXPECT_TRUE(PZero.bitwiseIsEqual(
3059 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p-126"), -24, RM)));
3062 APFloat SmallestF64 = APFloat::getSmallest(APFloat::IEEEdouble(), false);
3063 APFloat NegSmallestF64 = APFloat::getSmallest(APFloat::IEEEdouble(), true);
3065 APFloat LargestF64 = APFloat::getLargest(APFloat::IEEEdouble(), false);
3066 APFloat NegLargestF64 = APFloat::getLargest(APFloat::IEEEdouble(), true);
3068 APFloat SmallestNormalizedF64
3069 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), false);
3070 APFloat NegSmallestNormalizedF64
3071 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), true);
3073 APFloat LargestDenormalF64(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1023");
3074 APFloat NegLargestDenormalF64(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1023");
3077 EXPECT_TRUE(SmallestF64.bitwiseIsEqual(
3078 scalbn(APFloat(APFloat::IEEEdouble(), "0x1p-1074"), 0, RM)));
3079 EXPECT_TRUE(NegSmallestF64.bitwiseIsEqual(
3080 scalbn(APFloat(APFloat::IEEEdouble(), "-0x1p-1074"), 0, RM)));
3082 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+1023")
3083 .bitwiseIsEqual(scalbn(SmallestF64, 2097, RM)));
3085 EXPECT_TRUE(scalbn(SmallestF64, -2097, RM).isPosZero());
3086 EXPECT_TRUE(scalbn(SmallestF64, -2098, RM).isPosZero());
3087 EXPECT_TRUE(scalbn(SmallestF64, -2099, RM).isPosZero());
3088 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+1022")
3089 .bitwiseIsEqual(scalbn(SmallestF64, 2096, RM)));
3090 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+1023")
3091 .bitwiseIsEqual(scalbn(SmallestF64, 2097, RM)));
3092 EXPECT_TRUE(scalbn(SmallestF64, 2098, RM).isInfinity());
3093 EXPECT_TRUE(scalbn(SmallestF64, 2099, RM).isInfinity());
3095 // Test for integer overflows when adding to exponent.
3096 EXPECT_TRUE(scalbn(SmallestF64, -INT_MAX, RM).isPosZero());
3097 EXPECT_TRUE(scalbn(LargestF64, INT_MAX, RM).isInfinity());
3099 EXPECT_TRUE(LargestDenormalF64
3100 .bitwiseIsEqual(scalbn(LargestDenormalF64, 0, RM)));
3101 EXPECT_TRUE(NegLargestDenormalF64
3102 .bitwiseIsEqual(scalbn(NegLargestDenormalF64, 0, RM)));
3104 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1022")
3105 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1, RM)));
3106 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1021")
3107 .bitwiseIsEqual(scalbn(NegLargestDenormalF64, 2, RM)));
3109 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep+1")
3110 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1024, RM)));
3111 EXPECT_TRUE(scalbn(LargestDenormalF64, -1023, RM).isPosZero());
3112 EXPECT_TRUE(scalbn(LargestDenormalF64, -1024, RM).isPosZero());
3113 EXPECT_TRUE(scalbn(LargestDenormalF64, -2048, RM).isPosZero());
3114 EXPECT_TRUE(scalbn(LargestDenormalF64, 2047, RM).isInfinity());
3115 EXPECT_TRUE(scalbn(LargestDenormalF64, 2098, RM).isInfinity());
3116 EXPECT_TRUE(scalbn(LargestDenormalF64, 2099, RM).isInfinity());
3118 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-2")
3119 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1021, RM)));
3120 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1")
3121 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1022, RM)));
3122 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep+0")
3123 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1023, RM)));
3124 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep+1023")
3125 .bitwiseIsEqual(scalbn(LargestDenormalF64, 2046, RM)));
3126 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+974")
3127 .bitwiseIsEqual(scalbn(SmallestF64, 2048, RM)));
3129 APFloat RandomDenormalF64(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp+51");
3130 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-972")
3131 .bitwiseIsEqual(scalbn(RandomDenormalF64, -1023, RM)));
3132 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-1")
3133 .bitwiseIsEqual(scalbn(RandomDenormalF64, -52, RM)));
3134 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-2")
3135 .bitwiseIsEqual(scalbn(RandomDenormalF64, -53, RM)));
3136 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp+0")
3137 .bitwiseIsEqual(scalbn(RandomDenormalF64, -51, RM)));
3139 EXPECT_TRUE(scalbn(RandomDenormalF64, -2097, RM).isPosZero());
3140 EXPECT_TRUE(scalbn(RandomDenormalF64, -2090, RM).isPosZero());
3143 EXPECT_TRUE(
3144 APFloat(APFloat::IEEEdouble(), "-0x1p-1073")
3145 .bitwiseIsEqual(scalbn(NegLargestF64, -2097, RM)));
3147 EXPECT_TRUE(
3148 APFloat(APFloat::IEEEdouble(), "-0x1p-1024")
3149 .bitwiseIsEqual(scalbn(NegLargestF64, -2048, RM)));
3151 EXPECT_TRUE(
3152 APFloat(APFloat::IEEEdouble(), "0x1p-1073")
3153 .bitwiseIsEqual(scalbn(LargestF64, -2097, RM)));
3155 EXPECT_TRUE(
3156 APFloat(APFloat::IEEEdouble(), "0x1p-1074")
3157 .bitwiseIsEqual(scalbn(LargestF64, -2098, RM)));
3158 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1p-1074")
3159 .bitwiseIsEqual(scalbn(NegLargestF64, -2098, RM)));
3160 EXPECT_TRUE(scalbn(NegLargestF64, -2099, RM).isNegZero());
3161 EXPECT_TRUE(scalbn(LargestF64, 1, RM).isInfinity());
3164 EXPECT_TRUE(
3165 APFloat(APFloat::IEEEdouble(), "0x1p+0")
3166 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEdouble(), "0x1p+52"), -52, RM)));
3168 EXPECT_TRUE(
3169 APFloat(APFloat::IEEEdouble(), "0x1p-103")
3170 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEdouble(), "0x1p-51"), -52, RM)));
3173 TEST(APFloatTest, frexp) {
3174 const APFloat::roundingMode RM = APFloat::rmNearestTiesToEven;
3176 APFloat PZero = APFloat::getZero(APFloat::IEEEdouble(), false);
3177 APFloat MZero = APFloat::getZero(APFloat::IEEEdouble(), true);
3178 APFloat One(1.0);
3179 APFloat MOne(-1.0);
3180 APFloat Two(2.0);
3181 APFloat MTwo(-2.0);
3183 APFloat LargestDenormal(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1023");
3184 APFloat NegLargestDenormal(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1023");
3186 APFloat Smallest = APFloat::getSmallest(APFloat::IEEEdouble(), false);
3187 APFloat NegSmallest = APFloat::getSmallest(APFloat::IEEEdouble(), true);
3189 APFloat Largest = APFloat::getLargest(APFloat::IEEEdouble(), false);
3190 APFloat NegLargest = APFloat::getLargest(APFloat::IEEEdouble(), true);
3192 APFloat PInf = APFloat::getInf(APFloat::IEEEdouble(), false);
3193 APFloat MInf = APFloat::getInf(APFloat::IEEEdouble(), true);
3195 APFloat QPNaN = APFloat::getNaN(APFloat::IEEEdouble(), false);
3196 APFloat QMNaN = APFloat::getNaN(APFloat::IEEEdouble(), true);
3197 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEdouble(), false);
3199 // Make sure highest bit of payload is preserved.
3200 const APInt Payload(64, (UINT64_C(1) << 50) |
3201 (UINT64_C(1) << 49) |
3202 (UINT64_C(1234) << 32) |
3205 APFloat SNaNWithPayload = APFloat::getSNaN(APFloat::IEEEdouble(), false,
3206 &Payload);
3208 APFloat SmallestNormalized
3209 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), false);
3210 APFloat NegSmallestNormalized
3211 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), true);
3213 int Exp;
3214 APFloat Frac(APFloat::IEEEdouble());
3217 Frac = frexp(PZero, Exp, RM);
3218 EXPECT_EQ(0, Exp);
3219 EXPECT_TRUE(Frac.isPosZero());
3221 Frac = frexp(MZero, Exp, RM);
3222 EXPECT_EQ(0, Exp);
3223 EXPECT_TRUE(Frac.isNegZero());
3226 Frac = frexp(One, Exp, RM);
3227 EXPECT_EQ(1, Exp);
3228 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p-1").bitwiseIsEqual(Frac));
3230 Frac = frexp(MOne, Exp, RM);
3231 EXPECT_EQ(1, Exp);
3232 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1p-1").bitwiseIsEqual(Frac));
3234 Frac = frexp(LargestDenormal, Exp, RM);
3235 EXPECT_EQ(-1022, Exp);
3236 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1").bitwiseIsEqual(Frac));
3238 Frac = frexp(NegLargestDenormal, Exp, RM);
3239 EXPECT_EQ(-1022, Exp);
3240 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1").bitwiseIsEqual(Frac));
3243 Frac = frexp(Smallest, Exp, RM);
3244 EXPECT_EQ(-1073, Exp);
3245 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p-1").bitwiseIsEqual(Frac));
3247 Frac = frexp(NegSmallest, Exp, RM);
3248 EXPECT_EQ(-1073, Exp);
3249 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1p-1").bitwiseIsEqual(Frac));
3252 Frac = frexp(Largest, Exp, RM);
3253 EXPECT_EQ(1024, Exp);
3254 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.fffffffffffffp-1").bitwiseIsEqual(Frac));
3256 Frac = frexp(NegLargest, Exp, RM);
3257 EXPECT_EQ(1024, Exp);
3258 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1.fffffffffffffp-1").bitwiseIsEqual(Frac));
3261 Frac = frexp(PInf, Exp, RM);
3262 EXPECT_EQ(INT_MAX, Exp);
3263 EXPECT_TRUE(Frac.isInfinity() && !Frac.isNegative());
3265 Frac = frexp(MInf, Exp, RM);
3266 EXPECT_EQ(INT_MAX, Exp);
3267 EXPECT_TRUE(Frac.isInfinity() && Frac.isNegative());
3269 Frac = frexp(QPNaN, Exp, RM);
3270 EXPECT_EQ(INT_MIN, Exp);
3271 EXPECT_TRUE(Frac.isNaN());
3273 Frac = frexp(QMNaN, Exp, RM);
3274 EXPECT_EQ(INT_MIN, Exp);
3275 EXPECT_TRUE(Frac.isNaN());
3277 Frac = frexp(SNaN, Exp, RM);
3278 EXPECT_EQ(INT_MIN, Exp);
3279 EXPECT_TRUE(Frac.isNaN() && !Frac.isSignaling());
3281 Frac = frexp(SNaNWithPayload, Exp, RM);
3282 EXPECT_EQ(INT_MIN, Exp);
3283 EXPECT_TRUE(Frac.isNaN() && !Frac.isSignaling());
3284 EXPECT_EQ(Payload, Frac.bitcastToAPInt().getLoBits(51));
3286 Frac = frexp(APFloat(APFloat::IEEEdouble(), "0x0.ffffp-1"), Exp, RM);
3287 EXPECT_EQ(-1, Exp);
3288 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.fffep-1").bitwiseIsEqual(Frac));
3290 Frac = frexp(APFloat(APFloat::IEEEdouble(), "0x1p-51"), Exp, RM);
3291 EXPECT_EQ(-50, Exp);
3292 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p-1").bitwiseIsEqual(Frac));
3294 Frac = frexp(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp+51"), Exp, RM);
3295 EXPECT_EQ(52, Exp);
3296 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-1").bitwiseIsEqual(Frac));
3299 TEST(APFloatTest, mod) {
3301 APFloat f1(APFloat::IEEEdouble(), "1.5");
3302 APFloat f2(APFloat::IEEEdouble(), "1.0");
3303 APFloat expected(APFloat::IEEEdouble(), "0.5");
3304 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3305 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3308 APFloat f1(APFloat::IEEEdouble(), "0.5");
3309 APFloat f2(APFloat::IEEEdouble(), "1.0");
3310 APFloat expected(APFloat::IEEEdouble(), "0.5");
3311 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3312 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3315 APFloat f1(APFloat::IEEEdouble(), "0x1.3333333333333p-2"); // 0.3
3316 APFloat f2(APFloat::IEEEdouble(), "0x1.47ae147ae147bp-7"); // 0.01
3317 APFloat expected(APFloat::IEEEdouble(),
3318 "0x1.47ae147ae1471p-7"); // 0.009999999999999983
3319 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3320 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3323 APFloat f1(APFloat::IEEEdouble(), "0x1p64"); // 1.8446744073709552e19
3324 APFloat f2(APFloat::IEEEdouble(), "1.5");
3325 APFloat expected(APFloat::IEEEdouble(), "1.0");
3326 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3327 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3330 APFloat f1(APFloat::IEEEdouble(), "0x1p1000");
3331 APFloat f2(APFloat::IEEEdouble(), "0x1p-1000");
3332 APFloat expected(APFloat::IEEEdouble(), "0.0");
3333 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3334 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3337 APFloat f1(APFloat::IEEEdouble(), "0.0");
3338 APFloat f2(APFloat::IEEEdouble(), "1.0");
3339 APFloat expected(APFloat::IEEEdouble(), "0.0");
3340 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3341 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3344 APFloat f1(APFloat::IEEEdouble(), "1.0");
3345 APFloat f2(APFloat::IEEEdouble(), "0.0");
3346 EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp);
3347 EXPECT_TRUE(f1.isNaN());
3350 APFloat f1(APFloat::IEEEdouble(), "0.0");
3351 APFloat f2(APFloat::IEEEdouble(), "0.0");
3352 EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp);
3353 EXPECT_TRUE(f1.isNaN());
3356 APFloat f1 = APFloat::getInf(APFloat::IEEEdouble(), false);
3357 APFloat f2(APFloat::IEEEdouble(), "1.0");
3358 EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp);
3359 EXPECT_TRUE(f1.isNaN());
3362 APFloat f1(APFloat::IEEEdouble(), "-4.0");
3363 APFloat f2(APFloat::IEEEdouble(), "-2.0");
3364 APFloat expected(APFloat::IEEEdouble(), "-0.0");
3365 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3366 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3369 APFloat f1(APFloat::IEEEdouble(), "-4.0");
3370 APFloat f2(APFloat::IEEEdouble(), "2.0");
3371 APFloat expected(APFloat::IEEEdouble(), "-0.0");
3372 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3373 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3377 TEST(APFloatTest, PPCDoubleDoubleAddSpecial) {
3378 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t,
3379 APFloat::fltCategory, APFloat::roundingMode>;
3380 DataType Data[] = {
3381 // (1 + 0) + (-1 + 0) = fcZero
3382 std::make_tuple(0x3ff0000000000000ull, 0, 0xbff0000000000000ull, 0,
3383 APFloat::fcZero, APFloat::rmNearestTiesToEven),
3384 // LDBL_MAX + (1.1 >> (1023 - 106) + 0)) = fcInfinity
3385 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3386 0x7948000000000000ull, 0ull, APFloat::fcInfinity,
3387 APFloat::rmNearestTiesToEven),
3388 // TODO: change the 4th 0x75effffffffffffe to 0x75efffffffffffff when
3389 // semPPCDoubleDoubleLegacy is gone.
3390 // LDBL_MAX + (1.011111... >> (1023 - 106) + (1.1111111...0 >> (1023 -
3391 // 160))) = fcNormal
3392 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3393 0x7947ffffffffffffull, 0x75effffffffffffeull,
3394 APFloat::fcNormal, APFloat::rmNearestTiesToEven),
3395 // LDBL_MAX + (1.1 >> (1023 - 106) + 0)) = fcInfinity
3396 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3397 0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3398 APFloat::fcInfinity, APFloat::rmNearestTiesToEven),
3399 // NaN + (1 + 0) = fcNaN
3400 std::make_tuple(0x7ff8000000000000ull, 0, 0x3ff0000000000000ull, 0,
3401 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
3404 for (auto Tp : Data) {
3405 uint64_t Op1[2], Op2[2];
3406 APFloat::fltCategory Expected;
3407 APFloat::roundingMode RM;
3408 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected, RM) = Tp;
3411 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3412 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3413 A1.add(A2, RM);
3415 EXPECT_EQ(Expected, A1.getCategory())
3416 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op1[0], Op1[1],
3417 Op2[0], Op2[1])
3418 .str();
3421 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3422 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3423 A2.add(A1, RM);
3425 EXPECT_EQ(Expected, A2.getCategory())
3426 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op2[0], Op2[1],
3427 Op1[0], Op1[1])
3428 .str();
3433 TEST(APFloatTest, PPCDoubleDoubleAdd) {
3434 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
3435 uint64_t, APFloat::roundingMode>;
3436 DataType Data[] = {
3437 // (1 + 0) + (1e-105 + 0) = (1 + 1e-105)
3438 std::make_tuple(0x3ff0000000000000ull, 0, 0x3960000000000000ull, 0,
3439 0x3ff0000000000000ull, 0x3960000000000000ull,
3440 APFloat::rmNearestTiesToEven),
3441 // (1 + 0) + (1e-106 + 0) = (1 + 1e-106)
3442 std::make_tuple(0x3ff0000000000000ull, 0, 0x3950000000000000ull, 0,
3443 0x3ff0000000000000ull, 0x3950000000000000ull,
3444 APFloat::rmNearestTiesToEven),
3445 // (1 + 1e-106) + (1e-106 + 0) = (1 + 1e-105)
3446 std::make_tuple(0x3ff0000000000000ull, 0x3950000000000000ull,
3447 0x3950000000000000ull, 0, 0x3ff0000000000000ull,
3448 0x3960000000000000ull, APFloat::rmNearestTiesToEven),
3449 // (1 + 0) + (epsilon + 0) = (1 + epsilon)
3450 std::make_tuple(0x3ff0000000000000ull, 0, 0x0000000000000001ull, 0,
3451 0x3ff0000000000000ull, 0x0000000000000001ull,
3452 APFloat::rmNearestTiesToEven),
3453 // TODO: change 0xf950000000000000 to 0xf940000000000000, when
3454 // semPPCDoubleDoubleLegacy is gone.
3455 // (DBL_MAX - 1 << (1023 - 105)) + (1 << (1023 - 53) + 0) = DBL_MAX +
3456 // 1.11111... << (1023 - 52)
3457 std::make_tuple(0x7fefffffffffffffull, 0xf950000000000000ull,
3458 0x7c90000000000000ull, 0, 0x7fefffffffffffffull,
3459 0x7c8ffffffffffffeull, APFloat::rmNearestTiesToEven),
3460 // TODO: change 0xf950000000000000 to 0xf940000000000000, when
3461 // semPPCDoubleDoubleLegacy is gone.
3462 // (1 << (1023 - 53) + 0) + (DBL_MAX - 1 << (1023 - 105)) = DBL_MAX +
3463 // 1.11111... << (1023 - 52)
3464 std::make_tuple(0x7c90000000000000ull, 0, 0x7fefffffffffffffull,
3465 0xf950000000000000ull, 0x7fefffffffffffffull,
3466 0x7c8ffffffffffffeull, APFloat::rmNearestTiesToEven),
3469 for (auto Tp : Data) {
3470 uint64_t Op1[2], Op2[2], Expected[2];
3471 APFloat::roundingMode RM;
3472 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
3475 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3476 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3477 A1.add(A2, RM);
3479 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3480 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op1[0], Op1[1],
3481 Op2[0], Op2[1])
3482 .str();
3483 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3484 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op1[0], Op1[1],
3485 Op2[0], Op2[1])
3486 .str();
3489 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3490 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3491 A2.add(A1, RM);
3493 EXPECT_EQ(Expected[0], A2.bitcastToAPInt().getRawData()[0])
3494 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op2[0], Op2[1],
3495 Op1[0], Op1[1])
3496 .str();
3497 EXPECT_EQ(Expected[1], A2.bitcastToAPInt().getRawData()[1])
3498 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op2[0], Op2[1],
3499 Op1[0], Op1[1])
3500 .str();
3505 TEST(APFloatTest, PPCDoubleDoubleSubtract) {
3506 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
3507 uint64_t, APFloat::roundingMode>;
3508 DataType Data[] = {
3509 // (1 + 0) - (-1e-105 + 0) = (1 + 1e-105)
3510 std::make_tuple(0x3ff0000000000000ull, 0, 0xb960000000000000ull, 0,
3511 0x3ff0000000000000ull, 0x3960000000000000ull,
3512 APFloat::rmNearestTiesToEven),
3513 // (1 + 0) - (-1e-106 + 0) = (1 + 1e-106)
3514 std::make_tuple(0x3ff0000000000000ull, 0, 0xb950000000000000ull, 0,
3515 0x3ff0000000000000ull, 0x3950000000000000ull,
3516 APFloat::rmNearestTiesToEven),
3519 for (auto Tp : Data) {
3520 uint64_t Op1[2], Op2[2], Expected[2];
3521 APFloat::roundingMode RM;
3522 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
3524 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3525 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3526 A1.subtract(A2, RM);
3528 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3529 << formatv("({0:x} + {1:x}) - ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
3530 Op2[1])
3531 .str();
3532 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3533 << formatv("({0:x} + {1:x}) - ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
3534 Op2[1])
3535 .str();
3539 TEST(APFloatTest, PPCDoubleDoubleMultiplySpecial) {
3540 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t,
3541 APFloat::fltCategory, APFloat::roundingMode>;
3542 DataType Data[] = {
3543 // fcNaN * fcNaN = fcNaN
3544 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull, 0,
3545 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
3546 // fcNaN * fcZero = fcNaN
3547 std::make_tuple(0x7ff8000000000000ull, 0, 0, 0, APFloat::fcNaN,
3548 APFloat::rmNearestTiesToEven),
3549 // fcNaN * fcInfinity = fcNaN
3550 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff0000000000000ull, 0,
3551 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
3552 // fcNaN * fcNormal = fcNaN
3553 std::make_tuple(0x7ff8000000000000ull, 0, 0x3ff0000000000000ull, 0,
3554 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
3555 // fcInfinity * fcInfinity = fcInfinity
3556 std::make_tuple(0x7ff0000000000000ull, 0, 0x7ff0000000000000ull, 0,
3557 APFloat::fcInfinity, APFloat::rmNearestTiesToEven),
3558 // fcInfinity * fcZero = fcNaN
3559 std::make_tuple(0x7ff0000000000000ull, 0, 0, 0, APFloat::fcNaN,
3560 APFloat::rmNearestTiesToEven),
3561 // fcInfinity * fcNormal = fcInfinity
3562 std::make_tuple(0x7ff0000000000000ull, 0, 0x3ff0000000000000ull, 0,
3563 APFloat::fcInfinity, APFloat::rmNearestTiesToEven),
3564 // fcZero * fcZero = fcZero
3565 std::make_tuple(0, 0, 0, 0, APFloat::fcZero,
3566 APFloat::rmNearestTiesToEven),
3567 // fcZero * fcNormal = fcZero
3568 std::make_tuple(0, 0, 0x3ff0000000000000ull, 0, APFloat::fcZero,
3569 APFloat::rmNearestTiesToEven),
3572 for (auto Tp : Data) {
3573 uint64_t Op1[2], Op2[2];
3574 APFloat::fltCategory Expected;
3575 APFloat::roundingMode RM;
3576 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected, RM) = Tp;
3579 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3580 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3581 A1.multiply(A2, RM);
3583 EXPECT_EQ(Expected, A1.getCategory())
3584 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op1[0], Op1[1],
3585 Op2[0], Op2[1])
3586 .str();
3589 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3590 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3591 A2.multiply(A1, RM);
3593 EXPECT_EQ(Expected, A2.getCategory())
3594 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op2[0], Op2[1],
3595 Op1[0], Op1[1])
3596 .str();
3601 TEST(APFloatTest, PPCDoubleDoubleMultiply) {
3602 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
3603 uint64_t, APFloat::roundingMode>;
3604 DataType Data[] = {
3605 // 1/3 * 3 = 1.0
3606 std::make_tuple(0x3fd5555555555555ull, 0x3c75555555555556ull,
3607 0x4008000000000000ull, 0, 0x3ff0000000000000ull, 0,
3608 APFloat::rmNearestTiesToEven),
3609 // (1 + epsilon) * (1 + 0) = fcZero
3610 std::make_tuple(0x3ff0000000000000ull, 0x0000000000000001ull,
3611 0x3ff0000000000000ull, 0, 0x3ff0000000000000ull,
3612 0x0000000000000001ull, APFloat::rmNearestTiesToEven),
3613 // (1 + epsilon) * (1 + epsilon) = 1 + 2 * epsilon
3614 std::make_tuple(0x3ff0000000000000ull, 0x0000000000000001ull,
3615 0x3ff0000000000000ull, 0x0000000000000001ull,
3616 0x3ff0000000000000ull, 0x0000000000000002ull,
3617 APFloat::rmNearestTiesToEven),
3618 // -(1 + epsilon) * (1 + epsilon) = -1
3619 std::make_tuple(0xbff0000000000000ull, 0x0000000000000001ull,
3620 0x3ff0000000000000ull, 0x0000000000000001ull,
3621 0xbff0000000000000ull, 0, APFloat::rmNearestTiesToEven),
3622 // (0.5 + 0) * (1 + 2 * epsilon) = 0.5 + epsilon
3623 std::make_tuple(0x3fe0000000000000ull, 0, 0x3ff0000000000000ull,
3624 0x0000000000000002ull, 0x3fe0000000000000ull,
3625 0x0000000000000001ull, APFloat::rmNearestTiesToEven),
3626 // (0.5 + 0) * (1 + epsilon) = 0.5
3627 std::make_tuple(0x3fe0000000000000ull, 0, 0x3ff0000000000000ull,
3628 0x0000000000000001ull, 0x3fe0000000000000ull, 0,
3629 APFloat::rmNearestTiesToEven),
3630 // __LDBL_MAX__ * (1 + 1 << 106) = inf
3631 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3632 0x3ff0000000000000ull, 0x3950000000000000ull,
3633 0x7ff0000000000000ull, 0, APFloat::rmNearestTiesToEven),
3634 // __LDBL_MAX__ * (1 + 1 << 107) > __LDBL_MAX__, but not inf, yes =_=|||
3635 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3636 0x3ff0000000000000ull, 0x3940000000000000ull,
3637 0x7fefffffffffffffull, 0x7c8fffffffffffffull,
3638 APFloat::rmNearestTiesToEven),
3639 // __LDBL_MAX__ * (1 + 1 << 108) = __LDBL_MAX__
3640 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3641 0x3ff0000000000000ull, 0x3930000000000000ull,
3642 0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3643 APFloat::rmNearestTiesToEven),
3646 for (auto Tp : Data) {
3647 uint64_t Op1[2], Op2[2], Expected[2];
3648 APFloat::roundingMode RM;
3649 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
3652 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3653 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3654 A1.multiply(A2, RM);
3656 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3657 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op1[0], Op1[1],
3658 Op2[0], Op2[1])
3659 .str();
3660 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3661 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op1[0], Op1[1],
3662 Op2[0], Op2[1])
3663 .str();
3666 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3667 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3668 A2.multiply(A1, RM);
3670 EXPECT_EQ(Expected[0], A2.bitcastToAPInt().getRawData()[0])
3671 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op2[0], Op2[1],
3672 Op1[0], Op1[1])
3673 .str();
3674 EXPECT_EQ(Expected[1], A2.bitcastToAPInt().getRawData()[1])
3675 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op2[0], Op2[1],
3676 Op1[0], Op1[1])
3677 .str();
3682 TEST(APFloatTest, PPCDoubleDoubleDivide) {
3683 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
3684 uint64_t, APFloat::roundingMode>;
3685 // TODO: Only a sanity check for now. Add more edge cases when the
3686 // double-double algorithm is implemented.
3687 DataType Data[] = {
3688 // 1 / 3 = 1/3
3689 std::make_tuple(0x3ff0000000000000ull, 0, 0x4008000000000000ull, 0,
3690 0x3fd5555555555555ull, 0x3c75555555555556ull,
3691 APFloat::rmNearestTiesToEven),
3694 for (auto Tp : Data) {
3695 uint64_t Op1[2], Op2[2], Expected[2];
3696 APFloat::roundingMode RM;
3697 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
3699 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3700 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3701 A1.divide(A2, RM);
3703 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3704 << formatv("({0:x} + {1:x}) / ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
3705 Op2[1])
3706 .str();
3707 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3708 << formatv("({0:x} + {1:x}) / ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
3709 Op2[1])
3710 .str();
3714 TEST(APFloatTest, PPCDoubleDoubleRemainder) {
3715 using DataType =
3716 std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>;
3717 DataType Data[] = {
3718 // remainder(3.0 + 3.0 << 53, 1.25 + 1.25 << 53) = (0.5 + 0.5 << 53)
3719 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
3720 0x3ff4000000000000ull, 0x3ca4000000000000ull,
3721 0x3fe0000000000000ull, 0x3c90000000000000ull),
3722 // remainder(3.0 + 3.0 << 53, 1.75 + 1.75 << 53) = (-0.5 - 0.5 << 53)
3723 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
3724 0x3ffc000000000000ull, 0x3cac000000000000ull,
3725 0xbfe0000000000000ull, 0xbc90000000000000ull),
3728 for (auto Tp : Data) {
3729 uint64_t Op1[2], Op2[2], Expected[2];
3730 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1]) = Tp;
3732 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3733 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3734 A1.remainder(A2);
3736 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3737 << formatv("remainder({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
3738 Op2[0], Op2[1])
3739 .str();
3740 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3741 << formatv("remainder(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0],
3742 Op1[1], Op2[0], Op2[1])
3743 .str();
3747 TEST(APFloatTest, PPCDoubleDoubleMod) {
3748 using DataType =
3749 std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>;
3750 DataType Data[] = {
3751 // mod(3.0 + 3.0 << 53, 1.25 + 1.25 << 53) = (0.5 + 0.5 << 53)
3752 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
3753 0x3ff4000000000000ull, 0x3ca4000000000000ull,
3754 0x3fe0000000000000ull, 0x3c90000000000000ull),
3755 // mod(3.0 + 3.0 << 53, 1.75 + 1.75 << 53) = (1.25 + 1.25 << 53)
3756 // 0xbc98000000000000 doesn't seem right, but it's what we currently have.
3757 // TODO: investigate
3758 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
3759 0x3ffc000000000000ull, 0x3cac000000000000ull,
3760 0x3ff4000000000001ull, 0xbc98000000000000ull),
3763 for (auto Tp : Data) {
3764 uint64_t Op1[2], Op2[2], Expected[2];
3765 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1]) = Tp;
3767 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3768 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3769 A1.mod(A2);
3771 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3772 << formatv("fmod(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
3773 Op2[0], Op2[1])
3774 .str();
3775 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3776 << formatv("fmod(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
3777 Op2[0], Op2[1])
3778 .str();
3782 TEST(APFloatTest, PPCDoubleDoubleFMA) {
3783 // Sanity check for now.
3784 APFloat A(APFloat::PPCDoubleDouble(), "2");
3785 A.fusedMultiplyAdd(APFloat(APFloat::PPCDoubleDouble(), "3"),
3786 APFloat(APFloat::PPCDoubleDouble(), "4"),
3787 APFloat::rmNearestTiesToEven);
3788 EXPECT_EQ(APFloat::cmpEqual,
3789 APFloat(APFloat::PPCDoubleDouble(), "10").compare(A));
3792 TEST(APFloatTest, PPCDoubleDoubleRoundToIntegral) {
3794 APFloat A(APFloat::PPCDoubleDouble(), "1.5");
3795 A.roundToIntegral(APFloat::rmNearestTiesToEven);
3796 EXPECT_EQ(APFloat::cmpEqual,
3797 APFloat(APFloat::PPCDoubleDouble(), "2").compare(A));
3800 APFloat A(APFloat::PPCDoubleDouble(), "2.5");
3801 A.roundToIntegral(APFloat::rmNearestTiesToEven);
3802 EXPECT_EQ(APFloat::cmpEqual,
3803 APFloat(APFloat::PPCDoubleDouble(), "2").compare(A));
3807 TEST(APFloatTest, PPCDoubleDoubleCompare) {
3808 using DataType =
3809 std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, APFloat::cmpResult>;
3811 DataType Data[] = {
3812 // (1 + 0) = (1 + 0)
3813 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000000ull, 0,
3814 APFloat::cmpEqual),
3815 // (1 + 0) < (1.00...1 + 0)
3816 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000001ull, 0,
3817 APFloat::cmpLessThan),
3818 // (1.00...1 + 0) > (1 + 0)
3819 std::make_tuple(0x3ff0000000000001ull, 0, 0x3ff0000000000000ull, 0,
3820 APFloat::cmpGreaterThan),
3821 // (1 + 0) < (1 + epsilon)
3822 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000001ull,
3823 0x0000000000000001ull, APFloat::cmpLessThan),
3824 // NaN != NaN
3825 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull, 0,
3826 APFloat::cmpUnordered),
3827 // (1 + 0) != NaN
3828 std::make_tuple(0x3ff0000000000000ull, 0, 0x7ff8000000000000ull, 0,
3829 APFloat::cmpUnordered),
3830 // Inf = Inf
3831 std::make_tuple(0x7ff0000000000000ull, 0, 0x7ff0000000000000ull, 0,
3832 APFloat::cmpEqual),
3835 for (auto Tp : Data) {
3836 uint64_t Op1[2], Op2[2];
3837 APFloat::cmpResult Expected;
3838 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected) = Tp;
3840 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3841 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3842 EXPECT_EQ(Expected, A1.compare(A2))
3843 << formatv("compare(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
3844 Op2[0], Op2[1])
3845 .str();
3849 TEST(APFloatTest, PPCDoubleDoubleBitwiseIsEqual) {
3850 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, bool>;
3852 DataType Data[] = {
3853 // (1 + 0) = (1 + 0)
3854 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000000ull, 0, true),
3855 // (1 + 0) != (1.00...1 + 0)
3856 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000001ull, 0,
3857 false),
3858 // NaN = NaN
3859 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull, 0, true),
3860 // NaN != NaN with a different bit pattern
3861 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull,
3862 0x3ff0000000000000ull, false),
3863 // Inf = Inf
3864 std::make_tuple(0x7ff0000000000000ull, 0, 0x7ff0000000000000ull, 0, true),
3867 for (auto Tp : Data) {
3868 uint64_t Op1[2], Op2[2];
3869 bool Expected;
3870 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected) = Tp;
3872 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3873 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3874 EXPECT_EQ(Expected, A1.bitwiseIsEqual(A2))
3875 << formatv("({0:x} + {1:x}) = ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
3876 Op2[1])
3877 .str();
3881 TEST(APFloatTest, PPCDoubleDoubleHashValue) {
3882 uint64_t Data1[] = {0x3ff0000000000001ull, 0x0000000000000001ull};
3883 uint64_t Data2[] = {0x3ff0000000000001ull, 0};
3884 // The hash values are *hopefully* different.
3885 EXPECT_NE(
3886 hash_value(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Data1))),
3887 hash_value(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Data2))));
3890 TEST(APFloatTest, PPCDoubleDoubleChangeSign) {
3891 uint64_t Data[] = {
3892 0x400f000000000000ull, 0xbcb0000000000000ull,
3894 APFloat Float(APFloat::PPCDoubleDouble(), APInt(128, 2, Data));
3896 APFloat Actual =
3897 APFloat::copySign(Float, APFloat(APFloat::IEEEdouble(), "1"));
3898 EXPECT_EQ(0x400f000000000000ull, Actual.bitcastToAPInt().getRawData()[0]);
3899 EXPECT_EQ(0xbcb0000000000000ull, Actual.bitcastToAPInt().getRawData()[1]);
3902 APFloat Actual =
3903 APFloat::copySign(Float, APFloat(APFloat::IEEEdouble(), "-1"));
3904 EXPECT_EQ(0xc00f000000000000ull, Actual.bitcastToAPInt().getRawData()[0]);
3905 EXPECT_EQ(0x3cb0000000000000ull, Actual.bitcastToAPInt().getRawData()[1]);
3909 TEST(APFloatTest, PPCDoubleDoubleFactories) {
3911 uint64_t Data[] = {
3912 0, 0,
3914 EXPECT_EQ(APInt(128, 2, Data),
3915 APFloat::getZero(APFloat::PPCDoubleDouble()).bitcastToAPInt());
3918 uint64_t Data[] = {
3919 0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3921 EXPECT_EQ(APInt(128, 2, Data),
3922 APFloat::getLargest(APFloat::PPCDoubleDouble()).bitcastToAPInt());
3925 uint64_t Data[] = {
3926 0x0000000000000001ull, 0,
3928 EXPECT_EQ(
3929 APInt(128, 2, Data),
3930 APFloat::getSmallest(APFloat::PPCDoubleDouble()).bitcastToAPInt());
3933 uint64_t Data[] = {0x0360000000000000ull, 0};
3934 EXPECT_EQ(APInt(128, 2, Data),
3935 APFloat::getSmallestNormalized(APFloat::PPCDoubleDouble())
3936 .bitcastToAPInt());
3939 uint64_t Data[] = {
3940 0x8000000000000000ull, 0x0000000000000000ull,
3942 EXPECT_EQ(
3943 APInt(128, 2, Data),
3944 APFloat::getZero(APFloat::PPCDoubleDouble(), true).bitcastToAPInt());
3947 uint64_t Data[] = {
3948 0xffefffffffffffffull, 0xfc8ffffffffffffeull,
3950 EXPECT_EQ(
3951 APInt(128, 2, Data),
3952 APFloat::getLargest(APFloat::PPCDoubleDouble(), true).bitcastToAPInt());
3955 uint64_t Data[] = {
3956 0x8000000000000001ull, 0x0000000000000000ull,
3958 EXPECT_EQ(APInt(128, 2, Data),
3959 APFloat::getSmallest(APFloat::PPCDoubleDouble(), true)
3960 .bitcastToAPInt());
3963 uint64_t Data[] = {
3964 0x8360000000000000ull, 0x0000000000000000ull,
3966 EXPECT_EQ(APInt(128, 2, Data),
3967 APFloat::getSmallestNormalized(APFloat::PPCDoubleDouble(), true)
3968 .bitcastToAPInt());
3970 EXPECT_TRUE(APFloat::getSmallest(APFloat::PPCDoubleDouble()).isSmallest());
3971 EXPECT_TRUE(APFloat::getLargest(APFloat::PPCDoubleDouble()).isLargest());
3974 TEST(APFloatTest, PPCDoubleDoubleIsDenormal) {
3975 EXPECT_TRUE(APFloat::getSmallest(APFloat::PPCDoubleDouble()).isDenormal());
3976 EXPECT_FALSE(APFloat::getLargest(APFloat::PPCDoubleDouble()).isDenormal());
3977 EXPECT_FALSE(
3978 APFloat::getSmallestNormalized(APFloat::PPCDoubleDouble()).isDenormal());
3980 // (4 + 3) is not normalized
3981 uint64_t Data[] = {
3982 0x4010000000000000ull, 0x4008000000000000ull,
3984 EXPECT_TRUE(
3985 APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Data)).isDenormal());
3989 TEST(APFloatTest, PPCDoubleDoubleScalbn) {
3990 // 3.0 + 3.0 << 53
3991 uint64_t Input[] = {
3992 0x4008000000000000ull, 0x3cb8000000000000ull,
3994 APFloat Result =
3995 scalbn(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Input)), 1,
3996 APFloat::rmNearestTiesToEven);
3997 // 6.0 + 6.0 << 53
3998 EXPECT_EQ(0x4018000000000000ull, Result.bitcastToAPInt().getRawData()[0]);
3999 EXPECT_EQ(0x3cc8000000000000ull, Result.bitcastToAPInt().getRawData()[1]);
4002 TEST(APFloatTest, PPCDoubleDoubleFrexp) {
4003 // 3.0 + 3.0 << 53
4004 uint64_t Input[] = {
4005 0x4008000000000000ull, 0x3cb8000000000000ull,
4007 int Exp;
4008 // 0.75 + 0.75 << 53
4009 APFloat Result =
4010 frexp(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Input)), Exp,
4011 APFloat::rmNearestTiesToEven);
4012 EXPECT_EQ(2, Exp);
4013 EXPECT_EQ(0x3fe8000000000000ull, Result.bitcastToAPInt().getRawData()[0]);
4014 EXPECT_EQ(0x3c98000000000000ull, Result.bitcastToAPInt().getRawData()[1]);