mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-25 04:02:41 +01:00
0a576bc0e3
Multiplication followed by addition (https://en.wikipedia.org/wiki/Multiply–accumulate_operation) is a sufficiently common use-case to warrant a separate helper. Differential Revision: https://reviews.llvm.org/D48138 llvm-svn: 334635
85 lines
3.3 KiB
C++
85 lines
3.3 KiB
C++
#include "llvm/Support/CheckedArithmetic.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
|
|
TEST(CheckedArithmetic, CheckedAdd) {
|
|
const int64_t Max = std::numeric_limits<int64_t>::max();
|
|
const int64_t Min = std::numeric_limits<int64_t>::min();
|
|
EXPECT_EQ(checkedAdd<int64_t>(Max, Max), None);
|
|
EXPECT_EQ(checkedAdd<int64_t>(Min, -1), None);
|
|
EXPECT_EQ(checkedAdd<int64_t>(Max, 1), None);
|
|
EXPECT_EQ(checkedAdd<int64_t>(10, 1), Optional<int64_t>(11));
|
|
}
|
|
|
|
TEST(CheckedArithmetic, CheckedAddSmall) {
|
|
const int16_t Max = std::numeric_limits<int16_t>::max();
|
|
const int16_t Min = std::numeric_limits<int16_t>::min();
|
|
EXPECT_EQ(checkedAdd<int16_t>(Max, Max), None);
|
|
EXPECT_EQ(checkedAdd<int16_t>(Min, -1), None);
|
|
EXPECT_EQ(checkedAdd<int16_t>(Max, 1), None);
|
|
EXPECT_EQ(checkedAdd<int16_t>(10, 1), Optional<int64_t>(11));
|
|
}
|
|
|
|
TEST(CheckedArithmetic, CheckedMul) {
|
|
const int64_t Max = std::numeric_limits<int64_t>::max();
|
|
const int64_t Min = std::numeric_limits<int64_t>::min();
|
|
EXPECT_EQ(checkedMul<int64_t>(Max, 2), None);
|
|
EXPECT_EQ(checkedMul<int64_t>(Max, Max), None);
|
|
EXPECT_EQ(checkedMul<int64_t>(Min, 2), None);
|
|
EXPECT_EQ(checkedMul<int64_t>(10, 2), Optional<int64_t>(20));
|
|
}
|
|
|
|
TEST(CheckedArithmetic, CheckedMulAdd) {
|
|
const int64_t Max = std::numeric_limits<int64_t>::max();
|
|
const int64_t Min = std::numeric_limits<int64_t>::min();
|
|
EXPECT_EQ(checkedMulAdd<int64_t>(Max, 1, 2), None);
|
|
EXPECT_EQ(checkedMulAdd<int64_t>(1, 1, Max), None);
|
|
EXPECT_EQ(checkedMulAdd<int64_t>(1, -1, Min), None);
|
|
EXPECT_EQ(checkedMulAdd<int64_t>(10, 2, 3), Optional<int64_t>(23));
|
|
}
|
|
|
|
TEST(CheckedArithmetic, CheckedMulSmall) {
|
|
const int16_t Max = std::numeric_limits<int16_t>::max();
|
|
const int16_t Min = std::numeric_limits<int16_t>::min();
|
|
EXPECT_EQ(checkedMul<int16_t>(Max, 2), None);
|
|
EXPECT_EQ(checkedMul<int16_t>(Max, Max), None);
|
|
EXPECT_EQ(checkedMul<int16_t>(Min, 2), None);
|
|
EXPECT_EQ(checkedMul<int16_t>(10, 2), Optional<int16_t>(20));
|
|
}
|
|
|
|
TEST(CheckedArithmetic, CheckedMulAddSmall) {
|
|
const int16_t Max = std::numeric_limits<int16_t>::max();
|
|
const int16_t Min = std::numeric_limits<int16_t>::min();
|
|
EXPECT_EQ(checkedMulAdd<int16_t>(Max, 1, 2), None);
|
|
EXPECT_EQ(checkedMulAdd<int16_t>(1, 1, Max), None);
|
|
EXPECT_EQ(checkedMulAdd<int16_t>(1, -1, Min), None);
|
|
EXPECT_EQ(checkedMulAdd<int16_t>(10, 2, 3), Optional<int16_t>(23));
|
|
}
|
|
|
|
TEST(CheckedArithmetic, CheckedAddUnsigned) {
|
|
const uint64_t Max = std::numeric_limits<uint64_t>::max();
|
|
EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, Max), None);
|
|
EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, 1), None);
|
|
EXPECT_EQ(checkedAddUnsigned<uint64_t>(10, 1), Optional<uint64_t>(11));
|
|
}
|
|
|
|
TEST(CheckedArithmetic, CheckedMulUnsigned) {
|
|
const uint64_t Max = std::numeric_limits<uint64_t>::max();
|
|
EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, 2), llvm::None);
|
|
EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, Max), llvm::None);
|
|
EXPECT_EQ(checkedMulUnsigned<uint64_t>(10, 2), Optional<uint64_t>(20));
|
|
}
|
|
|
|
TEST(CheckedArithmetic, CheckedMulAddUnsigned) {
|
|
const uint64_t Max = std::numeric_limits<uint64_t>::max();
|
|
EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(Max, 1, 2), None);
|
|
EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(1, 1, Max), None);
|
|
EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(10, 2, 3), Optional<uint64_t>(23));
|
|
}
|
|
|
|
|
|
} // namespace
|