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