You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

numeric 3.1KB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // <experimental/numeric> -*- C++ -*-
  2. // Copyright (C) 2015-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file experimental/numeric
  21. * This is a TS C++ Library header.
  22. * @ingroup libfund-ts
  23. */
  24. //
  25. // N4336 Working Draft, C++ Extensions for Library Fundamentals, Version 2
  26. //
  27. #ifndef _GLIBCXX_EXPERIMENTAL_NUMERIC
  28. #define _GLIBCXX_EXPERIMENTAL_NUMERIC 1
  29. #pragma GCC system_header
  30. #if __cplusplus >= 201402L
  31. #include <numeric>
  32. #include <experimental/type_traits>
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. namespace experimental
  37. {
  38. inline namespace fundamentals_v2
  39. {
  40. #define __cpp_lib_experimental_gcd_lcm 201411
  41. /// Greatest common divisor
  42. template<typename _Mn, typename _Nn>
  43. constexpr common_type_t<_Mn, _Nn>
  44. gcd(_Mn __m, _Nn __n) noexcept
  45. {
  46. static_assert(is_integral_v<_Mn>,
  47. "std::experimental::gcd arguments must be integers");
  48. static_assert(is_integral_v<_Nn>,
  49. "std::experimental::gcd arguments must be integers");
  50. static_assert(_Mn(2) != _Mn(1),
  51. "std::experimental::gcd arguments must not be bool");
  52. static_assert(_Nn(2) != _Nn(1),
  53. "std::experimental::gcd arguments must not be bool");
  54. using _Up = make_unsigned_t<common_type_t<_Mn, _Nn>>;
  55. return std::__detail::__gcd(std::__detail::__absu<_Up>(__m),
  56. std::__detail::__absu<_Up>(__n));
  57. }
  58. /// Least common multiple
  59. template<typename _Mn, typename _Nn>
  60. constexpr common_type_t<_Mn, _Nn>
  61. lcm(_Mn __m, _Nn __n)
  62. {
  63. static_assert(is_integral_v<_Mn>,
  64. "std::experimental::lcm arguments must be integers");
  65. static_assert(is_integral_v<_Nn>,
  66. "std::experimental::lcm arguments must be integers");
  67. static_assert(_Mn(2) != _Mn(1),
  68. "std::experimental::lcm arguments must not be bool");
  69. static_assert(_Nn(2) != _Nn(1),
  70. "std::experimental::lcm arguments must not be bool");
  71. using _Up = make_unsigned_t<common_type_t<_Mn, _Nn>>;
  72. return std::__detail::__lcm(std::__detail::__absu<_Up>(__m),
  73. std::__detail::__absu<_Up>(__n));
  74. }
  75. } // namespace fundamentals_v2
  76. } // namespace experimental
  77. _GLIBCXX_END_NAMESPACE_VERSION
  78. } // namespace std
  79. #endif // __cplusplus <= 201103L
  80. #endif // _GLIBCXX_EXPERIMENTAL_NUMERIC