gcd_lcm.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___NUMERIC_GCD_LCM_H
  10. #define _LIBCPP___NUMERIC_GCD_LCM_H
  11. #include <__assert>
  12. #include <__config>
  13. #include <limits>
  14. #include <type_traits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_PUSH_MACROS
  19. #include <__undef_macros>
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. #if _LIBCPP_STD_VER > 14
  22. template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __ct_abs;
  23. template <typename _Result, typename _Source>
  24. struct __ct_abs<_Result, _Source, true> {
  25. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  26. _Result operator()(_Source __t) const noexcept
  27. {
  28. if (__t >= 0) return __t;
  29. if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
  30. return -__t;
  31. }
  32. };
  33. template <typename _Result, typename _Source>
  34. struct __ct_abs<_Result, _Source, false> {
  35. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  36. _Result operator()(_Source __t) const noexcept { return __t; }
  37. };
  38. template<class _Tp>
  39. _LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
  40. _Tp __gcd(_Tp __m, _Tp __n)
  41. {
  42. static_assert((!is_signed<_Tp>::value), "");
  43. return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
  44. }
  45. template<class _Tp, class _Up>
  46. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  47. common_type_t<_Tp,_Up>
  48. gcd(_Tp __m, _Up __n)
  49. {
  50. static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
  51. static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to gcd cannot be bool" );
  52. static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to gcd cannot be bool" );
  53. using _Rp = common_type_t<_Tp,_Up>;
  54. using _Wp = make_unsigned_t<_Rp>;
  55. return static_cast<_Rp>(_VSTD::__gcd(
  56. static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)),
  57. static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n))));
  58. }
  59. template<class _Tp, class _Up>
  60. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  61. common_type_t<_Tp,_Up>
  62. lcm(_Tp __m, _Up __n)
  63. {
  64. static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
  65. static_assert((!is_same<typename remove_cv<_Tp>::type, bool>::value), "First argument to lcm cannot be bool" );
  66. static_assert((!is_same<typename remove_cv<_Up>::type, bool>::value), "Second argument to lcm cannot be bool" );
  67. if (__m == 0 || __n == 0)
  68. return 0;
  69. using _Rp = common_type_t<_Tp,_Up>;
  70. _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
  71. _Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
  72. _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
  73. return __val1 * __val2;
  74. }
  75. #endif // _LIBCPP_STD_VER
  76. _LIBCPP_END_NAMESPACE_STD
  77. _LIBCPP_POP_MACROS
  78. #endif // _LIBCPP___NUMERIC_GCD_LCM_H