mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-19 18:24:00 +00:00
Fix a bad overflow check pointed out by Ben.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185226 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -47,9 +47,7 @@ static uint64_t div96bit(uint64_t W[2], uint32_t D) {
|
|||||||
uint64_t x = W[1];
|
uint64_t x = W[1];
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
||||||
// This is really a 64-bit division.
|
assert(x != 0 && "This is really a 64-bit division");
|
||||||
if (!x)
|
|
||||||
return y / D;
|
|
||||||
|
|
||||||
// This long division algorithm automatically saturates on overflow.
|
// This long division algorithm automatically saturates on overflow.
|
||||||
for (i = 0; i < 64 && x; ++i) {
|
for (i = 0; i < 64 && x; ++i) {
|
||||||
@ -75,7 +73,7 @@ void BlockFrequency::scale(uint32_t N, uint32_t D) {
|
|||||||
uint64_t MulRes = (MulHi << 32) + MulLo;
|
uint64_t MulRes = (MulHi << 32) + MulLo;
|
||||||
|
|
||||||
// If the product fits in 64 bits, just use built-in division.
|
// If the product fits in 64 bits, just use built-in division.
|
||||||
if (MulHi <= UINT32_MAX && MulRes <= MulLo) {
|
if (MulHi <= UINT32_MAX && MulRes >= MulLo) {
|
||||||
Frequency = MulRes / D;
|
Frequency = MulRes / D;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -68,6 +68,11 @@ TEST(BlockFrequencyTest, Saturate) {
|
|||||||
Freq = 0x1000000000000000ULL;
|
Freq = 0x1000000000000000ULL;
|
||||||
Freq /= BranchProbability(10000, 160000);
|
Freq /= BranchProbability(10000, 160000);
|
||||||
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
||||||
|
|
||||||
|
// Try to cheat the multiplication overflow check.
|
||||||
|
Freq = 0x00000001f0000001ull;
|
||||||
|
Freq /= BranchProbability(1000, 0xf000000f);
|
||||||
|
EXPECT_EQ(33506781356485509ULL, Freq.getFrequency());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(BlockFrequencyTest, ProbabilityCompare) {
|
TEST(BlockFrequencyTest, ProbabilityCompare) {
|
||||||
|
Reference in New Issue
Block a user