[Alignment] Use llvm::Align in MachineFunction and TargetLowering - fixes mir parsing
[llvm-core.git] / unittests / ADT / APFloatTest.cpp
blobc76347ed0564b92ebc56eb9108d0f5c3e455bef9
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 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1e").convertToDouble());
872 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "+1e").convertToDouble());
873 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-1e").convertToDouble());
875 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.e").convertToDouble());
876 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "+1.e").convertToDouble());
877 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-1.e").convertToDouble());
879 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e").convertToDouble());
880 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), "+.1e").convertToDouble());
881 EXPECT_EQ(-0.1, APFloat(APFloat::IEEEdouble(), "-.1e").convertToDouble());
883 EXPECT_EQ(1.1, APFloat(APFloat::IEEEdouble(), "1.1e").convertToDouble());
884 EXPECT_EQ(1.1, APFloat(APFloat::IEEEdouble(), "+1.1e").convertToDouble());
885 EXPECT_EQ(-1.1, APFloat(APFloat::IEEEdouble(), "-1.1e").convertToDouble());
887 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1e+").convertToDouble());
888 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1e-").convertToDouble());
890 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e").convertToDouble());
891 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e+").convertToDouble());
892 EXPECT_EQ(0.1, APFloat(APFloat::IEEEdouble(), ".1e-").convertToDouble());
894 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0e").convertToDouble());
895 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0e+").convertToDouble());
896 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "1.0e-").convertToDouble());
898 // These are "carefully selected" to overflow the fast log-base
899 // calculations in APFloat.cpp
900 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "99e99999").isInfinity());
901 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-99e99999").isInfinity());
902 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "1e-99999").isPosZero());
903 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-1e-99999").isNegZero());
905 EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
908 TEST(APFloatTest, fromToStringSpecials) {
909 auto expects = [] (const char *first, const char *second) {
910 std::string roundtrip = convertToString(convertToDoubleFromString(second), 0, 3);
911 EXPECT_STREQ(first, roundtrip.c_str());
913 expects("+Inf", "+Inf");
914 expects("+Inf", "INFINITY");
915 expects("+Inf", "inf");
916 expects("-Inf", "-Inf");
917 expects("-Inf", "-INFINITY");
918 expects("-Inf", "-inf");
919 expects("NaN", "NaN");
920 expects("NaN", "nan");
921 expects("NaN", "-NaN");
922 expects("NaN", "-nan");
925 TEST(APFloatTest, fromHexadecimalString) {
926 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(), "0x1p0").convertToDouble());
927 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p0").convertToDouble());
928 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-0x1p0").convertToDouble());
930 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(), "0x1p+0").convertToDouble());
931 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p+0").convertToDouble());
932 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-0x1p+0").convertToDouble());
934 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(), "0x1p-0").convertToDouble());
935 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p-0").convertToDouble());
936 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble(), "-0x1p-0").convertToDouble());
939 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble(), "0x1p1").convertToDouble());
940 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble(), "+0x1p1").convertToDouble());
941 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble(), "-0x1p1").convertToDouble());
943 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble(), "0x1p+1").convertToDouble());
944 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble(), "+0x1p+1").convertToDouble());
945 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble(), "-0x1p+1").convertToDouble());
947 EXPECT_EQ( 0.5, APFloat(APFloat::IEEEdouble(), "0x1p-1").convertToDouble());
948 EXPECT_EQ(+0.5, APFloat(APFloat::IEEEdouble(), "+0x1p-1").convertToDouble());
949 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble(), "-0x1p-1").convertToDouble());
952 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble(), "0x1.8p1").convertToDouble());
953 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble(), "+0x1.8p1").convertToDouble());
954 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble(), "-0x1.8p1").convertToDouble());
956 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble(), "0x1.8p+1").convertToDouble());
957 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble(), "+0x1.8p+1").convertToDouble());
958 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble(), "-0x1.8p+1").convertToDouble());
960 EXPECT_EQ( 0.75, APFloat(APFloat::IEEEdouble(), "0x1.8p-1").convertToDouble());
961 EXPECT_EQ(+0.75, APFloat(APFloat::IEEEdouble(), "+0x1.8p-1").convertToDouble());
962 EXPECT_EQ(-0.75, APFloat(APFloat::IEEEdouble(), "-0x1.8p-1").convertToDouble());
965 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000.000p1").convertToDouble());
966 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000.000p1").convertToDouble());
967 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000.000p1").convertToDouble());
969 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000.000p+1").convertToDouble());
970 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000.000p+1").convertToDouble());
971 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000.000p+1").convertToDouble());
973 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble(), "0x1000.000p-1").convertToDouble());
974 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble(), "+0x1000.000p-1").convertToDouble());
975 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble(), "-0x1000.000p-1").convertToDouble());
978 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000p1").convertToDouble());
979 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000p1").convertToDouble());
980 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000p1").convertToDouble());
982 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble(), "0x1000p+1").convertToDouble());
983 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble(), "+0x1000p+1").convertToDouble());
984 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble(), "-0x1000p+1").convertToDouble());
986 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble(), "0x1000p-1").convertToDouble());
987 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble(), "+0x1000p-1").convertToDouble());
988 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble(), "-0x1000p-1").convertToDouble());
991 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble(), "0x10p10").convertToDouble());
992 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble(), "+0x10p10").convertToDouble());
993 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble(), "-0x10p10").convertToDouble());
995 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble(), "0x10p+10").convertToDouble());
996 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble(), "+0x10p+10").convertToDouble());
997 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble(), "-0x10p+10").convertToDouble());
999 EXPECT_EQ( 0.015625, APFloat(APFloat::IEEEdouble(), "0x10p-10").convertToDouble());
1000 EXPECT_EQ(+0.015625, APFloat(APFloat::IEEEdouble(), "+0x10p-10").convertToDouble());
1001 EXPECT_EQ(-0.015625, APFloat(APFloat::IEEEdouble(), "-0x10p-10").convertToDouble());
1003 EXPECT_EQ(1.0625, APFloat(APFloat::IEEEdouble(), "0x1.1p0").convertToDouble());
1004 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble(), "0x1p0").convertToDouble());
1006 EXPECT_EQ(convertToDoubleFromString("0x1p-150"),
1007 convertToDoubleFromString("+0x800000000000000001.p-221"));
1008 EXPECT_EQ(2251799813685248.5,
1009 convertToDoubleFromString("0x80000000000004000000.010p-28"));
1012 TEST(APFloatTest, toString) {
1013 ASSERT_EQ("10", convertToString(10.0, 6, 3));
1014 ASSERT_EQ("1.0E+1", convertToString(10.0, 6, 0));
1015 ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2));
1016 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 4, 2));
1017 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 5, 1));
1018 ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2));
1019 ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2));
1020 ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 5, 1));
1021 ASSERT_EQ("0.78539816339744828", convertToString(0.78539816339744830961, 0, 3));
1022 ASSERT_EQ("4.9406564584124654E-324", convertToString(4.9406564584124654e-324, 0, 3));
1023 ASSERT_EQ("873.18340000000001", convertToString(873.1834, 0, 1));
1024 ASSERT_EQ("8.7318340000000001E+2", convertToString(873.1834, 0, 0));
1025 ASSERT_EQ("1.7976931348623157E+308", convertToString(1.7976931348623157E+308, 0, 0));
1026 ASSERT_EQ("10", convertToString(10.0, 6, 3, false));
1027 ASSERT_EQ("1.000000e+01", convertToString(10.0, 6, 0, false));
1028 ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2, false));
1029 ASSERT_EQ("1.0100e+04", convertToString(1.01E+4, 4, 2, false));
1030 ASSERT_EQ("1.01000e+04", convertToString(1.01E+4, 5, 1, false));
1031 ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2, false));
1032 ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2, false));
1033 ASSERT_EQ("1.01000e-02", convertToString(1.01E-2, 5, 1, false));
1034 ASSERT_EQ("0.78539816339744828",
1035 convertToString(0.78539816339744830961, 0, 3, false));
1036 ASSERT_EQ("4.94065645841246540e-324",
1037 convertToString(4.9406564584124654e-324, 0, 3, false));
1038 ASSERT_EQ("873.18340000000001", convertToString(873.1834, 0, 1, false));
1039 ASSERT_EQ("8.73183400000000010e+02", convertToString(873.1834, 0, 0, false));
1040 ASSERT_EQ("1.79769313486231570e+308",
1041 convertToString(1.7976931348623157E+308, 0, 0, false));
1044 SmallString<64> Str;
1045 APFloat UnnormalZero(APFloat::x87DoubleExtended(), APInt(80, {0, 1}));
1046 UnnormalZero.toString(Str);
1047 ASSERT_EQ("NaN", Str);
1051 TEST(APFloatTest, toInteger) {
1052 bool isExact = false;
1053 APSInt result(5, /*isUnsigned=*/true);
1055 EXPECT_EQ(APFloat::opOK,
1056 APFloat(APFloat::IEEEdouble(), "10")
1057 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1058 EXPECT_TRUE(isExact);
1059 EXPECT_EQ(APSInt(APInt(5, 10), true), result);
1061 EXPECT_EQ(APFloat::opInvalidOp,
1062 APFloat(APFloat::IEEEdouble(), "-10")
1063 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1064 EXPECT_FALSE(isExact);
1065 EXPECT_EQ(APSInt::getMinValue(5, true), result);
1067 EXPECT_EQ(APFloat::opInvalidOp,
1068 APFloat(APFloat::IEEEdouble(), "32")
1069 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1070 EXPECT_FALSE(isExact);
1071 EXPECT_EQ(APSInt::getMaxValue(5, true), result);
1073 EXPECT_EQ(APFloat::opInexact,
1074 APFloat(APFloat::IEEEdouble(), "7.9")
1075 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1076 EXPECT_FALSE(isExact);
1077 EXPECT_EQ(APSInt(APInt(5, 7), true), result);
1079 result.setIsUnsigned(false);
1080 EXPECT_EQ(APFloat::opOK,
1081 APFloat(APFloat::IEEEdouble(), "-10")
1082 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1083 EXPECT_TRUE(isExact);
1084 EXPECT_EQ(APSInt(APInt(5, -10, true), false), result);
1086 EXPECT_EQ(APFloat::opInvalidOp,
1087 APFloat(APFloat::IEEEdouble(), "-17")
1088 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1089 EXPECT_FALSE(isExact);
1090 EXPECT_EQ(APSInt::getMinValue(5, false), result);
1092 EXPECT_EQ(APFloat::opInvalidOp,
1093 APFloat(APFloat::IEEEdouble(), "16")
1094 .convertToInteger(result, APFloat::rmTowardZero, &isExact));
1095 EXPECT_FALSE(isExact);
1096 EXPECT_EQ(APSInt::getMaxValue(5, false), result);
1099 static APInt nanbitsFromAPInt(const fltSemantics &Sem, bool SNaN, bool Negative,
1100 uint64_t payload) {
1101 APInt appayload(64, payload);
1102 if (SNaN)
1103 return APFloat::getSNaN(Sem, Negative, &appayload).bitcastToAPInt();
1104 else
1105 return APFloat::getQNaN(Sem, Negative, &appayload).bitcastToAPInt();
1108 TEST(APFloatTest, makeNaN) {
1109 const struct {
1110 uint64_t expected;
1111 const fltSemantics &semantics;
1112 bool SNaN;
1113 bool Negative;
1114 uint64_t payload;
1115 } tests[] = {
1116 /* expected semantics SNaN Neg payload */
1117 { 0x7fc00000ULL, APFloat::IEEEsingle(), false, false, 0x00000000ULL },
1118 { 0xffc00000ULL, APFloat::IEEEsingle(), false, true, 0x00000000ULL },
1119 { 0x7fc0ae72ULL, APFloat::IEEEsingle(), false, false, 0x0000ae72ULL },
1120 { 0x7fffae72ULL, APFloat::IEEEsingle(), false, false, 0xffffae72ULL },
1121 { 0x7fdaae72ULL, APFloat::IEEEsingle(), false, false, 0x00daae72ULL },
1122 { 0x7fa00000ULL, APFloat::IEEEsingle(), true, false, 0x00000000ULL },
1123 { 0xffa00000ULL, APFloat::IEEEsingle(), true, true, 0x00000000ULL },
1124 { 0x7f80ae72ULL, APFloat::IEEEsingle(), true, false, 0x0000ae72ULL },
1125 { 0x7fbfae72ULL, APFloat::IEEEsingle(), true, false, 0xffffae72ULL },
1126 { 0x7f9aae72ULL, APFloat::IEEEsingle(), true, false, 0x001aae72ULL },
1127 { 0x7ff8000000000000ULL, APFloat::IEEEdouble(), false, false, 0x0000000000000000ULL },
1128 { 0xfff8000000000000ULL, APFloat::IEEEdouble(), false, true, 0x0000000000000000ULL },
1129 { 0x7ff800000000ae72ULL, APFloat::IEEEdouble(), false, false, 0x000000000000ae72ULL },
1130 { 0x7fffffffffffae72ULL, APFloat::IEEEdouble(), false, false, 0xffffffffffffae72ULL },
1131 { 0x7ffdaaaaaaaaae72ULL, APFloat::IEEEdouble(), false, false, 0x000daaaaaaaaae72ULL },
1132 { 0x7ff4000000000000ULL, APFloat::IEEEdouble(), true, false, 0x0000000000000000ULL },
1133 { 0xfff4000000000000ULL, APFloat::IEEEdouble(), true, true, 0x0000000000000000ULL },
1134 { 0x7ff000000000ae72ULL, APFloat::IEEEdouble(), true, false, 0x000000000000ae72ULL },
1135 { 0x7ff7ffffffffae72ULL, APFloat::IEEEdouble(), true, false, 0xffffffffffffae72ULL },
1136 { 0x7ff1aaaaaaaaae72ULL, APFloat::IEEEdouble(), true, false, 0x0001aaaaaaaaae72ULL },
1139 for (const auto &t : tests) {
1140 ASSERT_EQ(t.expected, nanbitsFromAPInt(t.semantics, t.SNaN, t.Negative, t.payload));
1144 #ifdef GTEST_HAS_DEATH_TEST
1145 #ifndef NDEBUG
1146 TEST(APFloatTest, SemanticsDeath) {
1147 EXPECT_DEATH(APFloat(APFloat::IEEEsingle(), 0.0f).convertToDouble(), "Float semantics are not IEEEdouble");
1148 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), 0.0 ).convertToFloat(), "Float semantics are not IEEEsingle");
1151 TEST(APFloatTest, StringDecimalDeath) {
1152 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ""), "Invalid string length");
1153 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+"), "String has no digits");
1154 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-"), "String has no digits");
1156 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("\0", 1)), "Invalid character in significand");
1157 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1\0", 2)), "Invalid character in significand");
1158 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1" "\0" "2", 3)), "Invalid character in significand");
1159 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1" "\0" "2e1", 5)), "Invalid character in significand");
1160 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1e\0", 3)), "Invalid character in exponent");
1161 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1e1\0", 4)), "Invalid character in exponent");
1162 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("1e1" "\0" "2", 5)), "Invalid character in exponent");
1164 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.0f"), "Invalid character in significand");
1166 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".."), "String contains multiple dots");
1167 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "..0"), "String contains multiple dots");
1168 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "1.0.0"), "String contains multiple dots");
1171 TEST(APFloatTest, StringDecimalSignificandDeath) {
1172 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "."), "Significand has no digits");
1173 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+."), "Significand has no digits");
1174 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-."), "Significand has no digits");
1177 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "e"), "Significand has no digits");
1178 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+e"), "Significand has no digits");
1179 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-e"), "Significand has no digits");
1181 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "e1"), "Significand has no digits");
1182 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+e1"), "Significand has no digits");
1183 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-e1"), "Significand has no digits");
1185 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".e1"), "Significand has no digits");
1186 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+.e1"), "Significand has no digits");
1187 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-.e1"), "Significand has no digits");
1190 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), ".e"), "Significand has no digits");
1191 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+.e"), "Significand has no digits");
1192 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-.e"), "Significand has no digits");
1195 TEST(APFloatTest, StringHexadecimalDeath) {
1196 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x"), "Invalid string");
1197 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x"), "Invalid string");
1198 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x"), "Invalid string");
1200 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x0"), "Hex strings require an exponent");
1201 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x0"), "Hex strings require an exponent");
1202 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");
1206 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x0."), "Hex strings require an exponent");
1208 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x.0"), "Hex strings require an exponent");
1209 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x.0"), "Hex strings require an exponent");
1210 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x.0"), "Hex strings require an exponent");
1212 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x0.0"), "Hex strings require an exponent");
1213 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x0.0"), "Hex strings require an exponent");
1214 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x0.0"), "Hex strings require an exponent");
1216 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x\0", 3)), "Invalid character in significand");
1217 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1\0", 4)), "Invalid character in significand");
1218 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1" "\0" "2", 5)), "Invalid character in significand");
1219 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1" "\0" "2p1", 7)), "Invalid character in significand");
1220 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1p\0", 5)), "Invalid character in exponent");
1221 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1p1\0", 6)), "Invalid character in exponent");
1222 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), StringRef("0x1p1" "\0" "2", 7)), "Invalid character in exponent");
1224 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1p0f"), "Invalid character in exponent");
1226 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x..p1"), "String contains multiple dots");
1227 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x..0p1"), "String contains multiple dots");
1228 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1.0.0p1"), "String contains multiple dots");
1231 TEST(APFloatTest, StringHexadecimalSignificandDeath) {
1232 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x."), "Significand has no digits");
1233 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x."), "Significand has no digits");
1234 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x."), "Significand has no digits");
1236 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0xp"), "Significand has no digits");
1237 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0xp"), "Significand has no digits");
1238 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");
1242 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");
1246 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0xp-"), "Significand has no digits");
1249 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x.p"), "Significand has no digits");
1250 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x.p"), "Significand has no digits");
1251 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");
1255 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");
1259 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x.p-"), "Significand has no digits");
1262 TEST(APFloatTest, StringHexadecimalExponentDeath) {
1263 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1p"), "Exponent has no digits");
1264 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1p"), "Exponent has no digits");
1265 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");
1269 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");
1273 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1p-"), "Exponent has no digits");
1276 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1.p"), "Exponent has no digits");
1277 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1.p"), "Exponent has no digits");
1278 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");
1282 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");
1286 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1.p-"), "Exponent has no digits");
1289 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x.1p"), "Exponent has no digits");
1290 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x.1p"), "Exponent has no digits");
1291 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");
1295 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");
1299 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x.1p-"), "Exponent has no digits");
1302 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "0x1.1p"), "Exponent has no digits");
1303 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "+0x1.1p"), "Exponent has no digits");
1304 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");
1308 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");
1312 EXPECT_DEATH(APFloat(APFloat::IEEEdouble(), "-0x1.1p-"), "Exponent has no digits");
1314 #endif
1315 #endif
1317 TEST(APFloatTest, exactInverse) {
1318 APFloat inv(0.0f);
1320 // Trivial operation.
1321 EXPECT_TRUE(APFloat(2.0).getExactInverse(&inv));
1322 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5)));
1323 EXPECT_TRUE(APFloat(2.0f).getExactInverse(&inv));
1324 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5f)));
1325 EXPECT_TRUE(APFloat(APFloat::IEEEquad(), "2.0").getExactInverse(&inv));
1326 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::IEEEquad(), "0.5")));
1327 EXPECT_TRUE(APFloat(APFloat::PPCDoubleDouble(), "2.0").getExactInverse(&inv));
1328 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::PPCDoubleDouble(), "0.5")));
1329 EXPECT_TRUE(APFloat(APFloat::x87DoubleExtended(), "2.0").getExactInverse(&inv));
1330 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::x87DoubleExtended(), "0.5")));
1332 // FLT_MIN
1333 EXPECT_TRUE(APFloat(1.17549435e-38f).getExactInverse(&inv));
1334 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(8.5070592e+37f)));
1336 // Large float, inverse is a denormal.
1337 EXPECT_FALSE(APFloat(1.7014118e38f).getExactInverse(nullptr));
1338 // Zero
1339 EXPECT_FALSE(APFloat(0.0).getExactInverse(nullptr));
1340 // Denormalized float
1341 EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(nullptr));
1344 TEST(APFloatTest, roundToIntegral) {
1345 APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble())), P(0.0);
1347 P = T;
1348 P.roundToIntegral(APFloat::rmTowardZero);
1349 EXPECT_EQ(-0.0, P.convertToDouble());
1350 P = T;
1351 P.roundToIntegral(APFloat::rmTowardNegative);
1352 EXPECT_EQ(-1.0, P.convertToDouble());
1353 P = T;
1354 P.roundToIntegral(APFloat::rmTowardPositive);
1355 EXPECT_EQ(-0.0, P.convertToDouble());
1356 P = T;
1357 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1358 EXPECT_EQ(-0.0, P.convertToDouble());
1360 P = S;
1361 P.roundToIntegral(APFloat::rmTowardZero);
1362 EXPECT_EQ(3.0, P.convertToDouble());
1363 P = S;
1364 P.roundToIntegral(APFloat::rmTowardNegative);
1365 EXPECT_EQ(3.0, P.convertToDouble());
1366 P = S;
1367 P.roundToIntegral(APFloat::rmTowardPositive);
1368 EXPECT_EQ(4.0, P.convertToDouble());
1369 P = S;
1370 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1371 EXPECT_EQ(3.0, P.convertToDouble());
1373 P = R;
1374 P.roundToIntegral(APFloat::rmTowardZero);
1375 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1376 P = R;
1377 P.roundToIntegral(APFloat::rmTowardNegative);
1378 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1379 P = R;
1380 P.roundToIntegral(APFloat::rmTowardPositive);
1381 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1382 P = R;
1383 P.roundToIntegral(APFloat::rmNearestTiesToEven);
1384 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1386 P = APFloat::getZero(APFloat::IEEEdouble());
1387 P.roundToIntegral(APFloat::rmTowardZero);
1388 EXPECT_EQ(0.0, P.convertToDouble());
1389 P = APFloat::getZero(APFloat::IEEEdouble(), true);
1390 P.roundToIntegral(APFloat::rmTowardZero);
1391 EXPECT_EQ(-0.0, P.convertToDouble());
1392 P = APFloat::getNaN(APFloat::IEEEdouble());
1393 P.roundToIntegral(APFloat::rmTowardZero);
1394 EXPECT_TRUE(std::isnan(P.convertToDouble()));
1395 P = APFloat::getInf(APFloat::IEEEdouble());
1396 P.roundToIntegral(APFloat::rmTowardZero);
1397 EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() > 0.0);
1398 P = APFloat::getInf(APFloat::IEEEdouble(), true);
1399 P.roundToIntegral(APFloat::rmTowardZero);
1400 EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() < 0.0);
1403 TEST(APFloatTest, isInteger) {
1404 APFloat T(-0.0);
1405 EXPECT_TRUE(T.isInteger());
1406 T = APFloat(3.14159);
1407 EXPECT_FALSE(T.isInteger());
1408 T = APFloat::getNaN(APFloat::IEEEdouble());
1409 EXPECT_FALSE(T.isInteger());
1410 T = APFloat::getInf(APFloat::IEEEdouble());
1411 EXPECT_FALSE(T.isInteger());
1412 T = APFloat::getInf(APFloat::IEEEdouble(), true);
1413 EXPECT_FALSE(T.isInteger());
1414 T = APFloat::getLargest(APFloat::IEEEdouble());
1415 EXPECT_TRUE(T.isInteger());
1418 TEST(DoubleAPFloatTest, isInteger) {
1419 APFloat F1(-0.0);
1420 APFloat F2(-0.0);
1421 llvm::detail::DoubleAPFloat T(APFloat::PPCDoubleDouble(), std::move(F1),
1422 std::move(F2));
1423 EXPECT_TRUE(T.isInteger());
1424 APFloat F3(3.14159);
1425 APFloat F4(-0.0);
1426 llvm::detail::DoubleAPFloat T2(APFloat::PPCDoubleDouble(), std::move(F3),
1427 std::move(F4));
1428 EXPECT_FALSE(T2.isInteger());
1429 APFloat F5(-0.0);
1430 APFloat F6(3.14159);
1431 llvm::detail::DoubleAPFloat T3(APFloat::PPCDoubleDouble(), std::move(F5),
1432 std::move(F6));
1433 EXPECT_FALSE(T3.isInteger());
1436 TEST(APFloatTest, getLargest) {
1437 EXPECT_EQ(3.402823466e+38f, APFloat::getLargest(APFloat::IEEEsingle()).convertToFloat());
1438 EXPECT_EQ(1.7976931348623158e+308, APFloat::getLargest(APFloat::IEEEdouble()).convertToDouble());
1441 TEST(APFloatTest, getSmallest) {
1442 APFloat test = APFloat::getSmallest(APFloat::IEEEsingle(), false);
1443 APFloat expected = APFloat(APFloat::IEEEsingle(), "0x0.000002p-126");
1444 EXPECT_FALSE(test.isNegative());
1445 EXPECT_TRUE(test.isFiniteNonZero());
1446 EXPECT_TRUE(test.isDenormal());
1447 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1449 test = APFloat::getSmallest(APFloat::IEEEsingle(), true);
1450 expected = APFloat(APFloat::IEEEsingle(), "-0x0.000002p-126");
1451 EXPECT_TRUE(test.isNegative());
1452 EXPECT_TRUE(test.isFiniteNonZero());
1453 EXPECT_TRUE(test.isDenormal());
1454 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1456 test = APFloat::getSmallest(APFloat::IEEEquad(), false);
1457 expected = APFloat(APFloat::IEEEquad(), "0x0.0000000000000000000000000001p-16382");
1458 EXPECT_FALSE(test.isNegative());
1459 EXPECT_TRUE(test.isFiniteNonZero());
1460 EXPECT_TRUE(test.isDenormal());
1461 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1463 test = APFloat::getSmallest(APFloat::IEEEquad(), true);
1464 expected = APFloat(APFloat::IEEEquad(), "-0x0.0000000000000000000000000001p-16382");
1465 EXPECT_TRUE(test.isNegative());
1466 EXPECT_TRUE(test.isFiniteNonZero());
1467 EXPECT_TRUE(test.isDenormal());
1468 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1471 TEST(APFloatTest, getSmallestNormalized) {
1472 APFloat test = APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
1473 APFloat expected = APFloat(APFloat::IEEEsingle(), "0x1p-126");
1474 EXPECT_FALSE(test.isNegative());
1475 EXPECT_TRUE(test.isFiniteNonZero());
1476 EXPECT_FALSE(test.isDenormal());
1477 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1479 test = APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
1480 expected = APFloat(APFloat::IEEEsingle(), "-0x1p-126");
1481 EXPECT_TRUE(test.isNegative());
1482 EXPECT_TRUE(test.isFiniteNonZero());
1483 EXPECT_FALSE(test.isDenormal());
1484 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1486 test = APFloat::getSmallestNormalized(APFloat::IEEEquad(), false);
1487 expected = APFloat(APFloat::IEEEquad(), "0x1p-16382");
1488 EXPECT_FALSE(test.isNegative());
1489 EXPECT_TRUE(test.isFiniteNonZero());
1490 EXPECT_FALSE(test.isDenormal());
1491 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1493 test = APFloat::getSmallestNormalized(APFloat::IEEEquad(), true);
1494 expected = APFloat(APFloat::IEEEquad(), "-0x1p-16382");
1495 EXPECT_TRUE(test.isNegative());
1496 EXPECT_TRUE(test.isFiniteNonZero());
1497 EXPECT_FALSE(test.isDenormal());
1498 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1501 TEST(APFloatTest, getZero) {
1502 struct {
1503 const fltSemantics *semantics;
1504 const bool sign;
1505 const unsigned long long bitPattern[2];
1506 const unsigned bitPatternLength;
1507 } const GetZeroTest[] = {
1508 { &APFloat::IEEEhalf(), false, {0, 0}, 1},
1509 { &APFloat::IEEEhalf(), true, {0x8000ULL, 0}, 1},
1510 { &APFloat::IEEEsingle(), false, {0, 0}, 1},
1511 { &APFloat::IEEEsingle(), true, {0x80000000ULL, 0}, 1},
1512 { &APFloat::IEEEdouble(), false, {0, 0}, 1},
1513 { &APFloat::IEEEdouble(), true, {0x8000000000000000ULL, 0}, 1},
1514 { &APFloat::IEEEquad(), false, {0, 0}, 2},
1515 { &APFloat::IEEEquad(), true, {0, 0x8000000000000000ULL}, 2},
1516 { &APFloat::PPCDoubleDouble(), false, {0, 0}, 2},
1517 { &APFloat::PPCDoubleDouble(), true, {0x8000000000000000ULL, 0}, 2},
1518 { &APFloat::x87DoubleExtended(), false, {0, 0}, 2},
1519 { &APFloat::x87DoubleExtended(), true, {0, 0x8000ULL}, 2},
1521 const unsigned NumGetZeroTests = 12;
1522 for (unsigned i = 0; i < NumGetZeroTests; ++i) {
1523 APFloat test = APFloat::getZero(*GetZeroTest[i].semantics,
1524 GetZeroTest[i].sign);
1525 const char *pattern = GetZeroTest[i].sign? "-0x0p+0" : "0x0p+0";
1526 APFloat expected = APFloat(*GetZeroTest[i].semantics,
1527 pattern);
1528 EXPECT_TRUE(test.isZero());
1529 EXPECT_TRUE(GetZeroTest[i].sign? test.isNegative() : !test.isNegative());
1530 EXPECT_TRUE(test.bitwiseIsEqual(expected));
1531 for (unsigned j = 0, je = GetZeroTest[i].bitPatternLength; j < je; ++j) {
1532 EXPECT_EQ(GetZeroTest[i].bitPattern[j],
1533 test.bitcastToAPInt().getRawData()[j]);
1538 TEST(APFloatTest, copySign) {
1539 EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
1540 APFloat::copySign(APFloat(42.0), APFloat(-1.0))));
1541 EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
1542 APFloat::copySign(APFloat(-42.0), APFloat(1.0))));
1543 EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
1544 APFloat::copySign(APFloat(-42.0), APFloat(-1.0))));
1545 EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
1546 APFloat::copySign(APFloat(42.0), APFloat(1.0))));
1549 TEST(APFloatTest, convert) {
1550 bool losesInfo;
1551 APFloat test(APFloat::IEEEdouble(), "1.0");
1552 test.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &losesInfo);
1553 EXPECT_EQ(1.0f, test.convertToFloat());
1554 EXPECT_FALSE(losesInfo);
1556 test = APFloat(APFloat::x87DoubleExtended(), "0x1p-53");
1557 test.add(APFloat(APFloat::x87DoubleExtended(), "1.0"), APFloat::rmNearestTiesToEven);
1558 test.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1559 EXPECT_EQ(1.0, test.convertToDouble());
1560 EXPECT_TRUE(losesInfo);
1562 test = APFloat(APFloat::IEEEquad(), "0x1p-53");
1563 test.add(APFloat(APFloat::IEEEquad(), "1.0"), APFloat::rmNearestTiesToEven);
1564 test.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1565 EXPECT_EQ(1.0, test.convertToDouble());
1566 EXPECT_TRUE(losesInfo);
1568 test = APFloat(APFloat::x87DoubleExtended(), "0xf.fffffffp+28");
1569 test.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo);
1570 EXPECT_EQ(4294967295.0, test.convertToDouble());
1571 EXPECT_FALSE(losesInfo);
1573 test = APFloat::getSNaN(APFloat::IEEEsingle());
1574 APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended());
1575 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1576 &losesInfo);
1577 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1578 EXPECT_FALSE(losesInfo);
1580 test = APFloat::getQNaN(APFloat::IEEEsingle());
1581 APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended());
1582 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1583 &losesInfo);
1584 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1585 EXPECT_FALSE(losesInfo);
1587 test = APFloat::getSNaN(APFloat::x87DoubleExtended());
1588 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1589 &losesInfo);
1590 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1591 EXPECT_FALSE(losesInfo);
1593 test = APFloat::getQNaN(APFloat::x87DoubleExtended());
1594 test.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven,
1595 &losesInfo);
1596 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1597 EXPECT_FALSE(losesInfo);
1600 TEST(APFloatTest, PPCDoubleDouble) {
1601 APFloat test(APFloat::PPCDoubleDouble(), "1.0");
1602 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1603 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1605 // LDBL_MAX
1606 test = APFloat(APFloat::PPCDoubleDouble(), "1.79769313486231580793728971405301e+308");
1607 EXPECT_EQ(0x7fefffffffffffffull, test.bitcastToAPInt().getRawData()[0]);
1608 EXPECT_EQ(0x7c8ffffffffffffeull, test.bitcastToAPInt().getRawData()[1]);
1610 // LDBL_MIN
1611 test = APFloat(APFloat::PPCDoubleDouble(), "2.00416836000897277799610805135016e-292");
1612 EXPECT_EQ(0x0360000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1613 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1615 // PR30869
1617 auto Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") +
1618 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1619 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1621 Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") -
1622 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1623 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1625 Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") *
1626 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1627 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1629 Result = APFloat(APFloat::PPCDoubleDouble(), "1.0") /
1630 APFloat(APFloat::PPCDoubleDouble(), "1.0");
1631 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1633 int Exp;
1634 Result = frexp(APFloat(APFloat::PPCDoubleDouble(), "1.0"), Exp,
1635 APFloat::rmNearestTiesToEven);
1636 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1638 Result = scalbn(APFloat(APFloat::PPCDoubleDouble(), "1.0"), 1,
1639 APFloat::rmNearestTiesToEven);
1640 EXPECT_EQ(&APFloat::PPCDoubleDouble(), &Result.getSemantics());
1644 TEST(APFloatTest, isNegative) {
1645 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1646 EXPECT_FALSE(t.isNegative());
1647 t = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
1648 EXPECT_TRUE(t.isNegative());
1650 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isNegative());
1651 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle(), true).isNegative());
1653 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isNegative());
1654 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle(), true).isNegative());
1656 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNegative());
1657 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle(), true).isNegative());
1659 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNegative());
1660 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), true).isNegative());
1663 TEST(APFloatTest, isNormal) {
1664 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1665 EXPECT_TRUE(t.isNormal());
1667 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isNormal());
1668 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isNormal());
1669 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNormal());
1670 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNormal());
1671 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isNormal());
1674 TEST(APFloatTest, isFinite) {
1675 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1676 EXPECT_TRUE(t.isFinite());
1677 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isFinite());
1678 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle(), false).isFinite());
1679 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isFinite());
1680 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isFinite());
1681 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isFinite());
1684 TEST(APFloatTest, isInfinity) {
1685 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1686 EXPECT_FALSE(t.isInfinity());
1687 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle(), false).isInfinity());
1688 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isInfinity());
1689 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isInfinity());
1690 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isInfinity());
1691 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isInfinity());
1694 TEST(APFloatTest, isNaN) {
1695 APFloat t(APFloat::IEEEsingle(), "0x1p+0");
1696 EXPECT_FALSE(t.isNaN());
1697 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isNaN());
1698 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isNaN());
1699 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle(), false).isNaN());
1700 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isNaN());
1701 EXPECT_FALSE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isNaN());
1704 TEST(APFloatTest, isFiniteNonZero) {
1705 // Test positive/negative normal value.
1706 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "0x1p+0").isFiniteNonZero());
1707 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "-0x1p+0").isFiniteNonZero());
1709 // Test positive/negative denormal value.
1710 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "0x1p-149").isFiniteNonZero());
1711 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "-0x1p-149").isFiniteNonZero());
1713 // Test +/- Infinity.
1714 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), false).isFiniteNonZero());
1715 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle(), true).isFiniteNonZero());
1717 // Test +/- Zero.
1718 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), false).isFiniteNonZero());
1719 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle(), true).isFiniteNonZero());
1721 // Test +/- qNaN. +/- dont mean anything with qNaN but paranoia can't hurt in
1722 // this instance.
1723 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), false).isFiniteNonZero());
1724 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle(), true).isFiniteNonZero());
1726 // Test +/- sNaN. +/- dont mean anything with sNaN but paranoia can't hurt in
1727 // this instance.
1728 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), false).isFiniteNonZero());
1729 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle(), true).isFiniteNonZero());
1732 TEST(APFloatTest, add) {
1733 // Test Special Cases against each other and normal values.
1735 // TODOS/NOTES:
1736 // 1. Since we perform only default exception handling all operations with
1737 // signaling NaNs should have a result that is a quiet NaN. Currently they
1738 // return sNaN.
1740 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
1741 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
1742 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
1743 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
1744 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
1745 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
1746 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
1747 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
1748 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
1749 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
1750 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
1751 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
1752 APFloat PSmallestNormalized =
1753 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
1754 APFloat MSmallestNormalized =
1755 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
1757 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
1759 const unsigned NumTests = 169;
1760 struct {
1761 APFloat x;
1762 APFloat y;
1763 const char *result;
1764 int status;
1765 int category;
1766 } SpecialCaseTests[NumTests] = {
1767 { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1768 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1769 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1770 { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1771 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1772 #if 0
1773 // See Note 1.
1774 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1775 #endif
1776 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1777 { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1778 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1779 { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1780 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1781 { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1782 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1783 { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1784 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1785 { MInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1786 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1787 { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1788 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1789 #if 0
1790 // See Note 1.
1791 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1792 #endif
1793 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1794 { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1795 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1796 { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1797 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1798 { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1799 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1800 { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1801 { PZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1802 { PZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1803 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1804 { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1805 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1806 #if 0
1807 // See Note 1.
1808 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1809 #endif
1810 { PZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1811 { PZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1812 { PZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1813 { PZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1814 { PZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1815 { PZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1816 { PZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1817 { PZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1818 { MZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1819 { MZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1820 { MZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1821 { MZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
1822 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1823 #if 0
1824 // See Note 1.
1825 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1826 #endif
1827 { MZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1828 { MZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1829 { MZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1830 { MZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1831 { MZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1832 { MZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1833 { MZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1834 { MZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1835 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
1836 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
1837 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
1838 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
1839 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1840 #if 0
1841 // See Note 1.
1842 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1843 #endif
1844 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
1845 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
1846 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1847 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1848 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1849 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1850 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
1851 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
1852 #if 0
1853 // See Note 1.
1854 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1855 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1856 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1857 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1858 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1859 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1860 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1861 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1862 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1863 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1864 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1865 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1866 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1867 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1868 #endif
1869 { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1870 { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1871 { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1872 { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1873 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1874 #if 0
1875 // See Note 1.
1876 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1877 #endif
1878 { PNormalValue, PNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal },
1879 { PNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1880 { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1881 { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1882 { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1883 { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1884 { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1885 { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1886 { MNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1887 { MNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1888 { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1889 { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1890 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1891 #if 0
1892 // See Note 1.
1893 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1894 #endif
1895 { MNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1896 { MNormalValue, MNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal },
1897 { MNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1898 { MNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1899 { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1900 { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1901 { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1902 { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1903 { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1904 { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1905 { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1906 { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1907 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1908 #if 0
1909 // See Note 1.
1910 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1911 #endif
1912 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1913 { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1914 { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
1915 { PLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1916 { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1917 { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1918 { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1919 { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1920 { MLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1921 { MLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1922 { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1923 { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1924 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1925 #if 0
1926 // See Note 1.
1927 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1928 #endif
1929 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1930 { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1931 { MLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1932 { MLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
1933 { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1934 { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1935 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1936 { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1937 { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1938 { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1939 { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1940 { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1941 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1942 #if 0
1943 // See Note 1.
1944 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1945 #endif
1946 { PSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1947 { PSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1948 { PSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1949 { PSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1950 { PSmallestValue, PSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal },
1951 { PSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1952 { PSmallestValue, PSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1953 { PSmallestValue, MSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1954 { MSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1955 { MSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1956 { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1957 { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1958 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1959 #if 0
1960 // See Note 1.
1961 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1962 #endif
1963 { MSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1964 { MSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1965 { MSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1966 { MSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1967 { MSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1968 { MSmallestValue, MSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal },
1969 { MSmallestValue, PSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1970 { MSmallestValue, MSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1971 { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1972 { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1973 { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1974 { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1975 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1976 #if 0
1977 // See Note 1.
1978 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1979 #endif
1980 { PSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1981 { PSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1982 { PSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1983 { PSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1984 { PSmallestNormalized, PSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1985 { PSmallestNormalized, MSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1986 { PSmallestNormalized, PSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
1987 { PSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1988 { MSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1989 { MSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1990 { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1991 { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1992 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1993 #if 0
1994 // See Note 1.
1995 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1996 #endif
1997 { MSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1998 { MSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1999 { MSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2000 { MSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2001 { MSmallestNormalized, PSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2002 { MSmallestNormalized, MSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2003 { MSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2004 { MSmallestNormalized, MSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal }
2007 for (size_t i = 0; i < NumTests; ++i) {
2008 APFloat x(SpecialCaseTests[i].x);
2009 APFloat y(SpecialCaseTests[i].y);
2010 APFloat::opStatus status = x.add(y, APFloat::rmNearestTiesToEven);
2012 APFloat result(APFloat::IEEEsingle(), SpecialCaseTests[i].result);
2014 EXPECT_TRUE(result.bitwiseIsEqual(x));
2015 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2016 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2020 TEST(APFloatTest, subtract) {
2021 // Test Special Cases against each other and normal values.
2023 // TODOS/NOTES:
2024 // 1. Since we perform only default exception handling all operations with
2025 // signaling NaNs should have a result that is a quiet NaN. Currently they
2026 // return sNaN.
2028 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2029 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2030 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2031 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2032 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2033 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
2034 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2035 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2036 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2037 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2038 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2039 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2040 APFloat PSmallestNormalized =
2041 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2042 APFloat MSmallestNormalized =
2043 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2045 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2047 const unsigned NumTests = 169;
2048 struct {
2049 APFloat x;
2050 APFloat y;
2051 const char *result;
2052 int status;
2053 int category;
2054 } SpecialCaseTests[NumTests] = {
2055 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2056 { PInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2057 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2058 { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2059 { PInf, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2060 #if 0
2061 // See Note 1.
2062 { PInf, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2063 #endif
2064 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2065 { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2066 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2067 { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2068 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2069 { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2070 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2071 { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2072 { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2073 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2074 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2075 { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2076 { MInf, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2077 #if 0
2078 // See Note 1.
2079 { MInf, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2080 #endif
2081 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2082 { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2083 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2084 { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2085 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2086 { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2087 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2088 { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2089 { PZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2090 { PZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2091 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2092 { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2093 { PZero, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2094 #if 0
2095 // See Note 1.
2096 { PZero, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2097 #endif
2098 { PZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2099 { PZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2100 { PZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2101 { PZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2102 { PZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2103 { PZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2104 { PZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2105 { PZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2106 { MZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2107 { MZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2108 { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2109 { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2110 { MZero, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2111 #if 0
2112 // See Note 1.
2113 { MZero, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2114 #endif
2115 { MZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2116 { MZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2117 { MZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2118 { MZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2119 { MZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2120 { MZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2121 { MZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2122 { MZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2123 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2124 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2125 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2126 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2127 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2128 #if 0
2129 // See Note 1.
2130 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2131 #endif
2132 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2133 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2134 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2135 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2136 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2137 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2138 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2139 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2140 #if 0
2141 // See Note 1.
2142 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2143 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2144 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2145 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2146 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2147 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2148 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2149 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2150 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2151 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2152 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2153 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2154 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2155 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2156 #endif
2157 { PNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2158 { PNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2159 { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2160 { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2161 { PNormalValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2162 #if 0
2163 // See Note 1.
2164 { PNormalValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2165 #endif
2166 { PNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2167 { PNormalValue, MNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal },
2168 { PNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2169 { PNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2170 { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2171 { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2172 { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2173 { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2174 { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2175 { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2176 { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2177 { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2178 { MNormalValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2179 #if 0
2180 // See Note 1.
2181 { MNormalValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2182 #endif
2183 { MNormalValue, PNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal },
2184 { MNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2185 { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2186 { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2187 { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2188 { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2189 { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2190 { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2191 { PLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2192 { PLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2193 { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2194 { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2195 { PLargestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2196 #if 0
2197 // See Note 1.
2198 { PLargestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2199 #endif
2200 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2201 { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2202 { PLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2203 { PLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2204 { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2205 { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2206 { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2207 { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2208 { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2209 { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2210 { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2211 { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2212 { MLargestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2213 #if 0
2214 // See Note 1.
2215 { MLargestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2216 #endif
2217 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2218 { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2219 { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2220 { MLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2221 { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2222 { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2223 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2224 { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2225 { PSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2226 { PSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2227 { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2228 { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2229 { PSmallestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2230 #if 0
2231 // See Note 1.
2232 { PSmallestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2233 #endif
2234 { PSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2235 { PSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2236 { PSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2237 { PSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2238 { PSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2239 { PSmallestValue, MSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal },
2240 { PSmallestValue, PSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2241 { PSmallestValue, MSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2242 { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2243 { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2244 { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2245 { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2246 { MSmallestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2247 #if 0
2248 // See Note 1.
2249 { MSmallestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2250 #endif
2251 { MSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2252 { MSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2253 { MSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2254 { MSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2255 { MSmallestValue, PSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal },
2256 { MSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2257 { MSmallestValue, PSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2258 { MSmallestValue, MSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2259 { PSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2260 { PSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2261 { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2262 { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2263 { PSmallestNormalized, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2264 #if 0
2265 // See Note 1.
2266 { PSmallestNormalized, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2267 #endif
2268 { PSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2269 { PSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2270 { PSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2271 { PSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2272 { PSmallestNormalized, PSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2273 { PSmallestNormalized, MSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2274 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2275 { PSmallestNormalized, MSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
2276 { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2277 { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2278 { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2279 { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2280 { MSmallestNormalized, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2281 #if 0
2282 // See Note 1.
2283 { MSmallestNormalized, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2284 #endif
2285 { MSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2286 { MSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2287 { MSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2288 { MSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2289 { MSmallestNormalized, PSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2290 { MSmallestNormalized, MSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2291 { MSmallestNormalized, PSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
2292 { MSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }
2295 for (size_t i = 0; i < NumTests; ++i) {
2296 APFloat x(SpecialCaseTests[i].x);
2297 APFloat y(SpecialCaseTests[i].y);
2298 APFloat::opStatus status = x.subtract(y, APFloat::rmNearestTiesToEven);
2300 APFloat result(APFloat::IEEEsingle(), SpecialCaseTests[i].result);
2302 EXPECT_TRUE(result.bitwiseIsEqual(x));
2303 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2304 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2308 TEST(APFloatTest, multiply) {
2309 // Test Special Cases against each other and normal values.
2311 // TODOS/NOTES:
2312 // 1. Since we perform only default exception handling all operations with
2313 // signaling NaNs should have a result that is a quiet NaN. Currently they
2314 // return sNaN.
2316 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2317 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2318 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2319 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2320 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2321 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
2322 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2323 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2324 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2325 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2326 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2327 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2328 APFloat PSmallestNormalized =
2329 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2330 APFloat MSmallestNormalized =
2331 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2333 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2334 const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact;
2336 const unsigned NumTests = 169;
2337 struct {
2338 APFloat x;
2339 APFloat y;
2340 const char *result;
2341 int status;
2342 int category;
2343 } SpecialCaseTests[NumTests] = {
2344 { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2345 { PInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2346 { PInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2347 { PInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2348 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2349 #if 0
2350 // See Note 1.
2351 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2352 #endif
2353 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2354 { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2355 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2356 { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2357 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2358 { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2359 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2360 { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2361 { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2362 { MInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2363 { MInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2364 { MInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2365 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2366 #if 0
2367 // See Note 1.
2368 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2369 #endif
2370 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2371 { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2372 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2373 { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2374 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2375 { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2376 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2377 { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2378 { PZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2379 { PZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2380 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2381 { PZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2382 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2383 #if 0
2384 // See Note 1.
2385 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2386 #endif
2387 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2388 { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2389 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2390 { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2391 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2392 { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2393 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2394 { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2395 { MZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2396 { MZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2397 { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2398 { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2399 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2400 #if 0
2401 // See Note 1.
2402 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2403 #endif
2404 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2405 { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2406 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2407 { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2408 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2409 { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2410 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2411 { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2412 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2413 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2414 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2415 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2416 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2417 #if 0
2418 // See Note 1.
2419 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2420 #endif
2421 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2422 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2423 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2424 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2425 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2426 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2427 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2428 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2429 #if 0
2430 // See Note 1.
2431 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2432 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2433 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2434 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2435 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2436 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2437 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2438 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2439 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2440 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2441 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2442 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2443 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2444 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2445 #endif
2446 { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2447 { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2448 { PNormalValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2449 { PNormalValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2450 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2451 #if 0
2452 // See Note 1.
2453 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2454 #endif
2455 { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2456 { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2457 { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2458 { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2459 { PNormalValue, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2460 { PNormalValue, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2461 { PNormalValue, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2462 { PNormalValue, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2463 { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2464 { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2465 { MNormalValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2466 { MNormalValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2467 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2468 #if 0
2469 // See Note 1.
2470 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2471 #endif
2472 { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2473 { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2474 { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2475 { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2476 { MNormalValue, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2477 { MNormalValue, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2478 { MNormalValue, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2479 { MNormalValue, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2480 { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2481 { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2482 { PLargestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2483 { PLargestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2484 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2485 #if 0
2486 // See Note 1.
2487 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2488 #endif
2489 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2490 { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2491 { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2492 { PLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2493 { PLargestValue, PSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2494 { PLargestValue, MSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2495 { PLargestValue, PSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2496 { PLargestValue, MSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2497 { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2498 { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2499 { MLargestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2500 { MLargestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2501 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2502 #if 0
2503 // See Note 1.
2504 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2505 #endif
2506 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2507 { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2508 { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2509 { MLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2510 { MLargestValue, PSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2511 { MLargestValue, MSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2512 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2513 { MLargestValue, MSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2514 { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2515 { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2516 { PSmallestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2517 { PSmallestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2518 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2519 #if 0
2520 // See Note 1.
2521 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2522 #endif
2523 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2524 { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2525 { PSmallestValue, PLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2526 { PSmallestValue, MLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2527 { PSmallestValue, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2528 { PSmallestValue, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2529 { PSmallestValue, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2530 { PSmallestValue, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2531 { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2532 { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2533 { MSmallestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2534 { MSmallestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2535 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2536 #if 0
2537 // See Note 1.
2538 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2539 #endif
2540 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2541 { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2542 { MSmallestValue, PLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2543 { MSmallestValue, MLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2544 { MSmallestValue, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2545 { MSmallestValue, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2546 { MSmallestValue, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2547 { MSmallestValue, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2548 { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2549 { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2550 { PSmallestNormalized, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2551 { PSmallestNormalized, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2552 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2553 #if 0
2554 // See Note 1.
2555 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2556 #endif
2557 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2558 { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2559 { PSmallestNormalized, PLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2560 { PSmallestNormalized, MLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2561 { PSmallestNormalized, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2562 { PSmallestNormalized, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2563 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2564 { PSmallestNormalized, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2565 { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2566 { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2567 { MSmallestNormalized, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2568 { MSmallestNormalized, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2569 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2570 #if 0
2571 // See Note 1.
2572 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2573 #endif
2574 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2575 { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2576 { MSmallestNormalized, PLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2577 { MSmallestNormalized, MLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2578 { MSmallestNormalized, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2579 { MSmallestNormalized, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2580 { MSmallestNormalized, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2581 { MSmallestNormalized, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero }
2584 for (size_t i = 0; i < NumTests; ++i) {
2585 APFloat x(SpecialCaseTests[i].x);
2586 APFloat y(SpecialCaseTests[i].y);
2587 APFloat::opStatus status = x.multiply(y, APFloat::rmNearestTiesToEven);
2589 APFloat result(APFloat::IEEEsingle(), SpecialCaseTests[i].result);
2591 EXPECT_TRUE(result.bitwiseIsEqual(x));
2592 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2593 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2597 TEST(APFloatTest, divide) {
2598 // Test Special Cases against each other and normal values.
2600 // TODOS/NOTES:
2601 // 1. Since we perform only default exception handling all operations with
2602 // signaling NaNs should have a result that is a quiet NaN. Currently they
2603 // return sNaN.
2605 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2606 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2607 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2608 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2609 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2610 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
2611 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2612 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2613 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2614 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2615 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2616 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2617 APFloat PSmallestNormalized =
2618 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2619 APFloat MSmallestNormalized =
2620 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2622 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2623 const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact;
2625 const unsigned NumTests = 169;
2626 struct {
2627 APFloat x;
2628 APFloat y;
2629 const char *result;
2630 int status;
2631 int category;
2632 } SpecialCaseTests[NumTests] = {
2633 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2634 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2635 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2636 { PInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2637 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2638 #if 0
2639 // See Note 1.
2640 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2641 #endif
2642 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2643 { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2644 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2645 { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2646 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2647 { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2648 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2649 { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2650 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2651 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2652 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2653 { MInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2654 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2655 #if 0
2656 // See Note 1.
2657 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2658 #endif
2659 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2660 { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2661 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2662 { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2663 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2664 { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2665 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2666 { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2667 { PZero, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2668 { PZero, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2669 { PZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2670 { PZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2671 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2672 #if 0
2673 // See Note 1.
2674 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2675 #endif
2676 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2677 { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2678 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2679 { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2680 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2681 { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2682 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2683 { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2684 { MZero, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2685 { MZero, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2686 { MZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2687 { MZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2688 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2689 #if 0
2690 // See Note 1.
2691 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2692 #endif
2693 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2694 { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2695 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2696 { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2697 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2698 { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2699 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2700 { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2701 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2702 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2703 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2704 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2705 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2706 #if 0
2707 // See Note 1.
2708 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2709 #endif
2710 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2711 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2712 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2713 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2714 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2715 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2716 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2717 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2718 #if 0
2719 // See Note 1.
2720 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2721 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2722 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2723 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2724 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2725 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2726 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2727 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2728 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2729 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2730 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2731 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2732 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2733 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2734 #endif
2735 { PNormalValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2736 { PNormalValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2737 { PNormalValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2738 { PNormalValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2739 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2740 #if 0
2741 // See Note 1.
2742 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2743 #endif
2744 { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2745 { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2746 { PNormalValue, PLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal },
2747 { PNormalValue, MLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal },
2748 { PNormalValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2749 { PNormalValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2750 { PNormalValue, PSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal },
2751 { PNormalValue, MSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal },
2752 { MNormalValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2753 { MNormalValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2754 { MNormalValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2755 { MNormalValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2756 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2757 #if 0
2758 // See Note 1.
2759 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2760 #endif
2761 { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2762 { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2763 { MNormalValue, PLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal },
2764 { MNormalValue, MLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal },
2765 { MNormalValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2766 { MNormalValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2767 { MNormalValue, PSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal },
2768 { MNormalValue, MSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal },
2769 { PLargestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2770 { PLargestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2771 { PLargestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2772 { PLargestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2773 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2774 #if 0
2775 // See Note 1.
2776 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2777 #endif
2778 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2779 { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2780 { PLargestValue, PLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2781 { PLargestValue, MLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2782 { PLargestValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2783 { PLargestValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2784 { PLargestValue, PSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity },
2785 { PLargestValue, MSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity },
2786 { MLargestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2787 { MLargestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2788 { MLargestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2789 { MLargestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2790 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2791 #if 0
2792 // See Note 1.
2793 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2794 #endif
2795 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2796 { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2797 { MLargestValue, PLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2798 { MLargestValue, MLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2799 { MLargestValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2800 { MLargestValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2801 { MLargestValue, PSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity },
2802 { MLargestValue, MSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity },
2803 { PSmallestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2804 { PSmallestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2805 { PSmallestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2806 { PSmallestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2807 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2808 #if 0
2809 // See Note 1.
2810 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2811 #endif
2812 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2813 { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2814 { PSmallestValue, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2815 { PSmallestValue, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2816 { PSmallestValue, PSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2817 { PSmallestValue, MSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2818 { PSmallestValue, PSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal },
2819 { PSmallestValue, MSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal },
2820 { MSmallestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2821 { MSmallestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2822 { MSmallestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2823 { MSmallestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2824 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2825 #if 0
2826 // See Note 1.
2827 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2828 #endif
2829 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2830 { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2831 { MSmallestValue, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2832 { MSmallestValue, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2833 { MSmallestValue, PSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2834 { MSmallestValue, MSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2835 { MSmallestValue, PSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal },
2836 { MSmallestValue, MSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal },
2837 { PSmallestNormalized, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2838 { PSmallestNormalized, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2839 { PSmallestNormalized, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2840 { PSmallestNormalized, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2841 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2842 #if 0
2843 // See Note 1.
2844 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2845 #endif
2846 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2847 { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2848 { PSmallestNormalized, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2849 { PSmallestNormalized, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2850 { PSmallestNormalized, PSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal },
2851 { PSmallestNormalized, MSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal },
2852 { PSmallestNormalized, PSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2853 { PSmallestNormalized, MSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2854 { MSmallestNormalized, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2855 { MSmallestNormalized, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2856 { MSmallestNormalized, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2857 { MSmallestNormalized, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2858 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2859 #if 0
2860 // See Note 1.
2861 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2862 #endif
2863 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2864 { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2865 { MSmallestNormalized, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2866 { MSmallestNormalized, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2867 { MSmallestNormalized, PSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal },
2868 { MSmallestNormalized, MSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal },
2869 { MSmallestNormalized, PSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2870 { MSmallestNormalized, MSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2873 for (size_t i = 0; i < NumTests; ++i) {
2874 APFloat x(SpecialCaseTests[i].x);
2875 APFloat y(SpecialCaseTests[i].y);
2876 APFloat::opStatus status = x.divide(y, APFloat::rmNearestTiesToEven);
2878 APFloat result(APFloat::IEEEsingle(), SpecialCaseTests[i].result);
2880 EXPECT_TRUE(result.bitwiseIsEqual(x));
2881 EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2882 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2886 TEST(APFloatTest, operatorOverloads) {
2887 // This is mostly testing that these operator overloads compile.
2888 APFloat One = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2889 APFloat Two = APFloat(APFloat::IEEEsingle(), "0x2p+0");
2890 EXPECT_TRUE(Two.bitwiseIsEqual(One + One));
2891 EXPECT_TRUE(One.bitwiseIsEqual(Two - One));
2892 EXPECT_TRUE(Two.bitwiseIsEqual(One * Two));
2893 EXPECT_TRUE(One.bitwiseIsEqual(Two / Two));
2896 TEST(APFloatTest, abs) {
2897 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
2898 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2899 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
2900 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2901 APFloat PQNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2902 APFloat MQNaN = APFloat::getNaN(APFloat::IEEEsingle(), true);
2903 APFloat PSNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
2904 APFloat MSNaN = APFloat::getSNaN(APFloat::IEEEsingle(), true);
2905 APFloat PNormalValue = APFloat(APFloat::IEEEsingle(), "0x1p+0");
2906 APFloat MNormalValue = APFloat(APFloat::IEEEsingle(), "-0x1p+0");
2907 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), false);
2908 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle(), true);
2909 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), false);
2910 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle(), true);
2911 APFloat PSmallestNormalized =
2912 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false);
2913 APFloat MSmallestNormalized =
2914 APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true);
2916 EXPECT_TRUE(PInf.bitwiseIsEqual(abs(PInf)));
2917 EXPECT_TRUE(PInf.bitwiseIsEqual(abs(MInf)));
2918 EXPECT_TRUE(PZero.bitwiseIsEqual(abs(PZero)));
2919 EXPECT_TRUE(PZero.bitwiseIsEqual(abs(MZero)));
2920 EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(PQNaN)));
2921 EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(MQNaN)));
2922 EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(PSNaN)));
2923 EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(MSNaN)));
2924 EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(PNormalValue)));
2925 EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(MNormalValue)));
2926 EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(PLargestValue)));
2927 EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(MLargestValue)));
2928 EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(PSmallestValue)));
2929 EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(MSmallestValue)));
2930 EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(PSmallestNormalized)));
2931 EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(MSmallestNormalized)));
2934 TEST(APFloatTest, neg) {
2935 APFloat One = APFloat(APFloat::IEEEsingle(), "1.0");
2936 APFloat NegOne = APFloat(APFloat::IEEEsingle(), "-1.0");
2937 APFloat Zero = APFloat::getZero(APFloat::IEEEsingle(), false);
2938 APFloat NegZero = APFloat::getZero(APFloat::IEEEsingle(), true);
2939 APFloat Inf = APFloat::getInf(APFloat::IEEEsingle(), false);
2940 APFloat NegInf = APFloat::getInf(APFloat::IEEEsingle(), true);
2941 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
2942 APFloat NegQNaN = APFloat::getNaN(APFloat::IEEEsingle(), true);
2944 EXPECT_TRUE(NegOne.bitwiseIsEqual(neg(One)));
2945 EXPECT_TRUE(One.bitwiseIsEqual(neg(NegOne)));
2946 EXPECT_TRUE(NegZero.bitwiseIsEqual(neg(Zero)));
2947 EXPECT_TRUE(Zero.bitwiseIsEqual(neg(NegZero)));
2948 EXPECT_TRUE(NegInf.bitwiseIsEqual(neg(Inf)));
2949 EXPECT_TRUE(Inf.bitwiseIsEqual(neg(NegInf)));
2950 EXPECT_TRUE(NegInf.bitwiseIsEqual(neg(Inf)));
2951 EXPECT_TRUE(Inf.bitwiseIsEqual(neg(NegInf)));
2952 EXPECT_TRUE(NegQNaN.bitwiseIsEqual(neg(QNaN)));
2953 EXPECT_TRUE(QNaN.bitwiseIsEqual(neg(NegQNaN)));
2956 TEST(APFloatTest, ilogb) {
2957 EXPECT_EQ(-1074, ilogb(APFloat::getSmallest(APFloat::IEEEdouble(), false)));
2958 EXPECT_EQ(-1074, ilogb(APFloat::getSmallest(APFloat::IEEEdouble(), true)));
2959 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1024")));
2960 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1023")));
2961 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1023")));
2962 EXPECT_EQ(-51, ilogb(APFloat(APFloat::IEEEdouble(), "0x1p-51")));
2963 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-1023")));
2964 EXPECT_EQ(-2, ilogb(APFloat(APFloat::IEEEdouble(), "0x0.ffffp-1")));
2965 EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble(), "0x1.fffep-1023")));
2966 EXPECT_EQ(1023, ilogb(APFloat::getLargest(APFloat::IEEEdouble(), false)));
2967 EXPECT_EQ(1023, ilogb(APFloat::getLargest(APFloat::IEEEdouble(), true)));
2970 EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle(), "0x1p+0")));
2971 EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle(), "-0x1p+0")));
2972 EXPECT_EQ(42, ilogb(APFloat(APFloat::IEEEsingle(), "0x1p+42")));
2973 EXPECT_EQ(-42, ilogb(APFloat(APFloat::IEEEsingle(), "0x1p-42")));
2975 EXPECT_EQ(APFloat::IEK_Inf,
2976 ilogb(APFloat::getInf(APFloat::IEEEsingle(), false)));
2977 EXPECT_EQ(APFloat::IEK_Inf,
2978 ilogb(APFloat::getInf(APFloat::IEEEsingle(), true)));
2979 EXPECT_EQ(APFloat::IEK_Zero,
2980 ilogb(APFloat::getZero(APFloat::IEEEsingle(), false)));
2981 EXPECT_EQ(APFloat::IEK_Zero,
2982 ilogb(APFloat::getZero(APFloat::IEEEsingle(), true)));
2983 EXPECT_EQ(APFloat::IEK_NaN,
2984 ilogb(APFloat::getNaN(APFloat::IEEEsingle(), false)));
2985 EXPECT_EQ(APFloat::IEK_NaN,
2986 ilogb(APFloat::getSNaN(APFloat::IEEEsingle(), false)));
2988 EXPECT_EQ(127, ilogb(APFloat::getLargest(APFloat::IEEEsingle(), false)));
2989 EXPECT_EQ(127, ilogb(APFloat::getLargest(APFloat::IEEEsingle(), true)));
2991 EXPECT_EQ(-149, ilogb(APFloat::getSmallest(APFloat::IEEEsingle(), false)));
2992 EXPECT_EQ(-149, ilogb(APFloat::getSmallest(APFloat::IEEEsingle(), true)));
2993 EXPECT_EQ(-126,
2994 ilogb(APFloat::getSmallestNormalized(APFloat::IEEEsingle(), false)));
2995 EXPECT_EQ(-126,
2996 ilogb(APFloat::getSmallestNormalized(APFloat::IEEEsingle(), true)));
2999 TEST(APFloatTest, scalbn) {
3001 const APFloat::roundingMode RM = APFloat::rmNearestTiesToEven;
3002 EXPECT_TRUE(
3003 APFloat(APFloat::IEEEsingle(), "0x1p+0")
3004 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), 0, RM)));
3005 EXPECT_TRUE(
3006 APFloat(APFloat::IEEEsingle(), "0x1p+42")
3007 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), 42, RM)));
3008 EXPECT_TRUE(
3009 APFloat(APFloat::IEEEsingle(), "0x1p-42")
3010 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), -42, RM)));
3012 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle(), false);
3013 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle(), true);
3014 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle(), false);
3015 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle(), true);
3016 APFloat QPNaN = APFloat::getNaN(APFloat::IEEEsingle(), false);
3017 APFloat QMNaN = APFloat::getNaN(APFloat::IEEEsingle(), true);
3018 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle(), false);
3020 EXPECT_TRUE(PInf.bitwiseIsEqual(scalbn(PInf, 0, RM)));
3021 EXPECT_TRUE(MInf.bitwiseIsEqual(scalbn(MInf, 0, RM)));
3022 EXPECT_TRUE(PZero.bitwiseIsEqual(scalbn(PZero, 0, RM)));
3023 EXPECT_TRUE(MZero.bitwiseIsEqual(scalbn(MZero, 0, RM)));
3024 EXPECT_TRUE(QPNaN.bitwiseIsEqual(scalbn(QPNaN, 0, RM)));
3025 EXPECT_TRUE(QMNaN.bitwiseIsEqual(scalbn(QMNaN, 0, RM)));
3026 EXPECT_FALSE(scalbn(SNaN, 0, RM).isSignaling());
3028 APFloat ScalbnSNaN = scalbn(SNaN, 1, RM);
3029 EXPECT_TRUE(ScalbnSNaN.isNaN() && !ScalbnSNaN.isSignaling());
3031 // Make sure highest bit of payload is preserved.
3032 const APInt Payload(64, (UINT64_C(1) << 50) |
3033 (UINT64_C(1) << 49) |
3034 (UINT64_C(1234) << 32) |
3037 APFloat SNaNWithPayload = APFloat::getSNaN(APFloat::IEEEdouble(), false,
3038 &Payload);
3039 APFloat QuietPayload = scalbn(SNaNWithPayload, 1, RM);
3040 EXPECT_TRUE(QuietPayload.isNaN() && !QuietPayload.isSignaling());
3041 EXPECT_EQ(Payload, QuietPayload.bitcastToAPInt().getLoBits(51));
3043 EXPECT_TRUE(PInf.bitwiseIsEqual(
3044 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+0"), 128, RM)));
3045 EXPECT_TRUE(MInf.bitwiseIsEqual(
3046 scalbn(APFloat(APFloat::IEEEsingle(), "-0x1p+0"), 128, RM)));
3047 EXPECT_TRUE(PInf.bitwiseIsEqual(
3048 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p+127"), 1, RM)));
3049 EXPECT_TRUE(PZero.bitwiseIsEqual(
3050 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p-127"), -127, RM)));
3051 EXPECT_TRUE(MZero.bitwiseIsEqual(
3052 scalbn(APFloat(APFloat::IEEEsingle(), "-0x1p-127"), -127, RM)));
3053 EXPECT_TRUE(APFloat(APFloat::IEEEsingle(), "-0x1p-149").bitwiseIsEqual(
3054 scalbn(APFloat(APFloat::IEEEsingle(), "-0x1p-127"), -22, RM)));
3055 EXPECT_TRUE(PZero.bitwiseIsEqual(
3056 scalbn(APFloat(APFloat::IEEEsingle(), "0x1p-126"), -24, RM)));
3059 APFloat SmallestF64 = APFloat::getSmallest(APFloat::IEEEdouble(), false);
3060 APFloat NegSmallestF64 = APFloat::getSmallest(APFloat::IEEEdouble(), true);
3062 APFloat LargestF64 = APFloat::getLargest(APFloat::IEEEdouble(), false);
3063 APFloat NegLargestF64 = APFloat::getLargest(APFloat::IEEEdouble(), true);
3065 APFloat SmallestNormalizedF64
3066 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), false);
3067 APFloat NegSmallestNormalizedF64
3068 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), true);
3070 APFloat LargestDenormalF64(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1023");
3071 APFloat NegLargestDenormalF64(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1023");
3074 EXPECT_TRUE(SmallestF64.bitwiseIsEqual(
3075 scalbn(APFloat(APFloat::IEEEdouble(), "0x1p-1074"), 0, RM)));
3076 EXPECT_TRUE(NegSmallestF64.bitwiseIsEqual(
3077 scalbn(APFloat(APFloat::IEEEdouble(), "-0x1p-1074"), 0, RM)));
3079 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+1023")
3080 .bitwiseIsEqual(scalbn(SmallestF64, 2097, RM)));
3082 EXPECT_TRUE(scalbn(SmallestF64, -2097, RM).isPosZero());
3083 EXPECT_TRUE(scalbn(SmallestF64, -2098, RM).isPosZero());
3084 EXPECT_TRUE(scalbn(SmallestF64, -2099, RM).isPosZero());
3085 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+1022")
3086 .bitwiseIsEqual(scalbn(SmallestF64, 2096, RM)));
3087 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+1023")
3088 .bitwiseIsEqual(scalbn(SmallestF64, 2097, RM)));
3089 EXPECT_TRUE(scalbn(SmallestF64, 2098, RM).isInfinity());
3090 EXPECT_TRUE(scalbn(SmallestF64, 2099, RM).isInfinity());
3092 // Test for integer overflows when adding to exponent.
3093 EXPECT_TRUE(scalbn(SmallestF64, -INT_MAX, RM).isPosZero());
3094 EXPECT_TRUE(scalbn(LargestF64, INT_MAX, RM).isInfinity());
3096 EXPECT_TRUE(LargestDenormalF64
3097 .bitwiseIsEqual(scalbn(LargestDenormalF64, 0, RM)));
3098 EXPECT_TRUE(NegLargestDenormalF64
3099 .bitwiseIsEqual(scalbn(NegLargestDenormalF64, 0, RM)));
3101 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1022")
3102 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1, RM)));
3103 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1021")
3104 .bitwiseIsEqual(scalbn(NegLargestDenormalF64, 2, RM)));
3106 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep+1")
3107 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1024, RM)));
3108 EXPECT_TRUE(scalbn(LargestDenormalF64, -1023, RM).isPosZero());
3109 EXPECT_TRUE(scalbn(LargestDenormalF64, -1024, RM).isPosZero());
3110 EXPECT_TRUE(scalbn(LargestDenormalF64, -2048, RM).isPosZero());
3111 EXPECT_TRUE(scalbn(LargestDenormalF64, 2047, RM).isInfinity());
3112 EXPECT_TRUE(scalbn(LargestDenormalF64, 2098, RM).isInfinity());
3113 EXPECT_TRUE(scalbn(LargestDenormalF64, 2099, RM).isInfinity());
3115 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-2")
3116 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1021, RM)));
3117 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1")
3118 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1022, RM)));
3119 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep+0")
3120 .bitwiseIsEqual(scalbn(LargestDenormalF64, 1023, RM)));
3121 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep+1023")
3122 .bitwiseIsEqual(scalbn(LargestDenormalF64, 2046, RM)));
3123 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p+974")
3124 .bitwiseIsEqual(scalbn(SmallestF64, 2048, RM)));
3126 APFloat RandomDenormalF64(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp+51");
3127 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-972")
3128 .bitwiseIsEqual(scalbn(RandomDenormalF64, -1023, RM)));
3129 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-1")
3130 .bitwiseIsEqual(scalbn(RandomDenormalF64, -52, RM)));
3131 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-2")
3132 .bitwiseIsEqual(scalbn(RandomDenormalF64, -53, RM)));
3133 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp+0")
3134 .bitwiseIsEqual(scalbn(RandomDenormalF64, -51, RM)));
3136 EXPECT_TRUE(scalbn(RandomDenormalF64, -2097, RM).isPosZero());
3137 EXPECT_TRUE(scalbn(RandomDenormalF64, -2090, RM).isPosZero());
3140 EXPECT_TRUE(
3141 APFloat(APFloat::IEEEdouble(), "-0x1p-1073")
3142 .bitwiseIsEqual(scalbn(NegLargestF64, -2097, RM)));
3144 EXPECT_TRUE(
3145 APFloat(APFloat::IEEEdouble(), "-0x1p-1024")
3146 .bitwiseIsEqual(scalbn(NegLargestF64, -2048, RM)));
3148 EXPECT_TRUE(
3149 APFloat(APFloat::IEEEdouble(), "0x1p-1073")
3150 .bitwiseIsEqual(scalbn(LargestF64, -2097, RM)));
3152 EXPECT_TRUE(
3153 APFloat(APFloat::IEEEdouble(), "0x1p-1074")
3154 .bitwiseIsEqual(scalbn(LargestF64, -2098, RM)));
3155 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1p-1074")
3156 .bitwiseIsEqual(scalbn(NegLargestF64, -2098, RM)));
3157 EXPECT_TRUE(scalbn(NegLargestF64, -2099, RM).isNegZero());
3158 EXPECT_TRUE(scalbn(LargestF64, 1, RM).isInfinity());
3161 EXPECT_TRUE(
3162 APFloat(APFloat::IEEEdouble(), "0x1p+0")
3163 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEdouble(), "0x1p+52"), -52, RM)));
3165 EXPECT_TRUE(
3166 APFloat(APFloat::IEEEdouble(), "0x1p-103")
3167 .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEdouble(), "0x1p-51"), -52, RM)));
3170 TEST(APFloatTest, frexp) {
3171 const APFloat::roundingMode RM = APFloat::rmNearestTiesToEven;
3173 APFloat PZero = APFloat::getZero(APFloat::IEEEdouble(), false);
3174 APFloat MZero = APFloat::getZero(APFloat::IEEEdouble(), true);
3175 APFloat One(1.0);
3176 APFloat MOne(-1.0);
3177 APFloat Two(2.0);
3178 APFloat MTwo(-2.0);
3180 APFloat LargestDenormal(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1023");
3181 APFloat NegLargestDenormal(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1023");
3183 APFloat Smallest = APFloat::getSmallest(APFloat::IEEEdouble(), false);
3184 APFloat NegSmallest = APFloat::getSmallest(APFloat::IEEEdouble(), true);
3186 APFloat Largest = APFloat::getLargest(APFloat::IEEEdouble(), false);
3187 APFloat NegLargest = APFloat::getLargest(APFloat::IEEEdouble(), true);
3189 APFloat PInf = APFloat::getInf(APFloat::IEEEdouble(), false);
3190 APFloat MInf = APFloat::getInf(APFloat::IEEEdouble(), true);
3192 APFloat QPNaN = APFloat::getNaN(APFloat::IEEEdouble(), false);
3193 APFloat QMNaN = APFloat::getNaN(APFloat::IEEEdouble(), true);
3194 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEdouble(), false);
3196 // Make sure highest bit of payload is preserved.
3197 const APInt Payload(64, (UINT64_C(1) << 50) |
3198 (UINT64_C(1) << 49) |
3199 (UINT64_C(1234) << 32) |
3202 APFloat SNaNWithPayload = APFloat::getSNaN(APFloat::IEEEdouble(), false,
3203 &Payload);
3205 APFloat SmallestNormalized
3206 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), false);
3207 APFloat NegSmallestNormalized
3208 = APFloat::getSmallestNormalized(APFloat::IEEEdouble(), true);
3210 int Exp;
3211 APFloat Frac(APFloat::IEEEdouble());
3214 Frac = frexp(PZero, Exp, RM);
3215 EXPECT_EQ(0, Exp);
3216 EXPECT_TRUE(Frac.isPosZero());
3218 Frac = frexp(MZero, Exp, RM);
3219 EXPECT_EQ(0, Exp);
3220 EXPECT_TRUE(Frac.isNegZero());
3223 Frac = frexp(One, Exp, RM);
3224 EXPECT_EQ(1, Exp);
3225 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p-1").bitwiseIsEqual(Frac));
3227 Frac = frexp(MOne, Exp, RM);
3228 EXPECT_EQ(1, Exp);
3229 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1p-1").bitwiseIsEqual(Frac));
3231 Frac = frexp(LargestDenormal, Exp, RM);
3232 EXPECT_EQ(-1022, Exp);
3233 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.ffffffffffffep-1").bitwiseIsEqual(Frac));
3235 Frac = frexp(NegLargestDenormal, Exp, RM);
3236 EXPECT_EQ(-1022, Exp);
3237 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1.ffffffffffffep-1").bitwiseIsEqual(Frac));
3240 Frac = frexp(Smallest, Exp, RM);
3241 EXPECT_EQ(-1073, Exp);
3242 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p-1").bitwiseIsEqual(Frac));
3244 Frac = frexp(NegSmallest, Exp, RM);
3245 EXPECT_EQ(-1073, Exp);
3246 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1p-1").bitwiseIsEqual(Frac));
3249 Frac = frexp(Largest, Exp, RM);
3250 EXPECT_EQ(1024, Exp);
3251 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.fffffffffffffp-1").bitwiseIsEqual(Frac));
3253 Frac = frexp(NegLargest, Exp, RM);
3254 EXPECT_EQ(1024, Exp);
3255 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "-0x1.fffffffffffffp-1").bitwiseIsEqual(Frac));
3258 Frac = frexp(PInf, Exp, RM);
3259 EXPECT_EQ(INT_MAX, Exp);
3260 EXPECT_TRUE(Frac.isInfinity() && !Frac.isNegative());
3262 Frac = frexp(MInf, Exp, RM);
3263 EXPECT_EQ(INT_MAX, Exp);
3264 EXPECT_TRUE(Frac.isInfinity() && Frac.isNegative());
3266 Frac = frexp(QPNaN, Exp, RM);
3267 EXPECT_EQ(INT_MIN, Exp);
3268 EXPECT_TRUE(Frac.isNaN());
3270 Frac = frexp(QMNaN, Exp, RM);
3271 EXPECT_EQ(INT_MIN, Exp);
3272 EXPECT_TRUE(Frac.isNaN());
3274 Frac = frexp(SNaN, Exp, RM);
3275 EXPECT_EQ(INT_MIN, Exp);
3276 EXPECT_TRUE(Frac.isNaN() && !Frac.isSignaling());
3278 Frac = frexp(SNaNWithPayload, Exp, RM);
3279 EXPECT_EQ(INT_MIN, Exp);
3280 EXPECT_TRUE(Frac.isNaN() && !Frac.isSignaling());
3281 EXPECT_EQ(Payload, Frac.bitcastToAPInt().getLoBits(51));
3283 Frac = frexp(APFloat(APFloat::IEEEdouble(), "0x0.ffffp-1"), Exp, RM);
3284 EXPECT_EQ(-1, Exp);
3285 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.fffep-1").bitwiseIsEqual(Frac));
3287 Frac = frexp(APFloat(APFloat::IEEEdouble(), "0x1p-51"), Exp, RM);
3288 EXPECT_EQ(-50, Exp);
3289 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1p-1").bitwiseIsEqual(Frac));
3291 Frac = frexp(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp+51"), Exp, RM);
3292 EXPECT_EQ(52, Exp);
3293 EXPECT_TRUE(APFloat(APFloat::IEEEdouble(), "0x1.c60f120d9f87cp-1").bitwiseIsEqual(Frac));
3296 TEST(APFloatTest, mod) {
3298 APFloat f1(APFloat::IEEEdouble(), "1.5");
3299 APFloat f2(APFloat::IEEEdouble(), "1.0");
3300 APFloat expected(APFloat::IEEEdouble(), "0.5");
3301 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3302 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3305 APFloat f1(APFloat::IEEEdouble(), "0.5");
3306 APFloat f2(APFloat::IEEEdouble(), "1.0");
3307 APFloat expected(APFloat::IEEEdouble(), "0.5");
3308 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3309 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3312 APFloat f1(APFloat::IEEEdouble(), "0x1.3333333333333p-2"); // 0.3
3313 APFloat f2(APFloat::IEEEdouble(), "0x1.47ae147ae147bp-7"); // 0.01
3314 APFloat expected(APFloat::IEEEdouble(),
3315 "0x1.47ae147ae1471p-7"); // 0.009999999999999983
3316 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3317 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3320 APFloat f1(APFloat::IEEEdouble(), "0x1p64"); // 1.8446744073709552e19
3321 APFloat f2(APFloat::IEEEdouble(), "1.5");
3322 APFloat expected(APFloat::IEEEdouble(), "1.0");
3323 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3324 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3327 APFloat f1(APFloat::IEEEdouble(), "0x1p1000");
3328 APFloat f2(APFloat::IEEEdouble(), "0x1p-1000");
3329 APFloat expected(APFloat::IEEEdouble(), "0.0");
3330 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3331 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3334 APFloat f1(APFloat::IEEEdouble(), "0.0");
3335 APFloat f2(APFloat::IEEEdouble(), "1.0");
3336 APFloat expected(APFloat::IEEEdouble(), "0.0");
3337 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3338 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3341 APFloat f1(APFloat::IEEEdouble(), "1.0");
3342 APFloat f2(APFloat::IEEEdouble(), "0.0");
3343 EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp);
3344 EXPECT_TRUE(f1.isNaN());
3347 APFloat f1(APFloat::IEEEdouble(), "0.0");
3348 APFloat f2(APFloat::IEEEdouble(), "0.0");
3349 EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp);
3350 EXPECT_TRUE(f1.isNaN());
3353 APFloat f1 = APFloat::getInf(APFloat::IEEEdouble(), false);
3354 APFloat f2(APFloat::IEEEdouble(), "1.0");
3355 EXPECT_EQ(f1.mod(f2), APFloat::opInvalidOp);
3356 EXPECT_TRUE(f1.isNaN());
3359 APFloat f1(APFloat::IEEEdouble(), "-4.0");
3360 APFloat f2(APFloat::IEEEdouble(), "-2.0");
3361 APFloat expected(APFloat::IEEEdouble(), "-0.0");
3362 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3363 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3366 APFloat f1(APFloat::IEEEdouble(), "-4.0");
3367 APFloat f2(APFloat::IEEEdouble(), "2.0");
3368 APFloat expected(APFloat::IEEEdouble(), "-0.0");
3369 EXPECT_EQ(f1.mod(f2), APFloat::opOK);
3370 EXPECT_TRUE(f1.bitwiseIsEqual(expected));
3374 TEST(APFloatTest, PPCDoubleDoubleAddSpecial) {
3375 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t,
3376 APFloat::fltCategory, APFloat::roundingMode>;
3377 DataType Data[] = {
3378 // (1 + 0) + (-1 + 0) = fcZero
3379 std::make_tuple(0x3ff0000000000000ull, 0, 0xbff0000000000000ull, 0,
3380 APFloat::fcZero, APFloat::rmNearestTiesToEven),
3381 // LDBL_MAX + (1.1 >> (1023 - 106) + 0)) = fcInfinity
3382 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3383 0x7948000000000000ull, 0ull, APFloat::fcInfinity,
3384 APFloat::rmNearestTiesToEven),
3385 // TODO: change the 4th 0x75effffffffffffe to 0x75efffffffffffff when
3386 // semPPCDoubleDoubleLegacy is gone.
3387 // LDBL_MAX + (1.011111... >> (1023 - 106) + (1.1111111...0 >> (1023 -
3388 // 160))) = fcNormal
3389 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3390 0x7947ffffffffffffull, 0x75effffffffffffeull,
3391 APFloat::fcNormal, APFloat::rmNearestTiesToEven),
3392 // LDBL_MAX + (1.1 >> (1023 - 106) + 0)) = fcInfinity
3393 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3394 0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3395 APFloat::fcInfinity, APFloat::rmNearestTiesToEven),
3396 // NaN + (1 + 0) = fcNaN
3397 std::make_tuple(0x7ff8000000000000ull, 0, 0x3ff0000000000000ull, 0,
3398 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
3401 for (auto Tp : Data) {
3402 uint64_t Op1[2], Op2[2];
3403 APFloat::fltCategory Expected;
3404 APFloat::roundingMode RM;
3405 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected, RM) = Tp;
3408 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3409 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3410 A1.add(A2, RM);
3412 EXPECT_EQ(Expected, A1.getCategory())
3413 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op1[0], Op1[1],
3414 Op2[0], Op2[1])
3415 .str();
3418 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3419 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3420 A2.add(A1, RM);
3422 EXPECT_EQ(Expected, A2.getCategory())
3423 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op2[0], Op2[1],
3424 Op1[0], Op1[1])
3425 .str();
3430 TEST(APFloatTest, PPCDoubleDoubleAdd) {
3431 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
3432 uint64_t, APFloat::roundingMode>;
3433 DataType Data[] = {
3434 // (1 + 0) + (1e-105 + 0) = (1 + 1e-105)
3435 std::make_tuple(0x3ff0000000000000ull, 0, 0x3960000000000000ull, 0,
3436 0x3ff0000000000000ull, 0x3960000000000000ull,
3437 APFloat::rmNearestTiesToEven),
3438 // (1 + 0) + (1e-106 + 0) = (1 + 1e-106)
3439 std::make_tuple(0x3ff0000000000000ull, 0, 0x3950000000000000ull, 0,
3440 0x3ff0000000000000ull, 0x3950000000000000ull,
3441 APFloat::rmNearestTiesToEven),
3442 // (1 + 1e-106) + (1e-106 + 0) = (1 + 1e-105)
3443 std::make_tuple(0x3ff0000000000000ull, 0x3950000000000000ull,
3444 0x3950000000000000ull, 0, 0x3ff0000000000000ull,
3445 0x3960000000000000ull, APFloat::rmNearestTiesToEven),
3446 // (1 + 0) + (epsilon + 0) = (1 + epsilon)
3447 std::make_tuple(0x3ff0000000000000ull, 0, 0x0000000000000001ull, 0,
3448 0x3ff0000000000000ull, 0x0000000000000001ull,
3449 APFloat::rmNearestTiesToEven),
3450 // TODO: change 0xf950000000000000 to 0xf940000000000000, when
3451 // semPPCDoubleDoubleLegacy is gone.
3452 // (DBL_MAX - 1 << (1023 - 105)) + (1 << (1023 - 53) + 0) = DBL_MAX +
3453 // 1.11111... << (1023 - 52)
3454 std::make_tuple(0x7fefffffffffffffull, 0xf950000000000000ull,
3455 0x7c90000000000000ull, 0, 0x7fefffffffffffffull,
3456 0x7c8ffffffffffffeull, APFloat::rmNearestTiesToEven),
3457 // TODO: change 0xf950000000000000 to 0xf940000000000000, when
3458 // semPPCDoubleDoubleLegacy is gone.
3459 // (1 << (1023 - 53) + 0) + (DBL_MAX - 1 << (1023 - 105)) = DBL_MAX +
3460 // 1.11111... << (1023 - 52)
3461 std::make_tuple(0x7c90000000000000ull, 0, 0x7fefffffffffffffull,
3462 0xf950000000000000ull, 0x7fefffffffffffffull,
3463 0x7c8ffffffffffffeull, APFloat::rmNearestTiesToEven),
3466 for (auto Tp : Data) {
3467 uint64_t Op1[2], Op2[2], Expected[2];
3468 APFloat::roundingMode RM;
3469 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
3472 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3473 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3474 A1.add(A2, RM);
3476 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3477 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op1[0], Op1[1],
3478 Op2[0], Op2[1])
3479 .str();
3480 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3481 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op1[0], Op1[1],
3482 Op2[0], Op2[1])
3483 .str();
3486 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3487 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3488 A2.add(A1, RM);
3490 EXPECT_EQ(Expected[0], A2.bitcastToAPInt().getRawData()[0])
3491 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op2[0], Op2[1],
3492 Op1[0], Op1[1])
3493 .str();
3494 EXPECT_EQ(Expected[1], A2.bitcastToAPInt().getRawData()[1])
3495 << formatv("({0:x} + {1:x}) + ({2:x} + {3:x})", Op2[0], Op2[1],
3496 Op1[0], Op1[1])
3497 .str();
3502 TEST(APFloatTest, PPCDoubleDoubleSubtract) {
3503 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
3504 uint64_t, APFloat::roundingMode>;
3505 DataType Data[] = {
3506 // (1 + 0) - (-1e-105 + 0) = (1 + 1e-105)
3507 std::make_tuple(0x3ff0000000000000ull, 0, 0xb960000000000000ull, 0,
3508 0x3ff0000000000000ull, 0x3960000000000000ull,
3509 APFloat::rmNearestTiesToEven),
3510 // (1 + 0) - (-1e-106 + 0) = (1 + 1e-106)
3511 std::make_tuple(0x3ff0000000000000ull, 0, 0xb950000000000000ull, 0,
3512 0x3ff0000000000000ull, 0x3950000000000000ull,
3513 APFloat::rmNearestTiesToEven),
3516 for (auto Tp : Data) {
3517 uint64_t Op1[2], Op2[2], Expected[2];
3518 APFloat::roundingMode RM;
3519 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
3521 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3522 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3523 A1.subtract(A2, RM);
3525 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3526 << formatv("({0:x} + {1:x}) - ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
3527 Op2[1])
3528 .str();
3529 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3530 << formatv("({0:x} + {1:x}) - ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
3531 Op2[1])
3532 .str();
3536 TEST(APFloatTest, PPCDoubleDoubleMultiplySpecial) {
3537 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t,
3538 APFloat::fltCategory, APFloat::roundingMode>;
3539 DataType Data[] = {
3540 // fcNaN * fcNaN = fcNaN
3541 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull, 0,
3542 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
3543 // fcNaN * fcZero = fcNaN
3544 std::make_tuple(0x7ff8000000000000ull, 0, 0, 0, APFloat::fcNaN,
3545 APFloat::rmNearestTiesToEven),
3546 // fcNaN * fcInfinity = fcNaN
3547 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff0000000000000ull, 0,
3548 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
3549 // fcNaN * fcNormal = fcNaN
3550 std::make_tuple(0x7ff8000000000000ull, 0, 0x3ff0000000000000ull, 0,
3551 APFloat::fcNaN, APFloat::rmNearestTiesToEven),
3552 // fcInfinity * fcInfinity = fcInfinity
3553 std::make_tuple(0x7ff0000000000000ull, 0, 0x7ff0000000000000ull, 0,
3554 APFloat::fcInfinity, APFloat::rmNearestTiesToEven),
3555 // fcInfinity * fcZero = fcNaN
3556 std::make_tuple(0x7ff0000000000000ull, 0, 0, 0, APFloat::fcNaN,
3557 APFloat::rmNearestTiesToEven),
3558 // fcInfinity * fcNormal = fcInfinity
3559 std::make_tuple(0x7ff0000000000000ull, 0, 0x3ff0000000000000ull, 0,
3560 APFloat::fcInfinity, APFloat::rmNearestTiesToEven),
3561 // fcZero * fcZero = fcZero
3562 std::make_tuple(0, 0, 0, 0, APFloat::fcZero,
3563 APFloat::rmNearestTiesToEven),
3564 // fcZero * fcNormal = fcZero
3565 std::make_tuple(0, 0, 0x3ff0000000000000ull, 0, APFloat::fcZero,
3566 APFloat::rmNearestTiesToEven),
3569 for (auto Tp : Data) {
3570 uint64_t Op1[2], Op2[2];
3571 APFloat::fltCategory Expected;
3572 APFloat::roundingMode RM;
3573 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected, RM) = Tp;
3576 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3577 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3578 A1.multiply(A2, RM);
3580 EXPECT_EQ(Expected, A1.getCategory())
3581 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op1[0], Op1[1],
3582 Op2[0], Op2[1])
3583 .str();
3586 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3587 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3588 A2.multiply(A1, RM);
3590 EXPECT_EQ(Expected, A2.getCategory())
3591 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op2[0], Op2[1],
3592 Op1[0], Op1[1])
3593 .str();
3598 TEST(APFloatTest, PPCDoubleDoubleMultiply) {
3599 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
3600 uint64_t, APFloat::roundingMode>;
3601 DataType Data[] = {
3602 // 1/3 * 3 = 1.0
3603 std::make_tuple(0x3fd5555555555555ull, 0x3c75555555555556ull,
3604 0x4008000000000000ull, 0, 0x3ff0000000000000ull, 0,
3605 APFloat::rmNearestTiesToEven),
3606 // (1 + epsilon) * (1 + 0) = fcZero
3607 std::make_tuple(0x3ff0000000000000ull, 0x0000000000000001ull,
3608 0x3ff0000000000000ull, 0, 0x3ff0000000000000ull,
3609 0x0000000000000001ull, APFloat::rmNearestTiesToEven),
3610 // (1 + epsilon) * (1 + epsilon) = 1 + 2 * epsilon
3611 std::make_tuple(0x3ff0000000000000ull, 0x0000000000000001ull,
3612 0x3ff0000000000000ull, 0x0000000000000001ull,
3613 0x3ff0000000000000ull, 0x0000000000000002ull,
3614 APFloat::rmNearestTiesToEven),
3615 // -(1 + epsilon) * (1 + epsilon) = -1
3616 std::make_tuple(0xbff0000000000000ull, 0x0000000000000001ull,
3617 0x3ff0000000000000ull, 0x0000000000000001ull,
3618 0xbff0000000000000ull, 0, APFloat::rmNearestTiesToEven),
3619 // (0.5 + 0) * (1 + 2 * epsilon) = 0.5 + epsilon
3620 std::make_tuple(0x3fe0000000000000ull, 0, 0x3ff0000000000000ull,
3621 0x0000000000000002ull, 0x3fe0000000000000ull,
3622 0x0000000000000001ull, APFloat::rmNearestTiesToEven),
3623 // (0.5 + 0) * (1 + epsilon) = 0.5
3624 std::make_tuple(0x3fe0000000000000ull, 0, 0x3ff0000000000000ull,
3625 0x0000000000000001ull, 0x3fe0000000000000ull, 0,
3626 APFloat::rmNearestTiesToEven),
3627 // __LDBL_MAX__ * (1 + 1 << 106) = inf
3628 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3629 0x3ff0000000000000ull, 0x3950000000000000ull,
3630 0x7ff0000000000000ull, 0, APFloat::rmNearestTiesToEven),
3631 // __LDBL_MAX__ * (1 + 1 << 107) > __LDBL_MAX__, but not inf, yes =_=|||
3632 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3633 0x3ff0000000000000ull, 0x3940000000000000ull,
3634 0x7fefffffffffffffull, 0x7c8fffffffffffffull,
3635 APFloat::rmNearestTiesToEven),
3636 // __LDBL_MAX__ * (1 + 1 << 108) = __LDBL_MAX__
3637 std::make_tuple(0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3638 0x3ff0000000000000ull, 0x3930000000000000ull,
3639 0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3640 APFloat::rmNearestTiesToEven),
3643 for (auto Tp : Data) {
3644 uint64_t Op1[2], Op2[2], Expected[2];
3645 APFloat::roundingMode RM;
3646 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
3649 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3650 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3651 A1.multiply(A2, RM);
3653 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3654 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op1[0], Op1[1],
3655 Op2[0], Op2[1])
3656 .str();
3657 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3658 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op1[0], Op1[1],
3659 Op2[0], Op2[1])
3660 .str();
3663 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3664 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3665 A2.multiply(A1, RM);
3667 EXPECT_EQ(Expected[0], A2.bitcastToAPInt().getRawData()[0])
3668 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op2[0], Op2[1],
3669 Op1[0], Op1[1])
3670 .str();
3671 EXPECT_EQ(Expected[1], A2.bitcastToAPInt().getRawData()[1])
3672 << formatv("({0:x} + {1:x}) * ({2:x} + {3:x})", Op2[0], Op2[1],
3673 Op1[0], Op1[1])
3674 .str();
3679 TEST(APFloatTest, PPCDoubleDoubleDivide) {
3680 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t,
3681 uint64_t, APFloat::roundingMode>;
3682 // TODO: Only a sanity check for now. Add more edge cases when the
3683 // double-double algorithm is implemented.
3684 DataType Data[] = {
3685 // 1 / 3 = 1/3
3686 std::make_tuple(0x3ff0000000000000ull, 0, 0x4008000000000000ull, 0,
3687 0x3fd5555555555555ull, 0x3c75555555555556ull,
3688 APFloat::rmNearestTiesToEven),
3691 for (auto Tp : Data) {
3692 uint64_t Op1[2], Op2[2], Expected[2];
3693 APFloat::roundingMode RM;
3694 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1], RM) = Tp;
3696 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3697 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3698 A1.divide(A2, RM);
3700 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3701 << formatv("({0:x} + {1:x}) / ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
3702 Op2[1])
3703 .str();
3704 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3705 << formatv("({0:x} + {1:x}) / ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
3706 Op2[1])
3707 .str();
3711 TEST(APFloatTest, PPCDoubleDoubleRemainder) {
3712 using DataType =
3713 std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>;
3714 DataType Data[] = {
3715 // remainder(3.0 + 3.0 << 53, 1.25 + 1.25 << 53) = (0.5 + 0.5 << 53)
3716 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
3717 0x3ff4000000000000ull, 0x3ca4000000000000ull,
3718 0x3fe0000000000000ull, 0x3c90000000000000ull),
3719 // remainder(3.0 + 3.0 << 53, 1.75 + 1.75 << 53) = (-0.5 - 0.5 << 53)
3720 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
3721 0x3ffc000000000000ull, 0x3cac000000000000ull,
3722 0xbfe0000000000000ull, 0xbc90000000000000ull),
3725 for (auto Tp : Data) {
3726 uint64_t Op1[2], Op2[2], Expected[2];
3727 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1]) = Tp;
3729 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3730 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3731 A1.remainder(A2);
3733 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3734 << formatv("remainder({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
3735 Op2[0], Op2[1])
3736 .str();
3737 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3738 << formatv("remainder(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0],
3739 Op1[1], Op2[0], Op2[1])
3740 .str();
3744 TEST(APFloatTest, PPCDoubleDoubleMod) {
3745 using DataType =
3746 std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t>;
3747 DataType Data[] = {
3748 // mod(3.0 + 3.0 << 53, 1.25 + 1.25 << 53) = (0.5 + 0.5 << 53)
3749 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
3750 0x3ff4000000000000ull, 0x3ca4000000000000ull,
3751 0x3fe0000000000000ull, 0x3c90000000000000ull),
3752 // mod(3.0 + 3.0 << 53, 1.75 + 1.75 << 53) = (1.25 + 1.25 << 53)
3753 // 0xbc98000000000000 doesn't seem right, but it's what we currently have.
3754 // TODO: investigate
3755 std::make_tuple(0x4008000000000000ull, 0x3cb8000000000000ull,
3756 0x3ffc000000000000ull, 0x3cac000000000000ull,
3757 0x3ff4000000000001ull, 0xbc98000000000000ull),
3760 for (auto Tp : Data) {
3761 uint64_t Op1[2], Op2[2], Expected[2];
3762 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected[0], Expected[1]) = Tp;
3764 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3765 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3766 A1.mod(A2);
3768 EXPECT_EQ(Expected[0], A1.bitcastToAPInt().getRawData()[0])
3769 << formatv("fmod(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
3770 Op2[0], Op2[1])
3771 .str();
3772 EXPECT_EQ(Expected[1], A1.bitcastToAPInt().getRawData()[1])
3773 << formatv("fmod(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
3774 Op2[0], Op2[1])
3775 .str();
3779 TEST(APFloatTest, PPCDoubleDoubleFMA) {
3780 // Sanity check for now.
3781 APFloat A(APFloat::PPCDoubleDouble(), "2");
3782 A.fusedMultiplyAdd(APFloat(APFloat::PPCDoubleDouble(), "3"),
3783 APFloat(APFloat::PPCDoubleDouble(), "4"),
3784 APFloat::rmNearestTiesToEven);
3785 EXPECT_EQ(APFloat::cmpEqual,
3786 APFloat(APFloat::PPCDoubleDouble(), "10").compare(A));
3789 TEST(APFloatTest, PPCDoubleDoubleRoundToIntegral) {
3791 APFloat A(APFloat::PPCDoubleDouble(), "1.5");
3792 A.roundToIntegral(APFloat::rmNearestTiesToEven);
3793 EXPECT_EQ(APFloat::cmpEqual,
3794 APFloat(APFloat::PPCDoubleDouble(), "2").compare(A));
3797 APFloat A(APFloat::PPCDoubleDouble(), "2.5");
3798 A.roundToIntegral(APFloat::rmNearestTiesToEven);
3799 EXPECT_EQ(APFloat::cmpEqual,
3800 APFloat(APFloat::PPCDoubleDouble(), "2").compare(A));
3804 TEST(APFloatTest, PPCDoubleDoubleCompare) {
3805 using DataType =
3806 std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, APFloat::cmpResult>;
3808 DataType Data[] = {
3809 // (1 + 0) = (1 + 0)
3810 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000000ull, 0,
3811 APFloat::cmpEqual),
3812 // (1 + 0) < (1.00...1 + 0)
3813 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000001ull, 0,
3814 APFloat::cmpLessThan),
3815 // (1.00...1 + 0) > (1 + 0)
3816 std::make_tuple(0x3ff0000000000001ull, 0, 0x3ff0000000000000ull, 0,
3817 APFloat::cmpGreaterThan),
3818 // (1 + 0) < (1 + epsilon)
3819 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000001ull,
3820 0x0000000000000001ull, APFloat::cmpLessThan),
3821 // NaN != NaN
3822 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull, 0,
3823 APFloat::cmpUnordered),
3824 // (1 + 0) != NaN
3825 std::make_tuple(0x3ff0000000000000ull, 0, 0x7ff8000000000000ull, 0,
3826 APFloat::cmpUnordered),
3827 // Inf = Inf
3828 std::make_tuple(0x7ff0000000000000ull, 0, 0x7ff0000000000000ull, 0,
3829 APFloat::cmpEqual),
3832 for (auto Tp : Data) {
3833 uint64_t Op1[2], Op2[2];
3834 APFloat::cmpResult Expected;
3835 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected) = Tp;
3837 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3838 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3839 EXPECT_EQ(Expected, A1.compare(A2))
3840 << formatv("compare(({0:x} + {1:x}), ({2:x} + {3:x}))", Op1[0], Op1[1],
3841 Op2[0], Op2[1])
3842 .str();
3846 TEST(APFloatTest, PPCDoubleDoubleBitwiseIsEqual) {
3847 using DataType = std::tuple<uint64_t, uint64_t, uint64_t, uint64_t, bool>;
3849 DataType Data[] = {
3850 // (1 + 0) = (1 + 0)
3851 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000000ull, 0, true),
3852 // (1 + 0) != (1.00...1 + 0)
3853 std::make_tuple(0x3ff0000000000000ull, 0, 0x3ff0000000000001ull, 0,
3854 false),
3855 // NaN = NaN
3856 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull, 0, true),
3857 // NaN != NaN with a different bit pattern
3858 std::make_tuple(0x7ff8000000000000ull, 0, 0x7ff8000000000000ull,
3859 0x3ff0000000000000ull, false),
3860 // Inf = Inf
3861 std::make_tuple(0x7ff0000000000000ull, 0, 0x7ff0000000000000ull, 0, true),
3864 for (auto Tp : Data) {
3865 uint64_t Op1[2], Op2[2];
3866 bool Expected;
3867 std::tie(Op1[0], Op1[1], Op2[0], Op2[1], Expected) = Tp;
3869 APFloat A1(APFloat::PPCDoubleDouble(), APInt(128, 2, Op1));
3870 APFloat A2(APFloat::PPCDoubleDouble(), APInt(128, 2, Op2));
3871 EXPECT_EQ(Expected, A1.bitwiseIsEqual(A2))
3872 << formatv("({0:x} + {1:x}) = ({2:x} + {3:x})", Op1[0], Op1[1], Op2[0],
3873 Op2[1])
3874 .str();
3878 TEST(APFloatTest, PPCDoubleDoubleHashValue) {
3879 uint64_t Data1[] = {0x3ff0000000000001ull, 0x0000000000000001ull};
3880 uint64_t Data2[] = {0x3ff0000000000001ull, 0};
3881 // The hash values are *hopefully* different.
3882 EXPECT_NE(
3883 hash_value(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Data1))),
3884 hash_value(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Data2))));
3887 TEST(APFloatTest, PPCDoubleDoubleChangeSign) {
3888 uint64_t Data[] = {
3889 0x400f000000000000ull, 0xbcb0000000000000ull,
3891 APFloat Float(APFloat::PPCDoubleDouble(), APInt(128, 2, Data));
3893 APFloat Actual =
3894 APFloat::copySign(Float, APFloat(APFloat::IEEEdouble(), "1"));
3895 EXPECT_EQ(0x400f000000000000ull, Actual.bitcastToAPInt().getRawData()[0]);
3896 EXPECT_EQ(0xbcb0000000000000ull, Actual.bitcastToAPInt().getRawData()[1]);
3899 APFloat Actual =
3900 APFloat::copySign(Float, APFloat(APFloat::IEEEdouble(), "-1"));
3901 EXPECT_EQ(0xc00f000000000000ull, Actual.bitcastToAPInt().getRawData()[0]);
3902 EXPECT_EQ(0x3cb0000000000000ull, Actual.bitcastToAPInt().getRawData()[1]);
3906 TEST(APFloatTest, PPCDoubleDoubleFactories) {
3908 uint64_t Data[] = {
3909 0, 0,
3911 EXPECT_EQ(APInt(128, 2, Data),
3912 APFloat::getZero(APFloat::PPCDoubleDouble()).bitcastToAPInt());
3915 uint64_t Data[] = {
3916 0x7fefffffffffffffull, 0x7c8ffffffffffffeull,
3918 EXPECT_EQ(APInt(128, 2, Data),
3919 APFloat::getLargest(APFloat::PPCDoubleDouble()).bitcastToAPInt());
3922 uint64_t Data[] = {
3923 0x0000000000000001ull, 0,
3925 EXPECT_EQ(
3926 APInt(128, 2, Data),
3927 APFloat::getSmallest(APFloat::PPCDoubleDouble()).bitcastToAPInt());
3930 uint64_t Data[] = {0x0360000000000000ull, 0};
3931 EXPECT_EQ(APInt(128, 2, Data),
3932 APFloat::getSmallestNormalized(APFloat::PPCDoubleDouble())
3933 .bitcastToAPInt());
3936 uint64_t Data[] = {
3937 0x8000000000000000ull, 0x0000000000000000ull,
3939 EXPECT_EQ(
3940 APInt(128, 2, Data),
3941 APFloat::getZero(APFloat::PPCDoubleDouble(), true).bitcastToAPInt());
3944 uint64_t Data[] = {
3945 0xffefffffffffffffull, 0xfc8ffffffffffffeull,
3947 EXPECT_EQ(
3948 APInt(128, 2, Data),
3949 APFloat::getLargest(APFloat::PPCDoubleDouble(), true).bitcastToAPInt());
3952 uint64_t Data[] = {
3953 0x8000000000000001ull, 0x0000000000000000ull,
3955 EXPECT_EQ(APInt(128, 2, Data),
3956 APFloat::getSmallest(APFloat::PPCDoubleDouble(), true)
3957 .bitcastToAPInt());
3960 uint64_t Data[] = {
3961 0x8360000000000000ull, 0x0000000000000000ull,
3963 EXPECT_EQ(APInt(128, 2, Data),
3964 APFloat::getSmallestNormalized(APFloat::PPCDoubleDouble(), true)
3965 .bitcastToAPInt());
3967 EXPECT_TRUE(APFloat::getSmallest(APFloat::PPCDoubleDouble()).isSmallest());
3968 EXPECT_TRUE(APFloat::getLargest(APFloat::PPCDoubleDouble()).isLargest());
3971 TEST(APFloatTest, PPCDoubleDoubleIsDenormal) {
3972 EXPECT_TRUE(APFloat::getSmallest(APFloat::PPCDoubleDouble()).isDenormal());
3973 EXPECT_FALSE(APFloat::getLargest(APFloat::PPCDoubleDouble()).isDenormal());
3974 EXPECT_FALSE(
3975 APFloat::getSmallestNormalized(APFloat::PPCDoubleDouble()).isDenormal());
3977 // (4 + 3) is not normalized
3978 uint64_t Data[] = {
3979 0x4010000000000000ull, 0x4008000000000000ull,
3981 EXPECT_TRUE(
3982 APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Data)).isDenormal());
3986 TEST(APFloatTest, PPCDoubleDoubleScalbn) {
3987 // 3.0 + 3.0 << 53
3988 uint64_t Input[] = {
3989 0x4008000000000000ull, 0x3cb8000000000000ull,
3991 APFloat Result =
3992 scalbn(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Input)), 1,
3993 APFloat::rmNearestTiesToEven);
3994 // 6.0 + 6.0 << 53
3995 EXPECT_EQ(0x4018000000000000ull, Result.bitcastToAPInt().getRawData()[0]);
3996 EXPECT_EQ(0x3cc8000000000000ull, Result.bitcastToAPInt().getRawData()[1]);
3999 TEST(APFloatTest, PPCDoubleDoubleFrexp) {
4000 // 3.0 + 3.0 << 53
4001 uint64_t Input[] = {
4002 0x4008000000000000ull, 0x3cb8000000000000ull,
4004 int Exp;
4005 // 0.75 + 0.75 << 53
4006 APFloat Result =
4007 frexp(APFloat(APFloat::PPCDoubleDouble(), APInt(128, 2, Input)), Exp,
4008 APFloat::rmNearestTiesToEven);
4009 EXPECT_EQ(2, Exp);
4010 EXPECT_EQ(0x3fe8000000000000ull, Result.bitcastToAPInt().getRawData()[0]);
4011 EXPECT_EQ(0x3c98000000000000ull, Result.bitcastToAPInt().getRawData()[1]);