gcd_lcm.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <__type_traits/common_type.h>
  14. #include <__type_traits/is_integral.h>
  15. #include <__type_traits/is_same.h>
  16. #include <__type_traits/is_signed.h>
  17. #include <__type_traits/make_unsigned.h>
  18. #include <limits>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_PUSH_MACROS
  23. #include <__undef_macros>
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. #if _LIBCPP_STD_VER > 14
  26. template <typename _Result, typename _Source, bool _IsSigned = is_signed<_Source>::value> struct __ct_abs;
  27. template <typename _Result, typename _Source>
  28. struct __ct_abs<_Result, _Source, true> {
  29. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  30. _Result operator()(_Source __t) const noexcept
  31. {
  32. if (__t >= 0) return __t;
  33. if (__t == numeric_limits<_Source>::min()) return -static_cast<_Result>(__t);
  34. return -__t;
  35. }
  36. };
  37. template <typename _Result, typename _Source>
  38. struct __ct_abs<_Result, _Source, false> {
  39. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  40. _Result operator()(_Source __t) const noexcept { return __t; }
  41. };
  42. template<class _Tp>
  43. _LIBCPP_CONSTEXPR _LIBCPP_HIDDEN
  44. _Tp __gcd(_Tp __m, _Tp __n)
  45. {
  46. static_assert((!is_signed<_Tp>::value), "");
  47. return __n == 0 ? __m : _VSTD::__gcd<_Tp>(__n, __m % __n);
  48. }
  49. template<class _Tp, class _Up>
  50. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  51. common_type_t<_Tp,_Up>
  52. gcd(_Tp __m, _Up __n)
  53. {
  54. static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to gcd must be integer types");
  55. static_assert((!is_same<__remove_cv_t<_Tp>, bool>::value), "First argument to gcd cannot be bool" );
  56. static_assert((!is_same<__remove_cv_t<_Up>, bool>::value), "Second argument to gcd cannot be bool" );
  57. using _Rp = common_type_t<_Tp,_Up>;
  58. using _Wp = make_unsigned_t<_Rp>;
  59. return static_cast<_Rp>(_VSTD::__gcd(
  60. static_cast<_Wp>(__ct_abs<_Rp, _Tp>()(__m)),
  61. static_cast<_Wp>(__ct_abs<_Rp, _Up>()(__n))));
  62. }
  63. template<class _Tp, class _Up>
  64. _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
  65. common_type_t<_Tp,_Up>
  66. lcm(_Tp __m, _Up __n)
  67. {
  68. static_assert((is_integral<_Tp>::value && is_integral<_Up>::value), "Arguments to lcm must be integer types");
  69. static_assert((!is_same<__remove_cv_t<_Tp>, bool>::value), "First argument to lcm cannot be bool" );
  70. static_assert((!is_same<__remove_cv_t<_Up>, bool>::value), "Second argument to lcm cannot be bool" );
  71. if (__m == 0 || __n == 0)
  72. return 0;
  73. using _Rp = common_type_t<_Tp,_Up>;
  74. _Rp __val1 = __ct_abs<_Rp, _Tp>()(__m) / _VSTD::gcd(__m, __n);
  75. _Rp __val2 = __ct_abs<_Rp, _Up>()(__n);
  76. _LIBCPP_ASSERT((numeric_limits<_Rp>::max() / __val1 > __val2), "Overflow in lcm");
  77. return __val1 * __val2;
  78. }
  79. #endif // _LIBCPP_STD_VER
  80. _LIBCPP_END_NAMESPACE_STD
  81. _LIBCPP_POP_MACROS
  82. #endif // _LIBCPP___NUMERIC_GCD_LCM_H