2009-08-16 23:36:19 +00:00
|
|
|
//===- llvm/unittest/ADT/APFloat.cpp - APFloat unit tests ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include <ostream>
|
2009-12-24 08:56:26 +00:00
|
|
|
#include <string>
|
2009-08-16 23:36:19 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "llvm/ADT/APFloat.h"
|
2011-07-15 07:04:56 +00:00
|
|
|
#include "llvm/ADT/APSInt.h"
|
2009-08-16 23:36:19 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-12-24 08:56:26 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2009-08-16 23:36:19 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-09-03 22:57:02 +00:00
|
|
|
static double convertToDoubleFromString(const char *Str) {
|
|
|
|
llvm::APFloat F(0.0);
|
|
|
|
F.convertFromString(Str, llvm::APFloat::rmNearestTiesToEven);
|
|
|
|
return F.convertToDouble();
|
|
|
|
}
|
|
|
|
|
2009-12-24 08:56:26 +00:00
|
|
|
static std::string convertToString(double d, unsigned Prec, unsigned Pad) {
|
|
|
|
llvm::SmallVector<char, 100> Buffer;
|
|
|
|
llvm::APFloat F(d);
|
|
|
|
F.toString(Buffer, Prec, Pad);
|
|
|
|
return std::string(Buffer.data(), Buffer.size());
|
|
|
|
}
|
|
|
|
|
2009-08-16 23:36:19 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(APFloatTest, Zero) {
|
2011-08-29 17:54:20 +00:00
|
|
|
EXPECT_EQ(0.0f, APFloat(0.0f).convertToFloat());
|
|
|
|
EXPECT_EQ(-0.0f, APFloat(-0.0f).convertToFloat());
|
|
|
|
EXPECT_TRUE(APFloat(-0.0f).isNegative());
|
2009-08-16 23:36:19 +00:00
|
|
|
|
2011-08-29 17:54:20 +00:00
|
|
|
EXPECT_EQ(0.0, APFloat(0.0).convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(-0.0).convertToDouble());
|
|
|
|
EXPECT_TRUE(APFloat(-0.0).isNegative());
|
2009-08-16 23:36:19 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 23:30:43 +00:00
|
|
|
TEST(APFloatTest, fromZeroDecimalString) {
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0").convertToDouble());
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0").convertToDouble());
|
2009-08-20 23:30:43 +00:00
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.").convertToDouble());
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.").convertToDouble());
|
2009-08-20 23:30:43 +00:00
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0").convertToDouble());
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0").convertToDouble());
|
2009-08-20 23:30:43 +00:00
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0").convertToDouble());
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0").convertToDouble());
|
2009-08-20 23:30:43 +00:00
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "00000.").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+00000.").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-00000.").convertToDouble());
|
|
|
|
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, ".00000").convertToDouble());
|
2009-08-20 23:30:43 +00:00
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.00000").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.00000").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0000.00000").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0000.00000").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0000.00000").convertToDouble());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, fromZeroDecimalSingleExponentString) {
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.e-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.e-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.e-1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, ".0e-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+.0e-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-.0e-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0.0e-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0.0e-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0.0e-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "000.0000e1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+000.0000e+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-000.0000e+1").convertToDouble());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, fromZeroDecimalLargeExponentString) {
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e1234").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e1234").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e1234").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e+1234").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e+1234").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e+1234").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0e-1234").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0e-1234").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0e-1234").convertToDouble());
|
|
|
|
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, "000.0000e1234").convertToDouble());
|
|
|
|
EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, "000.0000e-1234").convertToDouble());
|
2009-08-16 23:36:19 +00:00
|
|
|
|
2009-08-20 23:30:43 +00:00
|
|
|
EXPECT_EQ(0.0, APFloat(APFloat::IEEEdouble, StringRef("0e1234\02", 6)).convertToDouble());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, fromZeroHexadecimalString) {
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p1").convertToDouble());
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1").convertToDouble());
|
2009-08-20 23:30:43 +00:00
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.p-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.0p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x.0p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x.0p-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.0p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0x0.0p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0.0p-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1").convertToDouble());
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1").convertToDouble());
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1").convertToDouble());
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1").convertToDouble());
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0p1234").convertToDouble());
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_EQ(-0.0, APFloat(APFloat::IEEEdouble, "-0x0p1234").convertToDouble());
|
2009-08-20 23:30:43 +00:00
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x00000.p1234").convertToDouble());
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0000.00000p1234").convertToDouble());
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x.00000p1234").convertToDouble());
|
|
|
|
EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0x0.p1234").convertToDouble());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, fromDecimalString) {
|
|
|
|
EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "1").convertToDouble());
|
|
|
|
EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble, "2.").convertToDouble());
|
|
|
|
EXPECT_EQ(0.5, APFloat(APFloat::IEEEdouble, ".5").convertToDouble());
|
|
|
|
EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "1.0").convertToDouble());
|
|
|
|
EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-2").convertToDouble());
|
|
|
|
EXPECT_EQ(-4.0, APFloat(APFloat::IEEEdouble, "-4.").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble, "-.5").convertToDouble());
|
|
|
|
EXPECT_EQ(-1.5, APFloat(APFloat::IEEEdouble, "-1.5").convertToDouble());
|
|
|
|
EXPECT_EQ(1.25e12, APFloat(APFloat::IEEEdouble, "1.25e12").convertToDouble());
|
|
|
|
EXPECT_EQ(1.25e+12, APFloat(APFloat::IEEEdouble, "1.25e+12").convertToDouble());
|
|
|
|
EXPECT_EQ(1.25e-12, APFloat(APFloat::IEEEdouble, "1.25e-12").convertToDouble());
|
|
|
|
EXPECT_EQ(1024.0, APFloat(APFloat::IEEEdouble, "1024.").convertToDouble());
|
|
|
|
EXPECT_EQ(1024.05, APFloat(APFloat::IEEEdouble, "1024.05000").convertToDouble());
|
|
|
|
EXPECT_EQ(0.05, APFloat(APFloat::IEEEdouble, ".05000").convertToDouble());
|
|
|
|
EXPECT_EQ(2.0, APFloat(APFloat::IEEEdouble, "2.").convertToDouble());
|
|
|
|
EXPECT_EQ(2.0e2, APFloat(APFloat::IEEEdouble, "2.e2").convertToDouble());
|
|
|
|
EXPECT_EQ(2.0e+2, APFloat(APFloat::IEEEdouble, "2.e+2").convertToDouble());
|
|
|
|
EXPECT_EQ(2.0e-2, APFloat(APFloat::IEEEdouble, "2.e-2").convertToDouble());
|
|
|
|
EXPECT_EQ(2.05e2, APFloat(APFloat::IEEEdouble, "002.05000e2").convertToDouble());
|
|
|
|
EXPECT_EQ(2.05e+2, APFloat(APFloat::IEEEdouble, "002.05000e+2").convertToDouble());
|
|
|
|
EXPECT_EQ(2.05e-2, APFloat(APFloat::IEEEdouble, "002.05000e-2").convertToDouble());
|
|
|
|
EXPECT_EQ(2.05e12, APFloat(APFloat::IEEEdouble, "002.05000e12").convertToDouble());
|
|
|
|
EXPECT_EQ(2.05e+12, APFloat(APFloat::IEEEdouble, "002.05000e+12").convertToDouble());
|
|
|
|
EXPECT_EQ(2.05e-12, APFloat(APFloat::IEEEdouble, "002.05000e-12").convertToDouble());
|
2010-02-26 22:20:41 +00:00
|
|
|
|
|
|
|
// These are "carefully selected" to overflow the fast log-base
|
|
|
|
// calculations in APFloat.cpp
|
|
|
|
EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "99e99999").isInfinity());
|
|
|
|
EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-99e99999").isInfinity());
|
|
|
|
EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "1e-99999").isPosZero());
|
|
|
|
EXPECT_TRUE(APFloat(APFloat::IEEEdouble, "-1e-99999").isNegZero());
|
2009-08-20 23:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, fromHexadecimalString) {
|
|
|
|
EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p0").convertToDouble());
|
|
|
|
EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p0").convertToDouble());
|
|
|
|
EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p0").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p+0").convertToDouble());
|
|
|
|
EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p+0").convertToDouble());
|
|
|
|
EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p+0").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble, "0x1p-0").convertToDouble());
|
|
|
|
EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble, "+0x1p-0").convertToDouble());
|
|
|
|
EXPECT_EQ(-1.0, APFloat(APFloat::IEEEdouble, "-0x1p-0").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble, "0x1p1").convertToDouble());
|
|
|
|
EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p1").convertToDouble());
|
|
|
|
EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 2.0, APFloat(APFloat::IEEEdouble, "0x1p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+2.0, APFloat(APFloat::IEEEdouble, "+0x1p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-2.0, APFloat(APFloat::IEEEdouble, "-0x1p+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.5, APFloat(APFloat::IEEEdouble, "0x1p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.5, APFloat(APFloat::IEEEdouble, "+0x1p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.5, APFloat(APFloat::IEEEdouble, "-0x1p-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble, "0x1.8p1").convertToDouble());
|
|
|
|
EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p1").convertToDouble());
|
|
|
|
EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 3.0, APFloat(APFloat::IEEEdouble, "0x1.8p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+3.0, APFloat(APFloat::IEEEdouble, "+0x1.8p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-3.0, APFloat(APFloat::IEEEdouble, "-0x1.8p+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.75, APFloat(APFloat::IEEEdouble, "0x1.8p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.75, APFloat(APFloat::IEEEdouble, "+0x1.8p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.75, APFloat(APFloat::IEEEdouble, "-0x1.8p-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000.000p1").convertToDouble());
|
|
|
|
EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p1").convertToDouble());
|
|
|
|
EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000.000p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble, "0x1000.000p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000.000p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000.000p-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000p1").convertToDouble());
|
|
|
|
EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p1").convertToDouble());
|
|
|
|
EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 8192.0, APFloat(APFloat::IEEEdouble, "0x1000p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(+8192.0, APFloat(APFloat::IEEEdouble, "+0x1000p+1").convertToDouble());
|
|
|
|
EXPECT_EQ(-8192.0, APFloat(APFloat::IEEEdouble, "-0x1000p+1").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 2048.0, APFloat(APFloat::IEEEdouble, "0x1000p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(+2048.0, APFloat(APFloat::IEEEdouble, "+0x1000p-1").convertToDouble());
|
|
|
|
EXPECT_EQ(-2048.0, APFloat(APFloat::IEEEdouble, "-0x1000p-1").convertToDouble());
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble, "0x10p10").convertToDouble());
|
|
|
|
EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p10").convertToDouble());
|
|
|
|
EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p10").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 16384.0, APFloat(APFloat::IEEEdouble, "0x10p+10").convertToDouble());
|
|
|
|
EXPECT_EQ(+16384.0, APFloat(APFloat::IEEEdouble, "+0x10p+10").convertToDouble());
|
|
|
|
EXPECT_EQ(-16384.0, APFloat(APFloat::IEEEdouble, "-0x10p+10").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ( 0.015625, APFloat(APFloat::IEEEdouble, "0x10p-10").convertToDouble());
|
|
|
|
EXPECT_EQ(+0.015625, APFloat(APFloat::IEEEdouble, "+0x10p-10").convertToDouble());
|
|
|
|
EXPECT_EQ(-0.015625, APFloat(APFloat::IEEEdouble, "-0x10p-10").convertToDouble());
|
|
|
|
|
|
|
|
EXPECT_EQ(1.0625, APFloat(APFloat::IEEEdouble, "0x1.1p0").convertToDouble());
|
|
|
|
EXPECT_EQ(1.0, APFloat(APFloat::IEEEdouble, "0x1p0").convertToDouble());
|
2009-09-03 22:57:02 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
|
2009-08-16 23:36:19 +00:00
|
|
|
}
|
|
|
|
|
2009-12-24 08:56:26 +00:00
|
|
|
TEST(APFloatTest, toString) {
|
|
|
|
ASSERT_EQ("10", convertToString(10.0, 6, 3));
|
|
|
|
ASSERT_EQ("1.0E+1", convertToString(10.0, 6, 0));
|
|
|
|
ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2));
|
|
|
|
ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 4, 2));
|
|
|
|
ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 5, 1));
|
|
|
|
ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2));
|
2009-12-24 23:18:09 +00:00
|
|
|
ASSERT_EQ("0.0101", convertToString(1.01E-2, 4, 2));
|
2009-12-24 08:56:26 +00:00
|
|
|
ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 5, 1));
|
2009-12-24 23:18:09 +00:00
|
|
|
ASSERT_EQ("0.7853981633974483", convertToString(0.78539816339744830961, 0, 3));
|
|
|
|
ASSERT_EQ("4.940656458412465E-324", convertToString(4.9406564584124654e-324, 0, 3));
|
2010-02-03 03:42:44 +00:00
|
|
|
ASSERT_EQ("873.1834", convertToString(873.1834, 0, 1));
|
|
|
|
ASSERT_EQ("8.731834E+2", convertToString(873.1834, 0, 0));
|
2009-12-24 08:56:26 +00:00
|
|
|
}
|
|
|
|
|
2011-07-15 07:04:56 +00:00
|
|
|
TEST(APFloatTest, toInteger) {
|
|
|
|
bool isExact = false;
|
|
|
|
APSInt result(5, /*isUnsigned=*/true);
|
|
|
|
|
|
|
|
EXPECT_EQ(APFloat::opOK,
|
|
|
|
APFloat(APFloat::IEEEdouble, "10")
|
|
|
|
.convertToInteger(result, APFloat::rmTowardZero, &isExact));
|
|
|
|
EXPECT_TRUE(isExact);
|
|
|
|
EXPECT_EQ(APSInt(APInt(5, 10), true), result);
|
|
|
|
|
|
|
|
EXPECT_EQ(APFloat::opInvalidOp,
|
|
|
|
APFloat(APFloat::IEEEdouble, "-10")
|
|
|
|
.convertToInteger(result, APFloat::rmTowardZero, &isExact));
|
|
|
|
EXPECT_FALSE(isExact);
|
|
|
|
EXPECT_EQ(APSInt::getMinValue(5, true), result);
|
|
|
|
|
|
|
|
EXPECT_EQ(APFloat::opInvalidOp,
|
|
|
|
APFloat(APFloat::IEEEdouble, "32")
|
|
|
|
.convertToInteger(result, APFloat::rmTowardZero, &isExact));
|
|
|
|
EXPECT_FALSE(isExact);
|
|
|
|
EXPECT_EQ(APSInt::getMaxValue(5, true), result);
|
|
|
|
|
|
|
|
EXPECT_EQ(APFloat::opInexact,
|
|
|
|
APFloat(APFloat::IEEEdouble, "7.9")
|
|
|
|
.convertToInteger(result, APFloat::rmTowardZero, &isExact));
|
|
|
|
EXPECT_FALSE(isExact);
|
|
|
|
EXPECT_EQ(APSInt(APInt(5, 7), true), result);
|
|
|
|
|
|
|
|
result.setIsUnsigned(false);
|
|
|
|
EXPECT_EQ(APFloat::opOK,
|
|
|
|
APFloat(APFloat::IEEEdouble, "-10")
|
|
|
|
.convertToInteger(result, APFloat::rmTowardZero, &isExact));
|
|
|
|
EXPECT_TRUE(isExact);
|
|
|
|
EXPECT_EQ(APSInt(APInt(5, -10, true), false), result);
|
|
|
|
|
|
|
|
EXPECT_EQ(APFloat::opInvalidOp,
|
|
|
|
APFloat(APFloat::IEEEdouble, "-17")
|
|
|
|
.convertToInteger(result, APFloat::rmTowardZero, &isExact));
|
|
|
|
EXPECT_FALSE(isExact);
|
|
|
|
EXPECT_EQ(APSInt::getMinValue(5, false), result);
|
|
|
|
|
|
|
|
EXPECT_EQ(APFloat::opInvalidOp,
|
|
|
|
APFloat(APFloat::IEEEdouble, "16")
|
|
|
|
.convertToInteger(result, APFloat::rmTowardZero, &isExact));
|
|
|
|
EXPECT_FALSE(isExact);
|
|
|
|
EXPECT_EQ(APSInt::getMaxValue(5, false), result);
|
|
|
|
}
|
|
|
|
|
2010-02-28 02:51:25 +00:00
|
|
|
static APInt nanbits(const fltSemantics &Sem,
|
|
|
|
bool SNaN, bool Negative, uint64_t fill) {
|
|
|
|
APInt apfill(64, fill);
|
|
|
|
if (SNaN)
|
|
|
|
return APFloat::getSNaN(Sem, Negative, &apfill).bitcastToAPInt();
|
|
|
|
else
|
|
|
|
return APFloat::getQNaN(Sem, Negative, &apfill).bitcastToAPInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, makeNaN) {
|
|
|
|
ASSERT_EQ(0x7fc00000, nanbits(APFloat::IEEEsingle, false, false, 0));
|
|
|
|
ASSERT_EQ(0xffc00000, nanbits(APFloat::IEEEsingle, false, true, 0));
|
|
|
|
ASSERT_EQ(0x7fc0ae72, nanbits(APFloat::IEEEsingle, false, false, 0xae72));
|
|
|
|
ASSERT_EQ(0x7fffae72, nanbits(APFloat::IEEEsingle, false, false, 0xffffae72));
|
|
|
|
ASSERT_EQ(0x7fa00000, nanbits(APFloat::IEEEsingle, true, false, 0));
|
|
|
|
ASSERT_EQ(0xffa00000, nanbits(APFloat::IEEEsingle, true, true, 0));
|
|
|
|
ASSERT_EQ(0x7f80ae72, nanbits(APFloat::IEEEsingle, true, false, 0xae72));
|
|
|
|
ASSERT_EQ(0x7fbfae72, nanbits(APFloat::IEEEsingle, true, false, 0xffffae72));
|
|
|
|
|
|
|
|
ASSERT_EQ(0x7ff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, false, 0));
|
|
|
|
ASSERT_EQ(0xfff8000000000000ULL, nanbits(APFloat::IEEEdouble, false, true, 0));
|
|
|
|
ASSERT_EQ(0x7ff800000000ae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xae72));
|
|
|
|
ASSERT_EQ(0x7fffffffffffae72ULL, nanbits(APFloat::IEEEdouble, false, false, 0xffffffffffffae72ULL));
|
|
|
|
ASSERT_EQ(0x7ff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, false, 0));
|
|
|
|
ASSERT_EQ(0xfff4000000000000ULL, nanbits(APFloat::IEEEdouble, true, true, 0));
|
|
|
|
ASSERT_EQ(0x7ff000000000ae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xae72));
|
|
|
|
ASSERT_EQ(0x7ff7ffffffffae72ULL, nanbits(APFloat::IEEEdouble, true, false, 0xffffffffffffae72ULL));
|
|
|
|
}
|
|
|
|
|
2009-08-17 00:55:33 +00:00
|
|
|
#ifdef GTEST_HAS_DEATH_TEST
|
2010-03-17 01:18:45 +00:00
|
|
|
#ifndef NDEBUG
|
2009-08-17 00:55:33 +00:00
|
|
|
TEST(APFloatTest, SemanticsDeath) {
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEsingle, 0.0f).convertToDouble(), "Float semantics are not IEEEdouble");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, 0.0 ).convertToFloat(), "Float semantics are not IEEEsingle");
|
|
|
|
}
|
|
|
|
|
2009-08-20 23:30:43 +00:00
|
|
|
TEST(APFloatTest, StringDecimalDeath) {
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ""), "Invalid string length");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+"), "String has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-"), "String has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("\0", 1)), "Invalid character in significand");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\0", 2)), "Invalid character in significand");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02", 3)), "Invalid character in significand");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1\02e1", 5)), "Invalid character in significand");
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e\0", 3)), "Invalid character in exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\0", 4)), "Invalid character in exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("1e1\02", 5)), "Invalid character in exponent");
|
2009-08-16 23:36:19 +00:00
|
|
|
|
2009-08-20 23:30:43 +00:00
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0f"), "Invalid character in significand");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".."), "String contains multiple dots");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "..0"), "String contains multiple dots");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0.0"), "String contains multiple dots");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, StringDecimalSignificandDeath) {
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "."), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+."), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-."), "Significand has no digits");
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "e"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e"), "Significand has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "e1"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+e1"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-e1"), "Significand has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".e1"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e1"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e1"), "Significand has no digits");
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".e"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.e"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.e"), "Significand has no digits");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, StringDecimalExponentDeath) {
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1e"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1e"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.e"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1.e"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1.e"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+.1e"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-.1e"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.1e"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+1.1e"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-1.1e"), "Exponent has no digits");
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1e-"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, ".1e-"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "1.0e-"), "Exponent has no digits");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, StringHexadecimalDeath) {
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x"), "Invalid string");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x"), "Invalid string");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x"), "Invalid string");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0"), "Hex strings require an exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0"), "Hex strings require an exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0"), "Hex strings require an exponent");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0."), "Hex strings require an exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0."), "Hex strings require an exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0."), "Hex strings require an exponent");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.0"), "Hex strings require an exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.0"), "Hex strings require an exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.0"), "Hex strings require an exponent");
|
2009-08-16 23:36:19 +00:00
|
|
|
|
2009-08-20 23:30:43 +00:00
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x0.0"), "Hex strings require an exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x0.0"), "Hex strings require an exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x0.0"), "Hex strings require an exponent");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x\0", 3)), "Invalid character in significand");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\0", 4)), "Invalid character in significand");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02", 5)), "Invalid character in significand");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1\02p1", 7)), "Invalid character in significand");
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p\0", 5)), "Invalid character in exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\0", 6)), "Invalid character in exponent");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, StringRef("0x1p1\02", 7)), "Invalid character in exponent");
|
2009-08-16 23:36:19 +00:00
|
|
|
|
2009-08-17 00:14:11 +00:00
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p0f"), "Invalid character in exponent");
|
2009-08-20 23:30:43 +00:00
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..p1"), "String contains multiple dots");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x..0p1"), "String contains multiple dots");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.0.0p1"), "String contains multiple dots");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, StringHexadecimalSignificandDeath) {
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x."), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x."), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x."), "Significand has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp"), "Significand has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp+"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp+"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp+"), "Significand has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0xp-"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0xp-"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0xp-"), "Significand has no digits");
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p"), "Significand has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p+"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p+"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p+"), "Significand has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.p-"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.p-"), "Significand has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.p-"), "Significand has no digits");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(APFloatTest, StringHexadecimalExponentDeath) {
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p+"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1p-"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1p-"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1p-"), "Exponent has no digits");
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p+"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.p-"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.p-"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.p-"), "Exponent has no digits");
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p+"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x.1p-"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x.1p-"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x.1p-"), "Exponent has no digits");
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p+"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p+"), "Exponent has no digits");
|
|
|
|
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "0x1.1p-"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "+0x1.1p-"), "Exponent has no digits");
|
|
|
|
EXPECT_DEATH(APFloat(APFloat::IEEEdouble, "-0x1.1p-"), "Exponent has no digits");
|
2009-08-16 23:36:19 +00:00
|
|
|
}
|
2009-08-17 00:55:33 +00:00
|
|
|
#endif
|
2010-03-17 01:18:45 +00:00
|
|
|
#endif
|
2009-08-16 23:36:19 +00:00
|
|
|
|
2011-03-30 15:42:27 +00:00
|
|
|
TEST(APFloatTest, exactInverse) {
|
|
|
|
APFloat inv(0.0f);
|
|
|
|
|
|
|
|
// Trivial operation.
|
|
|
|
EXPECT_TRUE(APFloat(2.0).getExactInverse(&inv));
|
|
|
|
EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5)));
|
|
|
|
EXPECT_TRUE(APFloat(2.0f).getExactInverse(&inv));
|
|
|
|
EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(0.5f)));
|
|
|
|
|
|
|
|
// FLT_MIN
|
|
|
|
EXPECT_TRUE(APFloat(1.17549435e-38f).getExactInverse(&inv));
|
|
|
|
EXPECT_TRUE(inv.bitwiseIsEqual(APFloat(8.5070592e+37f)));
|
|
|
|
|
2011-03-30 17:02:54 +00:00
|
|
|
// Large float, inverse is a denormal.
|
|
|
|
EXPECT_FALSE(APFloat(1.7014118e38f).getExactInverse(0));
|
2011-03-30 15:42:27 +00:00
|
|
|
// Zero
|
|
|
|
EXPECT_FALSE(APFloat(0.0).getExactInverse(0));
|
|
|
|
// Denormalized float
|
|
|
|
EXPECT_FALSE(APFloat(1.40129846e-45f).getExactInverse(0));
|
|
|
|
}
|
|
|
|
|
2012-08-15 05:39:46 +00:00
|
|
|
TEST(APFloatTest, roundToIntegral) {
|
2012-08-15 18:28:45 +00:00
|
|
|
APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble)), P(0.0);
|
2012-08-15 05:39:46 +00:00
|
|
|
|
|
|
|
P = T;
|
|
|
|
P.roundToIntegral(APFloat::rmTowardZero);
|
|
|
|
EXPECT_EQ(-0.0, P.convertToDouble());
|
|
|
|
P = T;
|
|
|
|
P.roundToIntegral(APFloat::rmTowardNegative);
|
|
|
|
EXPECT_EQ(-1.0, P.convertToDouble());
|
|
|
|
P = T;
|
|
|
|
P.roundToIntegral(APFloat::rmTowardPositive);
|
|
|
|
EXPECT_EQ(-0.0, P.convertToDouble());
|
|
|
|
P = T;
|
|
|
|
P.roundToIntegral(APFloat::rmNearestTiesToEven);
|
|
|
|
EXPECT_EQ(-0.0, P.convertToDouble());
|
|
|
|
|
|
|
|
P = S;
|
|
|
|
P.roundToIntegral(APFloat::rmTowardZero);
|
|
|
|
EXPECT_EQ(3.0, P.convertToDouble());
|
|
|
|
P = S;
|
|
|
|
P.roundToIntegral(APFloat::rmTowardNegative);
|
|
|
|
EXPECT_EQ(3.0, P.convertToDouble());
|
|
|
|
P = S;
|
|
|
|
P.roundToIntegral(APFloat::rmTowardPositive);
|
|
|
|
EXPECT_EQ(4.0, P.convertToDouble());
|
|
|
|
P = S;
|
|
|
|
P.roundToIntegral(APFloat::rmNearestTiesToEven);
|
|
|
|
EXPECT_EQ(3.0, P.convertToDouble());
|
2012-08-15 18:28:45 +00:00
|
|
|
|
|
|
|
P = R;
|
|
|
|
P.roundToIntegral(APFloat::rmTowardZero);
|
|
|
|
EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
|
|
|
|
P = R;
|
|
|
|
P.roundToIntegral(APFloat::rmTowardNegative);
|
|
|
|
EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
|
|
|
|
P = R;
|
|
|
|
P.roundToIntegral(APFloat::rmTowardPositive);
|
|
|
|
EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
|
|
|
|
P = R;
|
|
|
|
P.roundToIntegral(APFloat::rmNearestTiesToEven);
|
|
|
|
EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
|
2012-09-26 14:06:58 +00:00
|
|
|
|
|
|
|
P = APFloat::getZero(APFloat::IEEEdouble);
|
|
|
|
P.roundToIntegral(APFloat::rmTowardZero);
|
|
|
|
EXPECT_EQ(0.0, P.convertToDouble());
|
|
|
|
P = APFloat::getZero(APFloat::IEEEdouble, true);
|
|
|
|
P.roundToIntegral(APFloat::rmTowardZero);
|
|
|
|
EXPECT_EQ(-0.0, P.convertToDouble());
|
|
|
|
P = APFloat::getNaN(APFloat::IEEEdouble);
|
|
|
|
P.roundToIntegral(APFloat::rmTowardZero);
|
|
|
|
EXPECT_TRUE(IsNAN(P.convertToDouble()));
|
|
|
|
P = APFloat::getInf(APFloat::IEEEdouble);
|
|
|
|
P.roundToIntegral(APFloat::rmTowardZero);
|
|
|
|
EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() > 0.0);
|
|
|
|
P = APFloat::getInf(APFloat::IEEEdouble, true);
|
|
|
|
P.roundToIntegral(APFloat::rmTowardZero);
|
|
|
|
EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() < 0.0);
|
|
|
|
|
2012-08-15 05:39:46 +00:00
|
|
|
}
|
|
|
|
|
2011-10-12 21:51:36 +00:00
|
|
|
TEST(APFloatTest, getLargest) {
|
2011-10-12 23:14:41 +00:00
|
|
|
EXPECT_EQ(3.402823466e+38f, APFloat::getLargest(APFloat::IEEEsingle).convertToFloat());
|
|
|
|
EXPECT_EQ(1.7976931348623158e+308, APFloat::getLargest(APFloat::IEEEdouble).convertToDouble());
|
2011-10-12 21:51:36 +00:00
|
|
|
}
|
|
|
|
|
2011-11-26 03:38:02 +00:00
|
|
|
TEST(APFloatTest, convert) {
|
|
|
|
bool losesInfo;
|
|
|
|
APFloat test(APFloat::IEEEdouble, "1.0");
|
|
|
|
test.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &losesInfo);
|
|
|
|
EXPECT_EQ(1.0f, test.convertToFloat());
|
|
|
|
EXPECT_FALSE(losesInfo);
|
|
|
|
|
|
|
|
test = APFloat(APFloat::x87DoubleExtended, "0x1p-53");
|
|
|
|
test.add(APFloat(APFloat::x87DoubleExtended, "1.0"), APFloat::rmNearestTiesToEven);
|
|
|
|
test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
|
|
|
|
EXPECT_EQ(1.0, test.convertToDouble());
|
|
|
|
EXPECT_TRUE(losesInfo);
|
|
|
|
|
|
|
|
test = APFloat(APFloat::IEEEquad, "0x1p-53");
|
|
|
|
test.add(APFloat(APFloat::IEEEquad, "1.0"), APFloat::rmNearestTiesToEven);
|
|
|
|
test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
|
|
|
|
EXPECT_EQ(1.0, test.convertToDouble());
|
|
|
|
EXPECT_TRUE(losesInfo);
|
|
|
|
|
|
|
|
test = APFloat(APFloat::x87DoubleExtended, "0xf.fffffffp+28");
|
|
|
|
test.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &losesInfo);
|
|
|
|
EXPECT_EQ(4294967295.0, test.convertToDouble());
|
|
|
|
EXPECT_FALSE(losesInfo);
|
|
|
|
}
|
2012-10-29 18:09:01 +00:00
|
|
|
|
|
|
|
TEST(APFloatTest, PPCDoubleDouble) {
|
|
|
|
APFloat test(APFloat::PPCDoubleDouble, "1.0");
|
|
|
|
EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
|
|
|
|
EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
|
|
|
|
|
|
|
|
test.divide(APFloat(APFloat::PPCDoubleDouble, "3.0"), APFloat::rmNearestTiesToEven);
|
|
|
|
EXPECT_EQ(0x3fd5555555555555ull, test.bitcastToAPInt().getRawData()[0]);
|
|
|
|
EXPECT_EQ(0x3c75555555555556ull, test.bitcastToAPInt().getRawData()[1]);
|
|
|
|
|
|
|
|
// LDBL_MAX
|
|
|
|
test = APFloat(APFloat::PPCDoubleDouble, "1.79769313486231580793728971405301e+308");
|
|
|
|
EXPECT_EQ(0x7fefffffffffffffull, test.bitcastToAPInt().getRawData()[0]);
|
|
|
|
EXPECT_EQ(0x7c8ffffffffffffeull, test.bitcastToAPInt().getRawData()[1]);
|
|
|
|
|
|
|
|
// LDBL_MIN
|
|
|
|
test = APFloat(APFloat::PPCDoubleDouble, "2.00416836000897277799610805135016e-292");
|
|
|
|
EXPECT_EQ(0x0360000000000000ull, test.bitcastToAPInt().getRawData()[0]);
|
|
|
|
EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
|
|
|
|
|
|
|
|
test = APFloat(APFloat::PPCDoubleDouble, "1.0");
|
|
|
|
test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-105"), APFloat::rmNearestTiesToEven);
|
|
|
|
EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
|
|
|
|
EXPECT_EQ(0x3960000000000000ull, test.bitcastToAPInt().getRawData()[1]);
|
|
|
|
|
|
|
|
test = APFloat(APFloat::PPCDoubleDouble, "1.0");
|
|
|
|
test.add(APFloat(APFloat::PPCDoubleDouble, "0x1p-106"), APFloat::rmNearestTiesToEven);
|
|
|
|
EXPECT_EQ(0x3ff0000000000000ull, test.bitcastToAPInt().getRawData()[0]);
|
|
|
|
#if 0 // XFAIL
|
|
|
|
// This is what we would expect with a true double-double implementation
|
|
|
|
EXPECT_EQ(0x3950000000000000ull, test.bitcastToAPInt().getRawData()[1]);
|
|
|
|
#else
|
|
|
|
// This is what we get with our 106-bit mantissa approximation
|
|
|
|
EXPECT_EQ(0x0000000000000000ull, test.bitcastToAPInt().getRawData()[1]);
|
|
|
|
#endif
|
|
|
|
}
|
2009-08-16 23:36:19 +00:00
|
|
|
}
|