aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/experimental/numeric
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2020-08-28 22:45:24 +0100
committerJonathan Wakely <jwakely@redhat.com>2020-08-28 23:03:28 +0100
commit82db1a42e9254c9009bbf8ac01366da4d1ab6df5 (patch)
tree8888e703096458531a7c9060fe8285f5a89db2a9 /libstdc++-v3/testsuite/experimental/numeric
parentd14c547abd484d3540b692bb8048c4a6efe92c8b (diff)
downloadgcc-82db1a42e9254c9009bbf8ac01366da4d1ab6df5.zip
gcc-82db1a42e9254c9009bbf8ac01366da4d1ab6df5.tar.gz
gcc-82db1a42e9254c9009bbf8ac01366da4d1ab6df5.tar.bz2
libstdc++: Fix std::gcd and std::lcm for unsigned integers [PR 92978]
This fixes a bug with mixed signed and unsigned types, where converting a negative value to the unsigned result type alters the value. The solution is to obtain the absolute values of the arguments immediately and to perform the actual GCD or LCM algorithm on two arguments of the same type. In order to operate on the most negative number without overflow when taking its absolute, use an unsigned type for the result of the abs operation. For example, -INT_MIN will overflow, but -(unsigned)INT_MIN is (unsigned)INT_MAX+1U which is the correct value. libstdc++-v3/ChangeLog: PR libstdc++/92978 * include/std/numeric (__abs_integral): Replace with ... (__detail::__absu): New function template that returns an unsigned type, guaranteeing it can represent the most negative signed value. (__detail::__gcd, __detail::__lcm): Require arguments to be unsigned and therefore already non-negative. (gcd, lcm): Convert arguments to absolute value as unsigned type before calling __detail::__gcd or __detail::__lcm. * include/experimental/numeric (gcd, lcm): Likewise. * testsuite/26_numerics/gcd/gcd_neg.cc: Adjust expected errors. * testsuite/26_numerics/lcm/lcm_neg.cc: Likewise. * testsuite/26_numerics/gcd/92978.cc: New test. * testsuite/26_numerics/lcm/92978.cc: New test. * testsuite/experimental/numeric/92978.cc: New test.
Diffstat (limited to 'libstdc++-v3/testsuite/experimental/numeric')
-rw-r--r--libstdc++-v3/testsuite/experimental/numeric/92978.cc48
1 files changed, 48 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/experimental/numeric/92978.cc b/libstdc++-v3/testsuite/experimental/numeric/92978.cc
new file mode 100644
index 0000000..8408fd4
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/numeric/92978.cc
@@ -0,0 +1,48 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// 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.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do compile { target c++14 } }
+
+#include <experimental/numeric>
+#include <limits.h>
+
+void
+test01()
+{
+ // PR libstdc++/92978
+ static_assert( std::experimental::gcd(-120, 10U) == 10,
+ "mixed signed/unsigned" );
+ static_assert( std::experimental::gcd(120U, -10) == 10,
+ "mixed signed/unsigned" );
+
+ static_assert( std::lcm(-42, 21U) == 42U );
+}
+
+void
+test02()
+{
+ static_assert( std::experimental::gcd(INT_MIN, 0LL) == 1LL+INT_MAX,
+ "|INT_MIN| should not be undefined as long as it fits in the result" );
+ static_assert( std::experimental::gcd(0LL, INT_MIN) == 1LL+INT_MAX,
+ "|INT_MIN| should not be undefined" );
+ static_assert( std::experimental::gcd(INT_MIN, 0LL + INT_MIN) == 1LL + INT_MAX,
+ "|INT_MIN| should not be undefined" );
+ static_assert( std::experimental::gcd(INT_MIN, 1LL + INT_MAX) == 1LL + INT_MAX,
+ "|INT_MIN| should not be undefined" );
+ static_assert( std::experimental::gcd(SHRT_MIN, 1U + SHRT_MAX) == 1U + SHRT_MAX,
+ "|SHRT_MIN| should not be undefined" );
+}