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)
124 mulUnsigned<uint8_t>(hi, low,
a,
b);
126 EXPECT_EQ(low, 0xfe);
130 mulUnsigned<uint8_t>(hi, low,
a,
b);
132 EXPECT_EQ(low, 0x7e);
136 mulUnsigned<uint8_t>(hi, low,
a,
b);
141TEST(IntmathTest, mulSignedNarrow)
147 mulSigned<int8_t>(hi, low,
a,
b);
149 EXPECT_EQ(low, -0x80);
153 mulSigned<int8_t>(hi, low,
a,
b);
154 EXPECT_EQ(hi, -0x01);
155 EXPECT_EQ(low, -0x7e);
159 mulSigned<int8_t>(hi, low,
a,
b);
164TEST(IntmathTest, mulUnsignedMid)
166 uint32_t
a = 0xffffffffULL;
167 uint32_t
b = 0x00000002ULL;
170 mulUnsigned<uint32_t>(hi, low,
a,
b);
172 EXPECT_EQ(low, 0xfffffffe);
176 mulUnsigned<uint32_t>(hi, low,
a,
b);
178 EXPECT_EQ(low, 0x99c16a40);
182 mulUnsigned<uint32_t>(hi, low,
a,
b);
189 int32_t
a = -0x80000000;
190 int32_t
b = -0x7fffffff;
193 mulSigned<int32_t>(hi, low,
a,
b);
194 EXPECT_EQ(hi, 0x3fffffff);
195 EXPECT_EQ(low, -0x80000000);
199 mulSigned<int32_t>(hi, low,
a,
b);
200 EXPECT_EQ(hi, -0x52);
201 EXPECT_EQ(low, -0x99c16a40);
205 mulSigned<int32_t>(hi, low,
a,
b);
210TEST(IntmathTest, mulUnsignedWide)
212 uint64_t
a = 0xffffffffffffffffULL;
213 uint64_t
b = 0x0000000000000002ULL;
216 mulUnsigned<uint64_t>(hi, low,
a,
b);
218 EXPECT_EQ(low, 0xfffffffffffffffe);
222 mulUnsignedManual<uint64_t>(hi, low,
a,
b);
224 EXPECT_EQ(low, 0xfffffffffffffffe);
228 std::tie(hi, low) = mulUnsigned<uint64_t>(
a,
b);
230 EXPECT_EQ(low, 0xfffffffffffffffe);
233 b = 0x5555555555555555;
234 mulUnsigned<uint64_t>(hi, low,
a,
b);
239TEST(IntmathTest, mulSignedWide)
241 int64_t
a = -0x8000000000000000;
242 int64_t
b = -0x7fffffffffffffff;
245 mulSigned<int64_t>(hi, low,
a,
b);
246 EXPECT_EQ(hi, 0x3fffffffffffffff);
247 EXPECT_EQ(low, -0x8000000000000000);
251 mulSignedManual<int64_t>(hi, low,
a,
b);
252 EXPECT_EQ(hi, 0x3fffffffffffffff);
253 EXPECT_EQ(low, -0x8000000000000000);
257 std::tie(hi, low) = mulSigned<int64_t>(
a,
b);
258 EXPECT_EQ(hi, 0x3fffffffffffffff);
259 EXPECT_EQ(low, -0x8000000000000000);
262 b = -0x5555555555555555;
263 mulSigned<int64_t>(hi, low,
a,
b);
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 bool isPowerOf2(const T &n)
static constexpr T divCeil(const T &a, const U &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 Arm Limited All rights reserved.