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 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 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 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 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.isZero() && !test.isNegative()); 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.isZero() && test.isNegative()); 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 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 479 TEST(APFloatTest, Denormal) { 480 APFloat::roundingMode rdmd = APFloat::rmNearestTiesToEven; 481 482 // Test single precision 483 { 484 const char *MinNormalStr = "1.17549435082228750797e-38"; 485 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, MinNormalStr).isDenormal()); 486 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, 0.0).isDenormal()); 487 488 APFloat Val2(APFloat::IEEEsingle, 2.0e0); 489 APFloat T(APFloat::IEEEsingle, MinNormalStr); 490 T.divide(Val2, rdmd); 491 EXPECT_TRUE(T.isDenormal()); 492 } 493 494 // Test double precision 495 { 496 const char *MinNormalStr = "2.22507385850720138309e-308"; 497 EXPECT_FALSE(APFloat(APFloat::IEEEdouble, MinNormalStr).isDenormal()); 498 EXPECT_FALSE(APFloat(APFloat::IEEEdouble, 0.0).isDenormal()); 499 500 APFloat Val2(APFloat::IEEEdouble, 2.0e0); 501 APFloat T(APFloat::IEEEdouble, MinNormalStr); 502 T.divide(Val2, rdmd); 503 EXPECT_TRUE(T.isDenormal()); 504 } 505 506 // Test Intel double-ext 507 { 508 const char *MinNormalStr = "3.36210314311209350626e-4932"; 509 EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, MinNormalStr).isDenormal()); 510 EXPECT_FALSE(APFloat(APFloat::x87DoubleExtended, 0.0).isDenormal()); 511 512 APFloat Val2(APFloat::x87DoubleExtended, 2.0e0); 513 APFloat T(APFloat::x87DoubleExtended, MinNormalStr); 514 T.divide(Val2, rdmd); 515 EXPECT_TRUE(T.isDenormal()); 516 } 517 518 // Test quadruple precision 519 { 520 const char *MinNormalStr = "3.36210314311209350626267781732175260e-4932"; 521 EXPECT_FALSE(APFloat(APFloat::IEEEquad, MinNormalStr).isDenormal()); 522 EXPECT_FALSE(APFloat(APFloat::IEEEquad, 0.0).isDenormal()); 523 524 APFloat Val2(APFloat::IEEEquad, 2.0e0); 525 APFloat T(APFloat::IEEEquad, MinNormalStr); 526 T.divide(Val2, rdmd); 527 EXPECT_TRUE(T.isDenormal()); 528 } 529 } 530 531 TEST(APFloatTest, Zero) { 532 EXPECT_EQ(0.0f, APFloat(0.0f).convertToFloat()); 533 EXPECT_EQ(-0.0f, APFloat(-0.0f).convertToFloat()); 534 EXPECT_TRUE(APFloat(-0.0f).isNegative()); 535 536 EXPECT_EQ(0.0, APFloat(0.0).convertToDouble()); 537 EXPECT_EQ(-0.0, APFloat(-0.0).convertToDouble()); 538 EXPECT_TRUE(APFloat(-0.0).isNegative()); 539 } 540 541 TEST(APFloatTest, DecimalStringsWithoutNullTerminators) { 542 // Make sure that we can parse strings without null terminators. 543 // rdar://14323230. 544 APFloat Val(APFloat::IEEEdouble); 545 Val.convertFromString(StringRef("0.00", 3), 546 llvm::APFloat::rmNearestTiesToEven); 547 EXPECT_EQ(Val.convertToDouble(), 0.0); 548 Val.convertFromString(StringRef("0.01", 3), 549 llvm::APFloat::rmNearestTiesToEven); 550 EXPECT_EQ(Val.convertToDouble(), 0.0); 551 Val.convertFromString(StringRef("0.09", 3), 552 llvm::APFloat::rmNearestTiesToEven); 553 EXPECT_EQ(Val.convertToDouble(), 0.0); 554 Val.convertFromString(StringRef("0.095", 4), 555 llvm::APFloat::rmNearestTiesToEven); 556 EXPECT_EQ(Val.convertToDouble(), 0.09); 557 Val.convertFromString(StringRef("0.00e+3", 7), 558 llvm::APFloat::rmNearestTiesToEven); 559 EXPECT_EQ(Val.convertToDouble(), 0.00); 560 Val.convertFromString(StringRef("0e+3", 4), 561 llvm::APFloat::rmNearestTiesToEven); 562 EXPECT_EQ(Val.convertToDouble(), 0.00); 563 564 } 565 566 TEST(APFloatTest, fromZeroDecimalString) { 567 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0").convertToDouble()); 568 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0").convertToDouble()); 569 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0").convertToDouble()); 570 571 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.").convertToDouble()); 572 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.").convertToDouble()); 573 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.").convertToDouble()); 574 575 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0").convertToDouble()); 576 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0").convertToDouble()); 577 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0").convertToDouble()); 578 579 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0").convertToDouble()); 580 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0").convertToDouble()); 581 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0").convertToDouble()); 582 583 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "00000.").convertToDouble()); 584 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+00000.").convertToDouble()); 585 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-00000.").convertToDouble()); 586 587 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, ".00000").convertToDouble()); 588 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.00000").convertToDouble()); 589 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.00000").convertToDouble()); 590 591 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0000.00000").convertToDouble()); 592 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0000.00000").convertToDouble()); 593 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0000.00000").convertToDouble()); 594 } 595 596 TEST(APFloatTest, fromZeroDecimalSingleExponentString) { 597 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e1").convertToDouble()); 598 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e1").convertToDouble()); 599 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e1").convertToDouble()); 600 601 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e+1").convertToDouble()); 602 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1").convertToDouble()); 603 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1").convertToDouble()); 604 605 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e-1").convertToDouble()); 606 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1").convertToDouble()); 607 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1").convertToDouble()); 608 609 610 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e1").convertToDouble()); 611 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e1").convertToDouble()); 612 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e1").convertToDouble()); 613 614 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e+1").convertToDouble()); 615 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e+1").convertToDouble()); 616 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e+1").convertToDouble()); 617 618 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e-1").convertToDouble()); 619 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e-1").convertToDouble()); 620 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e-1").convertToDouble()); 621 622 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e1").convertToDouble()); 623 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e1").convertToDouble()); 624 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e1").convertToDouble()); 625 626 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e+1").convertToDouble()); 627 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e+1").convertToDouble()); 628 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e+1").convertToDouble()); 629 630 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e-1").convertToDouble()); 631 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e-1").convertToDouble()); 632 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e-1").convertToDouble()); 633 634 635 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e1").convertToDouble()); 636 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e1").convertToDouble()); 637 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e1").convertToDouble()); 638 639 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e+1").convertToDouble()); 640 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e+1").convertToDouble()); 641 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e+1").convertToDouble()); 642 643 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e-1").convertToDouble()); 644 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e-1").convertToDouble()); 645 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e-1").convertToDouble()); 646 647 648 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "000.0000e1").convertToDouble()); 649 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+000.0000e+1").convertToDouble()); 650 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-000.0000e+1").convertToDouble()); 651 } 652 653 TEST(APFloatTest, fromZeroDecimalLargeExponentString) { 654 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e1234").convertToDouble()); 655 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e1234").convertToDouble()); 656 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e1234").convertToDouble()); 657 658 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e+1234").convertToDouble()); 659 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1234").convertToDouble()); 660 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1234").convertToDouble()); 661 662 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e-1234").convertToDouble()); 663 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1234").convertToDouble()); 664 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1234").convertToDouble()); 665 666 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, "000.0000e1234").convertToDouble()); 667 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, "000.0000e-1234").convertToDouble()); 668 669 EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, StringRef("0e1234\02", 6)).convertToDouble()); 670 } 671 672 TEST(APFloatTest, fromZeroHexadecimalString) { 673 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p1").convertToDouble()); 674 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p1").convertToDouble()); 675 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1").convertToDouble()); 676 677 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p+1").convertToDouble()); 678 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p+1").convertToDouble()); 679 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p+1").convertToDouble()); 680 681 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p-1").convertToDouble()); 682 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p-1").convertToDouble()); 683 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p-1").convertToDouble()); 684 685 686 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1").convertToDouble()); 687 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p1").convertToDouble()); 688 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p1").convertToDouble()); 689 690 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p+1").convertToDouble()); 691 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p+1").convertToDouble()); 692 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p+1").convertToDouble()); 693 694 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p-1").convertToDouble()); 695 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p-1").convertToDouble()); 696 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p-1").convertToDouble()); 697 698 699 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p1").convertToDouble()); 700 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p1").convertToDouble()); 701 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p1").convertToDouble()); 702 703 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p+1").convertToDouble()); 704 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p+1").convertToDouble()); 705 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p+1").convertToDouble()); 706 707 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p-1").convertToDouble()); 708 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p-1").convertToDouble()); 709 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p-1").convertToDouble()); 710 711 712 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p1").convertToDouble()); 713 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p1").convertToDouble()); 714 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p1").convertToDouble()); 715 716 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p+1").convertToDouble()); 717 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p+1").convertToDouble()); 718 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p+1").convertToDouble()); 719 720 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p-1").convertToDouble()); 721 EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p-1").convertToDouble()); 722 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p-1").convertToDouble()); 723 724 725 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1").convertToDouble()); 726 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1").convertToDouble()); 727 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1").convertToDouble()); 728 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1").convertToDouble()); 729 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p1234").convertToDouble()); 730 EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1234").convertToDouble()); 731 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1234").convertToDouble()); 732 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1234").convertToDouble()); 733 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1234").convertToDouble()); 734 EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1234").convertToDouble()); 735 } 736 737 TEST(APFloatTest, fromDecimalString) { 738 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "1").convertToDouble()); 739 EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble, "2.").convertToDouble()); 740 EXPECT_EQ(0.5, APFloat(APFloat::IEEEdouble, ".5").convertToDouble()); 741 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "1.0").convertToDouble()); 742 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-2").convertToDouble()); 743 EXPECT_EQ(-4.0, APFloat(APFloat::IEEEdouble, "-4.").convertToDouble()); 744 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble, "-.5").convertToDouble()); 745 EXPECT_EQ(-1.5, APFloat(APFloat::IEEEdouble, "-1.5").convertToDouble()); 746 EXPECT_EQ(1.25e12, APFloat(APFloat::IEEEdouble, "1.25e12").convertToDouble()); 747 EXPECT_EQ(1.25e+12, APFloat(APFloat::IEEEdouble, "1.25e+12").convertToDouble()); 748 EXPECT_EQ(1.25e-12, APFloat(APFloat::IEEEdouble, "1.25e-12").convertToDouble()); 749 EXPECT_EQ(1024.0, APFloat(APFloat::IEEEdouble, "1024.").convertToDouble()); 750 EXPECT_EQ(1024.05, APFloat(APFloat::IEEEdouble, "1024.05000").convertToDouble()); 751 EXPECT_EQ(0.05, APFloat(APFloat::IEEEdouble, ".05000").convertToDouble()); 752 EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble, "2.").convertToDouble()); 753 EXPECT_EQ(2.0e2, APFloat(APFloat::IEEEdouble, "2.e2").convertToDouble()); 754 EXPECT_EQ(2.0e+2, APFloat(APFloat::IEEEdouble, "2.e+2").convertToDouble()); 755 EXPECT_EQ(2.0e-2, APFloat(APFloat::IEEEdouble, "2.e-2").convertToDouble()); 756 EXPECT_EQ(2.05e2, APFloat(APFloat::IEEEdouble, "002.05000e2").convertToDouble()); 757 EXPECT_EQ(2.05e+2, APFloat(APFloat::IEEEdouble, "002.05000e+2").convertToDouble()); 758 EXPECT_EQ(2.05e-2, APFloat(APFloat::IEEEdouble, "002.05000e-2").convertToDouble()); 759 EXPECT_EQ(2.05e12, APFloat(APFloat::IEEEdouble, "002.05000e12").convertToDouble()); 760 EXPECT_EQ(2.05e+12, APFloat(APFloat::IEEEdouble, "002.05000e+12").convertToDouble()); 761 EXPECT_EQ(2.05e-12, APFloat(APFloat::IEEEdouble, "002.05000e-12").convertToDouble()); 762 763 // These are "carefully selected" to overflow the fast log-base 764 // calculations in APFloat.cpp 765 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "99e99999").isInfinity()); 766 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-99e99999").isInfinity()); 767 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "1e-99999").isPosZero()); 768 EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-1e-99999").isNegZero()); 769 770 EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828")); 771 } 772 773 TEST(APFloatTest, fromHexadecimalString) { 774 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p0").convertToDouble()); 775 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p0").convertToDouble()); 776 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p0").convertToDouble()); 777 778 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p+0").convertToDouble()); 779 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p+0").convertToDouble()); 780 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p+0").convertToDouble()); 781 782 EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p-0").convertToDouble()); 783 EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p-0").convertToDouble()); 784 EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p-0").convertToDouble()); 785 786 787 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble, "0x1p1").convertToDouble()); 788 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p1").convertToDouble()); 789 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p1").convertToDouble()); 790 791 EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble, "0x1p+1").convertToDouble()); 792 EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p+1").convertToDouble()); 793 EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p+1").convertToDouble()); 794 795 EXPECT_EQ( 0.5, APFloat(APFloat::IEEEdouble, "0x1p-1").convertToDouble()); 796 EXPECT_EQ(+0.5, APFloat(APFloat::IEEEdouble, "+0x1p-1").convertToDouble()); 797 EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble, "-0x1p-1").convertToDouble()); 798 799 800 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble, "0x1.8p1").convertToDouble()); 801 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p1").convertToDouble()); 802 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p1").convertToDouble()); 803 804 EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble, "0x1.8p+1").convertToDouble()); 805 EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p+1").convertToDouble()); 806 EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p+1").convertToDouble()); 807 808 EXPECT_EQ( 0.75, APFloat(APFloat::IEEEdouble, "0x1.8p-1").convertToDouble()); 809 EXPECT_EQ(+0.75, APFloat(APFloat::IEEEdouble, "+0x1.8p-1").convertToDouble()); 810 EXPECT_EQ(-0.75, APFloat(APFloat::IEEEdouble, "-0x1.8p-1").convertToDouble()); 811 812 813 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000.000p1").convertToDouble()); 814 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p1").convertToDouble()); 815 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p1").convertToDouble()); 816 817 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000.000p+1").convertToDouble()); 818 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p+1").convertToDouble()); 819 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p+1").convertToDouble()); 820 821 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble, "0x1000.000p-1").convertToDouble()); 822 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p-1").convertToDouble()); 823 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p-1").convertToDouble()); 824 825 826 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000p1").convertToDouble()); 827 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p1").convertToDouble()); 828 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p1").convertToDouble()); 829 830 EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000p+1").convertToDouble()); 831 EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p+1").convertToDouble()); 832 EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p+1").convertToDouble()); 833 834 EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble, "0x1000p-1").convertToDouble()); 835 EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000p-1").convertToDouble()); 836 EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000p-1").convertToDouble()); 837 838 839 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble, "0x10p10").convertToDouble()); 840 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p10").convertToDouble()); 841 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p10").convertToDouble()); 842 843 EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble, "0x10p+10").convertToDouble()); 844 EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p+10").convertToDouble()); 845 EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p+10").convertToDouble()); 846 847 EXPECT_EQ( 0.015625, APFloat(APFloat::IEEEdouble, "0x10p-10").convertToDouble()); 848 EXPECT_EQ(+0.015625, APFloat(APFloat::IEEEdouble, "+0x10p-10").convertToDouble()); 849 EXPECT_EQ(-0.015625, APFloat(APFloat::IEEEdouble, "-0x10p-10").convertToDouble()); 850 851 EXPECT_EQ(1.0625, APFloat(APFloat::IEEEdouble, "0x1.1p0").convertToDouble()); 852 EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "0x1p0").convertToDouble()); 853 854 EXPECT_EQ(convertToDoubleFromString("0x1p-150"), 855 convertToDoubleFromString("+0x800000000000000001.p-221")); 856 EXPECT_EQ(2251799813685248.5, 857 convertToDoubleFromString("0x80000000000004000000.010p-28")); 858 } 859 860 TEST(APFloatTest, toString) { 861 ASSERT_EQ("10", convertToString(10.0, 6, 3)); 862 ASSERT_EQ("1.0E+1", convertToString(10.0, 6, 0)); 863 ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2)); 864 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 4, 2)); 865 ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 5, 1)); 866 ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2)); 867 ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2)); 868 ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 5, 1)); 869 ASSERT_EQ("0.78539816339744828", convertToString(0.78539816339744830961, 0, 3)); 870 ASSERT_EQ("4.9406564584124654E-324", convertToString(4.9406564584124654e-324, 0, 3)); 871 ASSERT_EQ("873.18340000000001", convertToString(873.1834, 0, 1)); 872 ASSERT_EQ("8.7318340000000001E+2", convertToString(873.1834, 0, 0)); 873 ASSERT_EQ("1.7976931348623157E+308", convertToString(1.7976931348623157E+308, 0, 0)); 874 } 875 876 TEST(APFloatTest, toInteger) { 877 bool isExact = false; 878 APSInt result(5, /*isUnsigned=*/true); 879 880 EXPECT_EQ(APFloat::opOK, 881 APFloat(APFloat::IEEEdouble, "10") 882 .convertToInteger(result, APFloat::rmTowardZero, &isExact)); 883 EXPECT_TRUE(isExact); 884 EXPECT_EQ(APSInt(APInt(5, 10), true), result); 885 886 EXPECT_EQ(APFloat::opInvalidOp, 887 APFloat(APFloat::IEEEdouble, "-10") 888 .convertToInteger(result, APFloat::rmTowardZero, &isExact)); 889 EXPECT_FALSE(isExact); 890 EXPECT_EQ(APSInt::getMinValue(5, true), result); 891 892 EXPECT_EQ(APFloat::opInvalidOp, 893 APFloat(APFloat::IEEEdouble, "32") 894 .convertToInteger(result, APFloat::rmTowardZero, &isExact)); 895 EXPECT_FALSE(isExact); 896 EXPECT_EQ(APSInt::getMaxValue(5, true), result); 897 898 EXPECT_EQ(APFloat::opInexact, 899 APFloat(APFloat::IEEEdouble, "7.9") 900 .convertToInteger(result, APFloat::rmTowardZero, &isExact)); 901 EXPECT_FALSE(isExact); 902 EXPECT_EQ(APSInt(APInt(5, 7), true), result); 903 904 result.setIsUnsigned(false); 905 EXPECT_EQ(APFloat::opOK, 906 APFloat(APFloat::IEEEdouble, "-10") 907 .convertToInteger(result, APFloat::rmTowardZero, &isExact)); 908 EXPECT_TRUE(isExact); 909 EXPECT_EQ(APSInt(APInt(5, -10, true), false), result); 910 911 EXPECT_EQ(APFloat::opInvalidOp, 912 APFloat(APFloat::IEEEdouble, "-17") 913 .convertToInteger(result, APFloat::rmTowardZero, &isExact)); 914 EXPECT_FALSE(isExact); 915 EXPECT_EQ(APSInt::getMinValue(5, false), result); 916 917 EXPECT_EQ(APFloat::opInvalidOp, 918 APFloat(APFloat::IEEEdouble, "16") 919 .convertToInteger(result, APFloat::rmTowardZero, &isExact)); 920 EXPECT_FALSE(isExact); 921 EXPECT_EQ(APSInt::getMaxValue(5, false), result); 922 } 923 924 static APInt nanbits(const fltSemantics &Sem, 925 bool SNaN, bool Negative, uint64_t fill) { 926 APInt apfill(64, fill); 927 if (SNaN) 928 return APFloat::getSNaN(Sem, Negative, &apfill).bitcastToAPInt(); 929 else 930 return APFloat::getQNaN(Sem, Negative, &apfill).bitcastToAPInt(); 931 } 932 933 TEST(APFloatTest, makeNaN) { 934 ASSERT_EQ(0x7fc00000, nanbits(APFloat::IEEEsingle, false, false, 0)); 935 ASSERT_EQ(0xffc00000, nanbits(APFloat::IEEEsingle, false, true, 0)); 936 ASSERT_EQ(0x7fc0ae72, nanbits(APFloat::IEEEsingle, false, false, 0xae72)); 937 ASSERT_EQ(0x7fffae72, nanbits(APFloat::IEEEsingle, false, false, 0xffffae72)); 938 ASSERT_EQ(0x7fa00000, nanbits(APFloat::IEEEsingle, true, false, 0)); 939 ASSERT_EQ(0xffa00000, nanbits(APFloat::IEEEsingle, true, true, 0)); 940 ASSERT_EQ(0x7f80ae72, nanbits(APFloat::IEEEsingle, true, false, 0xae72)); 941 ASSERT_EQ(0x7fbfae72, nanbits(APFloat::IEEEsingle, true, false, 0xffffae72)); 942 943 ASSERT_EQ(0x7ff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, false, 0)); 944 ASSERT_EQ(0xfff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, true, 0)); 945 ASSERT_EQ(0x7ff800000000ae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xae72)); 946 ASSERT_EQ(0x7fffffffffffae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xffffffffffffae72ULL)); 947 ASSERT_EQ(0x7ff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, false, 0)); 948 ASSERT_EQ(0xfff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, true, 0)); 949 ASSERT_EQ(0x7ff000000000ae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xae72)); 950 ASSERT_EQ(0x7ff7ffffffffae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xffffffffffffae72ULL)); 951 } 952 953 #ifdef GTEST_HAS_DEATH_TEST 954 #ifndef NDEBUG 955 TEST(APFloatTest, SemanticsDeath) { 956 EXPECT_DEATH(APFloat(APFloat::IEEEsingle, 0.0f).convertToDouble(), "Float semantics are not IEEEdouble"); 957 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, 0.0 ).convertToFloat(), "Float semantics are not IEEEsingle"); 958 } 959 960 TEST(APFloatTest, StringDecimalDeath) { 961 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ""), "Invalid string length"); 962 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+"), "String has no digits"); 963 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-"), "String has no digits"); 964 965 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("\0", 1)), "Invalid character in significand"); 966 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\0", 2)), "Invalid character in significand"); 967 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02", 3)), "Invalid character in significand"); 968 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02e1", 5)), "Invalid character in significand"); 969 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e\0", 3)), "Invalid character in exponent"); 970 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\0", 4)), "Invalid character in exponent"); 971 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\02", 5)), "Invalid character in exponent"); 972 973 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0f"), "Invalid character in significand"); 974 975 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".."), "String contains multiple dots"); 976 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "..0"), "String contains multiple dots"); 977 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0.0"), "String contains multiple dots"); 978 } 979 980 TEST(APFloatTest, StringDecimalSignificandDeath) { 981 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "."), "Significand has no digits"); 982 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+."), "Significand has no digits"); 983 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-."), "Significand has no digits"); 984 985 986 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "e"), "Significand has no digits"); 987 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e"), "Significand has no digits"); 988 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e"), "Significand has no digits"); 989 990 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "e1"), "Significand has no digits"); 991 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e1"), "Significand has no digits"); 992 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e1"), "Significand has no digits"); 993 994 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".e1"), "Significand has no digits"); 995 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e1"), "Significand has no digits"); 996 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e1"), "Significand has no digits"); 997 998 999 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".e"), "Significand has no digits"); 1000 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e"), "Significand has no digits"); 1001 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e"), "Significand has no digits"); 1002 } 1003 1004 TEST(APFloatTest, StringDecimalExponentDeath) { 1005 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e"), "Exponent has no digits"); 1006 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1e"), "Exponent has no digits"); 1007 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1e"), "Exponent has no digits"); 1008 1009 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.e"), "Exponent has no digits"); 1010 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1.e"), "Exponent has no digits"); 1011 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1.e"), "Exponent has no digits"); 1012 1013 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e"), "Exponent has no digits"); 1014 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.1e"), "Exponent has no digits"); 1015 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.1e"), "Exponent has no digits"); 1016 1017 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.1e"), "Exponent has no digits"); 1018 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1.1e"), "Exponent has no digits"); 1019 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1.1e"), "Exponent has no digits"); 1020 1021 1022 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e+"), "Exponent has no digits"); 1023 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e-"), "Exponent has no digits"); 1024 1025 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e"), "Exponent has no digits"); 1026 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e+"), "Exponent has no digits"); 1027 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e-"), "Exponent has no digits"); 1028 1029 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e"), "Exponent has no digits"); 1030 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e+"), "Exponent has no digits"); 1031 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e-"), "Exponent has no digits"); 1032 } 1033 1034 TEST(APFloatTest, StringHexadecimalDeath) { 1035 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x"), "Invalid string"); 1036 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x"), "Invalid string"); 1037 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x"), "Invalid string"); 1038 1039 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0"), "Hex strings require an exponent"); 1040 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0"), "Hex strings require an exponent"); 1041 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0"), "Hex strings require an exponent"); 1042 1043 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0."), "Hex strings require an exponent"); 1044 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0."), "Hex strings require an exponent"); 1045 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0."), "Hex strings require an exponent"); 1046 1047 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.0"), "Hex strings require an exponent"); 1048 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.0"), "Hex strings require an exponent"); 1049 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.0"), "Hex strings require an exponent"); 1050 1051 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0.0"), "Hex strings require an exponent"); 1052 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0.0"), "Hex strings require an exponent"); 1053 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0.0"), "Hex strings require an exponent"); 1054 1055 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x\0", 3)), "Invalid character in significand"); 1056 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\0", 4)), "Invalid character in significand"); 1057 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02", 5)), "Invalid character in significand"); 1058 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02p1", 7)), "Invalid character in significand"); 1059 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p\0", 5)), "Invalid character in exponent"); 1060 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\0", 6)), "Invalid character in exponent"); 1061 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\02", 7)), "Invalid character in exponent"); 1062 1063 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p0f"), "Invalid character in exponent"); 1064 1065 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..p1"), "String contains multiple dots"); 1066 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..0p1"), "String contains multiple dots"); 1067 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.0.0p1"), "String contains multiple dots"); 1068 } 1069 1070 TEST(APFloatTest, StringHexadecimalSignificandDeath) { 1071 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x."), "Significand has no digits"); 1072 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x."), "Significand has no digits"); 1073 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x."), "Significand has no digits"); 1074 1075 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp"), "Significand has no digits"); 1076 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp"), "Significand has no digits"); 1077 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp"), "Significand has no digits"); 1078 1079 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp+"), "Significand has no digits"); 1080 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp+"), "Significand has no digits"); 1081 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp+"), "Significand has no digits"); 1082 1083 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp-"), "Significand has no digits"); 1084 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp-"), "Significand has no digits"); 1085 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp-"), "Significand has no digits"); 1086 1087 1088 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p"), "Significand has no digits"); 1089 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p"), "Significand has no digits"); 1090 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p"), "Significand has no digits"); 1091 1092 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p+"), "Significand has no digits"); 1093 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p+"), "Significand has no digits"); 1094 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p+"), "Significand has no digits"); 1095 1096 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p-"), "Significand has no digits"); 1097 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p-"), "Significand has no digits"); 1098 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p-"), "Significand has no digits"); 1099 } 1100 1101 TEST(APFloatTest, StringHexadecimalExponentDeath) { 1102 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p"), "Exponent has no digits"); 1103 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p"), "Exponent has no digits"); 1104 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p"), "Exponent has no digits"); 1105 1106 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p+"), "Exponent has no digits"); 1107 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p+"), "Exponent has no digits"); 1108 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p+"), "Exponent has no digits"); 1109 1110 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p-"), "Exponent has no digits"); 1111 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p-"), "Exponent has no digits"); 1112 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p-"), "Exponent has no digits"); 1113 1114 1115 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p"), "Exponent has no digits"); 1116 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p"), "Exponent has no digits"); 1117 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p"), "Exponent has no digits"); 1118 1119 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p+"), "Exponent has no digits"); 1120 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p+"), "Exponent has no digits"); 1121 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p+"), "Exponent has no digits"); 1122 1123 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p-"), "Exponent has no digits"); 1124 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p-"), "Exponent has no digits"); 1125 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p-"), "Exponent has no digits"); 1126 1127 1128 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p"), "Exponent has no digits"); 1129 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p"), "Exponent has no digits"); 1130 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p"), "Exponent has no digits"); 1131 1132 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p+"), "Exponent has no digits"); 1133 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p+"), "Exponent has no digits"); 1134 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p+"), "Exponent has no digits"); 1135 1136 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p-"), "Exponent has no digits"); 1137 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p-"), "Exponent has no digits"); 1138 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p-"), "Exponent has no digits"); 1139 1140 1141 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p"), "Exponent has no digits"); 1142 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p"), "Exponent has no digits"); 1143 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p"), "Exponent has no digits"); 1144 1145 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p+"), "Exponent has no digits"); 1146 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p+"), "Exponent has no digits"); 1147 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p+"), "Exponent has no digits"); 1148 1149 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p-"), "Exponent has no digits"); 1150 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p-"), "Exponent has no digits"); 1151 EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p-"), "Exponent has no digits"); 1152 } 1153 #endif 1154 #endif 1155 1156 TEST(APFloatTest, exactInverse) { 1157 APFloat inv(0.0f); 1158 1159 // Trivial operation. 1160 EXPECT_TRUE(APFloat(2.0).getExactInverse(&inv)); 1161 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5))); 1162 EXPECT_TRUE(APFloat(2.0f).getExactInverse(&inv)); 1163 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5f))); 1164 EXPECT_TRUE(APFloat(APFloat::IEEEquad, "2.0").getExactInverse(&inv)); 1165 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::IEEEquad, "0.5"))); 1166 EXPECT_TRUE(APFloat(APFloat::PPCDoubleDouble, "2.0").getExactInverse(&inv)); 1167 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::PPCDoubleDouble, "0.5"))); 1168 EXPECT_TRUE(APFloat(APFloat::x87DoubleExtended, "2.0").getExactInverse(&inv)); 1169 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(APFloat::x87DoubleExtended, "0.5"))); 1170 1171 // FLT_MIN 1172 EXPECT_TRUE(APFloat(1.17549435e-38f).getExactInverse(&inv)); 1173 EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(8.5070592e+37f))); 1174 1175 // Large float, inverse is a denormal. 1176 EXPECT_FALSE(APFloat(1.7014118e38f).getExactInverse(nullptr)); 1177 // Zero 1178 EXPECT_FALSE(APFloat(0.0).getExactInverse(nullptr)); 1179 // Denormalized float 1180 EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(nullptr)); 1181 } 1182 1183 TEST(APFloatTest, roundToIntegral) { 1184 APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble)), P(0.0); 1185 1186 P = T; 1187 P.roundToIntegral(APFloat::rmTowardZero); 1188 EXPECT_EQ(-0.0, P.convertToDouble()); 1189 P = T; 1190 P.roundToIntegral(APFloat::rmTowardNegative); 1191 EXPECT_EQ(-1.0, P.convertToDouble()); 1192 P = T; 1193 P.roundToIntegral(APFloat::rmTowardPositive); 1194 EXPECT_EQ(-0.0, P.convertToDouble()); 1195 P = T; 1196 P.roundToIntegral(APFloat::rmNearestTiesToEven); 1197 EXPECT_EQ(-0.0, P.convertToDouble()); 1198 1199 P = S; 1200 P.roundToIntegral(APFloat::rmTowardZero); 1201 EXPECT_EQ(3.0, P.convertToDouble()); 1202 P = S; 1203 P.roundToIntegral(APFloat::rmTowardNegative); 1204 EXPECT_EQ(3.0, P.convertToDouble()); 1205 P = S; 1206 P.roundToIntegral(APFloat::rmTowardPositive); 1207 EXPECT_EQ(4.0, P.convertToDouble()); 1208 P = S; 1209 P.roundToIntegral(APFloat::rmNearestTiesToEven); 1210 EXPECT_EQ(3.0, P.convertToDouble()); 1211 1212 P = R; 1213 P.roundToIntegral(APFloat::rmTowardZero); 1214 EXPECT_EQ(R.convertToDouble(), P.convertToDouble()); 1215 P = R; 1216 P.roundToIntegral(APFloat::rmTowardNegative); 1217 EXPECT_EQ(R.convertToDouble(), P.convertToDouble()); 1218 P = R; 1219 P.roundToIntegral(APFloat::rmTowardPositive); 1220 EXPECT_EQ(R.convertToDouble(), P.convertToDouble()); 1221 P = R; 1222 P.roundToIntegral(APFloat::rmNearestTiesToEven); 1223 EXPECT_EQ(R.convertToDouble(), P.convertToDouble()); 1224 1225 P = APFloat::getZero(APFloat::IEEEdouble); 1226 P.roundToIntegral(APFloat::rmTowardZero); 1227 EXPECT_EQ(0.0, P.convertToDouble()); 1228 P = APFloat::getZero(APFloat::IEEEdouble, true); 1229 P.roundToIntegral(APFloat::rmTowardZero); 1230 EXPECT_EQ(-0.0, P.convertToDouble()); 1231 P = APFloat::getNaN(APFloat::IEEEdouble); 1232 P.roundToIntegral(APFloat::rmTowardZero); 1233 EXPECT_TRUE(IsNAN(P.convertToDouble())); 1234 P = APFloat::getInf(APFloat::IEEEdouble); 1235 P.roundToIntegral(APFloat::rmTowardZero); 1236 EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() > 0.0); 1237 P = APFloat::getInf(APFloat::IEEEdouble, true); 1238 P.roundToIntegral(APFloat::rmTowardZero); 1239 EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() < 0.0); 1240 1241 } 1242 1243 TEST(APFloatTest, getLargest) { 1244 EXPECT_EQ(3.402823466e+38f, APFloat::getLargest(APFloat::IEEEsingle).convertToFloat()); 1245 EXPECT_EQ(1.7976931348623158e+308, APFloat::getLargest(APFloat::IEEEdouble).convertToDouble()); 1246 } 1247 1248 TEST(APFloatTest, getSmallest) { 1249 APFloat test = APFloat::getSmallest(APFloat::IEEEsingle, false); 1250 APFloat expected = APFloat(APFloat::IEEEsingle, "0x0.000002p-126"); 1251 EXPECT_FALSE(test.isNegative()); 1252 EXPECT_TRUE(test.isFiniteNonZero()); 1253 EXPECT_TRUE(test.isDenormal()); 1254 EXPECT_TRUE(test.bitwiseIsEqual(expected)); 1255 1256 test = APFloat::getSmallest(APFloat::IEEEsingle, true); 1257 expected = APFloat(APFloat::IEEEsingle, "-0x0.000002p-126"); 1258 EXPECT_TRUE(test.isNegative()); 1259 EXPECT_TRUE(test.isFiniteNonZero()); 1260 EXPECT_TRUE(test.isDenormal()); 1261 EXPECT_TRUE(test.bitwiseIsEqual(expected)); 1262 1263 test = APFloat::getSmallest(APFloat::IEEEquad, false); 1264 expected = APFloat(APFloat::IEEEquad, "0x0.0000000000000000000000000001p-16382"); 1265 EXPECT_FALSE(test.isNegative()); 1266 EXPECT_TRUE(test.isFiniteNonZero()); 1267 EXPECT_TRUE(test.isDenormal()); 1268 EXPECT_TRUE(test.bitwiseIsEqual(expected)); 1269 1270 test = APFloat::getSmallest(APFloat::IEEEquad, true); 1271 expected = APFloat(APFloat::IEEEquad, "-0x0.0000000000000000000000000001p-16382"); 1272 EXPECT_TRUE(test.isNegative()); 1273 EXPECT_TRUE(test.isFiniteNonZero()); 1274 EXPECT_TRUE(test.isDenormal()); 1275 EXPECT_TRUE(test.bitwiseIsEqual(expected)); 1276 } 1277 1278 TEST(APFloatTest, getSmallestNormalized) { 1279 APFloat test = APFloat::getSmallestNormalized(APFloat::IEEEsingle, false); 1280 APFloat expected = APFloat(APFloat::IEEEsingle, "0x1p-126"); 1281 EXPECT_FALSE(test.isNegative()); 1282 EXPECT_TRUE(test.isFiniteNonZero()); 1283 EXPECT_FALSE(test.isDenormal()); 1284 EXPECT_TRUE(test.bitwiseIsEqual(expected)); 1285 1286 test = APFloat::getSmallestNormalized(APFloat::IEEEsingle, true); 1287 expected = APFloat(APFloat::IEEEsingle, "-0x1p-126"); 1288 EXPECT_TRUE(test.isNegative()); 1289 EXPECT_TRUE(test.isFiniteNonZero()); 1290 EXPECT_FALSE(test.isDenormal()); 1291 EXPECT_TRUE(test.bitwiseIsEqual(expected)); 1292 1293 test = APFloat::getSmallestNormalized(APFloat::IEEEquad, false); 1294 expected = APFloat(APFloat::IEEEquad, "0x1p-16382"); 1295 EXPECT_FALSE(test.isNegative()); 1296 EXPECT_TRUE(test.isFiniteNonZero()); 1297 EXPECT_FALSE(test.isDenormal()); 1298 EXPECT_TRUE(test.bitwiseIsEqual(expected)); 1299 1300 test = APFloat::getSmallestNormalized(APFloat::IEEEquad, true); 1301 expected = APFloat(APFloat::IEEEquad, "-0x1p-16382"); 1302 EXPECT_TRUE(test.isNegative()); 1303 EXPECT_TRUE(test.isFiniteNonZero()); 1304 EXPECT_FALSE(test.isDenormal()); 1305 EXPECT_TRUE(test.bitwiseIsEqual(expected)); 1306 } 1307 1308 TEST(APFloatTest, getZero) { 1309 struct { 1310 const fltSemantics *semantics; 1311 const bool sign; 1312 const unsigned long long bitPattern[2]; 1313 const unsigned bitPatternLength; 1314 } const GetZeroTest[] = { 1315 { &APFloat::IEEEhalf, false, {0, 0}, 1}, 1316 { &APFloat::IEEEhalf, true, {0x8000ULL, 0}, 1}, 1317 { &APFloat::IEEEsingle, false, {0, 0}, 1}, 1318 { &APFloat::IEEEsingle, true, {0x80000000ULL, 0}, 1}, 1319 { &APFloat::IEEEdouble, false, {0, 0}, 1}, 1320 { &APFloat::IEEEdouble, true, {0x8000000000000000ULL, 0}, 1}, 1321 { &APFloat::IEEEquad, false, {0, 0}, 2}, 1322 { &APFloat::IEEEquad, true, {0, 0x8000000000000000ULL}, 2}, 1323 { &APFloat::PPCDoubleDouble, false, {0, 0}, 2}, 1324 { &APFloat::PPCDoubleDouble, true, {0x8000000000000000ULL, 0}, 2}, 1325 { &APFloat::x87DoubleExtended, false, {0, 0}, 2}, 1326 { &APFloat::x87DoubleExtended, true, {0, 0x8000ULL}, 2}, 1327 }; 1328 const unsigned NumGetZeroTests = 12; 1329 for (unsigned i = 0; i < NumGetZeroTests; ++i) { 1330 APFloat test = APFloat::getZero(*GetZeroTest[i].semantics, 1331 GetZeroTest[i].sign); 1332 const char *pattern = GetZeroTest[i].sign? "-0x0p+0" : "0x0p+0"; 1333 APFloat expected = APFloat(*GetZeroTest[i].semantics, 1334 pattern); 1335 EXPECT_TRUE(test.isZero()); 1336 EXPECT_TRUE(GetZeroTest[i].sign? test.isNegative() : !test.isNegative()); 1337 EXPECT_TRUE(test.bitwiseIsEqual(expected)); 1338 for (unsigned j = 0, je = GetZeroTest[i].bitPatternLength; j < je; ++j) { 1339 EXPECT_EQ(GetZeroTest[i].bitPattern[j], 1340 test.bitcastToAPInt().getRawData()[j]); 1341 } 1342 } 1343 } 1344 1345 TEST(APFloatTest, convert) { 1346 bool losesInfo; 1347 APFloat test(APFloat::IEEEdouble, "1.0"); 1348 test.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo); 1349 EXPECT_EQ(1.0f, test.convertToFloat()); 1350 EXPECT_FALSE(losesInfo); 1351 1352 test = APFloat(APFloat::x87DoubleExtended, "0x1p-53"); 1353 test.add(APFloat(APFloat::x87DoubleExtended, "1.0"), APFloat::rmNearestTiesToEven); 1354 test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); 1355 EXPECT_EQ(1.0, test.convertToDouble()); 1356 EXPECT_TRUE(losesInfo); 1357 1358 test = APFloat(APFloat::IEEEquad, "0x1p-53"); 1359 test.add(APFloat(APFloat::IEEEquad, "1.0"), APFloat::rmNearestTiesToEven); 1360 test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); 1361 EXPECT_EQ(1.0, test.convertToDouble()); 1362 EXPECT_TRUE(losesInfo); 1363 1364 test = APFloat(APFloat::x87DoubleExtended, "0xf.fffffffp+28"); 1365 test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo); 1366 EXPECT_EQ(4294967295.0, test.convertToDouble()); 1367 EXPECT_FALSE(losesInfo); 1368 1369 test = APFloat::getSNaN(APFloat::IEEEsingle); 1370 APFloat X87SNaN = APFloat::getSNaN(APFloat::x87DoubleExtended); 1371 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, 1372 &losesInfo); 1373 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN)); 1374 EXPECT_FALSE(losesInfo); 1375 1376 test = APFloat::getQNaN(APFloat::IEEEsingle); 1377 APFloat X87QNaN = APFloat::getQNaN(APFloat::x87DoubleExtended); 1378 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, 1379 &losesInfo); 1380 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN)); 1381 EXPECT_FALSE(losesInfo); 1382 1383 test = APFloat::getSNaN(APFloat::x87DoubleExtended); 1384 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, 1385 &losesInfo); 1386 EXPECT_TRUE(test.bitwiseIsEqual(X87SNaN)); 1387 EXPECT_FALSE(losesInfo); 1388 1389 test = APFloat::getQNaN(APFloat::x87DoubleExtended); 1390 test.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, 1391 &losesInfo); 1392 EXPECT_TRUE(test.bitwiseIsEqual(X87QNaN)); 1393 EXPECT_FALSE(losesInfo); 1394 } 1395 1396 TEST(APFloatTest, PPCDoubleDouble) { 1397 APFloat test(APFloat::PPCDoubleDouble, "1.0"); 1398 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]); 1399 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]); 1400 1401 test.divide(APFloat(APFloat::PPCDoubleDouble, "3.0"), APFloat::rmNearestTiesToEven); 1402 EXPECT_EQ(0x3fd5555555555555ull, test.bitcastToAPInt().getRawData()[0]); 1403 EXPECT_EQ(0x3c75555555555556ull, test.bitcastToAPInt().getRawData()[1]); 1404 1405 // LDBL_MAX 1406 test = APFloat(APFloat::PPCDoubleDouble, "1.79769313486231580793728971405301e+308"); 1407 EXPECT_EQ(0x7fefffffffffffffull, test.bitcastToAPInt().getRawData()[0]); 1408 EXPECT_EQ(0x7c8ffffffffffffeull, test.bitcastToAPInt().getRawData()[1]); 1409 1410 // LDBL_MIN 1411 test = APFloat(APFloat::PPCDoubleDouble, "2.00416836000897277799610805135016e-292"); 1412 EXPECT_EQ(0x0360000000000000ull, test.bitcastToAPInt().getRawData()[0]); 1413 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]); 1414 1415 test = APFloat(APFloat::PPCDoubleDouble, "1.0"); 1416 test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-105"), APFloat::rmNearestTiesToEven); 1417 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]); 1418 EXPECT_EQ(0x3960000000000000ull, test.bitcastToAPInt().getRawData()[1]); 1419 1420 test = APFloat(APFloat::PPCDoubleDouble, "1.0"); 1421 test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-106"), APFloat::rmNearestTiesToEven); 1422 EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]); 1423 #if 0 // XFAIL 1424 // This is what we would expect with a true double-double implementation 1425 EXPECT_EQ(0x3950000000000000ull, test.bitcastToAPInt().getRawData()[1]); 1426 #else 1427 // This is what we get with our 106-bit mantissa approximation 1428 EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]); 1429 #endif 1430 } 1431 1432 TEST(APFloatTest, isNegative) { 1433 APFloat t(APFloat::IEEEsingle, "0x1p+0"); 1434 EXPECT_FALSE(t.isNegative()); 1435 t = APFloat(APFloat::IEEEsingle, "-0x1p+0"); 1436 EXPECT_TRUE(t.isNegative()); 1437 1438 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNegative()); 1439 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle, true).isNegative()); 1440 1441 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNegative()); 1442 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle, true).isNegative()); 1443 1444 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isNegative()); 1445 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle, true).isNegative()); 1446 1447 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNegative()); 1448 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, true).isNegative()); 1449 } 1450 1451 TEST(APFloatTest, isNormal) { 1452 APFloat t(APFloat::IEEEsingle, "0x1p+0"); 1453 EXPECT_TRUE(t.isNormal()); 1454 1455 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNormal()); 1456 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNormal()); 1457 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isNormal()); 1458 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNormal()); 1459 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-149").isNormal()); 1460 } 1461 1462 TEST(APFloatTest, isFinite) { 1463 APFloat t(APFloat::IEEEsingle, "0x1p+0"); 1464 EXPECT_TRUE(t.isFinite()); 1465 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isFinite()); 1466 EXPECT_TRUE(APFloat::getZero(APFloat::IEEEsingle, false).isFinite()); 1467 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isFinite()); 1468 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isFinite()); 1469 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p-149").isFinite()); 1470 } 1471 1472 TEST(APFloatTest, isInfinity) { 1473 APFloat t(APFloat::IEEEsingle, "0x1p+0"); 1474 EXPECT_FALSE(t.isInfinity()); 1475 EXPECT_TRUE(APFloat::getInf(APFloat::IEEEsingle, false).isInfinity()); 1476 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isInfinity()); 1477 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isInfinity()); 1478 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isInfinity()); 1479 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-149").isInfinity()); 1480 } 1481 1482 TEST(APFloatTest, isNaN) { 1483 APFloat t(APFloat::IEEEsingle, "0x1p+0"); 1484 EXPECT_FALSE(t.isNaN()); 1485 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isNaN()); 1486 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isNaN()); 1487 EXPECT_TRUE(APFloat::getNaN(APFloat::IEEEsingle, false).isNaN()); 1488 EXPECT_TRUE(APFloat::getSNaN(APFloat::IEEEsingle, false).isNaN()); 1489 EXPECT_FALSE(APFloat(APFloat::IEEEsingle, "0x1p-149").isNaN()); 1490 } 1491 1492 TEST(APFloatTest, isFiniteNonZero) { 1493 // Test positive/negative normal value. 1494 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p+0").isFiniteNonZero()); 1495 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "-0x1p+0").isFiniteNonZero()); 1496 1497 // Test positive/negative denormal value. 1498 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "0x1p-149").isFiniteNonZero()); 1499 EXPECT_TRUE(APFloat(APFloat::IEEEsingle, "-0x1p-149").isFiniteNonZero()); 1500 1501 // Test +/- Infinity. 1502 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, false).isFiniteNonZero()); 1503 EXPECT_FALSE(APFloat::getInf(APFloat::IEEEsingle, true).isFiniteNonZero()); 1504 1505 // Test +/- Zero. 1506 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, false).isFiniteNonZero()); 1507 EXPECT_FALSE(APFloat::getZero(APFloat::IEEEsingle, true).isFiniteNonZero()); 1508 1509 // Test +/- qNaN. +/- dont mean anything with qNaN but paranoia can't hurt in 1510 // this instance. 1511 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, false).isFiniteNonZero()); 1512 EXPECT_FALSE(APFloat::getNaN(APFloat::IEEEsingle, true).isFiniteNonZero()); 1513 1514 // Test +/- sNaN. +/- dont mean anything with sNaN but paranoia can't hurt in 1515 // this instance. 1516 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, false).isFiniteNonZero()); 1517 EXPECT_FALSE(APFloat::getSNaN(APFloat::IEEEsingle, true).isFiniteNonZero()); 1518 } 1519 1520 TEST(APFloatTest, add) { 1521 // Test Special Cases against each other and normal values. 1522 1523 // TODOS/NOTES: 1524 // 1. Since we perform only default exception handling all operations with 1525 // signaling NaNs should have a result that is a quiet NaN. Currently they 1526 // return sNaN. 1527 1528 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false); 1529 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true); 1530 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false); 1531 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true); 1532 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false); 1533 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false); 1534 APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0"); 1535 APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0"); 1536 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false); 1537 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true); 1538 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false); 1539 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true); 1540 APFloat PSmallestNormalized = 1541 APFloat::getSmallestNormalized(APFloat::IEEEsingle, false); 1542 APFloat MSmallestNormalized = 1543 APFloat::getSmallestNormalized(APFloat::IEEEsingle, true); 1544 1545 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact; 1546 1547 const unsigned NumTests = 169; 1548 struct { 1549 APFloat x; 1550 APFloat y; 1551 const char *result; 1552 int status; 1553 int category; 1554 } SpecialCaseTests[NumTests] = { 1555 { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1556 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1557 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity }, 1558 { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity }, 1559 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1560 #if 0 1561 // See Note 1. 1562 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1563 #endif 1564 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1565 { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1566 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1567 { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1568 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1569 { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1570 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity }, 1571 { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity }, 1572 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1573 { MInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1574 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1575 { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1576 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1577 #if 0 1578 // See Note 1. 1579 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1580 #endif 1581 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1582 { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1583 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1584 { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1585 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1586 { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1587 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1588 { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1589 { PZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1590 { PZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1591 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1592 { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1593 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1594 #if 0 1595 // See Note 1. 1596 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1597 #endif 1598 { PZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1599 { PZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1600 { PZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1601 { PZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1602 { PZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1603 { PZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1604 { PZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1605 { PZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1606 { MZero, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1607 { MZero, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1608 { MZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1609 { MZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 1610 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1611 #if 0 1612 // See Note 1. 1613 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1614 #endif 1615 { MZero, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1616 { MZero, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1617 { MZero, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1618 { MZero, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1619 { MZero, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1620 { MZero, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1621 { MZero, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1622 { MZero, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1623 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN }, 1624 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN }, 1625 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN }, 1626 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN }, 1627 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1628 #if 0 1629 // See Note 1. 1630 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1631 #endif 1632 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1633 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1634 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1635 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1636 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1637 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1638 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN }, 1639 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN }, 1640 #if 0 1641 // See Note 1. 1642 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1643 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1644 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1645 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1646 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1647 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1648 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1649 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1650 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1651 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1652 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1653 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1654 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1655 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1656 #endif 1657 { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1658 { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1659 { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1660 { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1661 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1662 #if 0 1663 // See Note 1. 1664 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1665 #endif 1666 { PNormalValue, PNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal }, 1667 { PNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1668 { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1669 { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1670 { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1671 { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1672 { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1673 { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1674 { MNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1675 { MNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1676 { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1677 { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1678 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1679 #if 0 1680 // See Note 1. 1681 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1682 #endif 1683 { MNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1684 { MNormalValue, MNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal }, 1685 { MNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1686 { MNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1687 { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1688 { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1689 { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1690 { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1691 { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1692 { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1693 { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1694 { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1695 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1696 #if 0 1697 // See Note 1. 1698 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1699 #endif 1700 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1701 { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1702 { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity }, 1703 { PLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1704 { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1705 { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1706 { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1707 { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1708 { MLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1709 { MLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1710 { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1711 { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1712 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1713 #if 0 1714 // See Note 1. 1715 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1716 #endif 1717 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1718 { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1719 { MLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1720 { MLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity }, 1721 { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1722 { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1723 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1724 { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1725 { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1726 { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1727 { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1728 { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1729 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1730 #if 0 1731 // See Note 1. 1732 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1733 #endif 1734 { PSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1735 { PSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1736 { PSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1737 { PSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1738 { PSmallestValue, PSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal }, 1739 { PSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1740 { PSmallestValue, PSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal }, 1741 { PSmallestValue, MSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal }, 1742 { MSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1743 { MSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1744 { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1745 { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1746 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1747 #if 0 1748 // See Note 1. 1749 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1750 #endif 1751 { MSmallestValue, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1752 { MSmallestValue, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1753 { MSmallestValue, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1754 { MSmallestValue, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1755 { MSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1756 { MSmallestValue, MSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal }, 1757 { MSmallestValue, PSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal }, 1758 { MSmallestValue, MSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal }, 1759 { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1760 { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1761 { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1762 { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1763 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1764 #if 0 1765 // See Note 1. 1766 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1767 #endif 1768 { PSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1769 { PSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1770 { PSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1771 { PSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1772 { PSmallestNormalized, PSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal }, 1773 { PSmallestNormalized, MSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal }, 1774 { PSmallestNormalized, PSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal }, 1775 { PSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1776 { MSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1777 { MSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1778 { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1779 { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1780 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1781 #if 0 1782 // See Note 1. 1783 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1784 #endif 1785 { MSmallestNormalized, PNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1786 { MSmallestNormalized, MNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1787 { MSmallestNormalized, PLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1788 { MSmallestNormalized, MLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1789 { MSmallestNormalized, PSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal }, 1790 { MSmallestNormalized, MSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal }, 1791 { MSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1792 { MSmallestNormalized, MSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal } 1793 }; 1794 1795 for (size_t i = 0; i < NumTests; ++i) { 1796 APFloat x(SpecialCaseTests[i].x); 1797 APFloat y(SpecialCaseTests[i].y); 1798 APFloat::opStatus status = x.add(y, APFloat::rmNearestTiesToEven); 1799 1800 APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result); 1801 1802 EXPECT_TRUE(result.bitwiseIsEqual(x)); 1803 EXPECT_TRUE((int)status == SpecialCaseTests[i].status); 1804 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category); 1805 } 1806 } 1807 1808 TEST(APFloatTest, subtract) { 1809 // Test Special Cases against each other and normal values. 1810 1811 // TODOS/NOTES: 1812 // 1. Since we perform only default exception handling all operations with 1813 // signaling NaNs should have a result that is a quiet NaN. Currently they 1814 // return sNaN. 1815 1816 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false); 1817 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true); 1818 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false); 1819 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true); 1820 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false); 1821 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false); 1822 APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0"); 1823 APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0"); 1824 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false); 1825 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true); 1826 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false); 1827 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true); 1828 APFloat PSmallestNormalized = 1829 APFloat::getSmallestNormalized(APFloat::IEEEsingle, false); 1830 APFloat MSmallestNormalized = 1831 APFloat::getSmallestNormalized(APFloat::IEEEsingle, true); 1832 1833 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact; 1834 1835 const unsigned NumTests = 169; 1836 struct { 1837 APFloat x; 1838 APFloat y; 1839 const char *result; 1840 int status; 1841 int category; 1842 } SpecialCaseTests[NumTests] = { 1843 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1844 { PInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1845 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity }, 1846 { PInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity }, 1847 { PInf, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 1848 #if 0 1849 // See Note 1. 1850 { PInf, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1851 #endif 1852 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1853 { PInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1854 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1855 { PInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1856 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1857 { PInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 1858 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity }, 1859 { PInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity }, 1860 { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1861 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1862 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1863 { MInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1864 { MInf, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 1865 #if 0 1866 // See Note 1. 1867 { MInf, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1868 #endif 1869 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1870 { MInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1871 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1872 { MInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1873 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1874 { MInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1875 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1876 { MInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1877 { PZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1878 { PZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1879 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1880 { PZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1881 { PZero, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 1882 #if 0 1883 // See Note 1. 1884 { PZero, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1885 #endif 1886 { PZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1887 { PZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1888 { PZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1889 { PZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1890 { PZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1891 { PZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1892 { PZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1893 { PZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1894 { MZero, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1895 { MZero, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1896 { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 1897 { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1898 { MZero, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 1899 #if 0 1900 // See Note 1. 1901 { MZero, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1902 #endif 1903 { MZero, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1904 { MZero, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1905 { MZero, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1906 { MZero, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1907 { MZero, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1908 { MZero, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 1909 { MZero, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1910 { MZero, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 1911 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN }, 1912 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN }, 1913 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN }, 1914 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN }, 1915 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 1916 #if 0 1917 // See Note 1. 1918 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1919 #endif 1920 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1921 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1922 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1923 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1924 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1925 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 1926 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN }, 1927 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN }, 1928 #if 0 1929 // See Note 1. 1930 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1931 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1932 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1933 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1934 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1935 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1936 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1937 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1938 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1939 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1940 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1941 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1942 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1943 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1944 #endif 1945 { PNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1946 { PNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1947 { PNormalValue, PZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1948 { PNormalValue, MZero, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1949 { PNormalValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 1950 #if 0 1951 // See Note 1. 1952 { PNormalValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1953 #endif 1954 { PNormalValue, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1955 { PNormalValue, MNormalValue, "0x1p+1", APFloat::opOK, APFloat::fcNormal }, 1956 { PNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1957 { PNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1958 { PNormalValue, PSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1959 { PNormalValue, MSmallestValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1960 { PNormalValue, PSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1961 { PNormalValue, MSmallestNormalized, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1962 { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1963 { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1964 { MNormalValue, PZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1965 { MNormalValue, MZero, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 1966 { MNormalValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 1967 #if 0 1968 // See Note 1. 1969 { MNormalValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1970 #endif 1971 { MNormalValue, PNormalValue, "-0x1p+1", APFloat::opOK, APFloat::fcNormal }, 1972 { MNormalValue, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1973 { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1974 { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1975 { MNormalValue, PSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1976 { MNormalValue, MSmallestValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1977 { MNormalValue, PSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1978 { MNormalValue, MSmallestNormalized, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 1979 { PLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1980 { PLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1981 { PLargestValue, PZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1982 { PLargestValue, MZero, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1983 { PLargestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 1984 #if 0 1985 // See Note 1. 1986 { PLargestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 1987 #endif 1988 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1989 { PLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1990 { PLargestValue, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 1991 { PLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity }, 1992 { PLargestValue, PSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1993 { PLargestValue, MSmallestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1994 { PLargestValue, PSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1995 { PLargestValue, MSmallestNormalized, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 1996 { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 1997 { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 1998 { MLargestValue, PZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 1999 { MLargestValue, MZero, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2000 { MLargestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 2001 #if 0 2002 // See Note 1. 2003 { MLargestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2004 #endif 2005 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2006 { MLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2007 { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity }, 2008 { MLargestValue, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2009 { MLargestValue, PSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2010 { MLargestValue, MSmallestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2011 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2012 { MLargestValue, MSmallestNormalized, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2013 { PSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2014 { PSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2015 { PSmallestValue, PZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2016 { PSmallestValue, MZero, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2017 { PSmallestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 2018 #if 0 2019 // See Note 1. 2020 { PSmallestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2021 #endif 2022 { PSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 2023 { PSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 2024 { PSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2025 { PSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2026 { PSmallestValue, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2027 { PSmallestValue, MSmallestValue, "0x1p-148", APFloat::opOK, APFloat::fcNormal }, 2028 { PSmallestValue, PSmallestNormalized, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal }, 2029 { PSmallestValue, MSmallestNormalized, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal }, 2030 { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2031 { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2032 { MSmallestValue, PZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2033 { MSmallestValue, MZero, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2034 { MSmallestValue, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 2035 #if 0 2036 // See Note 1. 2037 { MSmallestValue, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2038 #endif 2039 { MSmallestValue, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 2040 { MSmallestValue, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 2041 { MSmallestValue, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2042 { MSmallestValue, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2043 { MSmallestValue, PSmallestValue, "-0x1p-148", APFloat::opOK, APFloat::fcNormal }, 2044 { MSmallestValue, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2045 { MSmallestValue, PSmallestNormalized, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal }, 2046 { MSmallestValue, MSmallestNormalized, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal }, 2047 { PSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2048 { PSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2049 { PSmallestNormalized, PZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2050 { PSmallestNormalized, MZero, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2051 { PSmallestNormalized, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 2052 #if 0 2053 // See Note 1. 2054 { PSmallestNormalized, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2055 #endif 2056 { PSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 2057 { PSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 2058 { PSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2059 { PSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2060 { PSmallestNormalized, PSmallestValue, "0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal }, 2061 { PSmallestNormalized, MSmallestValue, "0x1.000002p-126", APFloat::opOK, APFloat::fcNormal }, 2062 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2063 { PSmallestNormalized, MSmallestNormalized, "0x1p-125", APFloat::opOK, APFloat::fcNormal }, 2064 { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2065 { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2066 { MSmallestNormalized, PZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2067 { MSmallestNormalized, MZero, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2068 { MSmallestNormalized, QNaN, "-nan", APFloat::opOK, APFloat::fcNaN }, 2069 #if 0 2070 // See Note 1. 2071 { MSmallestNormalized, SNaN, "-nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2072 #endif 2073 { MSmallestNormalized, PNormalValue, "-0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 2074 { MSmallestNormalized, MNormalValue, "0x1p+0", APFloat::opInexact, APFloat::fcNormal }, 2075 { MSmallestNormalized, PLargestValue, "-0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2076 { MSmallestNormalized, MLargestValue, "0x1.fffffep+127", APFloat::opInexact, APFloat::fcNormal }, 2077 { MSmallestNormalized, PSmallestValue, "-0x1.000002p-126", APFloat::opOK, APFloat::fcNormal }, 2078 { MSmallestNormalized, MSmallestValue, "-0x1.fffffcp-127", APFloat::opOK, APFloat::fcNormal }, 2079 { MSmallestNormalized, PSmallestNormalized, "-0x1p-125", APFloat::opOK, APFloat::fcNormal }, 2080 { MSmallestNormalized, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero } 2081 }; 2082 2083 for (size_t i = 0; i < NumTests; ++i) { 2084 APFloat x(SpecialCaseTests[i].x); 2085 APFloat y(SpecialCaseTests[i].y); 2086 APFloat::opStatus status = x.subtract(y, APFloat::rmNearestTiesToEven); 2087 2088 APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result); 2089 2090 EXPECT_TRUE(result.bitwiseIsEqual(x)); 2091 EXPECT_TRUE((int)status == SpecialCaseTests[i].status); 2092 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category); 2093 } 2094 } 2095 2096 TEST(APFloatTest, multiply) { 2097 // Test Special Cases against each other and normal values. 2098 2099 // TODOS/NOTES: 2100 // 1. Since we perform only default exception handling all operations with 2101 // signaling NaNs should have a result that is a quiet NaN. Currently they 2102 // return sNaN. 2103 2104 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false); 2105 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true); 2106 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false); 2107 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true); 2108 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false); 2109 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false); 2110 APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0"); 2111 APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0"); 2112 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false); 2113 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true); 2114 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false); 2115 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true); 2116 APFloat PSmallestNormalized = 2117 APFloat::getSmallestNormalized(APFloat::IEEEsingle, false); 2118 APFloat MSmallestNormalized = 2119 APFloat::getSmallestNormalized(APFloat::IEEEsingle, true); 2120 2121 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact; 2122 const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact; 2123 2124 const unsigned NumTests = 169; 2125 struct { 2126 APFloat x; 2127 APFloat y; 2128 const char *result; 2129 int status; 2130 int category; 2131 } SpecialCaseTests[NumTests] = { 2132 { PInf, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2133 { PInf, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2134 { PInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2135 { PInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2136 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2137 #if 0 2138 // See Note 1. 2139 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2140 #endif 2141 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2142 { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2143 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2144 { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2145 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2146 { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2147 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity }, 2148 { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2149 { MInf, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2150 { MInf, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2151 { MInf, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2152 { MInf, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2153 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2154 #if 0 2155 // See Note 1. 2156 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2157 #endif 2158 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2159 { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2160 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2161 { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2162 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2163 { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2164 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2165 { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity }, 2166 { PZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2167 { PZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2168 { PZero, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2169 { PZero, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2170 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2171 #if 0 2172 // See Note 1. 2173 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2174 #endif 2175 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2176 { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2177 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2178 { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2179 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2180 { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2181 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2182 { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2183 { MZero, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2184 { MZero, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2185 { MZero, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2186 { MZero, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2187 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2188 #if 0 2189 // See Note 1. 2190 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2191 #endif 2192 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2193 { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2194 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2195 { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2196 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2197 { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2198 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2199 { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2200 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN }, 2201 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN }, 2202 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN }, 2203 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN }, 2204 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2205 #if 0 2206 // See Note 1. 2207 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2208 #endif 2209 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2210 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2211 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2212 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2213 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2214 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2215 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN }, 2216 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN }, 2217 #if 0 2218 // See Note 1. 2219 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2220 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2221 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2222 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2223 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2224 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2225 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2226 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2227 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2228 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2229 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2230 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2231 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2232 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2233 #endif 2234 { PNormalValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2235 { PNormalValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2236 { PNormalValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2237 { PNormalValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2238 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2239 #if 0 2240 // See Note 1. 2241 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2242 #endif 2243 { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2244 { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2245 { PNormalValue, PLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2246 { PNormalValue, MLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2247 { PNormalValue, PSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2248 { PNormalValue, MSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2249 { PNormalValue, PSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2250 { PNormalValue, MSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2251 { MNormalValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2252 { MNormalValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2253 { MNormalValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2254 { MNormalValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2255 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2256 #if 0 2257 // See Note 1. 2258 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2259 #endif 2260 { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2261 { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2262 { MNormalValue, PLargestValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2263 { MNormalValue, MLargestValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2264 { MNormalValue, PSmallestValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2265 { MNormalValue, MSmallestValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2266 { MNormalValue, PSmallestNormalized, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2267 { MNormalValue, MSmallestNormalized, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2268 { PLargestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2269 { PLargestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2270 { PLargestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2271 { PLargestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2272 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2273 #if 0 2274 // See Note 1. 2275 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2276 #endif 2277 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2278 { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2279 { PLargestValue, PLargestValue, "inf", OverflowStatus, APFloat::fcInfinity }, 2280 { PLargestValue, MLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity }, 2281 { PLargestValue, PSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal }, 2282 { PLargestValue, MSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal }, 2283 { PLargestValue, PSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal }, 2284 { PLargestValue, MSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal }, 2285 { MLargestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2286 { MLargestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2287 { MLargestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2288 { MLargestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2289 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2290 #if 0 2291 // See Note 1. 2292 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2293 #endif 2294 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2295 { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2296 { MLargestValue, PLargestValue, "-inf", OverflowStatus, APFloat::fcInfinity }, 2297 { MLargestValue, MLargestValue, "inf", OverflowStatus, APFloat::fcInfinity }, 2298 { MLargestValue, PSmallestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal }, 2299 { MLargestValue, MSmallestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal }, 2300 { MLargestValue, PSmallestNormalized, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal }, 2301 { MLargestValue, MSmallestNormalized, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal }, 2302 { PSmallestValue, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2303 { PSmallestValue, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2304 { PSmallestValue, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2305 { PSmallestValue, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2306 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2307 #if 0 2308 // See Note 1. 2309 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2310 #endif 2311 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2312 { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2313 { PSmallestValue, PLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal }, 2314 { PSmallestValue, MLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal }, 2315 { PSmallestValue, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2316 { PSmallestValue, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2317 { PSmallestValue, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2318 { PSmallestValue, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2319 { MSmallestValue, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2320 { MSmallestValue, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2321 { MSmallestValue, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2322 { MSmallestValue, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2323 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2324 #if 0 2325 // See Note 1. 2326 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2327 #endif 2328 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2329 { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2330 { MSmallestValue, PLargestValue, "-0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal }, 2331 { MSmallestValue, MLargestValue, "0x1.fffffep-22", APFloat::opOK, APFloat::fcNormal }, 2332 { MSmallestValue, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2333 { MSmallestValue, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2334 { MSmallestValue, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2335 { MSmallestValue, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2336 { PSmallestNormalized, PInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2337 { PSmallestNormalized, MInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2338 { PSmallestNormalized, PZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2339 { PSmallestNormalized, MZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2340 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2341 #if 0 2342 // See Note 1. 2343 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2344 #endif 2345 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2346 { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2347 { PSmallestNormalized, PLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal }, 2348 { PSmallestNormalized, MLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal }, 2349 { PSmallestNormalized, PSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2350 { PSmallestNormalized, MSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2351 { PSmallestNormalized, PSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2352 { PSmallestNormalized, MSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2353 { MSmallestNormalized, PInf, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2354 { MSmallestNormalized, MInf, "inf", APFloat::opOK, APFloat::fcInfinity }, 2355 { MSmallestNormalized, PZero, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2356 { MSmallestNormalized, MZero, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2357 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2358 #if 0 2359 // See Note 1. 2360 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2361 #endif 2362 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2363 { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2364 { MSmallestNormalized, PLargestValue, "-0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal }, 2365 { MSmallestNormalized, MLargestValue, "0x1.fffffep+1", APFloat::opOK, APFloat::fcNormal }, 2366 { MSmallestNormalized, PSmallestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2367 { MSmallestNormalized, MSmallestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2368 { MSmallestNormalized, PSmallestNormalized, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2369 { MSmallestNormalized, MSmallestNormalized, "0x0p+0", UnderflowStatus, APFloat::fcZero } 2370 }; 2371 2372 for (size_t i = 0; i < NumTests; ++i) { 2373 APFloat x(SpecialCaseTests[i].x); 2374 APFloat y(SpecialCaseTests[i].y); 2375 APFloat::opStatus status = x.multiply(y, APFloat::rmNearestTiesToEven); 2376 2377 APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result); 2378 2379 EXPECT_TRUE(result.bitwiseIsEqual(x)); 2380 EXPECT_TRUE((int)status == SpecialCaseTests[i].status); 2381 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category); 2382 } 2383 } 2384 2385 TEST(APFloatTest, divide) { 2386 // Test Special Cases against each other and normal values. 2387 2388 // TODOS/NOTES: 2389 // 1. Since we perform only default exception handling all operations with 2390 // signaling NaNs should have a result that is a quiet NaN. Currently they 2391 // return sNaN. 2392 2393 APFloat PInf = APFloat::getInf(APFloat::IEEEsingle, false); 2394 APFloat MInf = APFloat::getInf(APFloat::IEEEsingle, true); 2395 APFloat PZero = APFloat::getZero(APFloat::IEEEsingle, false); 2396 APFloat MZero = APFloat::getZero(APFloat::IEEEsingle, true); 2397 APFloat QNaN = APFloat::getNaN(APFloat::IEEEsingle, false); 2398 APFloat SNaN = APFloat::getSNaN(APFloat::IEEEsingle, false); 2399 APFloat PNormalValue = APFloat(APFloat::IEEEsingle, "0x1p+0"); 2400 APFloat MNormalValue = APFloat(APFloat::IEEEsingle, "-0x1p+0"); 2401 APFloat PLargestValue = APFloat::getLargest(APFloat::IEEEsingle, false); 2402 APFloat MLargestValue = APFloat::getLargest(APFloat::IEEEsingle, true); 2403 APFloat PSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, false); 2404 APFloat MSmallestValue = APFloat::getSmallest(APFloat::IEEEsingle, true); 2405 APFloat PSmallestNormalized = 2406 APFloat::getSmallestNormalized(APFloat::IEEEsingle, false); 2407 APFloat MSmallestNormalized = 2408 APFloat::getSmallestNormalized(APFloat::IEEEsingle, true); 2409 2410 const int OverflowStatus = APFloat::opOverflow | APFloat::opInexact; 2411 const int UnderflowStatus = APFloat::opUnderflow | APFloat::opInexact; 2412 2413 const unsigned NumTests = 169; 2414 struct { 2415 APFloat x; 2416 APFloat y; 2417 const char *result; 2418 int status; 2419 int category; 2420 } SpecialCaseTests[NumTests] = { 2421 { PInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2422 { PInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2423 { PInf, PZero, "inf", APFloat::opOK, APFloat::fcInfinity }, 2424 { PInf, MZero, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2425 { PInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2426 #if 0 2427 // See Note 1. 2428 { PInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2429 #endif 2430 { PInf, PNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2431 { PInf, MNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2432 { PInf, PLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2433 { PInf, MLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2434 { PInf, PSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2435 { PInf, MSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2436 { PInf, PSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity }, 2437 { PInf, MSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2438 { MInf, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2439 { MInf, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2440 { MInf, PZero, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2441 { MInf, MZero, "inf", APFloat::opOK, APFloat::fcInfinity }, 2442 { MInf, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2443 #if 0 2444 // See Note 1. 2445 { MInf, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2446 #endif 2447 { MInf, PNormalValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2448 { MInf, MNormalValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2449 { MInf, PLargestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2450 { MInf, MLargestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2451 { MInf, PSmallestValue, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2452 { MInf, MSmallestValue, "inf", APFloat::opOK, APFloat::fcInfinity }, 2453 { MInf, PSmallestNormalized, "-inf", APFloat::opOK, APFloat::fcInfinity }, 2454 { MInf, MSmallestNormalized, "inf", APFloat::opOK, APFloat::fcInfinity }, 2455 { PZero, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2456 { PZero, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2457 { PZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2458 { PZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2459 { PZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2460 #if 0 2461 // See Note 1. 2462 { PZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2463 #endif 2464 { PZero, PNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2465 { PZero, MNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2466 { PZero, PLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2467 { PZero, MLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2468 { PZero, PSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2469 { PZero, MSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2470 { PZero, PSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2471 { PZero, MSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2472 { MZero, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2473 { MZero, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2474 { MZero, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2475 { MZero, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2476 { MZero, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2477 #if 0 2478 // See Note 1. 2479 { MZero, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2480 #endif 2481 { MZero, PNormalValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2482 { MZero, MNormalValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2483 { MZero, PLargestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2484 { MZero, MLargestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2485 { MZero, PSmallestValue, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2486 { MZero, MSmallestValue, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2487 { MZero, PSmallestNormalized, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2488 { MZero, MSmallestNormalized, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2489 { QNaN, PInf, "nan", APFloat::opOK, APFloat::fcNaN }, 2490 { QNaN, MInf, "nan", APFloat::opOK, APFloat::fcNaN }, 2491 { QNaN, PZero, "nan", APFloat::opOK, APFloat::fcNaN }, 2492 { QNaN, MZero, "nan", APFloat::opOK, APFloat::fcNaN }, 2493 { QNaN, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2494 #if 0 2495 // See Note 1. 2496 { QNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2497 #endif 2498 { QNaN, PNormalValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2499 { QNaN, MNormalValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2500 { QNaN, PLargestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2501 { QNaN, MLargestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2502 { QNaN, PSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2503 { QNaN, MSmallestValue, "nan", APFloat::opOK, APFloat::fcNaN }, 2504 { QNaN, PSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN }, 2505 { QNaN, MSmallestNormalized, "nan", APFloat::opOK, APFloat::fcNaN }, 2506 #if 0 2507 // See Note 1. 2508 { SNaN, PInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2509 { SNaN, MInf, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2510 { SNaN, PZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2511 { SNaN, MZero, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2512 { SNaN, QNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2513 { SNaN, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2514 { SNaN, PNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2515 { SNaN, MNormalValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2516 { SNaN, PLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2517 { SNaN, MLargestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2518 { SNaN, PSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2519 { SNaN, MSmallestValue, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2520 { SNaN, PSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2521 { SNaN, MSmallestNormalized, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2522 #endif 2523 { PNormalValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2524 { PNormalValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2525 { PNormalValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2526 { PNormalValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2527 { PNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2528 #if 0 2529 // See Note 1. 2530 { PNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2531 #endif 2532 { PNormalValue, PNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2533 { PNormalValue, MNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2534 { PNormalValue, PLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal }, 2535 { PNormalValue, MLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal }, 2536 { PNormalValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity }, 2537 { PNormalValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity }, 2538 { PNormalValue, PSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal }, 2539 { PNormalValue, MSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal }, 2540 { MNormalValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2541 { MNormalValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2542 { MNormalValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2543 { MNormalValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2544 { MNormalValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2545 #if 0 2546 // See Note 1. 2547 { MNormalValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2548 #endif 2549 { MNormalValue, PNormalValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2550 { MNormalValue, MNormalValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2551 { MNormalValue, PLargestValue, "-0x1p-128", UnderflowStatus, APFloat::fcNormal }, 2552 { MNormalValue, MLargestValue, "0x1p-128", UnderflowStatus, APFloat::fcNormal }, 2553 { MNormalValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity }, 2554 { MNormalValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity }, 2555 { MNormalValue, PSmallestNormalized, "-0x1p+126", APFloat::opOK, APFloat::fcNormal }, 2556 { MNormalValue, MSmallestNormalized, "0x1p+126", APFloat::opOK, APFloat::fcNormal }, 2557 { PLargestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2558 { PLargestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2559 { PLargestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2560 { PLargestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2561 { PLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2562 #if 0 2563 // See Note 1. 2564 { PLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2565 #endif 2566 { PLargestValue, PNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2567 { PLargestValue, MNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2568 { PLargestValue, PLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2569 { PLargestValue, MLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2570 { PLargestValue, PSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity }, 2571 { PLargestValue, MSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity }, 2572 { PLargestValue, PSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity }, 2573 { PLargestValue, MSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity }, 2574 { MLargestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2575 { MLargestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2576 { MLargestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2577 { MLargestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2578 { MLargestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2579 #if 0 2580 // See Note 1. 2581 { MLargestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2582 #endif 2583 { MLargestValue, PNormalValue, "-0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2584 { MLargestValue, MNormalValue, "0x1.fffffep+127", APFloat::opOK, APFloat::fcNormal }, 2585 { MLargestValue, PLargestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2586 { MLargestValue, MLargestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2587 { MLargestValue, PSmallestValue, "-inf", OverflowStatus, APFloat::fcInfinity }, 2588 { MLargestValue, MSmallestValue, "inf", OverflowStatus, APFloat::fcInfinity }, 2589 { MLargestValue, PSmallestNormalized, "-inf", OverflowStatus, APFloat::fcInfinity }, 2590 { MLargestValue, MSmallestNormalized, "inf", OverflowStatus, APFloat::fcInfinity }, 2591 { PSmallestValue, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2592 { PSmallestValue, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2593 { PSmallestValue, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2594 { PSmallestValue, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2595 { PSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2596 #if 0 2597 // See Note 1. 2598 { PSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2599 #endif 2600 { PSmallestValue, PNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2601 { PSmallestValue, MNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2602 { PSmallestValue, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2603 { PSmallestValue, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2604 { PSmallestValue, PSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2605 { PSmallestValue, MSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2606 { PSmallestValue, PSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal }, 2607 { PSmallestValue, MSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal }, 2608 { MSmallestValue, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2609 { MSmallestValue, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2610 { MSmallestValue, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2611 { MSmallestValue, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2612 { MSmallestValue, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2613 #if 0 2614 // See Note 1. 2615 { MSmallestValue, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2616 #endif 2617 { MSmallestValue, PNormalValue, "-0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2618 { MSmallestValue, MNormalValue, "0x1p-149", APFloat::opOK, APFloat::fcNormal }, 2619 { MSmallestValue, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2620 { MSmallestValue, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2621 { MSmallestValue, PSmallestValue, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2622 { MSmallestValue, MSmallestValue, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2623 { MSmallestValue, PSmallestNormalized, "-0x1p-23", APFloat::opOK, APFloat::fcNormal }, 2624 { MSmallestValue, MSmallestNormalized, "0x1p-23", APFloat::opOK, APFloat::fcNormal }, 2625 { PSmallestNormalized, PInf, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2626 { PSmallestNormalized, MInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2627 { PSmallestNormalized, PZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2628 { PSmallestNormalized, MZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2629 { PSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2630 #if 0 2631 // See Note 1. 2632 { PSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2633 #endif 2634 { PSmallestNormalized, PNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2635 { PSmallestNormalized, MNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2636 { PSmallestNormalized, PLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2637 { PSmallestNormalized, MLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2638 { PSmallestNormalized, PSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal }, 2639 { PSmallestNormalized, MSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal }, 2640 { PSmallestNormalized, PSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2641 { PSmallestNormalized, MSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2642 { MSmallestNormalized, PInf, "-0x0p+0", APFloat::opOK, APFloat::fcZero }, 2643 { MSmallestNormalized, MInf, "0x0p+0", APFloat::opOK, APFloat::fcZero }, 2644 { MSmallestNormalized, PZero, "-inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2645 { MSmallestNormalized, MZero, "inf", APFloat::opDivByZero, APFloat::fcInfinity }, 2646 { MSmallestNormalized, QNaN, "nan", APFloat::opOK, APFloat::fcNaN }, 2647 #if 0 2648 // See Note 1. 2649 { MSmallestNormalized, SNaN, "nan", APFloat::opInvalidOp, APFloat::fcNaN }, 2650 #endif 2651 { MSmallestNormalized, PNormalValue, "-0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2652 { MSmallestNormalized, MNormalValue, "0x1p-126", APFloat::opOK, APFloat::fcNormal }, 2653 { MSmallestNormalized, PLargestValue, "-0x0p+0", UnderflowStatus, APFloat::fcZero }, 2654 { MSmallestNormalized, MLargestValue, "0x0p+0", UnderflowStatus, APFloat::fcZero }, 2655 { MSmallestNormalized, PSmallestValue, "-0x1p+23", APFloat::opOK, APFloat::fcNormal }, 2656 { MSmallestNormalized, MSmallestValue, "0x1p+23", APFloat::opOK, APFloat::fcNormal }, 2657 { MSmallestNormalized, PSmallestNormalized, "-0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2658 { MSmallestNormalized, MSmallestNormalized, "0x1p+0", APFloat::opOK, APFloat::fcNormal }, 2659 }; 2660 2661 for (size_t i = 0; i < NumTests; ++i) { 2662 APFloat x(SpecialCaseTests[i].x); 2663 APFloat y(SpecialCaseTests[i].y); 2664 APFloat::opStatus status = x.divide(y, APFloat::rmNearestTiesToEven); 2665 2666 APFloat result(APFloat::IEEEsingle, SpecialCaseTests[i].result); 2667 2668 EXPECT_TRUE(result.bitwiseIsEqual(x)); 2669 EXPECT_TRUE((int)status == SpecialCaseTests[i].status); 2670 EXPECT_TRUE((int)x.getCategory() == SpecialCaseTests[i].category); 2671 } 2672 } 2673 2674 } 2675