// Copyright 2014 Dolphin Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #include #include "Common/MathUtil.h" TEST(MathUtil, IntLog2) { EXPECT_EQ(0, MathUtil::IntLog2(1)); EXPECT_EQ(1, MathUtil::IntLog2(2)); EXPECT_EQ(2, MathUtil::IntLog2(4)); EXPECT_EQ(3, MathUtil::IntLog2(8)); EXPECT_EQ(63, MathUtil::IntLog2(0x8000000000000000ull)); // Rounding behavior. EXPECT_EQ(3, MathUtil::IntLog2(15)); EXPECT_EQ(63, MathUtil::IntLog2(0xFFFFFFFFFFFFFFFFull)); } TEST(MathUtil, NextPowerOf2) { EXPECT_EQ(4U, MathUtil::NextPowerOf2(3)); EXPECT_EQ(4U, MathUtil::NextPowerOf2(4)); EXPECT_EQ(8U, MathUtil::NextPowerOf2(6)); EXPECT_EQ(0x40000000U, MathUtil::NextPowerOf2(0x23456789)); } TEST(MathUtil, SaturatingCast) { // Cast from an integer type to a smaller type EXPECT_EQ(255u, (MathUtil::SaturatingCast(1000))); EXPECT_EQ(255u, (MathUtil::SaturatingCast(1000u))); EXPECT_EQ(255u, (MathUtil::SaturatingCast(1000))); // Cast from a signed integer type EXPECT_EQ(0u, (MathUtil::SaturatingCast(-1))); EXPECT_EQ(0u, (MathUtil::SaturatingCast(-1000))); EXPECT_EQ(0u, (MathUtil::SaturatingCast(-1))); EXPECT_EQ(-1000, (MathUtil::SaturatingCast(-1000))); EXPECT_EQ(-1000, (MathUtil::SaturatingCast(-1000))); EXPECT_EQ(-1000, (MathUtil::SaturatingCast(-1000))); // Cast from an unsigned integer type to a smaller integer type EXPECT_EQ(0x7fff, (MathUtil::SaturatingCast(0xffffffffu))); EXPECT_EQ(0x7fffffff, (MathUtil::SaturatingCast(0xffffffffu))); // Cast from a floating point type to an integer type EXPECT_EQ(255u, MathUtil::SaturatingCast(1234.0)); EXPECT_EQ(0u, MathUtil::SaturatingCast(-1234.0)); EXPECT_EQ(127, MathUtil::SaturatingCast(5678.0)); EXPECT_EQ(-128, MathUtil::SaturatingCast(-5678.0)); EXPECT_EQ(65535u, MathUtil::SaturatingCast(999999.0)); // Negative zero EXPECT_EQ(0u, MathUtil::SaturatingCast(0.0)); EXPECT_EQ(0u, MathUtil::SaturatingCast(-0.0)); EXPECT_EQ(0, MathUtil::SaturatingCast(0.0)); EXPECT_EQ(0, MathUtil::SaturatingCast(-0.0)); // Edge cases EXPECT_EQ(std::numeric_limits::max(), MathUtil::SaturatingCast(std::numeric_limits::infinity())); EXPECT_EQ(std::numeric_limits::min(), MathUtil::SaturatingCast(-std::numeric_limits::infinity())); // 16777217 = 2^24 + 1 is the first integer that cannot be represented correctly with a f32. EXPECT_EQ(16777216, MathUtil::SaturatingCast(float(16777216))); EXPECT_EQ(16777216, MathUtil::SaturatingCast(float(16777217))); }