2015-05-02 16:18:36 +01:00
|
|
|
// <experimental/numeric> -*- C++ -*-
|
|
|
|
|
|
2026-01-02 09:53:48 +01:00
|
|
|
// Copyright (C) 2015-2026 Free Software Foundation, Inc.
|
2015-05-02 16:18:36 +01:00
|
|
|
//
|
|
|
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
|
|
// software; you can redistribute it and/or modify it under the
|
|
|
|
|
// terms of the GNU General Public License as published by the
|
|
|
|
|
// Free Software Foundation; either version 3, or (at your option)
|
|
|
|
|
// any later version.
|
|
|
|
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
|
// permissions described in the GCC Runtime Library Exception, version
|
|
|
|
|
// 3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License and
|
|
|
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
/** @file experimental/numeric
|
|
|
|
|
* This is a TS C++ Library header.
|
2019-05-02 16:46:29 +01:00
|
|
|
* @ingroup libfund-ts
|
2015-05-02 16:18:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// N4336 Working Draft, C++ Extensions for Library Fundamentals, Version 2
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef _GLIBCXX_EXPERIMENTAL_NUMERIC
|
|
|
|
|
#define _GLIBCXX_EXPERIMENTAL_NUMERIC 1
|
|
|
|
|
|
2024-09-12 12:15:51 -04:00
|
|
|
#ifdef _GLIBCXX_SYSHDR
|
2015-05-02 16:18:36 +01:00
|
|
|
#pragma GCC system_header
|
2024-09-12 12:15:51 -04:00
|
|
|
#endif
|
2015-05-02 16:18:36 +01:00
|
|
|
|
2022-09-19 21:54:49 +02:00
|
|
|
#include <bits/requires_hosted.h> // experimental is currently omitted
|
|
|
|
|
|
2017-09-12 15:03:06 +01:00
|
|
|
#if __cplusplus >= 201402L
|
2015-05-02 16:18:36 +01:00
|
|
|
|
2016-09-30 14:43:37 +01:00
|
|
|
#include <numeric>
|
2015-05-02 16:18:36 +01:00
|
|
|
#include <experimental/type_traits>
|
|
|
|
|
|
|
|
|
|
namespace std _GLIBCXX_VISIBILITY(default)
|
|
|
|
|
{
|
2017-07-23 08:41:35 +00:00
|
|
|
_GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
|
|
|
|
2015-05-02 16:18:36 +01:00
|
|
|
namespace experimental
|
|
|
|
|
{
|
|
|
|
|
inline namespace fundamentals_v2
|
|
|
|
|
{
|
|
|
|
|
#define __cpp_lib_experimental_gcd_lcm 201411
|
|
|
|
|
|
2016-10-03 18:01:10 +01:00
|
|
|
/// Greatest common divisor
|
2015-05-02 16:18:36 +01:00
|
|
|
template<typename _Mn, typename _Nn>
|
|
|
|
|
constexpr common_type_t<_Mn, _Nn>
|
2020-08-28 22:45:24 +01:00
|
|
|
gcd(_Mn __m, _Nn __n) noexcept
|
2015-05-02 16:18:36 +01:00
|
|
|
{
|
libstdc++: Make std::lcm and std::gcd detect overflow [PR105844]
When I fixed PR libstdc++/92978 I introduced a regression whereby
std::lcm(INT_MIN, 1) and std::lcm(50000, 49999) would no longer produce
errors during constant evaluation. Those calls are undefined, because
they violate the preconditions that |m| and the result can be
represented in the return type (which is int in both those cases). The
regression occurred because __absu<unsigned>(INT_MIN) is well-formed,
due to the explicit casts to unsigned in that new helper function, and
the out-of-range multiplication is well-formed, because unsigned
arithmetic wraps instead of overflowing.
To fix 92978 I made std::gcm and std::lcm calculate |m| and |n|
immediately, yielding a common unsigned type that was used to calculate
the result. That was partly correct, but there's no need to use an
unsigned type. Doing so only suppresses the overflow errors so the
compiler can't detect them. This change replaces __absu with __abs_r
that returns the common type (not its corresponding unsigned type). This
way we can detect overflow in __abs_r when required, while still
supporting the most-negative value when it can be represented in the
result type. To detect LCM results that are out of range of the result
type we still need explicit checks, because neither constant evaluation
nor UBsan will complain about unsigned wrapping for cases such as
std::lcm(500000u, 499999u). We can detect those overflows efficiently by
using __builtin_mul_overflow and asserting.
libstdc++-v3/ChangeLog:
PR libstdc++/105844
* include/experimental/numeric (experimental::gcd): Simplify
assertions. Use __abs_r instead of __absu.
(experimental::lcm): Likewise. Remove use of __detail::__lcm so
overflow can be detected.
* include/std/numeric (__detail::__absu): Rename to __abs_r and
change to allow signed result type, so overflow can be detected.
(__detail::__lcm): Remove.
(gcd): Simplify assertions. Use __abs_r instead of __absu.
(lcm): Likewise. Remove use of __detail::__lcm so overflow can
be detected.
* testsuite/26_numerics/gcd/gcd_neg.cc: Adjust dg-error lines.
* testsuite/26_numerics/lcm/lcm_neg.cc: Likewise.
* testsuite/26_numerics/gcd/105844.cc: New test.
* testsuite/26_numerics/lcm/105844.cc: New test.
2022-06-10 14:39:13 +01:00
|
|
|
static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
|
|
|
|
|
"std::experimental::gcd arguments must be integers");
|
|
|
|
|
static_assert(_Mn(2) == 2 && _Nn(2) == 2,
|
|
|
|
|
"std::experimental::gcd arguments must not be bool");
|
|
|
|
|
namespace __detail = std::__detail;
|
|
|
|
|
using _Ct = common_type_t<_Mn, _Nn>;
|
|
|
|
|
const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
|
|
|
|
|
const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
|
|
|
|
|
return __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
|
2015-05-02 16:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
2016-10-03 18:01:10 +01:00
|
|
|
/// Least common multiple
|
2015-05-02 16:18:36 +01:00
|
|
|
template<typename _Mn, typename _Nn>
|
|
|
|
|
constexpr common_type_t<_Mn, _Nn>
|
|
|
|
|
lcm(_Mn __m, _Nn __n)
|
|
|
|
|
{
|
libstdc++: Make std::lcm and std::gcd detect overflow [PR105844]
When I fixed PR libstdc++/92978 I introduced a regression whereby
std::lcm(INT_MIN, 1) and std::lcm(50000, 49999) would no longer produce
errors during constant evaluation. Those calls are undefined, because
they violate the preconditions that |m| and the result can be
represented in the return type (which is int in both those cases). The
regression occurred because __absu<unsigned>(INT_MIN) is well-formed,
due to the explicit casts to unsigned in that new helper function, and
the out-of-range multiplication is well-formed, because unsigned
arithmetic wraps instead of overflowing.
To fix 92978 I made std::gcm and std::lcm calculate |m| and |n|
immediately, yielding a common unsigned type that was used to calculate
the result. That was partly correct, but there's no need to use an
unsigned type. Doing so only suppresses the overflow errors so the
compiler can't detect them. This change replaces __absu with __abs_r
that returns the common type (not its corresponding unsigned type). This
way we can detect overflow in __abs_r when required, while still
supporting the most-negative value when it can be represented in the
result type. To detect LCM results that are out of range of the result
type we still need explicit checks, because neither constant evaluation
nor UBsan will complain about unsigned wrapping for cases such as
std::lcm(500000u, 499999u). We can detect those overflows efficiently by
using __builtin_mul_overflow and asserting.
libstdc++-v3/ChangeLog:
PR libstdc++/105844
* include/experimental/numeric (experimental::gcd): Simplify
assertions. Use __abs_r instead of __absu.
(experimental::lcm): Likewise. Remove use of __detail::__lcm so
overflow can be detected.
* include/std/numeric (__detail::__absu): Rename to __abs_r and
change to allow signed result type, so overflow can be detected.
(__detail::__lcm): Remove.
(gcd): Simplify assertions. Use __abs_r instead of __absu.
(lcm): Likewise. Remove use of __detail::__lcm so overflow can
be detected.
* testsuite/26_numerics/gcd/gcd_neg.cc: Adjust dg-error lines.
* testsuite/26_numerics/lcm/lcm_neg.cc: Likewise.
* testsuite/26_numerics/gcd/105844.cc: New test.
* testsuite/26_numerics/lcm/105844.cc: New test.
2022-06-10 14:39:13 +01:00
|
|
|
static_assert(is_integral_v<_Mn> && is_integral_v<_Nn>,
|
2020-08-28 22:45:24 +01:00
|
|
|
"std::experimental::lcm arguments must be integers");
|
libstdc++: Make std::lcm and std::gcd detect overflow [PR105844]
When I fixed PR libstdc++/92978 I introduced a regression whereby
std::lcm(INT_MIN, 1) and std::lcm(50000, 49999) would no longer produce
errors during constant evaluation. Those calls are undefined, because
they violate the preconditions that |m| and the result can be
represented in the return type (which is int in both those cases). The
regression occurred because __absu<unsigned>(INT_MIN) is well-formed,
due to the explicit casts to unsigned in that new helper function, and
the out-of-range multiplication is well-formed, because unsigned
arithmetic wraps instead of overflowing.
To fix 92978 I made std::gcm and std::lcm calculate |m| and |n|
immediately, yielding a common unsigned type that was used to calculate
the result. That was partly correct, but there's no need to use an
unsigned type. Doing so only suppresses the overflow errors so the
compiler can't detect them. This change replaces __absu with __abs_r
that returns the common type (not its corresponding unsigned type). This
way we can detect overflow in __abs_r when required, while still
supporting the most-negative value when it can be represented in the
result type. To detect LCM results that are out of range of the result
type we still need explicit checks, because neither constant evaluation
nor UBsan will complain about unsigned wrapping for cases such as
std::lcm(500000u, 499999u). We can detect those overflows efficiently by
using __builtin_mul_overflow and asserting.
libstdc++-v3/ChangeLog:
PR libstdc++/105844
* include/experimental/numeric (experimental::gcd): Simplify
assertions. Use __abs_r instead of __absu.
(experimental::lcm): Likewise. Remove use of __detail::__lcm so
overflow can be detected.
* include/std/numeric (__detail::__absu): Rename to __abs_r and
change to allow signed result type, so overflow can be detected.
(__detail::__lcm): Remove.
(gcd): Simplify assertions. Use __abs_r instead of __absu.
(lcm): Likewise. Remove use of __detail::__lcm so overflow can
be detected.
* testsuite/26_numerics/gcd/gcd_neg.cc: Adjust dg-error lines.
* testsuite/26_numerics/lcm/lcm_neg.cc: Likewise.
* testsuite/26_numerics/gcd/105844.cc: New test.
* testsuite/26_numerics/lcm/105844.cc: New test.
2022-06-10 14:39:13 +01:00
|
|
|
static_assert(_Mn(2) == 2 && _Nn(2) == 2,
|
2020-08-28 22:45:24 +01:00
|
|
|
"std::experimental::lcm arguments must not be bool");
|
libstdc++: Make std::lcm and std::gcd detect overflow [PR105844]
When I fixed PR libstdc++/92978 I introduced a regression whereby
std::lcm(INT_MIN, 1) and std::lcm(50000, 49999) would no longer produce
errors during constant evaluation. Those calls are undefined, because
they violate the preconditions that |m| and the result can be
represented in the return type (which is int in both those cases). The
regression occurred because __absu<unsigned>(INT_MIN) is well-formed,
due to the explicit casts to unsigned in that new helper function, and
the out-of-range multiplication is well-formed, because unsigned
arithmetic wraps instead of overflowing.
To fix 92978 I made std::gcm and std::lcm calculate |m| and |n|
immediately, yielding a common unsigned type that was used to calculate
the result. That was partly correct, but there's no need to use an
unsigned type. Doing so only suppresses the overflow errors so the
compiler can't detect them. This change replaces __absu with __abs_r
that returns the common type (not its corresponding unsigned type). This
way we can detect overflow in __abs_r when required, while still
supporting the most-negative value when it can be represented in the
result type. To detect LCM results that are out of range of the result
type we still need explicit checks, because neither constant evaluation
nor UBsan will complain about unsigned wrapping for cases such as
std::lcm(500000u, 499999u). We can detect those overflows efficiently by
using __builtin_mul_overflow and asserting.
libstdc++-v3/ChangeLog:
PR libstdc++/105844
* include/experimental/numeric (experimental::gcd): Simplify
assertions. Use __abs_r instead of __absu.
(experimental::lcm): Likewise. Remove use of __detail::__lcm so
overflow can be detected.
* include/std/numeric (__detail::__absu): Rename to __abs_r and
change to allow signed result type, so overflow can be detected.
(__detail::__lcm): Remove.
(gcd): Simplify assertions. Use __abs_r instead of __absu.
(lcm): Likewise. Remove use of __detail::__lcm so overflow can
be detected.
* testsuite/26_numerics/gcd/gcd_neg.cc: Adjust dg-error lines.
* testsuite/26_numerics/lcm/lcm_neg.cc: Likewise.
* testsuite/26_numerics/gcd/105844.cc: New test.
* testsuite/26_numerics/lcm/105844.cc: New test.
2022-06-10 14:39:13 +01:00
|
|
|
namespace __detail = std::__detail;
|
|
|
|
|
using _Ct = common_type_t<_Mn, _Nn>;
|
|
|
|
|
const _Ct __m2 = __detail::__abs_r<_Ct>(__m);
|
|
|
|
|
const _Ct __n2 = __detail::__abs_r<_Ct>(__n);
|
|
|
|
|
if (__m2 == 0 || __n2 == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
_Ct __r = __m2 / __detail::__gcd<make_unsigned_t<_Ct>>(__m2, __n2);
|
|
|
|
|
|
libstdc++: Use constexpr-if for C++11 and C++14
Replace remaining uses of _GLIBCXX17_CONSTEXPR for constexpr-if, so that
we always use constexpr-if in C++11 and C++14. Diagnostic pragmas are
used to suppress diagnostics.
libstdc++-v3/ChangeLog:
* include/bits/char_traits.h (char_traits::assign): Use
constexpr-if unconditionally for C++11 and C++14.
* include/bits/locale_conv.h (__do_str_codecvt): Likewise.
* include/bits/ostream.h (basic_ostream::_S_cast_flt): Likewise.
* include/bits/random.tcc (shuffle_order_engine::operator())
(seed_seq::seed_seq(Iter, Iter)): Likewise.
* include/bits/shared_ptr_base.h (_Sp_counted_base::_M_release):
Likewise.
* include/bits/stl_tree.h (_Rb_tree::_M_move_data): Likewise.
* include/bits/uniform_int_dist.h
(uniform_int_distribution::operator()): Likewise.
* include/bits/valarray_array.h (__valarray_default_construct)
(__valarray_fill_construct, __valarray_copy_construct)
(__valarray_copy_construct, __valarray_destroy_elements):
Likewise.
* include/experimental/numeric (lcm): Likewise.
* include/std/bit (__rotl, __rotr, __countl_zero, __countr_zero)
(__popcount, __bit_ceil) Likewise.:
* include/std/bitset (operator>>): Likewise.
* include/std/charconv (__to_chars_8, __to_chars_i)
(__from_chars_alnum_to_val, from_chars): Likewise.
* include/tr2/dynamic_bitset (__dynamic_bitset_base): Likewise.
* testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error
line number.
Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-28 17:34:58 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
|
|
|
|
|
if constexpr (is_signed_v<_Ct>)
|
libstdc++: Make std::lcm and std::gcd detect overflow [PR105844]
When I fixed PR libstdc++/92978 I introduced a regression whereby
std::lcm(INT_MIN, 1) and std::lcm(50000, 49999) would no longer produce
errors during constant evaluation. Those calls are undefined, because
they violate the preconditions that |m| and the result can be
represented in the return type (which is int in both those cases). The
regression occurred because __absu<unsigned>(INT_MIN) is well-formed,
due to the explicit casts to unsigned in that new helper function, and
the out-of-range multiplication is well-formed, because unsigned
arithmetic wraps instead of overflowing.
To fix 92978 I made std::gcm and std::lcm calculate |m| and |n|
immediately, yielding a common unsigned type that was used to calculate
the result. That was partly correct, but there's no need to use an
unsigned type. Doing so only suppresses the overflow errors so the
compiler can't detect them. This change replaces __absu with __abs_r
that returns the common type (not its corresponding unsigned type). This
way we can detect overflow in __abs_r when required, while still
supporting the most-negative value when it can be represented in the
result type. To detect LCM results that are out of range of the result
type we still need explicit checks, because neither constant evaluation
nor UBsan will complain about unsigned wrapping for cases such as
std::lcm(500000u, 499999u). We can detect those overflows efficiently by
using __builtin_mul_overflow and asserting.
libstdc++-v3/ChangeLog:
PR libstdc++/105844
* include/experimental/numeric (experimental::gcd): Simplify
assertions. Use __abs_r instead of __absu.
(experimental::lcm): Likewise. Remove use of __detail::__lcm so
overflow can be detected.
* include/std/numeric (__detail::__absu): Rename to __abs_r and
change to allow signed result type, so overflow can be detected.
(__detail::__lcm): Remove.
(gcd): Simplify assertions. Use __abs_r instead of __absu.
(lcm): Likewise. Remove use of __detail::__lcm so overflow can
be detected.
* testsuite/26_numerics/gcd/gcd_neg.cc: Adjust dg-error lines.
* testsuite/26_numerics/lcm/lcm_neg.cc: Likewise.
* testsuite/26_numerics/gcd/105844.cc: New test.
* testsuite/26_numerics/lcm/105844.cc: New test.
2022-06-10 14:39:13 +01:00
|
|
|
if (__is_constant_evaluated())
|
|
|
|
|
return __r * __n2; // constant evaluation can detect overflow here.
|
libstdc++: Use constexpr-if for C++11 and C++14
Replace remaining uses of _GLIBCXX17_CONSTEXPR for constexpr-if, so that
we always use constexpr-if in C++11 and C++14. Diagnostic pragmas are
used to suppress diagnostics.
libstdc++-v3/ChangeLog:
* include/bits/char_traits.h (char_traits::assign): Use
constexpr-if unconditionally for C++11 and C++14.
* include/bits/locale_conv.h (__do_str_codecvt): Likewise.
* include/bits/ostream.h (basic_ostream::_S_cast_flt): Likewise.
* include/bits/random.tcc (shuffle_order_engine::operator())
(seed_seq::seed_seq(Iter, Iter)): Likewise.
* include/bits/shared_ptr_base.h (_Sp_counted_base::_M_release):
Likewise.
* include/bits/stl_tree.h (_Rb_tree::_M_move_data): Likewise.
* include/bits/uniform_int_dist.h
(uniform_int_distribution::operator()): Likewise.
* include/bits/valarray_array.h (__valarray_default_construct)
(__valarray_fill_construct, __valarray_copy_construct)
(__valarray_copy_construct, __valarray_destroy_elements):
Likewise.
* include/experimental/numeric (lcm): Likewise.
* include/std/bit (__rotl, __rotr, __countl_zero, __countr_zero)
(__popcount, __bit_ceil) Likewise.:
* include/std/bitset (operator>>): Likewise.
* include/std/charconv (__to_chars_8, __to_chars_i)
(__from_chars_alnum_to_val, from_chars): Likewise.
* include/tr2/dynamic_bitset (__dynamic_bitset_base): Likewise.
* testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error
line number.
Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-28 17:34:58 +01:00
|
|
|
#pragma GCC diagnostic pop
|
libstdc++: Make std::lcm and std::gcd detect overflow [PR105844]
When I fixed PR libstdc++/92978 I introduced a regression whereby
std::lcm(INT_MIN, 1) and std::lcm(50000, 49999) would no longer produce
errors during constant evaluation. Those calls are undefined, because
they violate the preconditions that |m| and the result can be
represented in the return type (which is int in both those cases). The
regression occurred because __absu<unsigned>(INT_MIN) is well-formed,
due to the explicit casts to unsigned in that new helper function, and
the out-of-range multiplication is well-formed, because unsigned
arithmetic wraps instead of overflowing.
To fix 92978 I made std::gcm and std::lcm calculate |m| and |n|
immediately, yielding a common unsigned type that was used to calculate
the result. That was partly correct, but there's no need to use an
unsigned type. Doing so only suppresses the overflow errors so the
compiler can't detect them. This change replaces __absu with __abs_r
that returns the common type (not its corresponding unsigned type). This
way we can detect overflow in __abs_r when required, while still
supporting the most-negative value when it can be represented in the
result type. To detect LCM results that are out of range of the result
type we still need explicit checks, because neither constant evaluation
nor UBsan will complain about unsigned wrapping for cases such as
std::lcm(500000u, 499999u). We can detect those overflows efficiently by
using __builtin_mul_overflow and asserting.
libstdc++-v3/ChangeLog:
PR libstdc++/105844
* include/experimental/numeric (experimental::gcd): Simplify
assertions. Use __abs_r instead of __absu.
(experimental::lcm): Likewise. Remove use of __detail::__lcm so
overflow can be detected.
* include/std/numeric (__detail::__absu): Rename to __abs_r and
change to allow signed result type, so overflow can be detected.
(__detail::__lcm): Remove.
(gcd): Simplify assertions. Use __abs_r instead of __absu.
(lcm): Likewise. Remove use of __detail::__lcm so overflow can
be detected.
* testsuite/26_numerics/gcd/gcd_neg.cc: Adjust dg-error lines.
* testsuite/26_numerics/lcm/lcm_neg.cc: Likewise.
* testsuite/26_numerics/gcd/105844.cc: New test.
* testsuite/26_numerics/lcm/105844.cc: New test.
2022-06-10 14:39:13 +01:00
|
|
|
|
|
|
|
|
bool __overflow = __builtin_mul_overflow(__r, __n2, &__r);
|
|
|
|
|
__glibcxx_assert(!__overflow);
|
|
|
|
|
return __r;
|
2015-05-02 16:18:36 +01:00
|
|
|
}
|
|
|
|
|
} // namespace fundamentals_v2
|
|
|
|
|
} // namespace experimental
|
2017-07-23 08:41:35 +00:00
|
|
|
|
|
|
|
|
_GLIBCXX_END_NAMESPACE_VERSION
|
2015-05-02 16:18:36 +01:00
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
|
|
#endif // __cplusplus <= 201103L
|
|
|
|
|
|
|
|
|
|
#endif // _GLIBCXX_EXPERIMENTAL_NUMERIC
|