mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-30 12:49:19 +00:00
AK: Add lowest common multiple and greatest common divisor functions
This commit is contained in:
parent
16e83f0b35
commit
31dea89fe0
Notes:
github-actions[bot]
2025-04-23 08:39:50 +00:00
Author: https://github.com/tcl3
Commit: 31dea89fe0
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4315
Reviewed-by: https://github.com/AtkinsSJ
Reviewed-by: https://github.com/shannonbooth
2 changed files with 90 additions and 0 deletions
|
@ -85,4 +85,61 @@ constexpr T reinterpret_as_octal(T decimal)
|
|||
return result;
|
||||
}
|
||||
|
||||
template<Unsigned T>
|
||||
constexpr T gcd(T x, T y)
|
||||
{
|
||||
if (x == 0)
|
||||
return y;
|
||||
if (y == 0)
|
||||
return x;
|
||||
|
||||
int shift = 0;
|
||||
while (((x | y) & 1) == 0) {
|
||||
x >>= 1;
|
||||
y >>= 1;
|
||||
shift++;
|
||||
}
|
||||
|
||||
while (x != y) {
|
||||
if (x & 1) {
|
||||
if (y & 1) {
|
||||
if (x > y)
|
||||
x -= y;
|
||||
else
|
||||
y -= x;
|
||||
} else {
|
||||
y >>= 1;
|
||||
}
|
||||
} else {
|
||||
x >>= 1;
|
||||
if (y & 1) {
|
||||
if (x < y)
|
||||
swap(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return x << shift;
|
||||
}
|
||||
|
||||
template<Signed T>
|
||||
constexpr T gcd(T x, T y)
|
||||
{
|
||||
return gcd(static_cast<MakeUnsigned<T>>(abs(x)), static_cast<MakeUnsigned<T>>(abs(y)));
|
||||
}
|
||||
|
||||
template<Unsigned T>
|
||||
constexpr T lcm(T x, T y)
|
||||
{
|
||||
if (x == 0 || y == 0)
|
||||
return 0;
|
||||
return x / gcd(x, y) * y;
|
||||
}
|
||||
|
||||
template<Signed T>
|
||||
constexpr T lcm(T x, T y)
|
||||
{
|
||||
return lcm(static_cast<MakeUnsigned<T>>(abs(x)), static_cast<MakeUnsigned<T>>(abs(y)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue