diff --git a/AK/IntegralMath.h b/AK/IntegralMath.h new file mode 100644 index 0000000000..03436832e1 --- /dev/null +++ b/AK/IntegralMath.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022, Leon Albrecht + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +#include + +namespace AK { + +template +constexpr I pow(I base, I exponent) +{ + // https://en.wikipedia.org/wiki/Exponentiation_by_squaring + if (exponent < 0) + return 0; + if (exponent == 0) + return 1; + + I res = 1; + while (exponent > 0) { + if (exponent & 1) + res *= base; + base *= base; + exponent /= 2u; + } + return res; +} + +} diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt index e1dc8eb076..eed34f479a 100644 --- a/Tests/AK/CMakeLists.txt +++ b/Tests/AK/CMakeLists.txt @@ -33,6 +33,7 @@ set(AK_TEST_SOURCES TestHex.cpp TestIPv4Address.cpp TestIndexSequence.cpp + TestIntegerMath.cpp TestIntrusiveList.cpp TestIntrusiveRedBlackTree.cpp TestJSON.cpp diff --git a/Tests/AK/TestIntegerMath.cpp b/Tests/AK/TestIntegerMath.cpp new file mode 100644 index 0000000000..c43c747c5d --- /dev/null +++ b/Tests/AK/TestIntegerMath.cpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include + +TEST_CASE(pow) +{ + EXPECT_EQ(AK::pow(10, 0), 1ull); + EXPECT_EQ(AK::pow(10, 1), 10ull); + EXPECT_EQ(AK::pow(10, 2), 100ull); + EXPECT_EQ(AK::pow(10, 3), 1'000ull); + EXPECT_EQ(AK::pow(10, 4), 10'000ull); + EXPECT_EQ(AK::pow(10, 5), 100'000ull); + EXPECT_EQ(AK::pow(10, 6), 1'000'000ull); +}