• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/unittest/ADT/APFloat.cpp - APFloat unit tests ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/ADT/APFloat.h"
11 #include "llvm/ADT/APSInt.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "gtest/gtest.h"
15 #include <cmath>
16 #include <ostream>
17 #include <string>
18 
19 using namespace llvm;
20 
convertToDoubleFromString(const char * Str)21 static double convertToDoubleFromString(const char *Str) {
22   llvm::APFloat F(0.0);
23   F.convertFromString(Str, llvm::APFloat::rmNearestTiesToEven);
24   return F.convertToDouble();
25 }
26 
convertToString(double d,unsigned Prec,unsigned Pad)27 static std::string convertToString(double d, unsigned Prec, unsigned Pad) {
28   llvm::SmallVector<char, 100> Buffer;
29   llvm::APFloat F(d);
30   F.toString(Buffer, Prec, Pad);
31   return std::string(Buffer.data(), Buffer.size());
32 }
33 
34 namespace {
35 
TEST(APFloatTest,isSignaling)36 TEST(APFloatTest, isSignaling) {
37   // We test qNaN, -qNaN, +sNaN, -sNaN with and without payloads. *NOTE* The
38   // positive/negative distinction is included only since the getQNaN/getSNaN
39   // API provides the option.
40   APInt payload = APInt::getOneBitSet(4, 2);
41   EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle, false).isSignaling());
42   EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle, true).isSignaling());
43   EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle, false, &payload).isSignaling());
44   EXPECT_FALSE(APFloat::getQNaN(APFloat::IEEEsingle, true, &payload).isSignaling());
45   EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, false).isSignaling());
46   EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, true).isSignaling());
47   EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, false, &payload).isSignaling());
48   EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, true, &payload).isSignaling());
49 }
50 
TEST(APFloatTest,next)51 TEST(APFloatTest, next) {
52 
53   APFloat test(APFloat::IEEEquad, APFloat::uninitialized);
54   APFloat expected(APFloat::IEEEquad, APFloat::uninitialized);
55 
56   // 1. Test Special Cases Values.
57   //
58   // Test all special values for nextUp and nextDown perscribed by IEEE-754R
59   // 2008. These are:
60   //   1.  +inf
61   //   2.  -inf
62   //   3.  getLargest()
63   //   4.  -getLargest()
64   //   5.  getSmallest()
65   //   6.  -getSmallest()
66   //   7.  qNaN
67   //   8.  sNaN
68   //   9.  +0
69   //   10. -0
70 
71   // nextUp(+inf) = +inf.
72   test = APFloat::getInf(APFloat::IEEEquad, false);
73   expected = APFloat::getInf(APFloat::IEEEquad, false);
74   EXPECT_EQ(test.next(false), APFloat::opOK);
75   EXPECT_TRUE(test.isInfinity());
76   EXPECT_TRUE(!test.isNegative());
77   EXPECT_TRUE(test.bitwiseIsEqual(expected));
78 
79   // nextDown(+inf) = -nextUp(-inf) = -(-getLargest()) = getLargest()
80   test = APFloat::getInf(APFloat::IEEEquad, false);
81   expected = APFloat::getLargest(APFloat::IEEEquad, false);
82   EXPECT_EQ(test.next(true), APFloat::opOK);
83   EXPECT_TRUE(!test.isNegative());
84   EXPECT_TRUE(test.bitwiseIsEqual(expected));
85 
86   // nextUp(-inf) = -getLargest()
87   test = APFloat::getInf(APFloat::IEEEquad, true);
88   expected = APFloat::getLargest(APFloat::IEEEquad, true);
89   EXPECT_EQ(test.next(false), APFloat::opOK);
90   EXPECT_TRUE(test.isNegative());
91   EXPECT_TRUE(test.bitwiseIsEqual(expected));
92 
93   // nextDown(-inf) = -nextUp(+inf) = -(+inf) = -inf.
94   test = APFloat::getInf(APFloat::IEEEquad, true);
95   expected = APFloat::getInf(APFloat::IEEEquad, true);
96   EXPECT_EQ(test.next(true), APFloat::opOK);
97   EXPECT_TRUE(test.isInfinity() && test.isNegative());
98   EXPECT_TRUE(test.bitwiseIsEqual(expected));
99 
100   // nextUp(getLargest()) = +inf
101   test = APFloat::getLargest(APFloat::IEEEquad, false);
102   expected = APFloat::getInf(APFloat::IEEEquad, false);
103   EXPECT_EQ(test.next(false), APFloat::opOK);
104   EXPECT_TRUE(test.isInfinity() && !test.isNegative());
105   EXPECT_TRUE(test.bitwiseIsEqual(expected));
106 
107   // nextDown(getLargest()) = -nextUp(-getLargest())
108   //                        = -(-getLargest() + inc)
109   //                        = getLargest() - inc.
110   test = APFloat::getLargest(APFloat::IEEEquad, false);
111   expected = APFloat(APFloat::IEEEquad,
112                      "0x1.fffffffffffffffffffffffffffep+16383");
113   EXPECT_EQ(test.next(true), APFloat::opOK);
114   EXPECT_TRUE(!test.isInfinity() && !test.isNegative());
115   EXPECT_TRUE(test.bitwiseIsEqual(expected));
116 
117   // nextUp(-getLargest()) = -getLargest() + inc.
118   test = APFloat::getLargest(APFloat::IEEEquad, true);
119   expected = APFloat(APFloat::IEEEquad,
120                      "-0x1.fffffffffffffffffffffffffffep+16383");
121   EXPECT_EQ(test.next(false), APFloat::opOK);
122   EXPECT_TRUE(test.bitwiseIsEqual(expected));
123 
124   // nextDown(-getLargest()) = -nextUp(getLargest()) = -(inf) = -inf.
125   test = APFloat::getLargest(APFloat::IEEEquad, true);
126   expected = APFloat::getInf(APFloat::IEEEquad, true);
127   EXPECT_EQ(test.next(true), APFloat::opOK);
128   EXPECT_TRUE(test.isInfinity() && test.isNegative());
129   EXPECT_TRUE(test.bitwiseIsEqual(expected));
130 
131   // nextUp(getSmallest()) = getSmallest() + inc.
132   test = APFloat(APFloat::IEEEquad, "0x0.0000000000000000000000000001p-16382");
133   expected = APFloat(APFloat::IEEEquad,
134                      "0x0.0000000000000000000000000002p-16382");
135   EXPECT_EQ(test.next(false), APFloat::opOK);
136   EXPECT_TRUE(test.bitwiseIsEqual(expected));
137 
138   // nextDown(getSmallest()) = -nextUp(-getSmallest()) = -(-0) = +0.
139   test = APFloat(APFloat::IEEEquad, "0x0.0000000000000000000000000001p-16382");
140   expected = APFloat::getZero(APFloat::IEEEquad, false);
141   EXPECT_EQ(test.next(true), APFloat::opOK);
142   EXPECT_TRUE(test.isPosZero());
143   EXPECT_TRUE(test.bitwiseIsEqual(expected));
144 
145   // nextUp(-getSmallest()) = -0.
146   test = APFloat(APFloat::IEEEquad, "-0x0.0000000000000000000000000001p-16382");
147   expected = APFloat::getZero(APFloat::IEEEquad, true);
148   EXPECT_EQ(test.next(false), APFloat::opOK);
149   EXPECT_TRUE(test.isNegZero());
150   EXPECT_TRUE(test.bitwiseIsEqual(expected));
151 
152   // nextDown(-getSmallest()) = -nextUp(getSmallest()) = -getSmallest() - inc.
153   test = APFloat(APFloat::IEEEquad, "-0x0.0000000000000000000000000001p-16382");
154   expected = APFloat(APFloat::IEEEquad,
155                      "-0x0.0000000000000000000000000002p-16382");
156   EXPECT_EQ(test.next(true), APFloat::opOK);
157   EXPECT_TRUE(test.bitwiseIsEqual(expected));
158 
159   // nextUp(qNaN) = qNaN
160   test = APFloat::getQNaN(APFloat::IEEEquad, false);
161   expected = APFloat::getQNaN(APFloat::IEEEquad, false);
162   EXPECT_EQ(test.next(false), APFloat::opOK);
163   EXPECT_TRUE(test.bitwiseIsEqual(expected));
164 
165   // nextDown(qNaN) = qNaN
166   test = APFloat::getQNaN(APFloat::IEEEquad, false);
167   expected = APFloat::getQNaN(APFloat::IEEEquad, false);
168   EXPECT_EQ(test.next(true), APFloat::opOK);
169   EXPECT_TRUE(test.bitwiseIsEqual(expected));
170 
171   // nextUp(sNaN) = qNaN
172   test = APFloat::getSNaN(APFloat::IEEEquad, false);
173   expected = APFloat::getQNaN(APFloat::IEEEquad, false);
174   EXPECT_EQ(test.next(false), APFloat::opInvalidOp);
175   EXPECT_TRUE(test.bitwiseIsEqual(expected));
176 
177   // nextDown(sNaN) = qNaN
178   test = APFloat::getSNaN(APFloat::IEEEquad, false);
179   expected = APFloat::getQNaN(APFloat::IEEEquad, false);
180   EXPECT_EQ(test.next(true), APFloat::opInvalidOp);
181   EXPECT_TRUE(test.bitwiseIsEqual(expected));
182 
183   // nextUp(+0) = +getSmallest()
184   test = APFloat::getZero(APFloat::IEEEquad, false);
185   expected = APFloat::getSmallest(APFloat::IEEEquad, false);
186   EXPECT_EQ(test.next(false), APFloat::opOK);
187   EXPECT_TRUE(test.bitwiseIsEqual(expected));
188 
189   // nextDown(+0) = -nextUp(-0) = -getSmallest()
190   test = APFloat::getZero(APFloat::IEEEquad, false);
191   expected = APFloat::getSmallest(APFloat::IEEEquad, true);
192   EXPECT_EQ(test.next(true), APFloat::opOK);
193   EXPECT_TRUE(test.bitwiseIsEqual(expected));
194 
195   // nextUp(-0) = +getSmallest()
196   test = APFloat::getZero(APFloat::IEEEquad, true);
197   expected = APFloat::getSmallest(APFloat::IEEEquad, false);
198   EXPECT_EQ(test.next(false), APFloat::opOK);
199   EXPECT_TRUE(test.bitwiseIsEqual(expected));
200 
201   // nextDown(-0) = -nextUp(0) = -getSmallest()
202   test = APFloat::getZero(APFloat::IEEEquad, true);
203   expected = APFloat::getSmallest(APFloat::IEEEquad, true);
204   EXPECT_EQ(test.next(true), APFloat::opOK);
205   EXPECT_TRUE(test.bitwiseIsEqual(expected));
206 
207   // 2. Binade Boundary Tests.
208 
209   // 2a. Test denormal <-> normal binade boundaries.
210   //     * nextUp(+Largest Denormal) -> +Smallest Normal.
211   //     * nextDown(-Largest Denormal) -> -Smallest Normal.
212   //     * nextUp(-Smallest Normal) -> -Largest Denormal.
213   //     * nextDown(+Smallest Normal) -> +Largest Denormal.
214 
215   // nextUp(+Largest Denormal) -> +Smallest Normal.
216   test = APFloat(APFloat::IEEEquad, "0x0.ffffffffffffffffffffffffffffp-16382");
217   expected = APFloat(APFloat::IEEEquad,
218                      "0x1.0000000000000000000000000000p-16382");
219   EXPECT_EQ(test.next(false), APFloat::opOK);
220   EXPECT_FALSE(test.isDenormal());
221   EXPECT_TRUE(test.bitwiseIsEqual(expected));
222 
223   // nextDown(-Largest Denormal) -> -Smallest Normal.
224   test = APFloat(APFloat::IEEEquad,
225                  "-0x0.ffffffffffffffffffffffffffffp-16382");
226   expected = APFloat(APFloat::IEEEquad,
227                      "-0x1.0000000000000000000000000000p-16382");
228   EXPECT_EQ(test.next(true), APFloat::opOK);
229   EXPECT_FALSE(test.isDenormal());
230   EXPECT_TRUE(test.bitwiseIsEqual(expected));
231 
232   // nextUp(-Smallest Normal) -> -LargestDenormal.
233   test = APFloat(APFloat::IEEEquad,
234                  "-0x1.0000000000000000000000000000p-16382");
235   expected = APFloat(APFloat::IEEEquad,
236                      "-0x0.ffffffffffffffffffffffffffffp-16382");
237   EXPECT_EQ(test.next(false), APFloat::opOK);
238   EXPECT_TRUE(test.isDenormal());
239   EXPECT_TRUE(test.bitwiseIsEqual(expected));
240 
241   // nextDown(+Smallest Normal) -> +Largest Denormal.
242   test = APFloat(APFloat::IEEEquad,
243                  "+0x1.0000000000000000000000000000p-16382");
244   expected = APFloat(APFloat::IEEEquad,
245                      "+0x0.ffffffffffffffffffffffffffffp-16382");
246   EXPECT_EQ(test.next(true), APFloat::opOK);
247   EXPECT_TRUE(test.isDenormal());
248   EXPECT_TRUE(test.bitwiseIsEqual(expected));
249 
250   // 2b. Test normal <-> normal binade boundaries.
251   //     * nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
252   //     * nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
253   //     * nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
254   //     * nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
255 
256   // nextUp(-Normal Binade Boundary) -> -Normal Binade Boundary + 1.
257   test = APFloat(APFloat::IEEEquad, "-0x1p+1");
258   expected = APFloat(APFloat::IEEEquad,
259                      "-0x1.ffffffffffffffffffffffffffffp+0");
260   EXPECT_EQ(test.next(false), APFloat::opOK);
261   EXPECT_TRUE(test.bitwiseIsEqual(expected));
262 
263   // nextDown(+Normal Binade Boundary) -> +Normal Binade Boundary - 1.
264   test = APFloat(APFloat::IEEEquad, "0x1p+1");
265   expected = APFloat(APFloat::IEEEquad, "0x1.ffffffffffffffffffffffffffffp+0");
266   EXPECT_EQ(test.next(true), APFloat::opOK);
267   EXPECT_TRUE(test.bitwiseIsEqual(expected));
268 
269   // nextUp(+Normal Binade Boundary - 1) -> +Normal Binade Boundary.
270   test = APFloat(APFloat::IEEEquad, "0x1.ffffffffffffffffffffffffffffp+0");
271   expected = APFloat(APFloat::IEEEquad, "0x1p+1");
272   EXPECT_EQ(test.next(false), APFloat::opOK);
273   EXPECT_TRUE(test.bitwiseIsEqual(expected));
274 
275   // nextDown(-Normal Binade Boundary + 1) -> -Normal Binade Boundary.
276   test = APFloat(APFloat::IEEEquad, "-0x1.ffffffffffffffffffffffffffffp+0");
277   expected = APFloat(APFloat::IEEEquad, "-0x1p+1");
278   EXPECT_EQ(test.next(true), APFloat::opOK);
279   EXPECT_TRUE(test.bitwiseIsEqual(expected));
280 
281   // 2c. Test using next at binade boundaries with a direction away from the
282   // binade boundary. Away from denormal <-> normal boundaries.
283   //
284   // This is to make sure that even though we are at a binade boundary, since
285   // we are rounding away, we do not trigger the binade boundary code. Thus we
286   // test:
287   //   * nextUp(-Largest Denormal) -> -Largest Denormal + inc.
288   //   * nextDown(+Largest Denormal) -> +Largest Denormal - inc.
289   //   * nextUp(+Smallest Normal) -> +Smallest Normal + inc.
290   //   * nextDown(-Smallest Normal) -> -Smallest Normal - inc.
291 
292   // nextUp(-Largest Denormal) -> -Largest Denormal + inc.
293   test = APFloat(APFloat::IEEEquad, "-0x0.ffffffffffffffffffffffffffffp-16382");
294   expected = APFloat(APFloat::IEEEquad,
295                      "-0x0.fffffffffffffffffffffffffffep-16382");
296   EXPECT_EQ(test.next(false), APFloat::opOK);
297   EXPECT_TRUE(test.isDenormal());
298   EXPECT_TRUE(test.isNegative());
299   EXPECT_TRUE(test.bitwiseIsEqual(expected));
300 
301   // nextDown(+Largest Denormal) -> +Largest Denormal - inc.
302   test = APFloat(APFloat::IEEEquad, "0x0.ffffffffffffffffffffffffffffp-16382");
303   expected = APFloat(APFloat::IEEEquad,
304                      "0x0.fffffffffffffffffffffffffffep-16382");
305   EXPECT_EQ(test.next(true), APFloat::opOK);
306   EXPECT_TRUE(test.isDenormal());
307   EXPECT_TRUE(!test.isNegative());
308   EXPECT_TRUE(test.bitwiseIsEqual(expected));
309 
310   // nextUp(+Smallest Normal) -> +Smallest Normal + inc.
311   test = APFloat(APFloat::IEEEquad, "0x1.0000000000000000000000000000p-16382");
312   expected = APFloat(APFloat::IEEEquad,
313                      "0x1.0000000000000000000000000001p-16382");
314   EXPECT_EQ(test.next(false), APFloat::opOK);
315   EXPECT_TRUE(!test.isDenormal());
316   EXPECT_TRUE(!test.isNegative());
317   EXPECT_TRUE(test.bitwiseIsEqual(expected));
318 
319   // nextDown(-Smallest Normal) -> -Smallest Normal - inc.
320   test = APFloat(APFloat::IEEEquad, "-0x1.0000000000000000000000000000p-16382");
321   expected = APFloat(APFloat::IEEEquad,
322                      "-0x1.0000000000000000000000000001p-16382");
323   EXPECT_EQ(test.next(true), APFloat::opOK);
324   EXPECT_TRUE(!test.isDenormal());
325   EXPECT_TRUE(test.isNegative());
326   EXPECT_TRUE(test.bitwiseIsEqual(expected));
327 
328   // 2d. Test values which cause our exponent to go to min exponent. This
329   // is to ensure that guards in the code to check for min exponent
330   // trigger properly.
331   //     * nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
332   //     * nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
333   //         -0x1p-16381
334   //     * nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16382
335   //     * nextDown(0x1p-16382) -> 0x1.ffffffffffffffffffffffffffffp-16382
336 
337   // nextUp(-0x1p-16381) -> -0x1.ffffffffffffffffffffffffffffp-16382
338   test = APFloat(APFloat::IEEEquad, "-0x1p-16381");
339   expected = APFloat(APFloat::IEEEquad,
340                      "-0x1.ffffffffffffffffffffffffffffp-16382");
341   EXPECT_EQ(test.next(false), APFloat::opOK);
342   EXPECT_TRUE(test.bitwiseIsEqual(expected));
343 
344   // nextDown(-0x1.ffffffffffffffffffffffffffffp-16382) ->
345   //         -0x1p-16381
346   test = APFloat(APFloat::IEEEquad, "-0x1.ffffffffffffffffffffffffffffp-16382");
347   expected = APFloat(APFloat::IEEEquad, "-0x1p-16381");
348   EXPECT_EQ(test.next(true), APFloat::opOK);
349   EXPECT_TRUE(test.bitwiseIsEqual(expected));
350 
351   // nextUp(0x1.ffffffffffffffffffffffffffffp-16382) -> 0x1p-16381
352   test = APFloat(APFloat::IEEEquad, "0x1.ffffffffffffffffffffffffffffp-16382");
353   expected = APFloat(APFloat::IEEEquad, "0x1p-16381");
354   EXPECT_EQ(test.next(false), APFloat::opOK);
355   EXPECT_TRUE(test.bitwiseIsEqual(expected));
356 
357   // nextDown(0x1p-16381) -> 0x1.ffffffffffffffffffffffffffffp-16382
358   test = APFloat(APFloat::IEEEquad, "0x1p-16381");
359   expected = APFloat(APFloat::IEEEquad,
360                      "0x1.ffffffffffffffffffffffffffffp-16382");
361   EXPECT_EQ(test.next(true), APFloat::opOK);
362   EXPECT_TRUE(test.bitwiseIsEqual(expected));
363 
364   // 3. Now we test both denormal/normal computation which will not cause us
365   // to go across binade boundaries. Specifically we test:
366   //   * nextUp(+Denormal) -> +Denormal.
367   //   * nextDown(+Denormal) -> +Denormal.
368   //   * nextUp(-Denormal) -> -Denormal.
369   //   * nextDown(-Denormal) -> -Denormal.
370   //   * nextUp(+Normal) -> +Normal.
371   //   * nextDown(+Normal) -> +Normal.
372   //   * nextUp(-Normal) -> -Normal.
373   //   * nextDown(-Normal) -> -Normal.
374 
375   // nextUp(+Denormal) -> +Denormal.
376   test = APFloat(APFloat::IEEEquad,
377                  "0x0.ffffffffffffffffffffffff000cp-16382");
378   expected = APFloat(APFloat::IEEEquad,
379                  "0x0.ffffffffffffffffffffffff000dp-16382");
380   EXPECT_EQ(test.next(false), APFloat::opOK);
381   EXPECT_TRUE(test.isDenormal());
382   EXPECT_TRUE(!test.isNegative());
383   EXPECT_TRUE(test.bitwiseIsEqual(expected));
384 
385   // nextDown(+Denormal) -> +Denormal.
386   test = APFloat(APFloat::IEEEquad,
387                  "0x0.ffffffffffffffffffffffff000cp-16382");
388   expected = APFloat(APFloat::IEEEquad,
389                  "0x0.ffffffffffffffffffffffff000bp-16382");
390   EXPECT_EQ(test.next(true), APFloat::opOK);
391   EXPECT_TRUE(test.isDenormal());
392   EXPECT_TRUE(!test.isNegative());
393   EXPECT_TRUE(test.bitwiseIsEqual(expected));
394 
395   // nextUp(-Denormal) -> -Denormal.
396   test = APFloat(APFloat::IEEEquad,
397                  "-0x0.ffffffffffffffffffffffff000cp-16382");
398   expected = APFloat(APFloat::IEEEquad,
399                  "-0x0.ffffffffffffffffffffffff000bp-16382");
400   EXPECT_EQ(test.next(false), APFloat::opOK);
401   EXPECT_TRUE(test.isDenormal());
402   EXPECT_TRUE(test.isNegative());
403   EXPECT_TRUE(test.bitwiseIsEqual(expected));
404 
405   // nextDown(-Denormal) -> -Denormal
406   test = APFloat(APFloat::IEEEquad,
407                  "-0x0.ffffffffffffffffffffffff000cp-16382");
408   expected = APFloat(APFloat::IEEEquad,
409                  "-0x0.ffffffffffffffffffffffff000dp-16382");
410   EXPECT_EQ(test.next(true), APFloat::opOK);
411   EXPECT_TRUE(test.isDenormal());
412   EXPECT_TRUE(test.isNegative());
413   EXPECT_TRUE(test.bitwiseIsEqual(expected));
414 
415   // nextUp(+Normal) -> +Normal.
416   test = APFloat(APFloat::IEEEquad,
417                  "0x1.ffffffffffffffffffffffff000cp-16000");
418   expected = APFloat(APFloat::IEEEquad,
419                  "0x1.ffffffffffffffffffffffff000dp-16000");
420   EXPECT_EQ(test.next(false), APFloat::opOK);
421   EXPECT_TRUE(!test.isDenormal());
422   EXPECT_TRUE(!test.isNegative());
423   EXPECT_TRUE(test.bitwiseIsEqual(expected));
424 
425   // nextDown(+Normal) -> +Normal.
426   test = APFloat(APFloat::IEEEquad,
427                  "0x1.ffffffffffffffffffffffff000cp-16000");
428   expected = APFloat(APFloat::IEEEquad,
429                  "0x1.ffffffffffffffffffffffff000bp-16000");
430   EXPECT_EQ(test.next(true), APFloat::opOK);
431   EXPECT_TRUE(!test.isDenormal());
432   EXPECT_TRUE(!test.isNegative());
433   EXPECT_TRUE(test.bitwiseIsEqual(expected));
434 
435   // nextUp(-Normal) -> -Normal.
436   test = APFloat(APFloat::IEEEquad,
437                  "-0x1.ffffffffffffffffffffffff000cp-16000");
438   expected = APFloat(APFloat::IEEEquad,
439                  "-0x1.ffffffffffffffffffffffff000bp-16000");
440   EXPECT_EQ(test.next(false), APFloat::opOK);
441   EXPECT_TRUE(!test.isDenormal());
442   EXPECT_TRUE(test.isNegative());
443   EXPECT_TRUE(test.bitwiseIsEqual(expected));
444 
445   // nextDown(-Normal) -> -Normal.
446   test = APFloat(APFloat::IEEEquad,
447                  "-0x1.ffffffffffffffffffffffff000cp-16000");
448   expected = APFloat(APFloat::IEEEquad,
449                  "-0x1.ffffffffffffffffffffffff000dp-16000");
450   EXPECT_EQ(test.next(true), APFloat::opOK);
451   EXPECT_TRUE(!test.isDenormal());
452   EXPECT_TRUE(test.isNegative());
453   EXPECT_TRUE(test.bitwiseIsEqual(expected));
454 }
455 
TEST(APFloatTest,FMA)456 TEST(APFloatTest, FMA) {
457   APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;
458 
459   {
460     APFloat f1(14.5f);
461     APFloat f2(-14.5f);
462     APFloat f3(225.0f);
463     f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
464     EXPECT_EQ(14.75f, f1.convertToFloat());
465   }
466 
467   {
468     APFloat Val2(2.0f);
469     APFloat f1((float)1.17549435e-38F);
470     APFloat f2((float)1.17549435e-38F);
471     f1.divide(Val2, rdmd);
472     f2.divide(Val2, rdmd);
473     APFloat f3(12.0f);
474     f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
475     EXPECT_EQ(12.0f, f1.convertToFloat());
476   }
477 
478   // Test for correct zero sign when answer is exactly zero.
479   // fma(1.0, -1.0, 1.0) -> +ve 0.
480   {
481     APFloat f1(1.0);
482     APFloat f2(-1.0);
483     APFloat f3(1.0);
484     f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
485     EXPECT_TRUE(!f1.isNegative() && f1.isZero());
486   }
487 
488   // Test for correct zero sign when answer is exactly zero and rounding towards
489   // negative.
490   // fma(1.0, -1.0, 1.0) -> +ve 0.
491   {
492     APFloat f1(1.0);
493     APFloat f2(-1.0);
494     APFloat f3(1.0);
495     f1.fusedMultiplyAdd(f2, f3, APFloat::rmTowardNegative);
496     EXPECT_TRUE(f1.isNegative() && f1.isZero());
497   }
498 
499   // Test for correct (in this case -ve) sign when adding like signed zeros.
500   // Test fma(0.0, -0.0, -0.0) -> -ve 0.
501   {
502     APFloat f1(0.0);
503     APFloat f2(-0.0);
504     APFloat f3(-0.0);
505     f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
506     EXPECT_TRUE(f1.isNegative() && f1.isZero());
507   }
508 
509   // Test -ve sign preservation when small negative results underflow.
510   {
511     APFloat f1(APFloat::IEEEdouble,  "-0x1p-1074");
512     APFloat f2(APFloat::IEEEdouble, "+0x1p-1074");
513     APFloat f3(0.0);
514     f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
515     EXPECT_TRUE(f1.isNegative() && f1.isZero());
516   }
517 
518   // Test x87 extended precision case from http://llvm.org/PR20728.
519   {
520     APFloat M1(APFloat::x87DoubleExtended, 1.0);
521     APFloat M2(APFloat::x87DoubleExtended, 1.0);
522     APFloat A(APFloat::x87DoubleExtended, 3.0);
523 
524     bool losesInfo = false;
525     M1.fusedMultiplyAdd(M1, A, APFloat::rmNearestTiesToEven);
526     M1.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
527     EXPECT_FALSE(losesInfo);
528     EXPECT_EQ(4.0f, M1.convertToFloat());
529   }
530 }
531 
TEST(APFloatTest,MinNum)532 TEST(APFloatTest, MinNum) {
533   APFloat f1(1.0);
534   APFloat f2(2.0);
535   APFloat nan = APFloat::getNaN(APFloat::IEEEdouble);
536 
537   EXPECT_EQ(1.0, minnum(f1, f2).convertToDouble());
538   EXPECT_EQ(1.0, minnum(f2, f1).convertToDouble());
539   EXPECT_EQ(1.0, minnum(f1, nan).convertToDouble());
540   EXPECT_EQ(1.0, minnum(nan, f1).convertToDouble());
541 }
542 
TEST(APFloatTest,MaxNum)543 TEST(APFloatTest, MaxNum) {
544   APFloat f1(1.0);
545   APFloat f2(2.0);
546   APFloat nan = APFloat::getNaN(APFloat::IEEEdouble);
547 
548   EXPECT_EQ(2.0, maxnum(f1, f2).convertToDouble());
549   EXPECT_EQ(2.0, maxnum(f2, f1).convertToDouble());
550   EXPECT_EQ(1.0, maxnum(f1, nan).convertToDouble());
551   EXPECT_EQ(1.0, minnum(nan, f1).convertToDouble());
552 }
553 
TEST(APFloatTest,Denormal)554 TEST(APFloatTest, Denormal) {
555   APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;
556 
557   // Test single precision
558   {
559     const char *MinNormalStr = "1.17549435082228750797e-38";
560     EXPECT_FALSE(APFloat(APFloat::IEEEsingle, MinNormalStr).isDenormal());
561     EXPECT_FALSE(APFloat(APFloat::IEEEsingle, 0.0).isDenormal());
562 
563     APFloat Val2(APFloat::IEEEsingle, 2.0e0);
564     APFloat T(APFloat::IEEEsingle, MinNormalStr);
565     T.divide(Val2, rdmd);
566     EXPECT_TRUE(T.isDenormal());
567   }
568 
569   // Test double precision
570   {
571     const char *MinNormalStr = "2.22507385850720138309e-308";
572     EXPECT_FALSE(APFloat(APFloat::IEEEdouble, MinNormalStr).isDenormal());
573     EXPECT_FALSE(APFloat(APFloat::IEEEdouble, 0.0).isDenormal());
574 
575     APFloat Val2(APFloat::IEEEdouble, 2.0e0);
576     APFloat T(APFloat::IEEEdouble, MinNormalStr);
577     T.divide(Val2, rdmd);
578     EXPECT_TRUE(T.isDenormal());
579   }
580 
581   // Test Intel double-ext
582   {
583     const char *MinNormalStr = "3.36210314311209350626e-4932";
584     EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, MinNormalStr).isDenormal());
585     EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, 0.0).isDenormal());
586 
587     APFloat Val2(APFloat::x87DoubleExtended, 2.0e0);
588     APFloat T(APFloat::x87DoubleExtended, MinNormalStr);
589     T.divide(Val2, rdmd);
590     EXPECT_TRUE(T.isDenormal());
591   }
592 
593   // Test quadruple precision
594   {
595     const char *MinNormalStr = "3.36210314311209350626267781732175260e-4932";
596     EXPECT_FALSE(APFloat(APFloat::IEEEquad, MinNormalStr).isDenormal());
597     EXPECT_FALSE(APFloat(APFloat::IEEEquad, 0.0).isDenormal());
598 
599     APFloat Val2(APFloat::IEEEquad, 2.0e0);
600     APFloat T(APFloat::IEEEquad, MinNormalStr);
601     T.divide(Val2, rdmd);
602     EXPECT_TRUE(T.isDenormal());
603   }
604 }
605 
TEST(APFloatTest,Zero)606 TEST(APFloatTest, Zero) {
607   EXPECT_EQ(0.0f,  APFloat(0.0f).convertToFloat());
608   EXPECT_EQ(-0.0f, APFloat(-0.0f).convertToFloat());
609   EXPECT_TRUE(APFloat(-0.0f).isNegative());
610 
611   EXPECT_EQ(0.0,  APFloat(0.0).convertToDouble());
612   EXPECT_EQ(-0.0, APFloat(-0.0).convertToDouble());
613   EXPECT_TRUE(APFloat(-0.0).isNegative());
614 }
615 
TEST(APFloatTest,DecimalStringsWithoutNullTerminators)616 TEST(APFloatTest, DecimalStringsWithoutNullTerminators) {
617   // Make sure that we can parse strings without null terminators.
618   // rdar://14323230.
619   APFloat Val(APFloat::IEEEdouble);
620   Val.convertFromString(StringRef("0.00", 3),
621                         llvm::APFloat::rmNearestTiesToEven);
622   EXPECT_EQ(Val.convertToDouble(), 0.0);
623   Val.convertFromString(StringRef("0.01", 3),
624                         llvm::APFloat::rmNearestTiesToEven);
625   EXPECT_EQ(Val.convertToDouble(), 0.0);
626   Val.convertFromString(StringRef("0.09", 3),
627                         llvm::APFloat::rmNearestTiesToEven);
628   EXPECT_EQ(Val.convertToDouble(), 0.0);
629   Val.convertFromString(StringRef("0.095", 4),
630                         llvm::APFloat::rmNearestTiesToEven);
631   EXPECT_EQ(Val.convertToDouble(), 0.09);
632   Val.convertFromString(StringRef("0.00e+3", 7),
633                         llvm::APFloat::rmNearestTiesToEven);
634   EXPECT_EQ(Val.convertToDouble(), 0.00);
635   Val.convertFromString(StringRef("0e+3", 4),
636                         llvm::APFloat::rmNearestTiesToEven);
637   EXPECT_EQ(Val.convertToDouble(), 0.00);
638 
639 }
640 
TEST(APFloatTest,fromZeroDecimalString)641 TEST(APFloatTest, fromZeroDecimalString) {
642   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0").convertToDouble());
643   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0").convertToDouble());
644   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0").convertToDouble());
645 
646   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.").convertToDouble());
647   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.").convertToDouble());
648   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.").convertToDouble());
649 
650   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  ".0").convertToDouble());
651   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0").convertToDouble());
652   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0").convertToDouble());
653 
654   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.0").convertToDouble());
655   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0").convertToDouble());
656   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0").convertToDouble());
657 
658   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "00000.").convertToDouble());
659   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+00000.").convertToDouble());
660   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-00000.").convertToDouble());
661 
662   EXPECT_EQ(0.0,  APFloat(APFloat::IEEEdouble, ".00000").convertToDouble());
663   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.00000").convertToDouble());
664   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.00000").convertToDouble());
665 
666   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0000.00000").convertToDouble());
667   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0000.00000").convertToDouble());
668   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0000.00000").convertToDouble());
669 }
670 
TEST(APFloatTest,fromZeroDecimalSingleExponentString)671 TEST(APFloatTest, fromZeroDecimalSingleExponentString) {
672   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,   "0e1").convertToDouble());
673   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble,  "+0e1").convertToDouble());
674   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble,  "-0e1").convertToDouble());
675 
676   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0e+1").convertToDouble());
677   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1").convertToDouble());
678   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1").convertToDouble());
679 
680   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0e-1").convertToDouble());
681   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1").convertToDouble());
682   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1").convertToDouble());
683 
684 
685   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,   "0.e1").convertToDouble());
686   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble,  "+0.e1").convertToDouble());
687   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble,  "-0.e1").convertToDouble());
688 
689   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.e+1").convertToDouble());
690   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e+1").convertToDouble());
691   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e+1").convertToDouble());
692 
693   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.e-1").convertToDouble());
694   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e-1").convertToDouble());
695   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e-1").convertToDouble());
696 
697   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,   ".0e1").convertToDouble());
698   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble,  "+.0e1").convertToDouble());
699   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble,  "-.0e1").convertToDouble());
700 
701   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  ".0e+1").convertToDouble());
702   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e+1").convertToDouble());
703   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e+1").convertToDouble());
704 
705   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  ".0e-1").convertToDouble());
706   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e-1").convertToDouble());
707   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e-1").convertToDouble());
708 
709 
710   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,   "0.0e1").convertToDouble());
711   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble,  "+0.0e1").convertToDouble());
712   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble,  "-0.0e1").convertToDouble());
713 
714   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.0e+1").convertToDouble());
715   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e+1").convertToDouble());
716   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e+1").convertToDouble());
717 
718   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.0e-1").convertToDouble());
719   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e-1").convertToDouble());
720   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e-1").convertToDouble());
721 
722 
723   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "000.0000e1").convertToDouble());
724   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+000.0000e+1").convertToDouble());
725   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-000.0000e+1").convertToDouble());
726 }
727 
TEST(APFloatTest,fromZeroDecimalLargeExponentString)728 TEST(APFloatTest, fromZeroDecimalLargeExponentString) {
729   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0e1234").convertToDouble());
730   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e1234").convertToDouble());
731   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e1234").convertToDouble());
732 
733   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0e+1234").convertToDouble());
734   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1234").convertToDouble());
735   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1234").convertToDouble());
736 
737   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0e-1234").convertToDouble());
738   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1234").convertToDouble());
739   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1234").convertToDouble());
740 
741   EXPECT_EQ(0.0,  APFloat(APFloat::IEEEdouble, "000.0000e1234").convertToDouble());
742   EXPECT_EQ(0.0,  APFloat(APFloat::IEEEdouble, "000.0000e-1234").convertToDouble());
743 
744   EXPECT_EQ(0.0,  APFloat(APFloat::IEEEdouble, StringRef("0e1234\02", 6)).convertToDouble());
745 }
746 
TEST(APFloatTest,fromZeroHexadecimalString)747 TEST(APFloatTest, fromZeroHexadecimalString) {
748   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0p1").convertToDouble());
749   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p1").convertToDouble());
750   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1").convertToDouble());
751 
752   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0p+1").convertToDouble());
753   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p+1").convertToDouble());
754   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p+1").convertToDouble());
755 
756   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0p-1").convertToDouble());
757   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p-1").convertToDouble());
758   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p-1").convertToDouble());
759 
760 
761   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.p1").convertToDouble());
762   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p1").convertToDouble());
763   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p1").convertToDouble());
764 
765   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.p+1").convertToDouble());
766   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p+1").convertToDouble());
767   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p+1").convertToDouble());
768 
769   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.p-1").convertToDouble());
770   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p-1").convertToDouble());
771   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p-1").convertToDouble());
772 
773 
774   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x.0p1").convertToDouble());
775   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p1").convertToDouble());
776   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p1").convertToDouble());
777 
778   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x.0p+1").convertToDouble());
779   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p+1").convertToDouble());
780   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p+1").convertToDouble());
781 
782   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x.0p-1").convertToDouble());
783   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p-1").convertToDouble());
784   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p-1").convertToDouble());
785 
786 
787   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.0p1").convertToDouble());
788   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p1").convertToDouble());
789   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p1").convertToDouble());
790 
791   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.0p+1").convertToDouble());
792   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p+1").convertToDouble());
793   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p+1").convertToDouble());
794 
795   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.0p-1").convertToDouble());
796   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p-1").convertToDouble());
797   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p-1").convertToDouble());
798 
799 
800   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1").convertToDouble());
801   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1").convertToDouble());
802   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1").convertToDouble());
803   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1").convertToDouble());
804   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p1234").convertToDouble());
805   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1234").convertToDouble());
806   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1234").convertToDouble());
807   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1234").convertToDouble());
808   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1234").convertToDouble());
809   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1234").convertToDouble());
810 }
811 
TEST(APFloatTest,fromDecimalString)812 TEST(APFloatTest, fromDecimalString) {
813   EXPECT_EQ(1.0,      APFloat(APFloat::IEEEdouble, "1").convertToDouble());
814   EXPECT_EQ(2.0,      APFloat(APFloat::IEEEdouble, "2.").convertToDouble());
815   EXPECT_EQ(0.5,      APFloat(APFloat::IEEEdouble, ".5").convertToDouble());
816   EXPECT_EQ(1.0,      APFloat(APFloat::IEEEdouble, "1.0").convertToDouble());
817   EXPECT_EQ(-2.0,     APFloat(APFloat::IEEEdouble, "-2").convertToDouble());
818   EXPECT_EQ(-4.0,     APFloat(APFloat::IEEEdouble, "-4.").convertToDouble());
819   EXPECT_EQ(-0.5,     APFloat(APFloat::IEEEdouble, "-.5").convertToDouble());
820   EXPECT_EQ(-1.5,     APFloat(APFloat::IEEEdouble, "-1.5").convertToDouble());
821   EXPECT_EQ(1.25e12,  APFloat(APFloat::IEEEdouble, "1.25e12").convertToDouble());
822   EXPECT_EQ(1.25e+12, APFloat(APFloat::IEEEdouble, "1.25e+12").convertToDouble());
823   EXPECT_EQ(1.25e-12, APFloat(APFloat::IEEEdouble, "1.25e-12").convertToDouble());
824   EXPECT_EQ(1024.0,   APFloat(APFloat::IEEEdouble, "1024.").convertToDouble());
825   EXPECT_EQ(1024.05,  APFloat(APFloat::IEEEdouble, "1024.05000").convertToDouble());
826   EXPECT_EQ(0.05,     APFloat(APFloat::IEEEdouble, ".05000").convertToDouble());
827   EXPECT_EQ(2.0,      APFloat(APFloat::IEEEdouble, "2.").convertToDouble());
828   EXPECT_EQ(2.0e2,    APFloat(APFloat::IEEEdouble, "2.e2").convertToDouble());
829   EXPECT_EQ(2.0e+2,   APFloat(APFloat::IEEEdouble, "2.e+2").convertToDouble());
830   EXPECT_EQ(2.0e-2,   APFloat(APFloat::IEEEdouble, "2.e-2").convertToDouble());
831   EXPECT_EQ(2.05e2,    APFloat(APFloat::IEEEdouble, "002.05000e2").convertToDouble());
832   EXPECT_EQ(2.05e+2,   APFloat(APFloat::IEEEdouble, "002.05000e+2").convertToDouble());
833   EXPECT_EQ(2.05e-2,   APFloat(APFloat::IEEEdouble, "002.05000e-2").convertToDouble());
834   EXPECT_EQ(2.05e12,   APFloat(APFloat::IEEEdouble, "002.05000e12").convertToDouble());
835   EXPECT_EQ(2.05e+12,  APFloat(APFloat::IEEEdouble, "002.05000e+12").convertToDouble());
836   EXPECT_EQ(2.05e-12,  APFloat(APFloat::IEEEdouble, "002.05000e-12").convertToDouble());
837 
838   // These are "carefully selected" to overflow the fast log-base
839   // calculations in APFloat.cpp
840   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "99e99999").isInfinity());
841   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-99e99999").isInfinity());
842   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "1e-99999").isPosZero());
843   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-1e-99999").isNegZero());
844 
845   EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
846 }
847 
TEST(APFloatTest,fromHexadecimalString)848 TEST(APFloatTest, fromHexadecimalString) {
849   EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble,  "0x1p0").convertToDouble());
850   EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p0").convertToDouble());
851   EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p0").convertToDouble());
852 
853   EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble,  "0x1p+0").convertToDouble());
854   EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p+0").convertToDouble());
855   EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p+0").convertToDouble());
856 
857   EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble,  "0x1p-0").convertToDouble());
858   EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p-0").convertToDouble());
859   EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p-0").convertToDouble());
860 
861 
862   EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble,  "0x1p1").convertToDouble());
863   EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p1").convertToDouble());
864   EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p1").convertToDouble());
865 
866   EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble,  "0x1p+1").convertToDouble());
867   EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p+1").convertToDouble());
868   EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p+1").convertToDouble());
869 
870   EXPECT_EQ( 0.5, APFloat(APFloat::IEEEdouble,  "0x1p-1").convertToDouble());
871   EXPECT_EQ(+0.5, APFloat(APFloat::IEEEdouble, "+0x1p-1").convertToDouble());
872   EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble, "-0x1p-1").convertToDouble());
873 
874 
875   EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble,  "0x1.8p1").convertToDouble());
876   EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p1").convertToDouble());
877   EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p1").convertToDouble());
878 
879   EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble,  "0x1.8p+1").convertToDouble());
880   EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p+1").convertToDouble());
881   EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p+1").convertToDouble());
882 
883   EXPECT_EQ( 0.75, APFloat(APFloat::IEEEdouble,  "0x1.8p-1").convertToDouble());
884   EXPECT_EQ(+0.75, APFloat(APFloat::IEEEdouble, "+0x1.8p-1").convertToDouble());
885   EXPECT_EQ(-0.75, APFloat(APFloat::IEEEdouble, "-0x1.8p-1").convertToDouble());
886 
887 
888   EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble,  "0x1000.000p1").convertToDouble());
889   EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p1").convertToDouble());
890   EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p1").convertToDouble());
891 
892   EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble,  "0x1000.000p+1").convertToDouble());
893   EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p+1").convertToDouble());
894   EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p+1").convertToDouble());
895 
896   EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble,  "0x1000.000p-1").convertToDouble());
897   EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p-1").convertToDouble());
898   EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p-1").convertToDouble());
899 
900 
901   EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble,  "0x1000p1").convertToDouble());
902   EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p1").convertToDouble());
903   EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p1").convertToDouble());
904 
905   EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble,  "0x1000p+1").convertToDouble());
906   EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p+1").convertToDouble());
907   EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p+1").convertToDouble());
908 
909   EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble,  "0x1000p-1").convertToDouble());
910   EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000p-1").convertToDouble());
911   EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000p-1").convertToDouble());
912 
913 
914   EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble,  "0x10p10").convertToDouble());
915   EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p10").convertToDouble());
916   EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p10").convertToDouble());
917 
918   EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble,  "0x10p+10").convertToDouble());
919   EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p+10").convertToDouble());
920   EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p+10").convertToDouble());
921 
922   EXPECT_EQ( 0.015625, APFloat(APFloat::IEEEdouble,  "0x10p-10").convertToDouble());
923   EXPECT_EQ(+0.015625, APFloat(APFloat::IEEEdouble, "+0x10p-10").convertToDouble());
924   EXPECT_EQ(-0.015625, APFloat(APFloat::IEEEdouble, "-0x10p-10").convertToDouble());
925 
926   EXPECT_EQ(1.0625, APFloat(APFloat::IEEEdouble, "0x1.1p0").convertToDouble());
927   EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "0x1p0").convertToDouble());
928 
929   EXPECT_EQ(convertToDoubleFromString("0x1p-150"),
930             convertToDoubleFromString("+0x800000000000000001.p-221"));
931   EXPECT_EQ(2251799813685248.5,
932             convertToDoubleFromString("0x80000000000004000000.010p-28"));
933 }
934 
TEST(APFloatTest,toString)935 TEST(APFloatTest, toString) {
936   ASSERT_EQ("10", convertToString(10.0, 6, 3));
937   ASSERT_EQ("1.0E+1", convertToString(10.0, 6, 0));
938   ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2));
939   ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 4, 2));
940   ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 5, 1));
941   ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2));
942   ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2));
943   ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 5, 1));
944   ASSERT_EQ("0.78539816339744828", convertToString(0.78539816339744830961, 0, 3));
945   ASSERT_EQ("4.9406564584124654E-324", convertToString(4.9406564584124654e-324, 0, 3));
946   ASSERT_EQ("873.18340000000001", convertToString(873.1834, 0, 1));
947   ASSERT_EQ("8.7318340000000001E+2", convertToString(873.1834, 0, 0));
948   ASSERT_EQ("1.7976931348623157E+308", convertToString(1.7976931348623157E+308, 0, 0));
949 }
950 
TEST(APFloatTest,toInteger)951 TEST(APFloatTest, toInteger) {
952   bool isExact = false;
953   APSInt result(5, /*isUnsigned=*/true);
954 
955   EXPECT_EQ(APFloat::opOK,
956             APFloat(APFloat::IEEEdouble, "10")
957             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
958   EXPECT_TRUE(isExact);
959   EXPECT_EQ(APSInt(APInt(5, 10), true), result);
960 
961   EXPECT_EQ(APFloat::opInvalidOp,
962             APFloat(APFloat::IEEEdouble, "-10")
963             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
964   EXPECT_FALSE(isExact);
965   EXPECT_EQ(APSInt::getMinValue(5, true), result);
966 
967   EXPECT_EQ(APFloat::opInvalidOp,
968             APFloat(APFloat::IEEEdouble, "32")
969             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
970   EXPECT_FALSE(isExact);
971   EXPECT_EQ(APSInt::getMaxValue(5, true), result);
972 
973   EXPECT_EQ(APFloat::opInexact,
974             APFloat(APFloat::IEEEdouble, "7.9")
975             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
976   EXPECT_FALSE(isExact);
977   EXPECT_EQ(APSInt(APInt(5, 7), true), result);
978 
979   result.setIsUnsigned(false);
980   EXPECT_EQ(APFloat::opOK,
981             APFloat(APFloat::IEEEdouble, "-10")
982             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
983   EXPECT_TRUE(isExact);
984   EXPECT_EQ(APSInt(APInt(5, -10, true), false), result);
985 
986   EXPECT_EQ(APFloat::opInvalidOp,
987             APFloat(APFloat::IEEEdouble, "-17")
988             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
989   EXPECT_FALSE(isExact);
990   EXPECT_EQ(APSInt::getMinValue(5, false), result);
991 
992   EXPECT_EQ(APFloat::opInvalidOp,
993             APFloat(APFloat::IEEEdouble, "16")
994             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
995   EXPECT_FALSE(isExact);
996   EXPECT_EQ(APSInt::getMaxValue(5, false), result);
997 }
998 
nanbits(const fltSemantics & Sem,bool SNaN,bool Negative,uint64_t fill)999 static APInt nanbits(const fltSemantics &Sem,
1000                      bool SNaN, bool Negative, uint64_t fill) {
1001   APInt apfill(64, fill);
1002   if (SNaN)
1003     return APFloat::getSNaN(Sem, Negative, &apfill).bitcastToAPInt();
1004   else
1005     return APFloat::getQNaN(Sem, Negative, &apfill).bitcastToAPInt();
1006 }
1007 
TEST(APFloatTest,makeNaN)1008 TEST(APFloatTest, makeNaN) {
1009   ASSERT_EQ(0x7fc00000, nanbits(APFloat::IEEEsingle, false, false, 0));
1010   ASSERT_EQ(0xffc00000, nanbits(APFloat::IEEEsingle, false, true, 0));
1011   ASSERT_EQ(0x7fc0ae72, nanbits(APFloat::IEEEsingle, false, false, 0xae72));
1012   ASSERT_EQ(0x7fffae72, nanbits(APFloat::IEEEsingle, false, false, 0xffffae72));
1013   ASSERT_EQ(0x7fa00000, nanbits(APFloat::IEEEsingle, true, false, 0));
1014   ASSERT_EQ(0xffa00000, nanbits(APFloat::IEEEsingle, true, true, 0));
1015   ASSERT_EQ(0x7f80ae72, nanbits(APFloat::IEEEsingle, true, false, 0xae72));
1016   ASSERT_EQ(0x7fbfae72, nanbits(APFloat::IEEEsingle, true, false, 0xffffae72));
1017 
1018   ASSERT_EQ(0x7ff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, false, 0));
1019   ASSERT_EQ(0xfff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, true, 0));
1020   ASSERT_EQ(0x7ff800000000ae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xae72));
1021   ASSERT_EQ(0x7fffffffffffae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xffffffffffffae72ULL));
1022   ASSERT_EQ(0x7ff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, false, 0));
1023   ASSERT_EQ(0xfff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, true, 0));
1024   ASSERT_EQ(0x7ff000000000ae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xae72));
1025   ASSERT_EQ(0x7ff7ffffffffae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xffffffffffffae72ULL));
1026 }
1027 
1028 #ifdef GTEST_HAS_DEATH_TEST
1029 #ifndef NDEBUG
TEST(APFloatTest,SemanticsDeath)1030 TEST(APFloatTest, SemanticsDeath) {
1031   EXPECT_DEATH(APFloat(APFloat::IEEEsingle, 0.0f).convertToDouble(), "Float semantics are not IEEEdouble");
1032   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, 0.0 ).convertToFloat(),  "Float semantics are not IEEEsingle");
1033 }
1034 
TEST(APFloatTest,StringDecimalDeath)1035 TEST(APFloatTest, StringDecimalDeath) {
1036   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  ""), "Invalid string length");
1037   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+"), "String has no digits");
1038   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-"), "String has no digits");
1039 
1040   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("\0", 1)), "Invalid character in significand");
1041   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\0", 2)), "Invalid character in significand");
1042   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02", 3)), "Invalid character in significand");
1043   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02e1", 5)), "Invalid character in significand");
1044   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e\0", 3)), "Invalid character in exponent");
1045   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\0", 4)), "Invalid character in exponent");
1046   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\02", 5)), "Invalid character in exponent");
1047 
1048   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0f"), "Invalid character in significand");
1049 
1050   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".."), "String contains multiple dots");
1051   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "..0"), "String contains multiple dots");
1052   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0.0"), "String contains multiple dots");
1053 }
1054 
TEST(APFloatTest,StringDecimalSignificandDeath)1055 TEST(APFloatTest, StringDecimalSignificandDeath) {
1056   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "."), "Significand has no digits");
1057   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+."), "Significand has no digits");
1058   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-."), "Significand has no digits");
1059 
1060 
1061   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "e"), "Significand has no digits");
1062   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e"), "Significand has no digits");
1063   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e"), "Significand has no digits");
1064 
1065   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "e1"), "Significand has no digits");
1066   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e1"), "Significand has no digits");
1067   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e1"), "Significand has no digits");
1068 
1069   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  ".e1"), "Significand has no digits");
1070   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e1"), "Significand has no digits");
1071   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e1"), "Significand has no digits");
1072 
1073 
1074   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  ".e"), "Significand has no digits");
1075   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e"), "Significand has no digits");
1076   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e"), "Significand has no digits");
1077 }
1078 
TEST(APFloatTest,StringDecimalExponentDeath)1079 TEST(APFloatTest, StringDecimalExponentDeath) {
1080   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,   "1e"), "Exponent has no digits");
1081   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "+1e"), "Exponent has no digits");
1082   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "-1e"), "Exponent has no digits");
1083 
1084   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,   "1.e"), "Exponent has no digits");
1085   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "+1.e"), "Exponent has no digits");
1086   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "-1.e"), "Exponent has no digits");
1087 
1088   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,   ".1e"), "Exponent has no digits");
1089   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "+.1e"), "Exponent has no digits");
1090   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "-.1e"), "Exponent has no digits");
1091 
1092   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,   "1.1e"), "Exponent has no digits");
1093   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "+1.1e"), "Exponent has no digits");
1094   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "-1.1e"), "Exponent has no digits");
1095 
1096 
1097   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e+"), "Exponent has no digits");
1098   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e-"), "Exponent has no digits");
1099 
1100   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  ".1e"), "Exponent has no digits");
1101   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e+"), "Exponent has no digits");
1102   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e-"), "Exponent has no digits");
1103 
1104   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "1.0e"), "Exponent has no digits");
1105   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e+"), "Exponent has no digits");
1106   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e-"), "Exponent has no digits");
1107 }
1108 
TEST(APFloatTest,StringHexadecimalDeath)1109 TEST(APFloatTest, StringHexadecimalDeath) {
1110   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x"), "Invalid string");
1111   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x"), "Invalid string");
1112   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x"), "Invalid string");
1113 
1114   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x0"), "Hex strings require an exponent");
1115   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0"), "Hex strings require an exponent");
1116   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0"), "Hex strings require an exponent");
1117 
1118   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x0."), "Hex strings require an exponent");
1119   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0."), "Hex strings require an exponent");
1120   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0."), "Hex strings require an exponent");
1121 
1122   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.0"), "Hex strings require an exponent");
1123   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.0"), "Hex strings require an exponent");
1124   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.0"), "Hex strings require an exponent");
1125 
1126   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x0.0"), "Hex strings require an exponent");
1127   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0.0"), "Hex strings require an exponent");
1128   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0.0"), "Hex strings require an exponent");
1129 
1130   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x\0", 3)), "Invalid character in significand");
1131   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\0", 4)), "Invalid character in significand");
1132   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02", 5)), "Invalid character in significand");
1133   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02p1", 7)), "Invalid character in significand");
1134   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p\0", 5)), "Invalid character in exponent");
1135   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\0", 6)), "Invalid character in exponent");
1136   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\02", 7)), "Invalid character in exponent");
1137 
1138   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p0f"), "Invalid character in exponent");
1139 
1140   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..p1"), "String contains multiple dots");
1141   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..0p1"), "String contains multiple dots");
1142   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.0.0p1"), "String contains multiple dots");
1143 }
1144 
TEST(APFloatTest,StringHexadecimalSignificandDeath)1145 TEST(APFloatTest, StringHexadecimalSignificandDeath) {
1146   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x."), "Significand has no digits");
1147   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x."), "Significand has no digits");
1148   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x."), "Significand has no digits");
1149 
1150   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0xp"), "Significand has no digits");
1151   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp"), "Significand has no digits");
1152   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp"), "Significand has no digits");
1153 
1154   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0xp+"), "Significand has no digits");
1155   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp+"), "Significand has no digits");
1156   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp+"), "Significand has no digits");
1157 
1158   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0xp-"), "Significand has no digits");
1159   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp-"), "Significand has no digits");
1160   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp-"), "Significand has no digits");
1161 
1162 
1163   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.p"), "Significand has no digits");
1164   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p"), "Significand has no digits");
1165   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p"), "Significand has no digits");
1166 
1167   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.p+"), "Significand has no digits");
1168   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p+"), "Significand has no digits");
1169   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p+"), "Significand has no digits");
1170 
1171   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.p-"), "Significand has no digits");
1172   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p-"), "Significand has no digits");
1173   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p-"), "Significand has no digits");
1174 }
1175 
TEST(APFloatTest,StringHexadecimalExponentDeath)1176 TEST(APFloatTest, StringHexadecimalExponentDeath) {
1177   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1p"), "Exponent has no digits");
1178   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p"), "Exponent has no digits");
1179   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p"), "Exponent has no digits");
1180 
1181   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1p+"), "Exponent has no digits");
1182   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p+"), "Exponent has no digits");
1183   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p+"), "Exponent has no digits");
1184 
1185   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1p-"), "Exponent has no digits");
1186   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p-"), "Exponent has no digits");
1187   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p-"), "Exponent has no digits");
1188 
1189 
1190   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.p"), "Exponent has no digits");
1191   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p"), "Exponent has no digits");
1192   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p"), "Exponent has no digits");
1193 
1194   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.p+"), "Exponent has no digits");
1195   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p+"), "Exponent has no digits");
1196   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p+"), "Exponent has no digits");
1197 
1198   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.p-"), "Exponent has no digits");
1199   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p-"), "Exponent has no digits");
1200   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p-"), "Exponent has no digits");
1201 
1202 
1203   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.1p"), "Exponent has no digits");
1204   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p"), "Exponent has no digits");
1205   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p"), "Exponent has no digits");
1206 
1207   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.1p+"), "Exponent has no digits");
1208   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p+"), "Exponent has no digits");
1209   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p+"), "Exponent has no digits");
1210 
1211   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.1p-"), "Exponent has no digits");
1212   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p-"), "Exponent has no digits");
1213   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p-"), "Exponent has no digits");
1214 
1215 
1216   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.1p"), "Exponent has no digits");
1217   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p"), "Exponent has no digits");
1218   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p"), "Exponent has no digits");
1219 
1220   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.1p+"), "Exponent has no digits");
1221   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p+"), "Exponent has no digits");
1222   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p+"), "Exponent has no digits");
1223 
1224   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.1p-"), "Exponent has no digits");
1225   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p-"), "Exponent has no digits");
1226   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p-"), "Exponent has no digits");
1227 }
1228 #endif
1229 #endif
1230 
TEST(APFloatTest,exactInverse)1231 TEST(APFloatTest, exactInverse) {
1232   APFloat inv(0.0f);
1233 
1234   // Trivial operation.
1235   EXPECT_TRUE(APFloat(2.0).getExactInverse(&inv));
1236   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5)));
1237   EXPECT_TRUE(APFloat(2.0f).getExactInverse(&inv));
1238   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5f)));
1239   EXPECT_TRUE(APFloat(APFloat::IEEEquad, "2.0").getExactInverse(&inv));
1240   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::IEEEquad, "0.5")));
1241   EXPECT_TRUE(APFloat(APFloat::PPCDoubleDouble, "2.0").getExactInverse(&inv));
1242   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::PPCDoubleDouble, "0.5")));
1243   EXPECT_TRUE(APFloat(APFloat::x87DoubleExtended, "2.0").getExactInverse(&inv));
1244   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::x87DoubleExtended, "0.5")));
1245 
1246   // FLT_MIN
1247   EXPECT_TRUE(APFloat(1.17549435e-38f).getExactInverse(&inv));
1248   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(8.5070592e+37f)));
1249 
1250   // Large float, inverse is a denormal.
1251   EXPECT_FALSE(APFloat(1.7014118e38f).getExactInverse(nullptr));
1252   // Zero
1253   EXPECT_FALSE(APFloat(0.0).getExactInverse(nullptr));
1254   // Denormalized float
1255   EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(nullptr));
1256 }
1257 
TEST(APFloatTest,roundToIntegral)1258 TEST(APFloatTest, roundToIntegral) {
1259   APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble)), P(0.0);
1260 
1261   P = T;
1262   P.roundToIntegral(APFloat::rmTowardZero);
1263   EXPECT_EQ(-0.0, P.convertToDouble());
1264   P = T;
1265   P.roundToIntegral(APFloat::rmTowardNegative);
1266   EXPECT_EQ(-1.0, P.convertToDouble());
1267   P = T;
1268   P.roundToIntegral(APFloat::rmTowardPositive);
1269   EXPECT_EQ(-0.0, P.convertToDouble());
1270   P = T;
1271   P.roundToIntegral(APFloat::rmNearestTiesToEven);
1272   EXPECT_EQ(-0.0, P.convertToDouble());
1273 
1274   P = S;
1275   P.roundToIntegral(APFloat::rmTowardZero);
1276   EXPECT_EQ(3.0, P.convertToDouble());
1277   P = S;
1278   P.roundToIntegral(APFloat::rmTowardNegative);
1279   EXPECT_EQ(3.0, P.convertToDouble());
1280   P = S;
1281   P.roundToIntegral(APFloat::rmTowardPositive);
1282   EXPECT_EQ(4.0, P.convertToDouble());
1283   P = S;
1284   P.roundToIntegral(APFloat::rmNearestTiesToEven);
1285   EXPECT_EQ(3.0, P.convertToDouble());
1286 
1287   P = R;
1288   P.roundToIntegral(APFloat::rmTowardZero);
1289   EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1290   P = R;
1291   P.roundToIntegral(APFloat::rmTowardNegative);
1292   EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1293   P = R;
1294   P.roundToIntegral(APFloat::rmTowardPositive);
1295   EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1296   P = R;
1297   P.roundToIntegral(APFloat::rmNearestTiesToEven);
1298   EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
1299 
1300   P = APFloat::getZero(APFloat::IEEEdouble);
1301   P.roundToIntegral(APFloat::rmTowardZero);
1302   EXPECT_EQ(0.0, P.convertToDouble());
1303   P = APFloat::getZero(APFloat::IEEEdouble, true);
1304   P.roundToIntegral(APFloat::rmTowardZero);
1305   EXPECT_EQ(-0.0, P.convertToDouble());
1306   P = APFloat::getNaN(APFloat::IEEEdouble);
1307   P.roundToIntegral(APFloat::rmTowardZero);
1308   EXPECT_TRUE(std::isnan(P.convertToDouble()));
1309   P = APFloat::getInf(APFloat::IEEEdouble);
1310   P.roundToIntegral(APFloat::rmTowardZero);
1311   EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() > 0.0);
1312   P = APFloat::getInf(APFloat::IEEEdouble, true);
1313   P.roundToIntegral(APFloat::rmTowardZero);
1314   EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() < 0.0);
1315 }
1316 
TEST(APFloatTest,isInteger)1317 TEST(APFloatTest, isInteger) {
1318   APFloat T(-0.0);
1319   EXPECT_TRUE(T.isInteger());
1320   T = APFloat(3.14159);
1321   EXPECT_FALSE(T.isInteger());
1322   T = APFloat::getNaN(APFloat::IEEEdouble);
1323   EXPECT_FALSE(T.isInteger());
1324   T = APFloat::getInf(APFloat::IEEEdouble);
1325   EXPECT_FALSE(T.isInteger());
1326   T = APFloat::getInf(APFloat::IEEEdouble, true);
1327   EXPECT_FALSE(T.isInteger());
1328   T = APFloat::getLargest(APFloat::IEEEdouble);
1329   EXPECT_TRUE(T.isInteger());
1330 }
1331 
TEST(APFloatTest,getLargest)1332 TEST(APFloatTest, getLargest) {
1333   EXPECT_EQ(3.402823466e+38f, APFloat::getLargest(APFloat::IEEEsingle).convertToFloat());
1334   EXPECT_EQ(1.7976931348623158e+308, APFloat::getLargest(APFloat::IEEEdouble).convertToDouble());
1335 }
1336 
TEST(APFloatTest,getSmallest)1337 TEST(APFloatTest, getSmallest) {
1338   APFloat test = APFloat::getSmallest(APFloat::IEEEsingle, false);
1339   APFloat expected = APFloat(APFloat::IEEEsingle, "0x0.000002p-126");
1340   EXPECT_FALSE(test.isNegative());
1341   EXPECT_TRUE(test.isFiniteNonZero());
1342   EXPECT_TRUE(test.isDenormal());
1343   EXPECT_TRUE(test.bitwiseIsEqual(expected));
1344 
1345   test = APFloat::getSmallest(APFloat::IEEEsingle, true);
1346   expected = APFloat(APFloat::IEEEsingle, "-0x0.000002p-126");
1347   EXPECT_TRUE(test.isNegative());
1348   EXPECT_TRUE(test.isFiniteNonZero());
1349   EXPECT_TRUE(test.isDenormal());
1350   EXPECT_TRUE(test.bitwiseIsEqual(expected));
1351 
1352   test = APFloat::getSmallest(APFloat::IEEEquad, false);
1353   expected = APFloat(APFloat::IEEEquad, "0x0.0000000000000000000000000001p-16382");
1354   EXPECT_FALSE(test.isNegative());
1355   EXPECT_TRUE(test.isFiniteNonZero());
1356   EXPECT_TRUE(test.isDenormal());
1357   EXPECT_TRUE(test.bitwiseIsEqual(expected));
1358 
1359   test = APFloat::getSmallest(APFloat::IEEEquad, true);
1360   expected = APFloat(APFloat::IEEEquad, "-0x0.0000000000000000000000000001p-16382");
1361   EXPECT_TRUE(test.isNegative());
1362   EXPECT_TRUE(test.isFiniteNonZero());
1363   EXPECT_TRUE(test.isDenormal());
1364   EXPECT_TRUE(test.bitwiseIsEqual(expected));
1365 }
1366 
TEST(APFloatTest,getSmallestNormalized)1367 TEST(APFloatTest, getSmallestNormalized) {
1368   APFloat test = APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
1369   APFloat expected = APFloat(APFloat::IEEEsingle, "0x1p-126");
1370   EXPECT_FALSE(test.isNegative());
1371   EXPECT_TRUE(test.isFiniteNonZero());
1372   EXPECT_FALSE(test.isDenormal());
1373   EXPECT_TRUE(test.bitwiseIsEqual(expected));
1374 
1375   test = APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
1376   expected = APFloat(APFloat::IEEEsingle, "-0x1p-126");
1377   EXPECT_TRUE(test.isNegative());
1378   EXPECT_TRUE(test.isFiniteNonZero());
1379   EXPECT_FALSE(test.isDenormal());
1380   EXPECT_TRUE(test.bitwiseIsEqual(expected));
1381 
1382   test = APFloat::getSmallestNormalized(APFloat::IEEEquad, false);
1383   expected = APFloat(APFloat::IEEEquad, "0x1p-16382");
1384   EXPECT_FALSE(test.isNegative());
1385   EXPECT_TRUE(test.isFiniteNonZero());
1386   EXPECT_FALSE(test.isDenormal());
1387   EXPECT_TRUE(test.bitwiseIsEqual(expected));
1388 
1389   test = APFloat::getSmallestNormalized(APFloat::IEEEquad, true);
1390   expected = APFloat(APFloat::IEEEquad, "-0x1p-16382");
1391   EXPECT_TRUE(test.isNegative());
1392   EXPECT_TRUE(test.isFiniteNonZero());
1393   EXPECT_FALSE(test.isDenormal());
1394   EXPECT_TRUE(test.bitwiseIsEqual(expected));
1395 }
1396 
TEST(APFloatTest,getZero)1397 TEST(APFloatTest, getZero) {
1398   struct {
1399     const fltSemantics *semantics;
1400     const bool sign;
1401     const unsigned long long bitPattern[2];
1402     const unsigned bitPatternLength;
1403   } const GetZeroTest[] = {
1404     { &APFloat::IEEEhalf, false, {0, 0}, 1},
1405     { &APFloat::IEEEhalf, true, {0x8000ULL, 0}, 1},
1406     { &APFloat::IEEEsingle, false, {0, 0}, 1},
1407     { &APFloat::IEEEsingle, true, {0x80000000ULL, 0}, 1},
1408     { &APFloat::IEEEdouble, false, {0, 0}, 1},
1409     { &APFloat::IEEEdouble, true, {0x8000000000000000ULL, 0}, 1},
1410     { &APFloat::IEEEquad, false, {0, 0}, 2},
1411     { &APFloat::IEEEquad, true, {0, 0x8000000000000000ULL}, 2},
1412     { &APFloat::PPCDoubleDouble, false, {0, 0}, 2},
1413     { &APFloat::PPCDoubleDouble, true, {0x8000000000000000ULL, 0}, 2},
1414     { &APFloat::x87DoubleExtended, false, {0, 0}, 2},
1415     { &APFloat::x87DoubleExtended, true, {0, 0x8000ULL}, 2},
1416   };
1417   const unsigned NumGetZeroTests = 12;
1418   for (unsigned i = 0; i < NumGetZeroTests; ++i) {
1419     APFloat test = APFloat::getZero(*GetZeroTest[i].semantics,
1420 				    GetZeroTest[i].sign);
1421     const char *pattern = GetZeroTest[i].sign? "-0x0p+0" : "0x0p+0";
1422     APFloat expected = APFloat(*GetZeroTest[i].semantics,
1423 			       pattern);
1424     EXPECT_TRUE(test.isZero());
1425     EXPECT_TRUE(GetZeroTest[i].sign? test.isNegative() : !test.isNegative());
1426     EXPECT_TRUE(test.bitwiseIsEqual(expected));
1427     for (unsigned j = 0, je = GetZeroTest[i].bitPatternLength; j < je; ++j) {
1428       EXPECT_EQ(GetZeroTest[i].bitPattern[j],
1429 		test.bitcastToAPInt().getRawData()[j]);
1430     }
1431   }
1432 }
1433 
TEST(APFloatTest,copySign)1434 TEST(APFloatTest, copySign) {
1435   EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
1436       APFloat::copySign(APFloat(42.0), APFloat(-1.0))));
1437   EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
1438       APFloat::copySign(APFloat(-42.0), APFloat(1.0))));
1439   EXPECT_TRUE(APFloat(-42.0).bitwiseIsEqual(
1440       APFloat::copySign(APFloat(-42.0), APFloat(-1.0))));
1441   EXPECT_TRUE(APFloat(42.0).bitwiseIsEqual(
1442       APFloat::copySign(APFloat(42.0), APFloat(1.0))));
1443 }
1444 
TEST(APFloatTest,convert)1445 TEST(APFloatTest, convert) {
1446   bool losesInfo;
1447   APFloat test(APFloat::IEEEdouble, "1.0");
1448   test.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
1449   EXPECT_EQ(1.0f, test.convertToFloat());
1450   EXPECT_FALSE(losesInfo);
1451 
1452   test = APFloat(APFloat::x87DoubleExtended, "0x1p-53");
1453   test.add(APFloat(APFloat::x87DoubleExtended, "1.0"), APFloat::rmNearestTiesToEven);
1454   test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1455   EXPECT_EQ(1.0, test.convertToDouble());
1456   EXPECT_TRUE(losesInfo);
1457 
1458   test = APFloat(APFloat::IEEEquad, "0x1p-53");
1459   test.add(APFloat(APFloat::IEEEquad, "1.0"), APFloat::rmNearestTiesToEven);
1460   test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1461   EXPECT_EQ(1.0, test.convertToDouble());
1462   EXPECT_TRUE(losesInfo);
1463 
1464   test = APFloat(APFloat::x87DoubleExtended, "0xf.fffffffp+28");
1465   test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
1466   EXPECT_EQ(4294967295.0, test.convertToDouble());
1467   EXPECT_FALSE(losesInfo);
1468 
1469   test = APFloat::getSNaN(APFloat::IEEEsingle);
1470   APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended);
1471   test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1472                &losesInfo);
1473   EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1474   EXPECT_FALSE(losesInfo);
1475 
1476   test = APFloat::getQNaN(APFloat::IEEEsingle);
1477   APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended);
1478   test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1479                &losesInfo);
1480   EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1481   EXPECT_FALSE(losesInfo);
1482 
1483   test = APFloat::getSNaN(APFloat::x87DoubleExtended);
1484   test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1485                &losesInfo);
1486   EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
1487   EXPECT_FALSE(losesInfo);
1488 
1489   test = APFloat::getQNaN(APFloat::x87DoubleExtended);
1490   test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
1491                &losesInfo);
1492   EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
1493   EXPECT_FALSE(losesInfo);
1494 }
1495 
TEST(APFloatTest,PPCDoubleDouble)1496 TEST(APFloatTest, PPCDoubleDouble) {
1497   APFloat test(APFloat::PPCDoubleDouble, "1.0");
1498   EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1499   EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1500 
1501   test.divide(APFloat(APFloat::PPCDoubleDouble, "3.0"), APFloat::rmNearestTiesToEven);
1502   EXPECT_EQ(0x3fd5555555555555ull, test.bitcastToAPInt().getRawData()[0]);
1503   EXPECT_EQ(0x3c75555555555556ull, test.bitcastToAPInt().getRawData()[1]);
1504 
1505   // LDBL_MAX
1506   test = APFloat(APFloat::PPCDoubleDouble, "1.79769313486231580793728971405301e+308");
1507   EXPECT_EQ(0x7fefffffffffffffull, test.bitcastToAPInt().getRawData()[0]);
1508   EXPECT_EQ(0x7c8ffffffffffffeull, test.bitcastToAPInt().getRawData()[1]);
1509 
1510   // LDBL_MIN
1511   test = APFloat(APFloat::PPCDoubleDouble, "2.00416836000897277799610805135016e-292");
1512   EXPECT_EQ(0x0360000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1513   EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1514 
1515   test = APFloat(APFloat::PPCDoubleDouble, "1.0");
1516   test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-105"), APFloat::rmNearestTiesToEven);
1517   EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1518   EXPECT_EQ(0x3960000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1519 
1520   test = APFloat(APFloat::PPCDoubleDouble, "1.0");
1521   test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-106"), APFloat::rmNearestTiesToEven);
1522   EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
1523 #if 0 // XFAIL
1524   // This is what we would expect with a true double-double implementation
1525   EXPECT_EQ(0x3950000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1526 #else
1527   // This is what we get with our 106-bit mantissa approximation
1528   EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
1529 #endif
1530 }
1531 
TEST(APFloatTest,isNegative)1532 TEST(APFloatTest, isNegative) {
1533   APFloat t(APFloat::IEEEsingle, "0x1p+0");
1534   EXPECT_FALSE(t.isNegative());
1535   t = APFloat(APFloat::IEEEsingle, "-0x1p+0");
1536   EXPECT_TRUE(t.isNegative());
1537 
1538   EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNegative());
1539   EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle, true).isNegative());
1540 
1541   EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNegative());
1542   EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle, true).isNegative());
1543 
1544   EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isNegative());
1545   EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle, true).isNegative());
1546 
1547   EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNegative());
1548   EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, true).isNegative());
1549 }
1550 
TEST(APFloatTest,isNormal)1551 TEST(APFloatTest, isNormal) {
1552   APFloat t(APFloat::IEEEsingle, "0x1p+0");
1553   EXPECT_TRUE(t.isNormal());
1554 
1555   EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNormal());
1556   EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNormal());
1557   EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isNormal());
1558   EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNormal());
1559   EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-149").isNormal());
1560 }
1561 
TEST(APFloatTest,isFinite)1562 TEST(APFloatTest, isFinite) {
1563   APFloat t(APFloat::IEEEsingle, "0x1p+0");
1564   EXPECT_TRUE(t.isFinite());
1565   EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isFinite());
1566   EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle, false).isFinite());
1567   EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isFinite());
1568   EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isFinite());
1569   EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p-149").isFinite());
1570 }
1571 
TEST(APFloatTest,isInfinity)1572 TEST(APFloatTest, isInfinity) {
1573   APFloat t(APFloat::IEEEsingle, "0x1p+0");
1574   EXPECT_FALSE(t.isInfinity());
1575   EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle, false).isInfinity());
1576   EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isInfinity());
1577   EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isInfinity());
1578   EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isInfinity());
1579   EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-149").isInfinity());
1580 }
1581 
TEST(APFloatTest,isNaN)1582 TEST(APFloatTest, isNaN) {
1583   APFloat t(APFloat::IEEEsingle, "0x1p+0");
1584   EXPECT_FALSE(t.isNaN());
1585   EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNaN());
1586   EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNaN());
1587   EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle, false).isNaN());
1588   EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNaN());
1589   EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-149").isNaN());
1590 }
1591 
TEST(APFloatTest,isFiniteNonZero)1592 TEST(APFloatTest, isFiniteNonZero) {
1593   // Test positive/negative normal value.
1594   EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p+0").isFiniteNonZero());
1595   EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "-0x1p+0").isFiniteNonZero());
1596 
1597   // Test positive/negative denormal value.
1598   EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p-149").isFiniteNonZero());
1599   EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "-0x1p-149").isFiniteNonZero());
1600 
1601   // Test +/- Infinity.
1602   EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isFiniteNonZero());
1603   EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, true).isFiniteNonZero());
1604 
1605   // Test +/- Zero.
1606   EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isFiniteNonZero());
1607   EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, true).isFiniteNonZero());
1608 
1609   // Test +/- qNaN. +/- dont mean anything with qNaN but paranoia can't hurt in
1610   // this instance.
1611   EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isFiniteNonZero());
1612   EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, true).isFiniteNonZero());
1613 
1614   // Test +/- sNaN. +/- dont mean anything with sNaN but paranoia can't hurt in
1615   // this instance.
1616   EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isFiniteNonZero());
1617   EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, true).isFiniteNonZero());
1618 }
1619 
TEST(APFloatTest,add)1620 TEST(APFloatTest, add) {
1621   // Test Special Cases against each other and normal values.
1622 
1623   // TODOS/NOTES:
1624   // 1. Since we perform only default exception handling all operations with
1625   // signaling NaNs should have a result that is a quiet NaN. Currently they
1626   // return sNaN.
1627 
1628   APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
1629   APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
1630   APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
1631   APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
1632   APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
1633   APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
1634   APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
1635   APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
1636   APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
1637   APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
1638   APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
1639   APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
1640   APFloat PSmallestNormalized =
1641     APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
1642   APFloat MSmallestNormalized =
1643     APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
1644 
1645   const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
1646 
1647   const unsigned NumTests = 169;
1648   struct {
1649     APFloat x;
1650     APFloat y;
1651     const char *result;
1652     int status;
1653     int category;
1654   } SpecialCaseTests[NumTests] = {
1655     { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1656     { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1657     { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1658     { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1659     { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1660 #if 0
1661     // See Note 1.
1662     { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1663 #endif
1664     { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1665     { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1666     { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1667     { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1668     { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1669     { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1670     { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1671     { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1672     { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1673     { MInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1674     { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1675     { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1676     { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1677 #if 0
1678     // See Note 1.
1679     { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1680 #endif
1681     { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1682     { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1683     { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1684     { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1685     { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1686     { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1687     { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1688     { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1689     { PZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1690     { PZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1691     { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1692     { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1693     { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1694 #if 0
1695     // See Note 1.
1696     { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1697 #endif
1698     { PZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1699     { PZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1700     { PZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1701     { PZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1702     { PZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1703     { PZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1704     { PZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1705     { PZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1706     { MZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1707     { MZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1708     { MZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1709     { MZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
1710     { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1711 #if 0
1712     // See Note 1.
1713     { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1714 #endif
1715     { MZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1716     { MZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1717     { MZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1718     { MZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1719     { MZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1720     { MZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1721     { MZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1722     { MZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1723     { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
1724     { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
1725     { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
1726     { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
1727     { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1728 #if 0
1729     // See Note 1.
1730     { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1731 #endif
1732     { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
1733     { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
1734     { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1735     { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1736     { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1737     { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
1738     { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
1739     { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
1740 #if 0
1741     // See Note 1.
1742     { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1743     { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1744     { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1745     { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1746     { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1747     { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1748     { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1749     { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1750     { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1751     { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1752     { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1753     { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1754     { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1755     { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1756 #endif
1757     { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1758     { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1759     { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1760     { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1761     { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1762 #if 0
1763     // See Note 1.
1764     { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1765 #endif
1766     { PNormalValue, PNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal },
1767     { PNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1768     { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1769     { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1770     { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1771     { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1772     { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1773     { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1774     { MNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1775     { MNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1776     { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1777     { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1778     { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1779 #if 0
1780     // See Note 1.
1781     { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1782 #endif
1783     { MNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1784     { MNormalValue, MNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal },
1785     { MNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1786     { MNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1787     { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1788     { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1789     { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1790     { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1791     { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1792     { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1793     { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1794     { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1795     { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1796 #if 0
1797     // See Note 1.
1798     { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1799 #endif
1800     { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1801     { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1802     { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
1803     { PLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1804     { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1805     { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1806     { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1807     { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1808     { MLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1809     { MLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1810     { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1811     { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1812     { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1813 #if 0
1814     // See Note 1.
1815     { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1816 #endif
1817     { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1818     { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1819     { MLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1820     { MLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
1821     { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1822     { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1823     { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1824     { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1825     { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1826     { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1827     { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1828     { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1829     { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1830 #if 0
1831     // See Note 1.
1832     { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1833 #endif
1834     { PSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1835     { PSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1836     { PSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1837     { PSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1838     { PSmallestValue, PSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal },
1839     { PSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1840     { PSmallestValue, PSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1841     { PSmallestValue, MSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1842     { MSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1843     { MSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1844     { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1845     { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1846     { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1847 #if 0
1848     // See Note 1.
1849     { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1850 #endif
1851     { MSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1852     { MSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1853     { MSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1854     { MSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1855     { MSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1856     { MSmallestValue, MSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal },
1857     { MSmallestValue, PSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1858     { MSmallestValue, MSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1859     { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1860     { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1861     { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1862     { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1863     { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1864 #if 0
1865 // See Note 1.
1866     { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1867 #endif
1868     { PSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1869     { PSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1870     { PSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1871     { PSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1872     { PSmallestNormalized, PSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1873     { PSmallestNormalized, MSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1874     { PSmallestNormalized, PSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
1875     { PSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1876     { MSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1877     { MSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1878     { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1879     { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1880     { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
1881 #if 0
1882     // See Note 1.
1883     { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1884 #endif
1885     { MSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1886     { MSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
1887     { MSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1888     { MSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
1889     { MSmallestNormalized, PSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
1890     { MSmallestNormalized, MSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
1891     { MSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1892     { MSmallestNormalized, MSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal }
1893   };
1894 
1895   for (size_t i = 0; i < NumTests; ++i) {
1896     APFloat x(SpecialCaseTests[i].x);
1897     APFloat y(SpecialCaseTests[i].y);
1898     APFloat::opStatus status = x.add(y, APFloat::rmNearestTiesToEven);
1899 
1900     APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result);
1901 
1902     EXPECT_TRUE(result.bitwiseIsEqual(x));
1903     EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
1904     EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
1905   }
1906 }
1907 
TEST(APFloatTest,subtract)1908 TEST(APFloatTest, subtract) {
1909   // Test Special Cases against each other and normal values.
1910 
1911   // TODOS/NOTES:
1912   // 1. Since we perform only default exception handling all operations with
1913   // signaling NaNs should have a result that is a quiet NaN. Currently they
1914   // return sNaN.
1915 
1916   APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
1917   APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
1918   APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
1919   APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
1920   APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
1921   APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
1922   APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
1923   APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
1924   APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
1925   APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
1926   APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
1927   APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
1928   APFloat PSmallestNormalized =
1929     APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
1930   APFloat MSmallestNormalized =
1931     APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
1932 
1933   const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
1934 
1935   const unsigned NumTests = 169;
1936   struct {
1937     APFloat x;
1938     APFloat y;
1939     const char *result;
1940     int status;
1941     int category;
1942   } SpecialCaseTests[NumTests] = {
1943     { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1944     { PInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1945     { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1946     { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
1947     { PInf, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
1948 #if 0
1949 // See Note 1.
1950     { PInf, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
1951 #endif
1952     { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1953     { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1954     { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1955     { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1956     { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1957     { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
1958     { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1959     { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
1960     { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1961     { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
1962     { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1963     { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
1964     { MInf, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
1965 #if 0
1966 // See Note 1.
1967     { MInf, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
1968 #endif
1969     { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1970     { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1971     { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1972     { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1973     { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1974     { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
1975     { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1976     { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
1977     { PZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1978     { PZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1979     { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1980     { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1981     { PZero, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
1982 #if 0
1983 // See Note 1.
1984     { PZero, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
1985 #endif
1986     { PZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
1987     { PZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
1988     { PZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1989     { PZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
1990     { PZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
1991     { PZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
1992     { PZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
1993     { PZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
1994     { MZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
1995     { MZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
1996     { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
1997     { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
1998     { MZero, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
1999 #if 0
2000 // See Note 1.
2001     { MZero, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2002 #endif
2003     { MZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2004     { MZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2005     { MZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2006     { MZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2007     { MZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2008     { MZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2009     { MZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2010     { MZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2011     { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2012     { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2013     { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2014     { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2015     { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2016 #if 0
2017 // See Note 1.
2018     { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2019 #endif
2020     { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2021     { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2022     { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2023     { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2024     { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2025     { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2026     { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2027     { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2028 #if 0
2029 // See Note 1.
2030     { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2031     { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2032     { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2033     { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2034     { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2035     { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2036     { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2037     { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2038     { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2039     { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2040     { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2041     { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2042     { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2043     { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2044 #endif
2045     { PNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2046     { PNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2047     { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2048     { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2049     { PNormalValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2050 #if 0
2051 // See Note 1.
2052     { PNormalValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2053 #endif
2054     { PNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2055     { PNormalValue, MNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal },
2056     { PNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2057     { PNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2058     { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2059     { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2060     { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2061     { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2062     { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2063     { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2064     { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2065     { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2066     { MNormalValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2067 #if 0
2068 // See Note 1.
2069     { MNormalValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2070 #endif
2071     { MNormalValue, PNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal },
2072     { MNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2073     { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2074     { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2075     { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2076     { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2077     { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2078     { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2079     { PLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2080     { PLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2081     { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2082     { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2083     { PLargestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2084 #if 0
2085 // See Note 1.
2086     { PLargestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2087 #endif
2088     { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2089     { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2090     { PLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2091     { PLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2092     { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2093     { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2094     { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2095     { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2096     { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2097     { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2098     { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2099     { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2100     { MLargestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2101 #if 0
2102 // See Note 1.
2103     { MLargestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2104 #endif
2105     { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2106     { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2107     { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2108     { MLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2109     { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2110     { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2111     { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2112     { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2113     { PSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2114     { PSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2115     { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2116     { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2117     { PSmallestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2118 #if 0
2119 // See Note 1.
2120     { PSmallestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2121 #endif
2122     { PSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2123     { PSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2124     { PSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2125     { PSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2126     { PSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2127     { PSmallestValue, MSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal },
2128     { PSmallestValue, PSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2129     { PSmallestValue, MSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2130     { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2131     { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2132     { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2133     { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2134     { MSmallestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2135 #if 0
2136 // See Note 1.
2137     { MSmallestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2138 #endif
2139     { MSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2140     { MSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2141     { MSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2142     { MSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2143     { MSmallestValue, PSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal },
2144     { MSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2145     { MSmallestValue, PSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2146     { MSmallestValue, MSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2147     { PSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2148     { PSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2149     { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2150     { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2151     { PSmallestNormalized, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2152 #if 0
2153 // See Note 1.
2154     { PSmallestNormalized, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2155 #endif
2156     { PSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2157     { PSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2158     { PSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2159     { PSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2160     { PSmallestNormalized, PSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2161     { PSmallestNormalized, MSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2162     { PSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2163     { PSmallestNormalized, MSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal },
2164     { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2165     { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2166     { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2167     { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2168     { MSmallestNormalized, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN },
2169 #if 0
2170 // See Note 1.
2171     { MSmallestNormalized, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN },
2172 #endif
2173     { MSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2174     { MSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal },
2175     { MSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2176     { MSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal },
2177     { MSmallestNormalized, PSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal },
2178     { MSmallestNormalized, MSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal },
2179     { MSmallestNormalized, PSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal },
2180     { MSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }
2181   };
2182 
2183   for (size_t i = 0; i < NumTests; ++i) {
2184     APFloat x(SpecialCaseTests[i].x);
2185     APFloat y(SpecialCaseTests[i].y);
2186     APFloat::opStatus status = x.subtract(y, APFloat::rmNearestTiesToEven);
2187 
2188     APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result);
2189 
2190     EXPECT_TRUE(result.bitwiseIsEqual(x));
2191     EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2192     EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2193   }
2194 }
2195 
TEST(APFloatTest,multiply)2196 TEST(APFloatTest, multiply) {
2197   // Test Special Cases against each other and normal values.
2198 
2199   // TODOS/NOTES:
2200   // 1. Since we perform only default exception handling all operations with
2201   // signaling NaNs should have a result that is a quiet NaN. Currently they
2202   // return sNaN.
2203 
2204   APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
2205   APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
2206   APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
2207   APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
2208   APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
2209   APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
2210   APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
2211   APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
2212   APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
2213   APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
2214   APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
2215   APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
2216   APFloat PSmallestNormalized =
2217     APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
2218   APFloat MSmallestNormalized =
2219     APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
2220 
2221   const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2222   const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact;
2223 
2224   const unsigned NumTests = 169;
2225   struct {
2226     APFloat x;
2227     APFloat y;
2228     const char *result;
2229     int status;
2230     int category;
2231   } SpecialCaseTests[NumTests] = {
2232     { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2233     { PInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2234     { PInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2235     { PInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2236     { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2237 #if 0
2238 // See Note 1.
2239     { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2240 #endif
2241     { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2242     { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2243     { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2244     { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2245     { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2246     { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2247     { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2248     { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2249     { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2250     { MInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2251     { MInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2252     { MInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2253     { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2254 #if 0
2255 // See Note 1.
2256     { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2257 #endif
2258     { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2259     { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2260     { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2261     { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2262     { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2263     { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2264     { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2265     { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2266     { PZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2267     { PZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2268     { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2269     { PZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2270     { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2271 #if 0
2272 // See Note 1.
2273     { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2274 #endif
2275     { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2276     { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2277     { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2278     { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2279     { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2280     { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2281     { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2282     { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2283     { MZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2284     { MZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2285     { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2286     { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2287     { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2288 #if 0
2289 // See Note 1.
2290     { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2291 #endif
2292     { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2293     { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2294     { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2295     { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2296     { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2297     { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2298     { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2299     { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2300     { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2301     { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2302     { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2303     { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2304     { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2305 #if 0
2306 // See Note 1.
2307     { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2308 #endif
2309     { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2310     { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2311     { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2312     { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2313     { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2314     { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2315     { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2316     { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2317 #if 0
2318 // See Note 1.
2319     { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2320     { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2321     { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2322     { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2323     { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2324     { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2325     { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2326     { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2327     { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2328     { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2329     { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2330     { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2331     { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2332     { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2333 #endif
2334     { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2335     { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2336     { PNormalValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2337     { PNormalValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2338     { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2339 #if 0
2340 // See Note 1.
2341     { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2342 #endif
2343     { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2344     { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2345     { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2346     { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2347     { PNormalValue, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2348     { PNormalValue, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2349     { PNormalValue, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2350     { PNormalValue, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2351     { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2352     { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2353     { MNormalValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2354     { MNormalValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2355     { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2356 #if 0
2357 // See Note 1.
2358     { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2359 #endif
2360     { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2361     { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2362     { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2363     { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2364     { MNormalValue, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2365     { MNormalValue, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2366     { MNormalValue, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2367     { MNormalValue, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2368     { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2369     { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2370     { PLargestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2371     { PLargestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2372     { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2373 #if 0
2374 // See Note 1.
2375     { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2376 #endif
2377     { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2378     { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2379     { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2380     { PLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2381     { PLargestValue, PSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2382     { PLargestValue, MSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2383     { PLargestValue, PSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2384     { PLargestValue, MSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2385     { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2386     { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2387     { MLargestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2388     { MLargestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2389     { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2390 #if 0
2391 // See Note 1.
2392     { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2393 #endif
2394     { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2395     { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2396     { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2397     { MLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2398     { MLargestValue, PSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2399     { MLargestValue, MSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2400     { MLargestValue, PSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2401     { MLargestValue, MSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2402     { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2403     { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2404     { PSmallestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2405     { PSmallestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2406     { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2407 #if 0
2408 // See Note 1.
2409     { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2410 #endif
2411     { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2412     { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2413     { PSmallestValue, PLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2414     { PSmallestValue, MLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2415     { PSmallestValue, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2416     { PSmallestValue, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2417     { PSmallestValue, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2418     { PSmallestValue, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2419     { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2420     { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2421     { MSmallestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2422     { MSmallestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2423     { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2424 #if 0
2425 // See Note 1.
2426     { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2427 #endif
2428     { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2429     { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2430     { MSmallestValue, PLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2431     { MSmallestValue, MLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal },
2432     { MSmallestValue, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2433     { MSmallestValue, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2434     { MSmallestValue, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2435     { MSmallestValue, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2436     { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2437     { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2438     { PSmallestNormalized, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2439     { PSmallestNormalized, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2440     { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2441 #if 0
2442 // See Note 1.
2443     { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2444 #endif
2445     { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2446     { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2447     { PSmallestNormalized, PLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2448     { PSmallestNormalized, MLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2449     { PSmallestNormalized, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2450     { PSmallestNormalized, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2451     { PSmallestNormalized, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2452     { PSmallestNormalized, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2453     { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity },
2454     { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity },
2455     { MSmallestNormalized, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2456     { MSmallestNormalized, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2457     { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2458 #if 0
2459 // See Note 1.
2460     { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2461 #endif
2462     { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2463     { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2464     { MSmallestNormalized, PLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2465     { MSmallestNormalized, MLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal },
2466     { MSmallestNormalized, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2467     { MSmallestNormalized, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2468     { MSmallestNormalized, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2469     { MSmallestNormalized, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero }
2470   };
2471 
2472   for (size_t i = 0; i < NumTests; ++i) {
2473     APFloat x(SpecialCaseTests[i].x);
2474     APFloat y(SpecialCaseTests[i].y);
2475     APFloat::opStatus status = x.multiply(y, APFloat::rmNearestTiesToEven);
2476 
2477     APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result);
2478 
2479     EXPECT_TRUE(result.bitwiseIsEqual(x));
2480     EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2481     EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2482   }
2483 }
2484 
TEST(APFloatTest,divide)2485 TEST(APFloatTest, divide) {
2486   // Test Special Cases against each other and normal values.
2487 
2488   // TODOS/NOTES:
2489   // 1. Since we perform only default exception handling all operations with
2490   // signaling NaNs should have a result that is a quiet NaN. Currently they
2491   // return sNaN.
2492 
2493   APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
2494   APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
2495   APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
2496   APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
2497   APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
2498   APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
2499   APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
2500   APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
2501   APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
2502   APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
2503   APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
2504   APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
2505   APFloat PSmallestNormalized =
2506     APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
2507   APFloat MSmallestNormalized =
2508     APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
2509 
2510   const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact;
2511   const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact;
2512 
2513   const unsigned NumTests = 169;
2514   struct {
2515     APFloat x;
2516     APFloat y;
2517     const char *result;
2518     int status;
2519     int category;
2520   } SpecialCaseTests[NumTests] = {
2521     { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2522     { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2523     { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2524     { PInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2525     { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2526 #if 0
2527 // See Note 1.
2528     { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2529 #endif
2530     { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2531     { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2532     { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2533     { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2534     { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2535     { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2536     { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2537     { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2538     { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2539     { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2540     { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity },
2541     { MInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity },
2542     { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2543 #if 0
2544 // See Note 1.
2545     { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2546 #endif
2547     { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2548     { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2549     { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2550     { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2551     { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity },
2552     { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity },
2553     { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity },
2554     { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity },
2555     { PZero, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2556     { PZero, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2557     { PZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2558     { PZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2559     { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2560 #if 0
2561 // See Note 1.
2562     { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2563 #endif
2564     { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2565     { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2566     { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2567     { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2568     { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2569     { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2570     { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2571     { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2572     { MZero, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2573     { MZero, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2574     { MZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2575     { MZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2576     { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2577 #if 0
2578 // See Note 1.
2579     { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2580 #endif
2581     { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2582     { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2583     { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2584     { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2585     { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2586     { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2587     { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2588     { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2589     { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN },
2590     { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN },
2591     { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN },
2592     { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN },
2593     { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2594 #if 0
2595 // See Note 1.
2596     { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2597 #endif
2598     { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2599     { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN },
2600     { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2601     { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2602     { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2603     { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN },
2604     { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2605     { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN },
2606 #if 0
2607 // See Note 1.
2608     { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2609     { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2610     { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2611     { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2612     { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2613     { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2614     { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2615     { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2616     { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2617     { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2618     { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2619     { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2620     { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2621     { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2622 #endif
2623     { PNormalValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2624     { PNormalValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2625     { PNormalValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2626     { PNormalValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2627     { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2628 #if 0
2629 // See Note 1.
2630     { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2631 #endif
2632     { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2633     { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2634     { PNormalValue, PLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal },
2635     { PNormalValue, MLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal },
2636     { PNormalValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2637     { PNormalValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2638     { PNormalValue, PSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal },
2639     { PNormalValue, MSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal },
2640     { MNormalValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2641     { MNormalValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2642     { MNormalValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2643     { MNormalValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2644     { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2645 #if 0
2646 // See Note 1.
2647     { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2648 #endif
2649     { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2650     { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2651     { MNormalValue, PLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal },
2652     { MNormalValue, MLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal },
2653     { MNormalValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2654     { MNormalValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2655     { MNormalValue, PSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal },
2656     { MNormalValue, MSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal },
2657     { PLargestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2658     { PLargestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2659     { PLargestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2660     { PLargestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2661     { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2662 #if 0
2663 // See Note 1.
2664     { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2665 #endif
2666     { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2667     { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2668     { PLargestValue, PLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2669     { PLargestValue, MLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2670     { PLargestValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2671     { PLargestValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2672     { PLargestValue, PSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity },
2673     { PLargestValue, MSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity },
2674     { MLargestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2675     { MLargestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2676     { MLargestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2677     { MLargestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2678     { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2679 #if 0
2680 // See Note 1.
2681     { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2682 #endif
2683     { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2684     { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal },
2685     { MLargestValue, PLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2686     { MLargestValue, MLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2687     { MLargestValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity },
2688     { MLargestValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity },
2689     { MLargestValue, PSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity },
2690     { MLargestValue, MSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity },
2691     { PSmallestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2692     { PSmallestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2693     { PSmallestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2694     { PSmallestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2695     { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2696 #if 0
2697 // See Note 1.
2698     { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2699 #endif
2700     { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2701     { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2702     { PSmallestValue, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2703     { PSmallestValue, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2704     { PSmallestValue, PSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2705     { PSmallestValue, MSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2706     { PSmallestValue, PSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal },
2707     { PSmallestValue, MSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal },
2708     { MSmallestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2709     { MSmallestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2710     { MSmallestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2711     { MSmallestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2712     { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2713 #if 0
2714 // See Note 1.
2715     { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2716 #endif
2717     { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal },
2718     { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal },
2719     { MSmallestValue, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2720     { MSmallestValue, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2721     { MSmallestValue, PSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2722     { MSmallestValue, MSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2723     { MSmallestValue, PSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal },
2724     { MSmallestValue, MSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal },
2725     { PSmallestNormalized, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2726     { PSmallestNormalized, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2727     { PSmallestNormalized, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2728     { PSmallestNormalized, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2729     { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2730 #if 0
2731 // See Note 1.
2732     { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2733 #endif
2734     { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2735     { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2736     { PSmallestNormalized, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2737     { PSmallestNormalized, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2738     { PSmallestNormalized, PSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal },
2739     { PSmallestNormalized, MSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal },
2740     { PSmallestNormalized, PSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2741     { PSmallestNormalized, MSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2742     { MSmallestNormalized, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero },
2743     { MSmallestNormalized, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero },
2744     { MSmallestNormalized, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity },
2745     { MSmallestNormalized, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity },
2746     { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN },
2747 #if 0
2748 // See Note 1.
2749     { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN },
2750 #endif
2751     { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal },
2752     { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal },
2753     { MSmallestNormalized, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero },
2754     { MSmallestNormalized, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero },
2755     { MSmallestNormalized, PSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal },
2756     { MSmallestNormalized, MSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal },
2757     { MSmallestNormalized, PSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal },
2758     { MSmallestNormalized, MSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal },
2759   };
2760 
2761   for (size_t i = 0; i < NumTests; ++i) {
2762     APFloat x(SpecialCaseTests[i].x);
2763     APFloat y(SpecialCaseTests[i].y);
2764     APFloat::opStatus status = x.divide(y, APFloat::rmNearestTiesToEven);
2765 
2766     APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result);
2767 
2768     EXPECT_TRUE(result.bitwiseIsEqual(x));
2769     EXPECT_TRUE((int)status == SpecialCaseTests[i].status);
2770     EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category);
2771   }
2772 }
2773 
TEST(APFloatTest,operatorOverloads)2774 TEST(APFloatTest, operatorOverloads) {
2775   // This is mostly testing that these operator overloads compile.
2776   APFloat One = APFloat(APFloat::IEEEsingle, "0x1p+0");
2777   APFloat Two = APFloat(APFloat::IEEEsingle, "0x2p+0");
2778   EXPECT_TRUE(Two.bitwiseIsEqual(One + One));
2779   EXPECT_TRUE(One.bitwiseIsEqual(Two - One));
2780   EXPECT_TRUE(Two.bitwiseIsEqual(One * Two));
2781   EXPECT_TRUE(One.bitwiseIsEqual(Two / Two));
2782 }
2783 
TEST(APFloatTest,abs)2784 TEST(APFloatTest, abs) {
2785   APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
2786   APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
2787   APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
2788   APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
2789   APFloat PQNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
2790   APFloat MQNaN = APFloat::getNaN(APFloat::IEEEsingle, true);
2791   APFloat PSNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
2792   APFloat MSNaN = APFloat::getSNaN(APFloat::IEEEsingle, true);
2793   APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0");
2794   APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0");
2795   APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false);
2796   APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true);
2797   APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false);
2798   APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true);
2799   APFloat PSmallestNormalized =
2800     APFloat::getSmallestNormalized(APFloat::IEEEsingle, false);
2801   APFloat MSmallestNormalized =
2802     APFloat::getSmallestNormalized(APFloat::IEEEsingle, true);
2803 
2804   EXPECT_TRUE(PInf.bitwiseIsEqual(abs(PInf)));
2805   EXPECT_TRUE(PInf.bitwiseIsEqual(abs(MInf)));
2806   EXPECT_TRUE(PZero.bitwiseIsEqual(abs(PZero)));
2807   EXPECT_TRUE(PZero.bitwiseIsEqual(abs(MZero)));
2808   EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(PQNaN)));
2809   EXPECT_TRUE(PQNaN.bitwiseIsEqual(abs(MQNaN)));
2810   EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(PSNaN)));
2811   EXPECT_TRUE(PSNaN.bitwiseIsEqual(abs(MSNaN)));
2812   EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(PNormalValue)));
2813   EXPECT_TRUE(PNormalValue.bitwiseIsEqual(abs(MNormalValue)));
2814   EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(PLargestValue)));
2815   EXPECT_TRUE(PLargestValue.bitwiseIsEqual(abs(MLargestValue)));
2816   EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(PSmallestValue)));
2817   EXPECT_TRUE(PSmallestValue.bitwiseIsEqual(abs(MSmallestValue)));
2818   EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(PSmallestNormalized)));
2819   EXPECT_TRUE(PSmallestNormalized.bitwiseIsEqual(abs(MSmallestNormalized)));
2820 }
2821 
TEST(APFloatTest,ilogb)2822 TEST(APFloatTest, ilogb) {
2823   EXPECT_EQ(-1074, ilogb(APFloat::getSmallest(APFloat::IEEEdouble, false)));
2824   EXPECT_EQ(-1074, ilogb(APFloat::getSmallest(APFloat::IEEEdouble, true)));
2825   EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble, "0x1.ffffffffffffep-1024")));
2826   EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble, "0x1.ffffffffffffep-1023")));
2827   EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble, "-0x1.ffffffffffffep-1023")));
2828   EXPECT_EQ(-51, ilogb(APFloat(APFloat::IEEEdouble, "0x1p-51")));
2829   EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble, "0x1.c60f120d9f87cp-1023")));
2830   EXPECT_EQ(-2, ilogb(APFloat(APFloat::IEEEdouble, "0x0.ffffp-1")));
2831   EXPECT_EQ(-1023, ilogb(APFloat(APFloat::IEEEdouble, "0x1.fffep-1023")));
2832   EXPECT_EQ(1023, ilogb(APFloat::getLargest(APFloat::IEEEdouble, false)));
2833   EXPECT_EQ(1023, ilogb(APFloat::getLargest(APFloat::IEEEdouble, true)));
2834 
2835 
2836   EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle, "0x1p+0")));
2837   EXPECT_EQ(0, ilogb(APFloat(APFloat::IEEEsingle, "-0x1p+0")));
2838   EXPECT_EQ(42, ilogb(APFloat(APFloat::IEEEsingle, "0x1p+42")));
2839   EXPECT_EQ(-42, ilogb(APFloat(APFloat::IEEEsingle, "0x1p-42")));
2840 
2841   EXPECT_EQ(APFloat::IEK_Inf,
2842             ilogb(APFloat::getInf(APFloat::IEEEsingle, false)));
2843   EXPECT_EQ(APFloat::IEK_Inf,
2844             ilogb(APFloat::getInf(APFloat::IEEEsingle, true)));
2845   EXPECT_EQ(APFloat::IEK_Zero,
2846             ilogb(APFloat::getZero(APFloat::IEEEsingle, false)));
2847   EXPECT_EQ(APFloat::IEK_Zero,
2848             ilogb(APFloat::getZero(APFloat::IEEEsingle, true)));
2849   EXPECT_EQ(APFloat::IEK_NaN,
2850             ilogb(APFloat::getNaN(APFloat::IEEEsingle, false)));
2851   EXPECT_EQ(APFloat::IEK_NaN,
2852             ilogb(APFloat::getSNaN(APFloat::IEEEsingle, false)));
2853 
2854   EXPECT_EQ(127, ilogb(APFloat::getLargest(APFloat::IEEEsingle, false)));
2855   EXPECT_EQ(127, ilogb(APFloat::getLargest(APFloat::IEEEsingle, true)));
2856 
2857   EXPECT_EQ(-149, ilogb(APFloat::getSmallest(APFloat::IEEEsingle, false)));
2858   EXPECT_EQ(-149, ilogb(APFloat::getSmallest(APFloat::IEEEsingle, true)));
2859   EXPECT_EQ(-126,
2860             ilogb(APFloat::getSmallestNormalized(APFloat::IEEEsingle, false)));
2861   EXPECT_EQ(-126,
2862             ilogb(APFloat::getSmallestNormalized(APFloat::IEEEsingle, true)));
2863 }
2864 
TEST(APFloatTest,scalbn)2865 TEST(APFloatTest, scalbn) {
2866 
2867   const APFloat::roundingMode RM = APFloat::rmNearestTiesToEven;
2868   EXPECT_TRUE(
2869       APFloat(APFloat::IEEEsingle, "0x1p+0")
2870       .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), 0, RM)));
2871   EXPECT_TRUE(
2872       APFloat(APFloat::IEEEsingle, "0x1p+42")
2873       .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), 42, RM)));
2874   EXPECT_TRUE(
2875       APFloat(APFloat::IEEEsingle, "0x1p-42")
2876       .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), -42, RM)));
2877 
2878   APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false);
2879   APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true);
2880   APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false);
2881   APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true);
2882   APFloat QPNaN = APFloat::getNaN(APFloat::IEEEsingle, false);
2883   APFloat QMNaN = APFloat::getNaN(APFloat::IEEEsingle, true);
2884   APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false);
2885 
2886   EXPECT_TRUE(PInf.bitwiseIsEqual(scalbn(PInf, 0, RM)));
2887   EXPECT_TRUE(MInf.bitwiseIsEqual(scalbn(MInf, 0, RM)));
2888   EXPECT_TRUE(PZero.bitwiseIsEqual(scalbn(PZero, 0, RM)));
2889   EXPECT_TRUE(MZero.bitwiseIsEqual(scalbn(MZero, 0, RM)));
2890   EXPECT_TRUE(QPNaN.bitwiseIsEqual(scalbn(QPNaN, 0, RM)));
2891   EXPECT_TRUE(QMNaN.bitwiseIsEqual(scalbn(QMNaN, 0, RM)));
2892   EXPECT_FALSE(scalbn(SNaN, 0, RM).isSignaling());
2893 
2894   APFloat ScalbnSNaN = scalbn(SNaN, 1, RM);
2895   EXPECT_TRUE(ScalbnSNaN.isNaN() && !ScalbnSNaN.isSignaling());
2896 
2897   // Make sure highest bit of payload is preserved.
2898   const APInt Payload(64, (UINT64_C(1) << 50) |
2899                       (UINT64_C(1) << 49) |
2900                       (UINT64_C(1234) << 32) |
2901                       1);
2902 
2903   APFloat SNaNWithPayload = APFloat::getSNaN(APFloat::IEEEdouble, false,
2904                                              &Payload);
2905   APFloat QuietPayload = scalbn(SNaNWithPayload, 1, RM);
2906   EXPECT_TRUE(QuietPayload.isNaN() && !QuietPayload.isSignaling());
2907   EXPECT_EQ(Payload, QuietPayload.bitcastToAPInt().getLoBits(51));
2908 
2909   EXPECT_TRUE(PInf.bitwiseIsEqual(
2910                 scalbn(APFloat(APFloat::IEEEsingle, "0x1p+0"), 128, RM)));
2911   EXPECT_TRUE(MInf.bitwiseIsEqual(
2912                 scalbn(APFloat(APFloat::IEEEsingle, "-0x1p+0"), 128, RM)));
2913   EXPECT_TRUE(PInf.bitwiseIsEqual(
2914                 scalbn(APFloat(APFloat::IEEEsingle, "0x1p+127"), 1, RM)));
2915   EXPECT_TRUE(PZero.bitwiseIsEqual(
2916                 scalbn(APFloat(APFloat::IEEEsingle, "0x1p-127"), -127, RM)));
2917   EXPECT_TRUE(MZero.bitwiseIsEqual(
2918                 scalbn(APFloat(APFloat::IEEEsingle, "-0x1p-127"), -127, RM)));
2919   EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "-0x1p-149").bitwiseIsEqual(
2920                 scalbn(APFloat(APFloat::IEEEsingle, "-0x1p-127"), -22, RM)));
2921   EXPECT_TRUE(PZero.bitwiseIsEqual(
2922                 scalbn(APFloat(APFloat::IEEEsingle, "0x1p-126"), -24, RM)));
2923 
2924 
2925   APFloat SmallestF64 = APFloat::getSmallest(APFloat::IEEEdouble, false);
2926   APFloat NegSmallestF64 = APFloat::getSmallest(APFloat::IEEEdouble, true);
2927 
2928   APFloat LargestF64 = APFloat::getLargest(APFloat::IEEEdouble, false);
2929   APFloat NegLargestF64 = APFloat::getLargest(APFloat::IEEEdouble, true);
2930 
2931   APFloat SmallestNormalizedF64
2932     = APFloat::getSmallestNormalized(APFloat::IEEEdouble, false);
2933   APFloat NegSmallestNormalizedF64
2934     = APFloat::getSmallestNormalized(APFloat::IEEEdouble, true);
2935 
2936   APFloat LargestDenormalF64(APFloat::IEEEdouble, "0x1.ffffffffffffep-1023");
2937   APFloat NegLargestDenormalF64(APFloat::IEEEdouble, "-0x1.ffffffffffffep-1023");
2938 
2939 
2940   EXPECT_TRUE(SmallestF64.bitwiseIsEqual(
2941                 scalbn(APFloat(APFloat::IEEEdouble, "0x1p-1074"), 0, RM)));
2942   EXPECT_TRUE(NegSmallestF64.bitwiseIsEqual(
2943                 scalbn(APFloat(APFloat::IEEEdouble, "-0x1p-1074"), 0, RM)));
2944 
2945   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1p+1023")
2946               .bitwiseIsEqual(scalbn(SmallestF64, 2097, RM)));
2947 
2948   EXPECT_TRUE(scalbn(SmallestF64, -2097, RM).isPosZero());
2949   EXPECT_TRUE(scalbn(SmallestF64, -2098, RM).isPosZero());
2950   EXPECT_TRUE(scalbn(SmallestF64, -2099, RM).isPosZero());
2951   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1p+1022")
2952               .bitwiseIsEqual(scalbn(SmallestF64, 2096, RM)));
2953   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1p+1023")
2954               .bitwiseIsEqual(scalbn(SmallestF64, 2097, RM)));
2955   EXPECT_TRUE(scalbn(SmallestF64, 2098, RM).isInfinity());
2956   EXPECT_TRUE(scalbn(SmallestF64, 2099, RM).isInfinity());
2957 
2958   // Test for integer overflows when adding to exponent.
2959   EXPECT_TRUE(scalbn(SmallestF64, -INT_MAX, RM).isPosZero());
2960   EXPECT_TRUE(scalbn(LargestF64, INT_MAX, RM).isInfinity());
2961 
2962   EXPECT_TRUE(LargestDenormalF64
2963               .bitwiseIsEqual(scalbn(LargestDenormalF64, 0, RM)));
2964   EXPECT_TRUE(NegLargestDenormalF64
2965               .bitwiseIsEqual(scalbn(NegLargestDenormalF64, 0, RM)));
2966 
2967   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.ffffffffffffep-1022")
2968               .bitwiseIsEqual(scalbn(LargestDenormalF64, 1, RM)));
2969   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-0x1.ffffffffffffep-1021")
2970               .bitwiseIsEqual(scalbn(NegLargestDenormalF64, 2, RM)));
2971 
2972   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.ffffffffffffep+1")
2973               .bitwiseIsEqual(scalbn(LargestDenormalF64, 1024, RM)));
2974   EXPECT_TRUE(scalbn(LargestDenormalF64, -1023, RM).isPosZero());
2975   EXPECT_TRUE(scalbn(LargestDenormalF64, -1024, RM).isPosZero());
2976   EXPECT_TRUE(scalbn(LargestDenormalF64, -2048, RM).isPosZero());
2977   EXPECT_TRUE(scalbn(LargestDenormalF64, 2047, RM).isInfinity());
2978   EXPECT_TRUE(scalbn(LargestDenormalF64, 2098, RM).isInfinity());
2979   EXPECT_TRUE(scalbn(LargestDenormalF64, 2099, RM).isInfinity());
2980 
2981   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.ffffffffffffep-2")
2982               .bitwiseIsEqual(scalbn(LargestDenormalF64, 1021, RM)));
2983   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.ffffffffffffep-1")
2984               .bitwiseIsEqual(scalbn(LargestDenormalF64, 1022, RM)));
2985   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.ffffffffffffep+0")
2986               .bitwiseIsEqual(scalbn(LargestDenormalF64, 1023, RM)));
2987   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.ffffffffffffep+1023")
2988               .bitwiseIsEqual(scalbn(LargestDenormalF64, 2046, RM)));
2989   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1p+974")
2990               .bitwiseIsEqual(scalbn(SmallestF64, 2048, RM)));
2991 
2992   APFloat RandomDenormalF64(APFloat::IEEEdouble, "0x1.c60f120d9f87cp+51");
2993   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.c60f120d9f87cp-972")
2994               .bitwiseIsEqual(scalbn(RandomDenormalF64, -1023, RM)));
2995   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.c60f120d9f87cp-1")
2996               .bitwiseIsEqual(scalbn(RandomDenormalF64, -52, RM)));
2997   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.c60f120d9f87cp-2")
2998               .bitwiseIsEqual(scalbn(RandomDenormalF64, -53, RM)));
2999   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.c60f120d9f87cp+0")
3000               .bitwiseIsEqual(scalbn(RandomDenormalF64, -51, RM)));
3001 
3002   EXPECT_TRUE(scalbn(RandomDenormalF64, -2097, RM).isPosZero());
3003   EXPECT_TRUE(scalbn(RandomDenormalF64, -2090, RM).isPosZero());
3004 
3005 
3006   EXPECT_TRUE(
3007     APFloat(APFloat::IEEEdouble, "-0x1p-1073")
3008     .bitwiseIsEqual(scalbn(NegLargestF64, -2097, RM)));
3009 
3010   EXPECT_TRUE(
3011     APFloat(APFloat::IEEEdouble, "-0x1p-1024")
3012     .bitwiseIsEqual(scalbn(NegLargestF64, -2048, RM)));
3013 
3014   EXPECT_TRUE(
3015     APFloat(APFloat::IEEEdouble, "0x1p-1073")
3016     .bitwiseIsEqual(scalbn(LargestF64, -2097, RM)));
3017 
3018   EXPECT_TRUE(
3019     APFloat(APFloat::IEEEdouble, "0x1p-1074")
3020     .bitwiseIsEqual(scalbn(LargestF64, -2098, RM)));
3021   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-0x1p-1074")
3022               .bitwiseIsEqual(scalbn(NegLargestF64, -2098, RM)));
3023   EXPECT_TRUE(scalbn(NegLargestF64, -2099, RM).isNegZero());
3024   EXPECT_TRUE(scalbn(LargestF64, 1, RM).isInfinity());
3025 
3026 
3027   EXPECT_TRUE(
3028     APFloat(APFloat::IEEEdouble, "0x1p+0")
3029     .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEdouble, "0x1p+52"), -52, RM)));
3030 
3031   EXPECT_TRUE(
3032     APFloat(APFloat::IEEEdouble, "0x1p-103")
3033     .bitwiseIsEqual(scalbn(APFloat(APFloat::IEEEdouble, "0x1p-51"), -52, RM)));
3034 }
3035 
TEST(APFloatTest,frexp)3036 TEST(APFloatTest, frexp) {
3037   const APFloat::roundingMode RM = APFloat::rmNearestTiesToEven;
3038 
3039   APFloat PZero = APFloat::getZero(APFloat::IEEEdouble, false);
3040   APFloat MZero = APFloat::getZero(APFloat::IEEEdouble, true);
3041   APFloat One(1.0);
3042   APFloat MOne(-1.0);
3043   APFloat Two(2.0);
3044   APFloat MTwo(-2.0);
3045 
3046   APFloat LargestDenormal(APFloat::IEEEdouble, "0x1.ffffffffffffep-1023");
3047   APFloat NegLargestDenormal(APFloat::IEEEdouble, "-0x1.ffffffffffffep-1023");
3048 
3049   APFloat Smallest = APFloat::getSmallest(APFloat::IEEEdouble, false);
3050   APFloat NegSmallest = APFloat::getSmallest(APFloat::IEEEdouble, true);
3051 
3052   APFloat Largest = APFloat::getLargest(APFloat::IEEEdouble, false);
3053   APFloat NegLargest = APFloat::getLargest(APFloat::IEEEdouble, true);
3054 
3055   APFloat PInf = APFloat::getInf(APFloat::IEEEdouble, false);
3056   APFloat MInf = APFloat::getInf(APFloat::IEEEdouble, true);
3057 
3058   APFloat QPNaN = APFloat::getNaN(APFloat::IEEEdouble, false);
3059   APFloat QMNaN = APFloat::getNaN(APFloat::IEEEdouble, true);
3060   APFloat SNaN = APFloat::getSNaN(APFloat::IEEEdouble, false);
3061 
3062   // Make sure highest bit of payload is preserved.
3063   const APInt Payload(64, (UINT64_C(1) << 50) |
3064                       (UINT64_C(1) << 49) |
3065                       (UINT64_C(1234) << 32) |
3066                       1);
3067 
3068   APFloat SNaNWithPayload = APFloat::getSNaN(APFloat::IEEEdouble, false,
3069                                              &Payload);
3070 
3071   APFloat SmallestNormalized
3072     = APFloat::getSmallestNormalized(APFloat::IEEEdouble, false);
3073   APFloat NegSmallestNormalized
3074     = APFloat::getSmallestNormalized(APFloat::IEEEdouble, true);
3075 
3076   int Exp;
3077   APFloat Frac(APFloat::IEEEdouble);
3078 
3079 
3080   Frac = frexp(PZero, Exp, RM);
3081   EXPECT_EQ(0, Exp);
3082   EXPECT_TRUE(Frac.isPosZero());
3083 
3084   Frac = frexp(MZero, Exp, RM);
3085   EXPECT_EQ(0, Exp);
3086   EXPECT_TRUE(Frac.isNegZero());
3087 
3088 
3089   Frac = frexp(One, Exp, RM);
3090   EXPECT_EQ(1, Exp);
3091   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1p-1").bitwiseIsEqual(Frac));
3092 
3093   Frac = frexp(MOne, Exp, RM);
3094   EXPECT_EQ(1, Exp);
3095   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-0x1p-1").bitwiseIsEqual(Frac));
3096 
3097   Frac = frexp(LargestDenormal, Exp, RM);
3098   EXPECT_EQ(-1022, Exp);
3099   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.ffffffffffffep-1").bitwiseIsEqual(Frac));
3100 
3101   Frac = frexp(NegLargestDenormal, Exp, RM);
3102   EXPECT_EQ(-1022, Exp);
3103   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-0x1.ffffffffffffep-1").bitwiseIsEqual(Frac));
3104 
3105 
3106   Frac = frexp(Smallest, Exp, RM);
3107   EXPECT_EQ(-1073, Exp);
3108   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1p-1").bitwiseIsEqual(Frac));
3109 
3110   Frac = frexp(NegSmallest, Exp, RM);
3111   EXPECT_EQ(-1073, Exp);
3112   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-0x1p-1").bitwiseIsEqual(Frac));
3113 
3114 
3115   Frac = frexp(Largest, Exp, RM);
3116   EXPECT_EQ(1024, Exp);
3117   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.fffffffffffffp-1").bitwiseIsEqual(Frac));
3118 
3119   Frac = frexp(NegLargest, Exp, RM);
3120   EXPECT_EQ(1024, Exp);
3121   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-0x1.fffffffffffffp-1").bitwiseIsEqual(Frac));
3122 
3123 
3124   Frac = frexp(PInf, Exp, RM);
3125   EXPECT_EQ(INT_MAX, Exp);
3126   EXPECT_TRUE(Frac.isInfinity() && !Frac.isNegative());
3127 
3128   Frac = frexp(MInf, Exp, RM);
3129   EXPECT_EQ(INT_MAX, Exp);
3130   EXPECT_TRUE(Frac.isInfinity() && Frac.isNegative());
3131 
3132   Frac = frexp(QPNaN, Exp, RM);
3133   EXPECT_EQ(INT_MIN, Exp);
3134   EXPECT_TRUE(Frac.isNaN());
3135 
3136   Frac = frexp(QMNaN, Exp, RM);
3137   EXPECT_EQ(INT_MIN, Exp);
3138   EXPECT_TRUE(Frac.isNaN());
3139 
3140   Frac = frexp(SNaN, Exp, RM);
3141   EXPECT_EQ(INT_MIN, Exp);
3142   EXPECT_TRUE(Frac.isNaN() && !Frac.isSignaling());
3143 
3144   Frac = frexp(SNaNWithPayload, Exp, RM);
3145   EXPECT_EQ(INT_MIN, Exp);
3146   EXPECT_TRUE(Frac.isNaN() && !Frac.isSignaling());
3147   EXPECT_EQ(Payload, Frac.bitcastToAPInt().getLoBits(51));
3148 
3149   Frac = frexp(APFloat(APFloat::IEEEdouble, "0x0.ffffp-1"), Exp, RM);
3150   EXPECT_EQ(-1, Exp);
3151   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.fffep-1").bitwiseIsEqual(Frac));
3152 
3153   Frac = frexp(APFloat(APFloat::IEEEdouble, "0x1p-51"), Exp, RM);
3154   EXPECT_EQ(-50, Exp);
3155   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1p-1").bitwiseIsEqual(Frac));
3156 
3157   Frac = frexp(APFloat(APFloat::IEEEdouble, "0x1.c60f120d9f87cp+51"), Exp, RM);
3158   EXPECT_EQ(52, Exp);
3159   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "0x1.c60f120d9f87cp-1").bitwiseIsEqual(Frac));
3160 }
3161 }
3162