L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
numeric
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * Copyright (C) 2025 Kernkonzept GmbH.
4 * Author(s): Martin Decky <martin.decky@kernkonzept.com>
5 *
6 * License: see LICENSE.spdx (in this directory or the directories above)
7 */
8
9#pragma once
10
11#include <l4/cxx/type_traits>
12
13namespace cxx {
14
33template <typename T>
34constexpr T gcd(T a, T b)
35{
36 static_assert(Type_traits<T>::is_unsigned, "Type must be unsigned");
37
38 while (b != 0)
39 {
40 T remainder = a % b;
41 a = b;
42 b = remainder;
43 }
44
45 return a;
46}
47
67template <typename T>
68constexpr T lcm(T a, T b)
69{
70 static_assert(Type_traits<T>::is_unsigned, "Type must be unsigned");
71
72 if (a == 0 || b == 0)
73 return 0;
74
75 return (a / gcd(a, b)) * b;
76}
77
78}
Our C++ library.
Definition arith:11
constexpr T gcd(T a, T b)
Compute the greatest common divisor of two unsigned values.
Definition numeric:34
constexpr T lcm(T a, T b)
Compute the least common multiple of two unsigned values.
Definition numeric:68