Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
gcd.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_GCD_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_GCD_HPP_
7
8// IWYU pragma: private, include "arene/base/math.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11#include "arene/base/constraints/constraints.hpp"
12#include "arene/base/math/abs.hpp"
13#include "arene/base/stdlib_choice/enable_if.hpp"
14#include "arene/base/stdlib_choice/is_integral.hpp"
15
16namespace arene {
17namespace base {
18
19/// @brief Calculates the Greatest Common Divisor (GCD) of two Integral values.
20/// @tparam Integral The type of the values
21/// @param lhs First value
22/// @param rhs Second value
23/// @return Integral value that is GCD of @c lhs and @c rhs . If either @c lhs or @c rhs is @c 0 , then the absolute
24/// value of the non-zero operand is returned. If both are @c 0 , then returns @c 0 .
25template <typename Integral, constraints<std::enable_if_t<std::is_integral<Integral>::value> > = nullptr>
26constexpr auto gcd(Integral lhs, Integral rhs) noexcept -> Integral {
27 while (rhs != Integral{}) {
29 lhs = rhs;
30 rhs = tmp;
31 }
32 return abs(lhs);
33}
34
35} // namespace base
36} // namespace arene
37
38#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_GCD_HPP_
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10