From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 7DC203857400 for ; Fri, 17 Nov 2023 17:31:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7DC203857400 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7DC203857400 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700242323; cv=none; b=ZYgadM+G+RZJ54qfzNdg0cCXPqq2i5O/Ej9rYiproPL6YoFgwwgcWd0vMHPO1I4d5v6FwggHh17MrimZXTDX+f46Mj0TaRYr0y8oWqBhPVhCpnVuQiLKTBOUy+uUMCKHifB5Kw0CnQ67BBznz2FH8454kZA7bahL6WcQoKZza7k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700242323; c=relaxed/simple; bh=C7RbtKmqJEB85FuUnPmr5ZDbIl9AkWNodttLyAifhjI=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=dUg0sduMcWX+WBOHoGxe3ZMQuzGMrK7c0aP76AF9lcfvb8xawDIGunF1Rk5drd8DvblNW3bO8a6I2cJZ3Igtx2yNJ4aAdaba/NrGN6WUY8MeufvkjG2OXKv6eZJJKdRd4QwLMmA8VroJNAzMgT+ypicYHJ3LHWXRh2kfise8RqE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700242313; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jb+wsSpLB7inTQn53Ua3sdzPIP4AvqETbnfIa31z/Ww=; b=UiOODyelZLnE1Wd+Aojkh/Aq+cqpRZkpqCvvijlw4uL7T+1PwdXEeqn/E+0Cq8omyY/2hl xMCHwUPX9uOrI9g/O78rajaiezF9j/yU7MBBumie4PkwQM8lHqnGhAwSsuOfxx7cLB8ezu P++3avDQXs8tZz+sdAH1t2efSMWZCQo= Received: from mail-yw1-f199.google.com (mail-yw1-f199.google.com [209.85.128.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-198-uUR3bwVUP5CaHBSqBjdCxA-1; Fri, 17 Nov 2023 12:31:51 -0500 X-MC-Unique: uUR3bwVUP5CaHBSqBjdCxA-1 Received: by mail-yw1-f199.google.com with SMTP id 00721157ae682-5afc00161daso33040997b3.1 for ; Fri, 17 Nov 2023 09:31:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700242311; x=1700847111; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Jb+wsSpLB7inTQn53Ua3sdzPIP4AvqETbnfIa31z/Ww=; b=C4cFmqQDZwRopL2TiZMdFJYMv7vEp/lojBJ7UtadihSrGhoaW/R94TdfuMWRivBgYL tFFIciyykR9DZ+fZvjHuquxV09vVo9xX86tljjj4hoB1a7vvE3Yx1Nyn19rz7r4aE20i UtjUfLlbkLqCRREroEEnxl7hijGz8Gk3Gjw11afJubFTY953p21WqpjCp7kBu4n4HB9V wofa06ihSEjCROb0PfhGtMf4Kjt5VJ8wRH9jvRGFn5LilFTfZFV3uQmm19pc1FylWJ7K IsYXfBUdFTOP2BhD1tLq/fPzQijNyqAVevpCGWuy1TMdV/N6K9tWZOWRKhUoCot/7lV9 V/JA== X-Gm-Message-State: AOJu0YzoaBoY3UwIXD5URRHCjgqugoZcGXAfS35A/m1VRfAnbde/fJ/j PxpOUTzh1JdpxWkhB9zFF+gK2RRUQY6nhlt6Q25lKA9EkNiFcPbjgtdUzt/MdN9Yrg079W+IrQe /NAk7QsvWJ67nf29dXMHRA2k3ggKDwBpgRA== X-Received: by 2002:a81:a214:0:b0:599:b570:2db7 with SMTP id w20-20020a81a214000000b00599b5702db7mr285390ywg.30.1700242311026; Fri, 17 Nov 2023 09:31:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IEmE47ZzOw/OEBysPr+MjIFPCMMH/2BAN7q3fzwbi7K3o4x3f1EGlPm88rLQTTmQ6F/WP6V2FozlZlLkcm+rjI= X-Received: by 2002:a81:a214:0:b0:599:b570:2db7 with SMTP id w20-20020a81a214000000b00599b5702db7mr285365ywg.30.1700242310697; Fri, 17 Nov 2023 09:31:50 -0800 (PST) MIME-Version: 1.0 References: <20231117153138.1510158-1-jwakely@redhat.com> In-Reply-To: From: Jonathan Wakely Date: Fri, 17 Nov 2023 17:31:39 +0000 Message-ID: Subject: Re: [committed] libstdc++: Define C++26 saturation arithmetic functions (P0543R3) To: =?UTF-8?Q?Daniel_Kr=C3=BCgler?= Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE,URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, 17 Nov 2023 at 17:01, Daniel Kr=C3=BCgler wrote: > > Am Fr., 17. Nov. 2023 um 16:32 Uhr schrieb Jonathan Wakely : > > > > Tested x86_64-linux. Pushed to trunk. > > > > GCC generates better code for add_sat if we use: > > > > unsigned z =3D x + y; > > z |=3D -(z < x); > > return z; > > > > If the compiler can't be improved we should consider using that instead > > of __builtin_add_overflow. > > > > > > -- >8 -- > > > > > > This was approved for C++26 last week at the WG21 meeting in Kona. > > > > libstdc++-v3/ChangeLog: > > > > * include/Makefile.am: Add new header. > > * include/Makefile.in: Regenerate. > > * include/bits/version.def (saturation_arithmetic): Define. > > * include/bits/version.h: Regenerate. > > * include/std/numeric: Include new header. > > * include/bits/sat_arith.h: New file. > > * testsuite/26_numerics/saturation/add.cc: New test. > > * testsuite/26_numerics/saturation/cast.cc: New test. > > * testsuite/26_numerics/saturation/div.cc: New test. > > * testsuite/26_numerics/saturation/mul.cc: New test. > > * testsuite/26_numerics/saturation/sub.cc: New test. > > * testsuite/26_numerics/saturation/version.cc: New test. > > --- > > libstdc++-v3/include/Makefile.am | 1 + > > libstdc++-v3/include/Makefile.in | 1 + > > libstdc++-v3/include/bits/sat_arith.h | 148 ++++++++++++++++++ > > libstdc++-v3/include/bits/version.def | 8 + > > libstdc++-v3/include/bits/version.h | 11 ++ > > libstdc++-v3/include/std/numeric | 5 + > > .../testsuite/26_numerics/saturation/add.cc | 73 +++++++++ > > .../testsuite/26_numerics/saturation/cast.cc | 24 +++ > > .../testsuite/26_numerics/saturation/div.cc | 45 ++++++ > > .../testsuite/26_numerics/saturation/mul.cc | 34 ++++ > > .../testsuite/26_numerics/saturation/sub.cc | 86 ++++++++++ > > .../26_numerics/saturation/version.cc | 19 +++ > > 12 files changed, 455 insertions(+) > > create mode 100644 libstdc++-v3/include/bits/sat_arith.h > > create mode 100644 libstdc++-v3/testsuite/26_numerics/saturation/add.c= c > > create mode 100644 libstdc++-v3/testsuite/26_numerics/saturation/cast.= cc > > create mode 100644 libstdc++-v3/testsuite/26_numerics/saturation/div.c= c > > create mode 100644 libstdc++-v3/testsuite/26_numerics/saturation/mul.c= c > > create mode 100644 libstdc++-v3/testsuite/26_numerics/saturation/sub.c= c > > create mode 100644 libstdc++-v3/testsuite/26_numerics/saturation/versi= on.cc > > > > diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Ma= kefile.am > > index dab9f720cbb..17d9d9cec31 100644 > > --- a/libstdc++-v3/include/Makefile.am > > +++ b/libstdc++-v3/include/Makefile.am > > @@ -142,6 +142,7 @@ bits_freestanding =3D \ > > ${bits_srcdir}/ranges_uninitialized.h \ > > ${bits_srcdir}/ranges_util.h \ > > ${bits_srcdir}/refwrap.h \ > > + ${bits_srcdir}/sat_arith.h \ > > ${bits_srcdir}/stl_algo.h \ > > ${bits_srcdir}/stl_algobase.h \ > > ${bits_srcdir}/stl_construct.h \ > > diff --git a/libstdc++-v3/include/Makefile.in b/libstdc++-v3/include/Ma= kefile.in > > index 4f7ab2dfbab..f038af709cc 100644 > > --- a/libstdc++-v3/include/Makefile.in > > +++ b/libstdc++-v3/include/Makefile.in > > @@ -497,6 +497,7 @@ bits_freestanding =3D \ > > ${bits_srcdir}/ranges_uninitialized.h \ > > ${bits_srcdir}/ranges_util.h \ > > ${bits_srcdir}/refwrap.h \ > > + ${bits_srcdir}/sat_arith.h \ > > ${bits_srcdir}/stl_algo.h \ > > ${bits_srcdir}/stl_algobase.h \ > > ${bits_srcdir}/stl_construct.h \ > > diff --git a/libstdc++-v3/include/bits/sat_arith.h b/libstdc++-v3/inclu= de/bits/sat_arith.h > > new file mode 100644 > > index 00000000000..71793467984 > > --- /dev/null > > +++ b/libstdc++-v3/include/bits/sat_arith.h > > @@ -0,0 +1,148 @@ > > +// Saturation arithmetic -*- C++ -*- > > + > > +// Copyright The GNU Toolchain Authors. > > +// > > +// 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 a= nd > > +// a copy of the GCC Runtime Library Exception along with this program= ; > > +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, s= ee > > +// . > > + > > +/** @file include/bits/sat_arith.h > > + * This is an internal header file, included by other library headers= . > > + * Do not attempt to use it directly. @headername{numeric} > > + */ > > + > > +#ifndef _GLIBCXX_SAT_ARITH_H > > +#define _GLIBCXX_SAT_ARITH_H 1 > > + > > +#pragma GCC system_header > > + > > +#include > > + > > +#ifdef __glibcxx_saturation_arithmetic // C++ >=3D 26 > > + > > +#include > > +#include > > + > > +namespace std _GLIBCXX_VISIBILITY(default) > > +{ > > +_GLIBCXX_BEGIN_NAMESPACE_VERSION > > + > > + /// Add two integers, with saturation in case of overflow. > > + template requires __is_standard_integer<_Tp>::value > > + constexpr _Tp > > + add_sat(_Tp __x, _Tp __y) noexcept > > + { > > + _Tp __z; > > + if (!__builtin_add_overflow(__x, __y, &__z)) > > + return __z; > > + if constexpr (is_unsigned_v<_Tp>) > > + return __gnu_cxx::__int_traits<_Tp>::__max; > > + else if (__x < 0) > > + return __gnu_cxx::__int_traits<_Tp>::__min; > > My apologies, but why does the sign of x decide the direction of the > result, shouldn't that be the sign of the returned value of z? z is incorrect at this point, it only has the correct value if no overflow occurred. But we know that an overflow occurred because the built-in returned true. We need to determine whether the overflow was positive, i.e. greater than numeric_limits::max(), or negative, i.e. lower than numeric_limits::min(). For unsigned types, it must have been a positive overflow, because neither value is negative so that's easy. If x is negative, then there is no possible y that can cause a positive overflow. If we consider Tp=3D=3Dint, then the maximum y is INT_MAX, so if x is negative, x+INT_MAX < INT_MAX. So if x is negative, we must have had a negative overflow, and so the result saturates to INT_MIN. If x is positive, there is no possible y that can cause a negative overflow. The minimum y is INT_MIN, and so if x is positive, x + INT_MIN > INT_MIN. So if x is positive, we must have had a positive overflow. (And x can't be zero, because 0+y would not overflow).