40#include <gtest/gtest.h>
92 EXPECT_DEATH_IF_SUPPORTED(
floorLog2(0),
"x > 0");
105 EXPECT_EQ(42,
ceilLog2(2821109907456));
112 EXPECT_EQ(90,
divCeil(7922, 89));
113 EXPECT_EQ(4,
divCeil(4800, 1442));
115 EXPECT_EQ(46,
divCeil(451, 10));
118TEST(IntmathTest, mulUnsignedNarrow)
126 EXPECT_EQ(low, 0xfe);
132 EXPECT_EQ(low, 0x7e);
141TEST(IntmathTest, mulSignedNarrow)
149 EXPECT_EQ(low, -0x80);
154 EXPECT_EQ(hi, -0x01);
155 EXPECT_EQ(low, -0x7e);
164TEST(IntmathTest, mulUnsignedMid)
166 uint32_t
a = 0xffffffffULL;
167 uint32_t
b = 0x00000002ULL;
172 EXPECT_EQ(low, 0xfffffffe);
178 EXPECT_EQ(low, 0x99c16a40);
189 int32_t
a = -0x80000000;
190 int32_t
b = -0x7fffffff;
194 EXPECT_EQ(hi, 0x3fffffff);
195 EXPECT_EQ(low, -0x80000000);
200 EXPECT_EQ(hi, -0x52);
201 EXPECT_EQ(low, -0x99c16a40);
210TEST(IntmathTest, mulUnsignedWide)
212 uint64_t
a = 0xffffffffffffffffULL;
213 uint64_t
b = 0x0000000000000002ULL;
218 EXPECT_EQ(low, 0xfffffffffffffffe);
224 EXPECT_EQ(low, 0xfffffffffffffffe);
230 EXPECT_EQ(low, 0xfffffffffffffffe);
233 b = 0x5555555555555555;
239TEST(IntmathTest, mulSignedWide)
241 int64_t
a = -0x8000000000000000;
242 int64_t
b = -0x7fffffffffffffff;
246 EXPECT_EQ(hi, 0x3fffffffffffffff);
247 EXPECT_EQ(low, -0x8000000000000000);
252 EXPECT_EQ(hi, 0x3fffffffffffffff);
253 EXPECT_EQ(low, -0x8000000000000000);
258 EXPECT_EQ(hi, 0x3fffffffffffffff);
259 EXPECT_EQ(low, -0x8000000000000000);
262 b = -0x5555555555555555;
270 EXPECT_EQ(4104,
roundUp(4101, 4));
271 EXPECT_EQ(4112,
roundUp(4105, 8));
272 EXPECT_EQ(4112,
roundUp(4101, 16));
273 EXPECT_EQ(8192,
roundUp(7991, 256));
292 for (
int value = 1; value > 0;
expected++, value <<= 1) {
297 EXPECT_EQ(
expected,
sizeof(
int) * 8 - 1);
307TEST(IntmathDeathTest, Log2iDeath)
311 GTEST_SKIP() <<
"Skipping as assertions are "
312 "stripped out of fast builds";
319 },
"value > 0.*failed");
323 const int value = -1;
325 },
"value > 0.*failed");
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })
static constexpr int log2i(int value)
Calculate the log2 of a power of 2 integer.
static constexpr std::enable_if_t< std::is_integral_v< T >, int > floorLog2(T x)
static constexpr int ceilLog2(const T &n)
static constexpr std::enable_if_t< sizeof(T)<=sizeof(uint32_t)> mulUnsigned(std::make_unsigned_t< T > &high, std::make_unsigned_t< T > &low, std::make_unsigned_t< T > val_a, std::make_unsigned_t< T > val_b)
static constexpr bool isPowerOf2(const T &n)
static constexpr T divCeil(const T &a, const U &b)
static constexpr std::enable_if_t< sizeof(T)<=sizeof(uint32_t)> mulSigned(std::make_signed_t< T > &high, std::make_signed_t< T > &low, std::make_signed_t< T > val_a, std::make_signed_t< T > val_b)
static constexpr T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
static constexpr T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
TEST(IntmathTest, isPowerOf2)
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
static constexpr std::enable_if_t< sizeof(T)==sizeof(uint64_t)> mulSignedManual(std::make_signed_t< T > &high, std::make_signed_t< T > &low, std::make_signed_t< T > val_a, std::make_signed_t< T > val_b)
static constexpr std::enable_if_t< sizeof(T)==sizeof(uint64_t)> mulUnsignedManual(std::make_unsigned_t< T > &high, std::make_unsigned_t< T > &low, std::make_unsigned_t< T > val_a, std::make_unsigned_t< T > val_b)
Multiply two values with place value p.