Skip to content

Commit

Permalink
llvm-stress: stop abusing ConstantFP::get()
Browse files Browse the repository at this point in the history
ConstantFP::get(Type*, double) is unreliably host-specific:
it can't handle a type like PPC128 on an x86 host. It even
has a comment to that effect: "This should only be used for
simple constant values like 2.0/1.0 etc, that are
known-valid both as host double and as the target format."

Instead, use APFloat. While we're at it, randomize the floating
point value more thoroughly; it was previously limited
to the range 0 to 2**19 - 1.

PR12451.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154446 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
nobled committed Apr 10, 2012
1 parent 701de8f commit 83f17f2
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions tools/llvm-stress/llvm-stress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,28 @@ class Random {
public:
/// C'tor
Random(unsigned _seed):Seed(_seed) {}
/// Return the next random value.
unsigned Rand() {
unsigned Val = Seed + 0x000b07a1;

/// Return a random integer, up to a
/// maximum of 2**19 - 1.
uint32_t Rand() {
uint32_t Val = Seed + 0x000b07a1;
Seed = (Val * 0x3c7c0ac1);
// Only lowest 19 bits are random-ish.
return Seed & 0x7ffff;
}

/// Return a random 32 bit integer.
uint32_t Rand32() {
uint32_t Val = Rand();
Val &= 0xffff;
return Val | (Rand() << 16);
}

/// Return a random 64 bit integer.
uint64_t Rand64() {
uint64_t Val = Rand32();
return Val | (uint64_t(Rand32()) << 32);
}
private:
unsigned Seed;
};
Expand Down Expand Up @@ -348,10 +362,20 @@ struct ConstModifier: public Modifier {
}

if (Ty->isFloatingPointTy()) {
// Generate 128 random bits, the size of the (currently)
// largest floating-point types.
uint64_t RandomBits[2];
for (unsigned i = 0; i < 2; ++i)
RandomBits[i] = Ran->Rand64();

APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits));

bool isIEEE = !Ty->isX86_FP80Ty() && !Ty->isPPC_FP128Ty();
APFloat RandomFloat(RandomInt, isIEEE);

if (Ran->Rand() & 1)
return PT->push_back(ConstantFP::getNullValue(Ty));
return PT->push_back(ConstantFP::get(Ty,
static_cast<double>(1)/Ran->Rand()));
return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
}

if (Ty->isIntegerTy()) {
Expand Down

0 comments on commit 83f17f2

Please sign in to comment.