numeric 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // <numeric> -*- C++ -*-
  2. // Copyright (C) 2001-2018 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. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996,1997
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file include/numeric
  46. * This is a Standard C++ Library header.
  47. */
  48. #ifndef _GLIBCXX_NUMERIC
  49. #define _GLIBCXX_NUMERIC 1
  50. #pragma GCC system_header
  51. #include <bits/c++config.h>
  52. #include <bits/stl_iterator_base_types.h>
  53. #include <bits/stl_numeric.h>
  54. #ifdef _GLIBCXX_PARALLEL
  55. # include <parallel/numeric>
  56. #endif
  57. /**
  58. * @defgroup numerics Numerics
  59. *
  60. * Components for performing numeric operations. Includes support for
  61. * for complex number types, random number generation, numeric
  62. * (n-at-a-time) arrays, generalized numeric algorithms, and special
  63. * math functions.
  64. */
  65. #if __cplusplus >= 201402L
  66. #include <type_traits>
  67. namespace std _GLIBCXX_VISIBILITY(default)
  68. {
  69. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  70. namespace __detail
  71. {
  72. // std::abs is not constexpr and doesn't support unsigned integers.
  73. template<typename _Tp>
  74. constexpr
  75. enable_if_t<__and_<is_integral<_Tp>, is_signed<_Tp>>::value, _Tp>
  76. __abs_integral(_Tp __val)
  77. { return __val < 0 ? -__val : __val; }
  78. template<typename _Tp>
  79. constexpr
  80. enable_if_t<__and_<is_integral<_Tp>, is_unsigned<_Tp>>::value, _Tp>
  81. __abs_integral(_Tp __val)
  82. { return __val; }
  83. void __abs_integral(bool) = delete;
  84. template<typename _Mn, typename _Nn>
  85. constexpr common_type_t<_Mn, _Nn>
  86. __gcd(_Mn __m, _Nn __n)
  87. {
  88. return __m == 0 ? __detail::__abs_integral(__n)
  89. : __n == 0 ? __detail::__abs_integral(__m)
  90. : __detail::__gcd(__n, __m % __n);
  91. }
  92. /// Least common multiple
  93. template<typename _Mn, typename _Nn>
  94. constexpr common_type_t<_Mn, _Nn>
  95. __lcm(_Mn __m, _Nn __n)
  96. {
  97. return (__m != 0 && __n != 0)
  98. ? (__detail::__abs_integral(__m) / __detail::__gcd(__m, __n))
  99. * __detail::__abs_integral(__n)
  100. : 0;
  101. }
  102. } // namespace __detail
  103. #if __cplusplus > 201402L
  104. #define __cpp_lib_gcd_lcm 201606
  105. // These were used in drafts of SD-6:
  106. #define __cpp_lib_gcd 201606
  107. #define __cpp_lib_lcm 201606
  108. /// Greatest common divisor
  109. template<typename _Mn, typename _Nn>
  110. constexpr common_type_t<_Mn, _Nn>
  111. gcd(_Mn __m, _Nn __n)
  112. {
  113. static_assert(is_integral_v<_Mn>, "gcd arguments are integers");
  114. static_assert(is_integral_v<_Nn>, "gcd arguments are integers");
  115. static_assert(!is_same_v<remove_cv_t<_Mn>, bool>,
  116. "gcd arguments are not bools");
  117. static_assert(!is_same_v<remove_cv_t<_Nn>, bool>,
  118. "gcd arguments are not bools");
  119. return __detail::__gcd(__m, __n);
  120. }
  121. /// Least common multiple
  122. template<typename _Mn, typename _Nn>
  123. constexpr common_type_t<_Mn, _Nn>
  124. lcm(_Mn __m, _Nn __n)
  125. {
  126. static_assert(is_integral_v<_Mn>, "lcm arguments are integers");
  127. static_assert(is_integral_v<_Nn>, "lcm arguments are integers");
  128. static_assert(!is_same_v<remove_cv_t<_Mn>, bool>,
  129. "lcm arguments are not bools");
  130. static_assert(!is_same_v<remove_cv_t<_Nn>, bool>,
  131. "lcm arguments are not bools");
  132. return __detail::__lcm(__m, __n);
  133. }
  134. #endif // C++17
  135. _GLIBCXX_END_NAMESPACE_VERSION
  136. } // namespace std
  137. #endif // C++14
  138. #endif /* _GLIBCXX_NUMERIC */