public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: [committed] libstdc++: Fix std::gcd and std::lcm for unsigned integers [PR 92978]
Date: Wed, 2 Sep 2020 17:24:29 +0100	[thread overview]
Message-ID: <20200902162429.GN3400@redhat.com> (raw)
In-Reply-To: <20200828221126.GA1290141@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 3114 bytes --]

On 28/08/20 23:11 +0100, Jonathan Wakely wrote:
>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.
>
>Tested powerpc64le-linux. Committed to trunk.
>

[snip]

>diff --git a/libstdc++-v3/testsuite/experimental/numeric/92978.cc b/libstdc++-v3/testsuite/experimental/numeric/92978.cc
>new file mode 100644
>index 00000000000..8408fd4d9ce
>--- /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 );

This test is supposed to be usingthe experimental functions, but this
calls std::lcm (and so the test fails if run as C++14).

Fixed with the attached patch, committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 1008 bytes --]

commit c71644776f4e8477289a4de16239dbb420db6945
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Sep 2 17:20:37 2020

    libstdc++: Fix test to use correct function
    
    This was copied from a test for std::lcm but I forgot to change one of
    the calls to use the experimental version of the function.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/92978
            * testsuite/experimental/numeric/92978.cc: Use experimental::lcm
            not std::lcm.

diff --git a/libstdc++-v3/testsuite/experimental/numeric/92978.cc b/libstdc++-v3/testsuite/experimental/numeric/92978.cc
index 8408fd4d9ce..e2a4b1adefa 100644
--- a/libstdc++-v3/testsuite/experimental/numeric/92978.cc
+++ b/libstdc++-v3/testsuite/experimental/numeric/92978.cc
@@ -29,7 +29,7 @@ test01()
   static_assert( std::experimental::gcd(120U, -10) == 10,
       "mixed signed/unsigned" );
 
-  static_assert( std::lcm(-42, 21U) == 42U );
+  static_assert( std::experimental::lcm(-42, 21U) == 42U );
 }
 
 void

      parent reply	other threads:[~2020-09-02 16:24 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-28 22:11 Jonathan Wakely
2020-08-28 22:20 ` Daniel Krügler
2020-08-28 22:53   ` Jonathan Wakely
2020-08-29 17:24     ` Jonathan Wakely
2020-09-02 16:24 ` Jonathan Wakely [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200902162429.GN3400@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).