xref: /llvm-project/libcxx/include/__numeric/gcd_lcm.h (revision f7ff3cde96c4b81b032c58cafee7bf77233f5517)
1a0efb175SChristopher Di Bella // -*- C++ -*-
2a0efb175SChristopher Di Bella //===----------------------------------------------------------------------===//
3a0efb175SChristopher Di Bella //
4a0efb175SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5a0efb175SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
6a0efb175SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7a0efb175SChristopher Di Bella //
8a0efb175SChristopher Di Bella //===----------------------------------------------------------------------===//
9a0efb175SChristopher Di Bella 
10a0efb175SChristopher Di Bella #ifndef _LIBCPP___NUMERIC_GCD_LCM_H
11a0efb175SChristopher Di Bella #define _LIBCPP___NUMERIC_GCD_LCM_H
12a0efb175SChristopher Di Bella 
1327a062e9Sserge-sans-paille #include <__algorithm/min.h>
14f87aa19bSLouis Dionne #include <__assert>
1527a062e9Sserge-sans-paille #include <__bit/countr.h>
16a0efb175SChristopher Di Bella #include <__config>
17fafed06bSNikolas Klauser #include <__type_traits/common_type.h>
18fafed06bSNikolas Klauser #include <__type_traits/is_integral.h>
19fafed06bSNikolas Klauser #include <__type_traits/is_same.h>
20fafed06bSNikolas Klauser #include <__type_traits/is_signed.h>
21fafed06bSNikolas Klauser #include <__type_traits/make_unsigned.h>
22a0efb175SChristopher Di Bella #include <limits>
23a0efb175SChristopher Di Bella 
24a0efb175SChristopher Di Bella #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25a0efb175SChristopher Di Bella #  pragma GCC system_header
26a0efb175SChristopher Di Bella #endif
27a0efb175SChristopher Di Bella 
28a0efb175SChristopher Di Bella _LIBCPP_PUSH_MACROS
29a0efb175SChristopher Di Bella #include <__undef_macros>
30a0efb175SChristopher Di Bella 
31a0efb175SChristopher Di Bella _LIBCPP_BEGIN_NAMESPACE_STD
32a0efb175SChristopher Di Bella 
334f15267dSNikolas Klauser #if _LIBCPP_STD_VER >= 17
34a0efb175SChristopher Di Bella 
359783f28cSLouis Dionne template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value>
369783f28cSLouis Dionne struct __ct_abs;
37a0efb175SChristopher Di Bella 
38a0efb175SChristopher Di Bella template <typename _Result, typename _Source>
39a0efb175SChristopher Di Bella struct __ct_abs<_Result, _Source, true> {
401f98ac09SNikolas Klauser   constexpr _LIBCPP_HIDE_FROM_ABI _Result operator()(_Source __t) const noexcept {
419783f28cSLouis Dionne     if (__t >= 0)
429783f28cSLouis Dionne       return __t;
439783f28cSLouis Dionne     if (__t == numeric_limits<_Source>::min())
449783f28cSLouis Dionne       return -static_cast<_Result>(__t);
45a0efb175SChristopher Di Bella     return -__t;
46a0efb175SChristopher Di Bella   }
47a0efb175SChristopher Di Bella };
48a0efb175SChristopher Di Bella 
49a0efb175SChristopher Di Bella template <typename _Result, typename _Source>
50a0efb175SChristopher Di Bella struct __ct_abs<_Result, _Source, false> {
511f98ac09SNikolas Klauser   constexpr _LIBCPP_HIDE_FROM_ABI _Result operator()(_Source __t) const noexcept { return __t; }
52a0efb175SChristopher Di Bella };
53a0efb175SChristopher Di Bella 
54a0efb175SChristopher Di Bella template <class _Tp>
551f98ac09SNikolas Klauser constexpr _LIBCPP_HIDDEN _Tp __gcd(_Tp __a, _Tp __b) {
566b4b29f8SNikolas Klauser   static_assert(!is_signed<_Tp>::value, "");
5727a062e9Sserge-sans-paille 
58*f7ff3cdeSserge-sans-paille   // Using Binary GCD algorithm https://en.wikipedia.org/wiki/Binary_GCD_algorithm, based on an implementation
59*f7ff3cdeSserge-sans-paille   // from https://lemire.me/blog/2024/04/13/greatest-common-divisor-the-extended-euclidean-algorithm-and-speed/
6027a062e9Sserge-sans-paille   //
6127a062e9Sserge-sans-paille   // If power of two divides both numbers, we can push it out.
6227a062e9Sserge-sans-paille   // - gcd( 2^x * a, 2^x * b) = 2^x * gcd(a, b)
6327a062e9Sserge-sans-paille   //
6427a062e9Sserge-sans-paille   // If and only if exactly one number is even, we can divide that number by that power.
6527a062e9Sserge-sans-paille   // - if a, b are odd, then gcd(2^x * a, b) = gcd(a, b)
6627a062e9Sserge-sans-paille   //
6727a062e9Sserge-sans-paille   // And standard gcd algorithm where instead of modulo, minus is used.
6827a062e9Sserge-sans-paille 
6927a062e9Sserge-sans-paille   if (__a < __b) {
7027a062e9Sserge-sans-paille     _Tp __tmp = __b;
7127a062e9Sserge-sans-paille     __b       = __a;
7227a062e9Sserge-sans-paille     __a       = __tmp;
7327a062e9Sserge-sans-paille   }
7427a062e9Sserge-sans-paille   if (__b == 0)
7527a062e9Sserge-sans-paille     return __a;
7627a062e9Sserge-sans-paille   __a %= __b; // Make both argument of the same size, and early result in the easy case.
7727a062e9Sserge-sans-paille   if (__a == 0)
7827a062e9Sserge-sans-paille     return __b;
7927a062e9Sserge-sans-paille 
80*f7ff3cdeSserge-sans-paille   _Tp __c     = __a | __b;
81*f7ff3cdeSserge-sans-paille   int __shift = std::__countr_zero(__c);
82*f7ff3cdeSserge-sans-paille   __a >>= std::__countr_zero(__a);
8327a062e9Sserge-sans-paille   do {
84*f7ff3cdeSserge-sans-paille     _Tp __t = __b >> std::__countr_zero(__b);
85*f7ff3cdeSserge-sans-paille     if (__a > __t) {
86*f7ff3cdeSserge-sans-paille       __b = __a - __t;
87*f7ff3cdeSserge-sans-paille       __a = __t;
8827a062e9Sserge-sans-paille     } else {
89*f7ff3cdeSserge-sans-paille       __b = __t - __a;
9027a062e9Sserge-sans-paille     }
9127a062e9Sserge-sans-paille   } while (__b != 0);
9227a062e9Sserge-sans-paille   return __a << __shift;
93a0efb175SChristopher Di Bella }
94a0efb175SChristopher Di Bella 
95a0efb175SChristopher Di Bella template <class _Tp, class _Up>
961f98ac09SNikolas Klauser constexpr _LIBCPP_HIDE_FROM_ABI common_type_t<_Tp, _Up> gcd(_Tp __m, _Up __n) {
976b4b29f8SNikolas Klauser   static_assert(is_integral<_Tp>::value && is_integral<_Up>::value, "Arguments to gcd must be integer types");
986b4b29f8SNikolas Klauser   static_assert(!is_same<__remove_cv_t<_Tp>, bool>::value, "First argument to gcd cannot be bool");
996b4b29f8SNikolas Klauser   static_assert(!is_same<__remove_cv_t<_Up>, bool>::value, "Second argument to gcd cannot be bool");
100a0efb175SChristopher Di Bella   using _Rp = common_type_t<_Tp, _Up>;
101a0efb175SChristopher Di Bella   using _Wp = make_unsigned_t<_Rp>;
1029783f28cSLouis Dionne   return static_cast<_Rp>(
1039783f28cSLouis Dionne       std::__gcd(static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)), static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n))));
104a0efb175SChristopher Di Bella }
105a0efb175SChristopher Di Bella 
106a0efb175SChristopher Di Bella template <class _Tp, class _Up>
1071f98ac09SNikolas Klauser constexpr _LIBCPP_HIDE_FROM_ABI common_type_t<_Tp, _Up> lcm(_Tp __m, _Up __n) {
1086b4b29f8SNikolas Klauser   static_assert(is_integral<_Tp>::value && is_integral<_Up>::value, "Arguments to lcm must be integer types");
1096b4b29f8SNikolas Klauser   static_assert(!is_same<__remove_cv_t<_Tp>, bool>::value, "First argument to lcm cannot be bool");
1106b4b29f8SNikolas Klauser   static_assert(!is_same<__remove_cv_t<_Up>, bool>::value, "Second argument to lcm cannot be bool");
111a0efb175SChristopher Di Bella   if (__m == 0 || __n == 0)
112a0efb175SChristopher Di Bella     return 0;
113a0efb175SChristopher Di Bella 
114a0efb175SChristopher Di Bella   using _Rp  = common_type_t<_Tp, _Up>;
11577a00c0dSLouis Dionne   _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / std::gcd(__m, __n);
116a0efb175SChristopher Di Bella   _Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
117fd62906dSLouis Dionne   _Rp __res;
118fd62906dSLouis Dionne   [[maybe_unused]] bool __overflow = __builtin_mul_overflow(__val1, __val2, &__res);
119fd62906dSLouis Dionne   _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(!__overflow, "Overflow in lcm");
120fd62906dSLouis Dionne   return __res;
121a0efb175SChristopher Di Bella }
122a0efb175SChristopher Di Bella 
123fd62906dSLouis Dionne #endif // _LIBCPP_STD_VER >= 17
124a0efb175SChristopher Di Bella 
125a0efb175SChristopher Di Bella _LIBCPP_END_NAMESPACE_STD
126a0efb175SChristopher Di Bella 
127a0efb175SChristopher Di Bella _LIBCPP_POP_MACROS
128a0efb175SChristopher Di Bella 
129a0efb175SChristopher Di Bella #endif // _LIBCPP___NUMERIC_GCD_LCM_H
130