mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-25 12:12:47 +01:00
ae65e281f3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
128 lines
3.5 KiB
C++
128 lines
3.5 KiB
C++
//===- unittests/Support/BlockFrequencyTest.cpp - BlockFrequency tests ----===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
|
#include "llvm/Support/BranchProbability.h"
|
|
#include "llvm/Support/DataTypes.h"
|
|
#include "gtest/gtest.h"
|
|
#include <climits>
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
|
|
TEST(BlockFrequencyTest, OneToZero) {
|
|
BlockFrequency Freq(1);
|
|
BranchProbability Prob(UINT32_MAX / 3, UINT32_MAX);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), 0u);
|
|
|
|
Freq = BlockFrequency(1);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), 0u);
|
|
}
|
|
|
|
TEST(BlockFrequencyTest, OneToOne) {
|
|
BlockFrequency Freq(1);
|
|
BranchProbability Prob(UINT32_MAX, UINT32_MAX);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
|
|
|
Freq = BlockFrequency(1);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
|
}
|
|
|
|
TEST(BlockFrequencyTest, ThreeToOne) {
|
|
BlockFrequency Freq(3);
|
|
BranchProbability Prob(3000000, 9000000);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
|
|
|
Freq = BlockFrequency(3);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), 1u);
|
|
}
|
|
|
|
TEST(BlockFrequencyTest, MaxToHalfMax) {
|
|
BlockFrequency Freq(UINT64_MAX);
|
|
BranchProbability Prob(UINT32_MAX / 2, UINT32_MAX);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), 9223372036854775807ULL);
|
|
|
|
Freq = BlockFrequency(UINT64_MAX);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), 9223372036854775807ULL);
|
|
}
|
|
|
|
TEST(BlockFrequencyTest, BigToBig) {
|
|
const uint64_t Big = 387246523487234346LL;
|
|
const uint32_t P = 123456789;
|
|
BlockFrequency Freq(Big);
|
|
BranchProbability Prob(P, P);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), Big);
|
|
|
|
Freq = BlockFrequency(Big);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), Big);
|
|
}
|
|
|
|
TEST(BlockFrequencyTest, MaxToMax) {
|
|
BlockFrequency Freq(UINT64_MAX);
|
|
BranchProbability Prob(UINT32_MAX, UINT32_MAX);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
|
|
|
// This additionally makes sure if we have a value equal to our saturating
|
|
// value, we do not signal saturation if the result equals said value, but
|
|
// saturating does not occur.
|
|
Freq = BlockFrequency(UINT64_MAX);
|
|
Freq *= Prob;
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
|
}
|
|
|
|
TEST(BlockFrequencyTest, Subtract) {
|
|
BlockFrequency Freq1(0), Freq2(1);
|
|
EXPECT_EQ((Freq1 - Freq2).getFrequency(), 0u);
|
|
EXPECT_EQ((Freq2 - Freq1).getFrequency(), 1u);
|
|
}
|
|
|
|
TEST(BlockFrequency, Divide) {
|
|
BlockFrequency Freq(0x3333333333333333ULL);
|
|
Freq /= BranchProbability(1, 2);
|
|
EXPECT_EQ(Freq.getFrequency(), 0x6666666666666666ULL);
|
|
}
|
|
|
|
TEST(BlockFrequencyTest, Saturate) {
|
|
BlockFrequency Freq(0x3333333333333333ULL);
|
|
Freq /= BranchProbability(100, 300);
|
|
EXPECT_EQ(Freq.getFrequency(), 0x9999999866666668ULL);
|
|
Freq /= BranchProbability(1, 2);
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
|
|
|
Freq = 0x1000000000000000ULL;
|
|
Freq /= BranchProbability(10000, 170000);
|
|
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
|
|
|
// Try to cheat the multiplication overflow check.
|
|
Freq = 0x00000001f0000001ull;
|
|
Freq /= BranchProbability(1000, 0xf000000f);
|
|
EXPECT_EQ(33527736066704712ULL, Freq.getFrequency());
|
|
}
|
|
|
|
TEST(BlockFrequencyTest, SaturatingRightShift) {
|
|
BlockFrequency Freq(0x10080ULL);
|
|
Freq >>= 2;
|
|
EXPECT_EQ(Freq.getFrequency(), 0x4020ULL);
|
|
Freq >>= 20;
|
|
EXPECT_EQ(Freq.getFrequency(), 0x1ULL);
|
|
}
|
|
|
|
}
|