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