Loading...
Searching...
No Matches
numeric
// vi:set ft=cpp: -*- Mode: C++ -*-
/*
* Copyright (C) 2025 Kernkonzept GmbH.
* Author(s): Martin Decky <martin.decky@kernkonzept.com>
*
* License: see LICENSE.spdx (in this directory or the directories above)
*/
#pragma once
#include <l4/cxx/type_traits>
namespace cxx {
template <typename T>
constexpr T gcd(T a, T b)
{
static_assert(Type_traits<T>::is_unsigned, "Type must be unsigned");
while (b != 0)
{
T remainder = a % b;
a = b;
b = remainder;
}
return a;
}
template <typename T>
constexpr T lcm(T a, T b)
{
static_assert(Type_traits<T>::is_unsigned, "Type must be unsigned");
if (a == 0 || b == 0)
return 0;
return (a / gcd(a, b)) * b;
}
}
cxx
Our C++ library.
Definition arith:11
cxx::gcd
constexpr T gcd(T a, T b)
Compute the greatest common divisor of two unsigned values.
Definition numeric:34
cxx::lcm
constexpr T lcm(T a, T b)
Compute the least common multiple of two unsigned values.
Definition numeric:68