• 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/SmallString.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "gtest/gtest.h"
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,Denormal)36 TEST(APFloatTest, Denormal) {
37   APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven;
38 
39   // Test single precision
40   {
41     const char *MinNormalStr = "1.17549435082228750797e-38";
42     EXPECT_FALSE(APFloat(APFloat::IEEEsingle, MinNormalStr).isDenormal());
43     EXPECT_FALSE(APFloat(APFloat::IEEEsingle, 0.0).isDenormal());
44 
45     APFloat Val2(APFloat::IEEEsingle, 2.0e0);
46     APFloat T(APFloat::IEEEsingle, MinNormalStr);
47     T.divide(Val2, rdmd);
48     EXPECT_TRUE(T.isDenormal());
49   }
50 
51   // Test double precision
52   {
53     const char *MinNormalStr = "2.22507385850720138309e-308";
54     EXPECT_FALSE(APFloat(APFloat::IEEEdouble, MinNormalStr).isDenormal());
55     EXPECT_FALSE(APFloat(APFloat::IEEEdouble, 0.0).isDenormal());
56 
57     APFloat Val2(APFloat::IEEEdouble, 2.0e0);
58     APFloat T(APFloat::IEEEdouble, MinNormalStr);
59     T.divide(Val2, rdmd);
60     EXPECT_TRUE(T.isDenormal());
61   }
62 
63   // Test Intel double-ext
64   {
65     const char *MinNormalStr = "3.36210314311209350626e-4932";
66     EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, MinNormalStr).isDenormal());
67     EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, 0.0).isDenormal());
68 
69     APFloat Val2(APFloat::x87DoubleExtended, 2.0e0);
70     APFloat T(APFloat::x87DoubleExtended, MinNormalStr);
71     T.divide(Val2, rdmd);
72     EXPECT_TRUE(T.isDenormal());
73   }
74 
75   // Test quadruple precision
76   {
77     const char *MinNormalStr = "3.36210314311209350626267781732175260e-4932";
78     EXPECT_FALSE(APFloat(APFloat::IEEEquad, MinNormalStr).isDenormal());
79     EXPECT_FALSE(APFloat(APFloat::IEEEquad, 0.0).isDenormal());
80 
81     APFloat Val2(APFloat::IEEEquad, 2.0e0);
82     APFloat T(APFloat::IEEEquad, MinNormalStr);
83     T.divide(Val2, rdmd);
84     EXPECT_TRUE(T.isDenormal());
85   }
86 }
87 
TEST(APFloatTest,Zero)88 TEST(APFloatTest, Zero) {
89   EXPECT_EQ(0.0f,  APFloat(0.0f).convertToFloat());
90   EXPECT_EQ(-0.0f, APFloat(-0.0f).convertToFloat());
91   EXPECT_TRUE(APFloat(-0.0f).isNegative());
92 
93   EXPECT_EQ(0.0,  APFloat(0.0).convertToDouble());
94   EXPECT_EQ(-0.0, APFloat(-0.0).convertToDouble());
95   EXPECT_TRUE(APFloat(-0.0).isNegative());
96 }
97 
TEST(APFloatTest,fromZeroDecimalString)98 TEST(APFloatTest, fromZeroDecimalString) {
99   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0").convertToDouble());
100   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0").convertToDouble());
101   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0").convertToDouble());
102 
103   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.").convertToDouble());
104   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.").convertToDouble());
105   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.").convertToDouble());
106 
107   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  ".0").convertToDouble());
108   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0").convertToDouble());
109   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0").convertToDouble());
110 
111   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.0").convertToDouble());
112   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0").convertToDouble());
113   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0").convertToDouble());
114 
115   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "00000.").convertToDouble());
116   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+00000.").convertToDouble());
117   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-00000.").convertToDouble());
118 
119   EXPECT_EQ(0.0,  APFloat(APFloat::IEEEdouble, ".00000").convertToDouble());
120   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.00000").convertToDouble());
121   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.00000").convertToDouble());
122 
123   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0000.00000").convertToDouble());
124   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0000.00000").convertToDouble());
125   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0000.00000").convertToDouble());
126 }
127 
TEST(APFloatTest,fromZeroDecimalSingleExponentString)128 TEST(APFloatTest, fromZeroDecimalSingleExponentString) {
129   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,   "0e1").convertToDouble());
130   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble,  "+0e1").convertToDouble());
131   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble,  "-0e1").convertToDouble());
132 
133   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0e+1").convertToDouble());
134   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1").convertToDouble());
135   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1").convertToDouble());
136 
137   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0e-1").convertToDouble());
138   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1").convertToDouble());
139   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1").convertToDouble());
140 
141 
142   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,   "0.e1").convertToDouble());
143   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble,  "+0.e1").convertToDouble());
144   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble,  "-0.e1").convertToDouble());
145 
146   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.e+1").convertToDouble());
147   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e+1").convertToDouble());
148   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e+1").convertToDouble());
149 
150   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.e-1").convertToDouble());
151   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e-1").convertToDouble());
152   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e-1").convertToDouble());
153 
154   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,   ".0e1").convertToDouble());
155   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble,  "+.0e1").convertToDouble());
156   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble,  "-.0e1").convertToDouble());
157 
158   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  ".0e+1").convertToDouble());
159   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e+1").convertToDouble());
160   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e+1").convertToDouble());
161 
162   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  ".0e-1").convertToDouble());
163   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e-1").convertToDouble());
164   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e-1").convertToDouble());
165 
166 
167   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,   "0.0e1").convertToDouble());
168   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble,  "+0.0e1").convertToDouble());
169   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble,  "-0.0e1").convertToDouble());
170 
171   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.0e+1").convertToDouble());
172   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e+1").convertToDouble());
173   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e+1").convertToDouble());
174 
175   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0.0e-1").convertToDouble());
176   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e-1").convertToDouble());
177   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e-1").convertToDouble());
178 
179 
180   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "000.0000e1").convertToDouble());
181   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+000.0000e+1").convertToDouble());
182   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-000.0000e+1").convertToDouble());
183 }
184 
TEST(APFloatTest,fromZeroDecimalLargeExponentString)185 TEST(APFloatTest, fromZeroDecimalLargeExponentString) {
186   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0e1234").convertToDouble());
187   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e1234").convertToDouble());
188   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e1234").convertToDouble());
189 
190   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0e+1234").convertToDouble());
191   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1234").convertToDouble());
192   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1234").convertToDouble());
193 
194   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0e-1234").convertToDouble());
195   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1234").convertToDouble());
196   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1234").convertToDouble());
197 
198   EXPECT_EQ(0.0,  APFloat(APFloat::IEEEdouble, "000.0000e1234").convertToDouble());
199   EXPECT_EQ(0.0,  APFloat(APFloat::IEEEdouble, "000.0000e-1234").convertToDouble());
200 
201   EXPECT_EQ(0.0,  APFloat(APFloat::IEEEdouble, StringRef("0e1234\02", 6)).convertToDouble());
202 }
203 
TEST(APFloatTest,fromZeroHexadecimalString)204 TEST(APFloatTest, fromZeroHexadecimalString) {
205   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0p1").convertToDouble());
206   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p1").convertToDouble());
207   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1").convertToDouble());
208 
209   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0p+1").convertToDouble());
210   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p+1").convertToDouble());
211   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p+1").convertToDouble());
212 
213   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0p-1").convertToDouble());
214   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p-1").convertToDouble());
215   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p-1").convertToDouble());
216 
217 
218   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.p1").convertToDouble());
219   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p1").convertToDouble());
220   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p1").convertToDouble());
221 
222   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.p+1").convertToDouble());
223   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p+1").convertToDouble());
224   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p+1").convertToDouble());
225 
226   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.p-1").convertToDouble());
227   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p-1").convertToDouble());
228   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p-1").convertToDouble());
229 
230 
231   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x.0p1").convertToDouble());
232   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p1").convertToDouble());
233   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p1").convertToDouble());
234 
235   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x.0p+1").convertToDouble());
236   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p+1").convertToDouble());
237   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p+1").convertToDouble());
238 
239   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x.0p-1").convertToDouble());
240   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p-1").convertToDouble());
241   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p-1").convertToDouble());
242 
243 
244   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.0p1").convertToDouble());
245   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p1").convertToDouble());
246   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p1").convertToDouble());
247 
248   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.0p+1").convertToDouble());
249   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p+1").convertToDouble());
250   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p+1").convertToDouble());
251 
252   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble,  "0x0.0p-1").convertToDouble());
253   EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p-1").convertToDouble());
254   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p-1").convertToDouble());
255 
256 
257   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1").convertToDouble());
258   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1").convertToDouble());
259   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1").convertToDouble());
260   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1").convertToDouble());
261   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p1234").convertToDouble());
262   EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1234").convertToDouble());
263   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1234").convertToDouble());
264   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1234").convertToDouble());
265   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1234").convertToDouble());
266   EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1234").convertToDouble());
267 }
268 
TEST(APFloatTest,fromDecimalString)269 TEST(APFloatTest, fromDecimalString) {
270   EXPECT_EQ(1.0,      APFloat(APFloat::IEEEdouble, "1").convertToDouble());
271   EXPECT_EQ(2.0,      APFloat(APFloat::IEEEdouble, "2.").convertToDouble());
272   EXPECT_EQ(0.5,      APFloat(APFloat::IEEEdouble, ".5").convertToDouble());
273   EXPECT_EQ(1.0,      APFloat(APFloat::IEEEdouble, "1.0").convertToDouble());
274   EXPECT_EQ(-2.0,     APFloat(APFloat::IEEEdouble, "-2").convertToDouble());
275   EXPECT_EQ(-4.0,     APFloat(APFloat::IEEEdouble, "-4.").convertToDouble());
276   EXPECT_EQ(-0.5,     APFloat(APFloat::IEEEdouble, "-.5").convertToDouble());
277   EXPECT_EQ(-1.5,     APFloat(APFloat::IEEEdouble, "-1.5").convertToDouble());
278   EXPECT_EQ(1.25e12,  APFloat(APFloat::IEEEdouble, "1.25e12").convertToDouble());
279   EXPECT_EQ(1.25e+12, APFloat(APFloat::IEEEdouble, "1.25e+12").convertToDouble());
280   EXPECT_EQ(1.25e-12, APFloat(APFloat::IEEEdouble, "1.25e-12").convertToDouble());
281   EXPECT_EQ(1024.0,   APFloat(APFloat::IEEEdouble, "1024.").convertToDouble());
282   EXPECT_EQ(1024.05,  APFloat(APFloat::IEEEdouble, "1024.05000").convertToDouble());
283   EXPECT_EQ(0.05,     APFloat(APFloat::IEEEdouble, ".05000").convertToDouble());
284   EXPECT_EQ(2.0,      APFloat(APFloat::IEEEdouble, "2.").convertToDouble());
285   EXPECT_EQ(2.0e2,    APFloat(APFloat::IEEEdouble, "2.e2").convertToDouble());
286   EXPECT_EQ(2.0e+2,   APFloat(APFloat::IEEEdouble, "2.e+2").convertToDouble());
287   EXPECT_EQ(2.0e-2,   APFloat(APFloat::IEEEdouble, "2.e-2").convertToDouble());
288   EXPECT_EQ(2.05e2,    APFloat(APFloat::IEEEdouble, "002.05000e2").convertToDouble());
289   EXPECT_EQ(2.05e+2,   APFloat(APFloat::IEEEdouble, "002.05000e+2").convertToDouble());
290   EXPECT_EQ(2.05e-2,   APFloat(APFloat::IEEEdouble, "002.05000e-2").convertToDouble());
291   EXPECT_EQ(2.05e12,   APFloat(APFloat::IEEEdouble, "002.05000e12").convertToDouble());
292   EXPECT_EQ(2.05e+12,  APFloat(APFloat::IEEEdouble, "002.05000e+12").convertToDouble());
293   EXPECT_EQ(2.05e-12,  APFloat(APFloat::IEEEdouble, "002.05000e-12").convertToDouble());
294 
295   // These are "carefully selected" to overflow the fast log-base
296   // calculations in APFloat.cpp
297   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "99e99999").isInfinity());
298   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-99e99999").isInfinity());
299   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "1e-99999").isPosZero());
300   EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-1e-99999").isNegZero());
301 }
302 
TEST(APFloatTest,fromHexadecimalString)303 TEST(APFloatTest, fromHexadecimalString) {
304   EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble,  "0x1p0").convertToDouble());
305   EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p0").convertToDouble());
306   EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p0").convertToDouble());
307 
308   EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble,  "0x1p+0").convertToDouble());
309   EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p+0").convertToDouble());
310   EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p+0").convertToDouble());
311 
312   EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble,  "0x1p-0").convertToDouble());
313   EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p-0").convertToDouble());
314   EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p-0").convertToDouble());
315 
316 
317   EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble,  "0x1p1").convertToDouble());
318   EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p1").convertToDouble());
319   EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p1").convertToDouble());
320 
321   EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble,  "0x1p+1").convertToDouble());
322   EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p+1").convertToDouble());
323   EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p+1").convertToDouble());
324 
325   EXPECT_EQ( 0.5, APFloat(APFloat::IEEEdouble,  "0x1p-1").convertToDouble());
326   EXPECT_EQ(+0.5, APFloat(APFloat::IEEEdouble, "+0x1p-1").convertToDouble());
327   EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble, "-0x1p-1").convertToDouble());
328 
329 
330   EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble,  "0x1.8p1").convertToDouble());
331   EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p1").convertToDouble());
332   EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p1").convertToDouble());
333 
334   EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble,  "0x1.8p+1").convertToDouble());
335   EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p+1").convertToDouble());
336   EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p+1").convertToDouble());
337 
338   EXPECT_EQ( 0.75, APFloat(APFloat::IEEEdouble,  "0x1.8p-1").convertToDouble());
339   EXPECT_EQ(+0.75, APFloat(APFloat::IEEEdouble, "+0x1.8p-1").convertToDouble());
340   EXPECT_EQ(-0.75, APFloat(APFloat::IEEEdouble, "-0x1.8p-1").convertToDouble());
341 
342 
343   EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble,  "0x1000.000p1").convertToDouble());
344   EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p1").convertToDouble());
345   EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p1").convertToDouble());
346 
347   EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble,  "0x1000.000p+1").convertToDouble());
348   EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p+1").convertToDouble());
349   EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p+1").convertToDouble());
350 
351   EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble,  "0x1000.000p-1").convertToDouble());
352   EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p-1").convertToDouble());
353   EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p-1").convertToDouble());
354 
355 
356   EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble,  "0x1000p1").convertToDouble());
357   EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p1").convertToDouble());
358   EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p1").convertToDouble());
359 
360   EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble,  "0x1000p+1").convertToDouble());
361   EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p+1").convertToDouble());
362   EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p+1").convertToDouble());
363 
364   EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble,  "0x1000p-1").convertToDouble());
365   EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000p-1").convertToDouble());
366   EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000p-1").convertToDouble());
367 
368 
369   EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble,  "0x10p10").convertToDouble());
370   EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p10").convertToDouble());
371   EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p10").convertToDouble());
372 
373   EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble,  "0x10p+10").convertToDouble());
374   EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p+10").convertToDouble());
375   EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p+10").convertToDouble());
376 
377   EXPECT_EQ( 0.015625, APFloat(APFloat::IEEEdouble,  "0x10p-10").convertToDouble());
378   EXPECT_EQ(+0.015625, APFloat(APFloat::IEEEdouble, "+0x10p-10").convertToDouble());
379   EXPECT_EQ(-0.015625, APFloat(APFloat::IEEEdouble, "-0x10p-10").convertToDouble());
380 
381   EXPECT_EQ(1.0625, APFloat(APFloat::IEEEdouble, "0x1.1p0").convertToDouble());
382   EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "0x1p0").convertToDouble());
383 
384   EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
385 }
386 
TEST(APFloatTest,toString)387 TEST(APFloatTest, toString) {
388   ASSERT_EQ("10", convertToString(10.0, 6, 3));
389   ASSERT_EQ("1.0E+1", convertToString(10.0, 6, 0));
390   ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2));
391   ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 4, 2));
392   ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 5, 1));
393   ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2));
394   ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2));
395   ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 5, 1));
396   ASSERT_EQ("0.7853981633974483", convertToString(0.78539816339744830961, 0, 3));
397   ASSERT_EQ("4.940656458412465E-324", convertToString(4.9406564584124654e-324, 0, 3));
398   ASSERT_EQ("873.1834", convertToString(873.1834, 0, 1));
399   ASSERT_EQ("8.731834E+2", convertToString(873.1834, 0, 0));
400 }
401 
TEST(APFloatTest,toInteger)402 TEST(APFloatTest, toInteger) {
403   bool isExact = false;
404   APSInt result(5, /*isUnsigned=*/true);
405 
406   EXPECT_EQ(APFloat::opOK,
407             APFloat(APFloat::IEEEdouble, "10")
408             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
409   EXPECT_TRUE(isExact);
410   EXPECT_EQ(APSInt(APInt(5, 10), true), result);
411 
412   EXPECT_EQ(APFloat::opInvalidOp,
413             APFloat(APFloat::IEEEdouble, "-10")
414             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
415   EXPECT_FALSE(isExact);
416   EXPECT_EQ(APSInt::getMinValue(5, true), result);
417 
418   EXPECT_EQ(APFloat::opInvalidOp,
419             APFloat(APFloat::IEEEdouble, "32")
420             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
421   EXPECT_FALSE(isExact);
422   EXPECT_EQ(APSInt::getMaxValue(5, true), result);
423 
424   EXPECT_EQ(APFloat::opInexact,
425             APFloat(APFloat::IEEEdouble, "7.9")
426             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
427   EXPECT_FALSE(isExact);
428   EXPECT_EQ(APSInt(APInt(5, 7), true), result);
429 
430   result.setIsUnsigned(false);
431   EXPECT_EQ(APFloat::opOK,
432             APFloat(APFloat::IEEEdouble, "-10")
433             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
434   EXPECT_TRUE(isExact);
435   EXPECT_EQ(APSInt(APInt(5, -10, true), false), result);
436 
437   EXPECT_EQ(APFloat::opInvalidOp,
438             APFloat(APFloat::IEEEdouble, "-17")
439             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
440   EXPECT_FALSE(isExact);
441   EXPECT_EQ(APSInt::getMinValue(5, false), result);
442 
443   EXPECT_EQ(APFloat::opInvalidOp,
444             APFloat(APFloat::IEEEdouble, "16")
445             .convertToInteger(result, APFloat::rmTowardZero, &isExact));
446   EXPECT_FALSE(isExact);
447   EXPECT_EQ(APSInt::getMaxValue(5, false), result);
448 }
449 
nanbits(const fltSemantics & Sem,bool SNaN,bool Negative,uint64_t fill)450 static APInt nanbits(const fltSemantics &Sem,
451                      bool SNaN, bool Negative, uint64_t fill) {
452   APInt apfill(64, fill);
453   if (SNaN)
454     return APFloat::getSNaN(Sem, Negative, &apfill).bitcastToAPInt();
455   else
456     return APFloat::getQNaN(Sem, Negative, &apfill).bitcastToAPInt();
457 }
458 
TEST(APFloatTest,makeNaN)459 TEST(APFloatTest, makeNaN) {
460   ASSERT_EQ(0x7fc00000, nanbits(APFloat::IEEEsingle, false, false, 0));
461   ASSERT_EQ(0xffc00000, nanbits(APFloat::IEEEsingle, false, true, 0));
462   ASSERT_EQ(0x7fc0ae72, nanbits(APFloat::IEEEsingle, false, false, 0xae72));
463   ASSERT_EQ(0x7fffae72, nanbits(APFloat::IEEEsingle, false, false, 0xffffae72));
464   ASSERT_EQ(0x7fa00000, nanbits(APFloat::IEEEsingle, true, false, 0));
465   ASSERT_EQ(0xffa00000, nanbits(APFloat::IEEEsingle, true, true, 0));
466   ASSERT_EQ(0x7f80ae72, nanbits(APFloat::IEEEsingle, true, false, 0xae72));
467   ASSERT_EQ(0x7fbfae72, nanbits(APFloat::IEEEsingle, true, false, 0xffffae72));
468 
469   ASSERT_EQ(0x7ff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, false, 0));
470   ASSERT_EQ(0xfff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, true, 0));
471   ASSERT_EQ(0x7ff800000000ae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xae72));
472   ASSERT_EQ(0x7fffffffffffae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xffffffffffffae72ULL));
473   ASSERT_EQ(0x7ff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, false, 0));
474   ASSERT_EQ(0xfff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, true, 0));
475   ASSERT_EQ(0x7ff000000000ae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xae72));
476   ASSERT_EQ(0x7ff7ffffffffae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xffffffffffffae72ULL));
477 }
478 
479 #ifdef GTEST_HAS_DEATH_TEST
480 #ifndef NDEBUG
TEST(APFloatTest,SemanticsDeath)481 TEST(APFloatTest, SemanticsDeath) {
482   EXPECT_DEATH(APFloat(APFloat::IEEEsingle, 0.0f).convertToDouble(), "Float semantics are not IEEEdouble");
483   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, 0.0 ).convertToFloat(),  "Float semantics are not IEEEsingle");
484 }
485 
TEST(APFloatTest,StringDecimalDeath)486 TEST(APFloatTest, StringDecimalDeath) {
487   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  ""), "Invalid string length");
488   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+"), "String has no digits");
489   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-"), "String has no digits");
490 
491   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("\0", 1)), "Invalid character in significand");
492   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\0", 2)), "Invalid character in significand");
493   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02", 3)), "Invalid character in significand");
494   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02e1", 5)), "Invalid character in significand");
495   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e\0", 3)), "Invalid character in exponent");
496   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\0", 4)), "Invalid character in exponent");
497   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\02", 5)), "Invalid character in exponent");
498 
499   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0f"), "Invalid character in significand");
500 
501   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".."), "String contains multiple dots");
502   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "..0"), "String contains multiple dots");
503   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0.0"), "String contains multiple dots");
504 }
505 
TEST(APFloatTest,StringDecimalSignificandDeath)506 TEST(APFloatTest, StringDecimalSignificandDeath) {
507   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "."), "Significand has no digits");
508   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+."), "Significand has no digits");
509   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-."), "Significand has no digits");
510 
511 
512   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "e"), "Significand has no digits");
513   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e"), "Significand has no digits");
514   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e"), "Significand has no digits");
515 
516   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "e1"), "Significand has no digits");
517   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e1"), "Significand has no digits");
518   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e1"), "Significand has no digits");
519 
520   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  ".e1"), "Significand has no digits");
521   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e1"), "Significand has no digits");
522   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e1"), "Significand has no digits");
523 
524 
525   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  ".e"), "Significand has no digits");
526   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e"), "Significand has no digits");
527   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e"), "Significand has no digits");
528 }
529 
TEST(APFloatTest,StringDecimalExponentDeath)530 TEST(APFloatTest, StringDecimalExponentDeath) {
531   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,   "1e"), "Exponent has no digits");
532   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "+1e"), "Exponent has no digits");
533   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "-1e"), "Exponent has no digits");
534 
535   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,   "1.e"), "Exponent has no digits");
536   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "+1.e"), "Exponent has no digits");
537   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "-1.e"), "Exponent has no digits");
538 
539   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,   ".1e"), "Exponent has no digits");
540   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "+.1e"), "Exponent has no digits");
541   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "-.1e"), "Exponent has no digits");
542 
543   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,   "1.1e"), "Exponent has no digits");
544   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "+1.1e"), "Exponent has no digits");
545   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "-1.1e"), "Exponent has no digits");
546 
547 
548   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e+"), "Exponent has no digits");
549   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e-"), "Exponent has no digits");
550 
551   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  ".1e"), "Exponent has no digits");
552   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e+"), "Exponent has no digits");
553   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e-"), "Exponent has no digits");
554 
555   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "1.0e"), "Exponent has no digits");
556   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e+"), "Exponent has no digits");
557   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e-"), "Exponent has no digits");
558 }
559 
TEST(APFloatTest,StringHexadecimalDeath)560 TEST(APFloatTest, StringHexadecimalDeath) {
561   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x"), "Invalid string");
562   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x"), "Invalid string");
563   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x"), "Invalid string");
564 
565   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x0"), "Hex strings require an exponent");
566   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0"), "Hex strings require an exponent");
567   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0"), "Hex strings require an exponent");
568 
569   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x0."), "Hex strings require an exponent");
570   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0."), "Hex strings require an exponent");
571   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0."), "Hex strings require an exponent");
572 
573   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.0"), "Hex strings require an exponent");
574   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.0"), "Hex strings require an exponent");
575   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.0"), "Hex strings require an exponent");
576 
577   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x0.0"), "Hex strings require an exponent");
578   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0.0"), "Hex strings require an exponent");
579   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0.0"), "Hex strings require an exponent");
580 
581   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x\0", 3)), "Invalid character in significand");
582   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\0", 4)), "Invalid character in significand");
583   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02", 5)), "Invalid character in significand");
584   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02p1", 7)), "Invalid character in significand");
585   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p\0", 5)), "Invalid character in exponent");
586   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\0", 6)), "Invalid character in exponent");
587   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\02", 7)), "Invalid character in exponent");
588 
589   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p0f"), "Invalid character in exponent");
590 
591   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..p1"), "String contains multiple dots");
592   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..0p1"), "String contains multiple dots");
593   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.0.0p1"), "String contains multiple dots");
594 }
595 
TEST(APFloatTest,StringHexadecimalSignificandDeath)596 TEST(APFloatTest, StringHexadecimalSignificandDeath) {
597   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x."), "Significand has no digits");
598   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x."), "Significand has no digits");
599   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x."), "Significand has no digits");
600 
601   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0xp"), "Significand has no digits");
602   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp"), "Significand has no digits");
603   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp"), "Significand has no digits");
604 
605   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0xp+"), "Significand has no digits");
606   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp+"), "Significand has no digits");
607   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp+"), "Significand has no digits");
608 
609   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0xp-"), "Significand has no digits");
610   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp-"), "Significand has no digits");
611   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp-"), "Significand has no digits");
612 
613 
614   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.p"), "Significand has no digits");
615   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p"), "Significand has no digits");
616   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p"), "Significand has no digits");
617 
618   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.p+"), "Significand has no digits");
619   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p+"), "Significand has no digits");
620   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p+"), "Significand has no digits");
621 
622   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.p-"), "Significand has no digits");
623   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p-"), "Significand has no digits");
624   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p-"), "Significand has no digits");
625 }
626 
TEST(APFloatTest,StringHexadecimalExponentDeath)627 TEST(APFloatTest, StringHexadecimalExponentDeath) {
628   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1p"), "Exponent has no digits");
629   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p"), "Exponent has no digits");
630   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p"), "Exponent has no digits");
631 
632   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1p+"), "Exponent has no digits");
633   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p+"), "Exponent has no digits");
634   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p+"), "Exponent has no digits");
635 
636   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1p-"), "Exponent has no digits");
637   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p-"), "Exponent has no digits");
638   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p-"), "Exponent has no digits");
639 
640 
641   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.p"), "Exponent has no digits");
642   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p"), "Exponent has no digits");
643   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p"), "Exponent has no digits");
644 
645   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.p+"), "Exponent has no digits");
646   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p+"), "Exponent has no digits");
647   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p+"), "Exponent has no digits");
648 
649   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.p-"), "Exponent has no digits");
650   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p-"), "Exponent has no digits");
651   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p-"), "Exponent has no digits");
652 
653 
654   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.1p"), "Exponent has no digits");
655   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p"), "Exponent has no digits");
656   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p"), "Exponent has no digits");
657 
658   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.1p+"), "Exponent has no digits");
659   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p+"), "Exponent has no digits");
660   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p+"), "Exponent has no digits");
661 
662   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x.1p-"), "Exponent has no digits");
663   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p-"), "Exponent has no digits");
664   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p-"), "Exponent has no digits");
665 
666 
667   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.1p"), "Exponent has no digits");
668   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p"), "Exponent has no digits");
669   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p"), "Exponent has no digits");
670 
671   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.1p+"), "Exponent has no digits");
672   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p+"), "Exponent has no digits");
673   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p+"), "Exponent has no digits");
674 
675   EXPECT_DEATH(APFloat(APFloat::IEEEdouble,  "0x1.1p-"), "Exponent has no digits");
676   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p-"), "Exponent has no digits");
677   EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p-"), "Exponent has no digits");
678 }
679 #endif
680 #endif
681 
TEST(APFloatTest,exactInverse)682 TEST(APFloatTest, exactInverse) {
683   APFloat inv(0.0f);
684 
685   // Trivial operation.
686   EXPECT_TRUE(APFloat(2.0).getExactInverse(&inv));
687   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5)));
688   EXPECT_TRUE(APFloat(2.0f).getExactInverse(&inv));
689   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5f)));
690   EXPECT_TRUE(APFloat(APFloat::IEEEquad, "2.0").getExactInverse(&inv));
691   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::IEEEquad, "0.5")));
692   EXPECT_TRUE(APFloat(APFloat::PPCDoubleDouble, "2.0").getExactInverse(&inv));
693   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::PPCDoubleDouble, "0.5")));
694   EXPECT_TRUE(APFloat(APFloat::x87DoubleExtended, "2.0").getExactInverse(&inv));
695   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::x87DoubleExtended, "0.5")));
696 
697   // FLT_MIN
698   EXPECT_TRUE(APFloat(1.17549435e-38f).getExactInverse(&inv));
699   EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(8.5070592e+37f)));
700 
701   // Large float, inverse is a denormal.
702   EXPECT_FALSE(APFloat(1.7014118e38f).getExactInverse(0));
703   // Zero
704   EXPECT_FALSE(APFloat(0.0).getExactInverse(0));
705   // Denormalized float
706   EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(0));
707 }
708 
TEST(APFloatTest,roundToIntegral)709 TEST(APFloatTest, roundToIntegral) {
710   APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble)), P(0.0);
711 
712   P = T;
713   P.roundToIntegral(APFloat::rmTowardZero);
714   EXPECT_EQ(-0.0, P.convertToDouble());
715   P = T;
716   P.roundToIntegral(APFloat::rmTowardNegative);
717   EXPECT_EQ(-1.0, P.convertToDouble());
718   P = T;
719   P.roundToIntegral(APFloat::rmTowardPositive);
720   EXPECT_EQ(-0.0, P.convertToDouble());
721   P = T;
722   P.roundToIntegral(APFloat::rmNearestTiesToEven);
723   EXPECT_EQ(-0.0, P.convertToDouble());
724 
725   P = S;
726   P.roundToIntegral(APFloat::rmTowardZero);
727   EXPECT_EQ(3.0, P.convertToDouble());
728   P = S;
729   P.roundToIntegral(APFloat::rmTowardNegative);
730   EXPECT_EQ(3.0, P.convertToDouble());
731   P = S;
732   P.roundToIntegral(APFloat::rmTowardPositive);
733   EXPECT_EQ(4.0, P.convertToDouble());
734   P = S;
735   P.roundToIntegral(APFloat::rmNearestTiesToEven);
736   EXPECT_EQ(3.0, P.convertToDouble());
737 
738   P = R;
739   P.roundToIntegral(APFloat::rmTowardZero);
740   EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
741   P = R;
742   P.roundToIntegral(APFloat::rmTowardNegative);
743   EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
744   P = R;
745   P.roundToIntegral(APFloat::rmTowardPositive);
746   EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
747   P = R;
748   P.roundToIntegral(APFloat::rmNearestTiesToEven);
749   EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
750 
751   P = APFloat::getZero(APFloat::IEEEdouble);
752   P.roundToIntegral(APFloat::rmTowardZero);
753   EXPECT_EQ(0.0, P.convertToDouble());
754   P = APFloat::getZero(APFloat::IEEEdouble, true);
755   P.roundToIntegral(APFloat::rmTowardZero);
756   EXPECT_EQ(-0.0, P.convertToDouble());
757   P = APFloat::getNaN(APFloat::IEEEdouble);
758   P.roundToIntegral(APFloat::rmTowardZero);
759   EXPECT_TRUE(IsNAN(P.convertToDouble()));
760   P = APFloat::getInf(APFloat::IEEEdouble);
761   P.roundToIntegral(APFloat::rmTowardZero);
762   EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() > 0.0);
763   P = APFloat::getInf(APFloat::IEEEdouble, true);
764   P.roundToIntegral(APFloat::rmTowardZero);
765   EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() < 0.0);
766 
767 }
768 
TEST(APFloatTest,getLargest)769 TEST(APFloatTest, getLargest) {
770   EXPECT_EQ(3.402823466e+38f, APFloat::getLargest(APFloat::IEEEsingle).convertToFloat());
771   EXPECT_EQ(1.7976931348623158e+308, APFloat::getLargest(APFloat::IEEEdouble).convertToDouble());
772 }
773 
TEST(APFloatTest,convert)774 TEST(APFloatTest, convert) {
775   bool losesInfo;
776   APFloat test(APFloat::IEEEdouble, "1.0");
777   test.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
778   EXPECT_EQ(1.0f, test.convertToFloat());
779   EXPECT_FALSE(losesInfo);
780 
781   test = APFloat(APFloat::x87DoubleExtended, "0x1p-53");
782   test.add(APFloat(APFloat::x87DoubleExtended, "1.0"), APFloat::rmNearestTiesToEven);
783   test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
784   EXPECT_EQ(1.0, test.convertToDouble());
785   EXPECT_TRUE(losesInfo);
786 
787   test = APFloat(APFloat::IEEEquad, "0x1p-53");
788   test.add(APFloat(APFloat::IEEEquad, "1.0"), APFloat::rmNearestTiesToEven);
789   test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
790   EXPECT_EQ(1.0, test.convertToDouble());
791   EXPECT_TRUE(losesInfo);
792 
793   test = APFloat(APFloat::x87DoubleExtended, "0xf.fffffffp+28");
794   test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
795   EXPECT_EQ(4294967295.0, test.convertToDouble());
796   EXPECT_FALSE(losesInfo);
797 
798   test = APFloat::getSNaN(APFloat::IEEEsingle);
799   APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended);
800   test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
801                &losesInfo);
802   EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
803   EXPECT_FALSE(losesInfo);
804 
805   test = APFloat::getQNaN(APFloat::IEEEsingle);
806   APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended);
807   test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
808                &losesInfo);
809   EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
810   EXPECT_FALSE(losesInfo);
811 
812   test = APFloat::getSNaN(APFloat::x87DoubleExtended);
813   test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
814                &losesInfo);
815   EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN));
816   EXPECT_FALSE(losesInfo);
817 
818   test = APFloat::getQNaN(APFloat::x87DoubleExtended);
819   test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
820                &losesInfo);
821   EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN));
822   EXPECT_FALSE(losesInfo);
823 }
824 
TEST(APFloatTest,PPCDoubleDouble)825 TEST(APFloatTest, PPCDoubleDouble) {
826   APFloat test(APFloat::PPCDoubleDouble, "1.0");
827   EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
828   EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
829 
830   test.divide(APFloat(APFloat::PPCDoubleDouble, "3.0"), APFloat::rmNearestTiesToEven);
831   EXPECT_EQ(0x3fd5555555555555ull, test.bitcastToAPInt().getRawData()[0]);
832   EXPECT_EQ(0x3c75555555555556ull, test.bitcastToAPInt().getRawData()[1]);
833 
834   // LDBL_MAX
835   test = APFloat(APFloat::PPCDoubleDouble, "1.79769313486231580793728971405301e+308");
836   EXPECT_EQ(0x7fefffffffffffffull, test.bitcastToAPInt().getRawData()[0]);
837   EXPECT_EQ(0x7c8ffffffffffffeull, test.bitcastToAPInt().getRawData()[1]);
838 
839   // LDBL_MIN
840   test = APFloat(APFloat::PPCDoubleDouble, "2.00416836000897277799610805135016e-292");
841   EXPECT_EQ(0x0360000000000000ull, test.bitcastToAPInt().getRawData()[0]);
842   EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
843 
844   test = APFloat(APFloat::PPCDoubleDouble, "1.0");
845   test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-105"), APFloat::rmNearestTiesToEven);
846   EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
847   EXPECT_EQ(0x3960000000000000ull, test.bitcastToAPInt().getRawData()[1]);
848 
849   test = APFloat(APFloat::PPCDoubleDouble, "1.0");
850   test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-106"), APFloat::rmNearestTiesToEven);
851   EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
852 #if 0 // XFAIL
853   // This is what we would expect with a true double-double implementation
854   EXPECT_EQ(0x3950000000000000ull, test.bitcastToAPInt().getRawData()[1]);
855 #else
856   // This is what we get with our 106-bit mantissa approximation
857   EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
858 #endif
859 }
860 }
861