From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id C2CE638582B0; Fri, 17 Nov 2023 17:38:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C2CE638582B0 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C2CE638582B0 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::430 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700242688; cv=none; b=Bpfdkn/zM322VV4PKcNlMV1oyAp7y18Scdym9QByOAVqI59d9ZF/FQ8rEviGouKqPGDR+05aojdpiLDYn2l0aOWXRof8Ytga1c7cWlSPxABLkSlolRa9/93/fSrtDZQzti2d4Vdl5CFLMXYBqAANupXDidolnQ916S/pykI8HQc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700242688; c=relaxed/simple; bh=MHJ7JeuZmUblnXDc88sLiLzKfJKWAb+QS7IPdUjlJR8=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=ROxAvvC/YvKsAKiK3UA0OUXtJONkWWJpIeB43iLUt2UlW32YKCvBbZAy05Ry4+PZ/SjFm5P5fQqYb4pTaydYseKIgeM7OPLYY93x0XhncRn8kgw4mh8yvf6M6K43ra88yrsEoUy/hpY8BI/Ho27rgDfY3lzaxDeIwUayPDkVmqM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x430.google.com with SMTP id ffacd0b85a97d-32f7c80ab33so1492557f8f.0; Fri, 17 Nov 2023 09:38:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700242685; x=1700847485; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=axwOoOonBBSOEUK14LC9WCJBGGSFNq2Khys/Gw5uTT0=; b=mX65uIE07MvS4QrJIcHsseJxxWfKM5rRof2cnyYwza1tsJT8euLflcDfibJz+NJFVQ KYyZ2pd2F10tQF9mZNErS881A6WhFn/KPc9GGLDJ/Wv5S7Bq/Q1jjTm8ST1wXVDLxwZ4 uVtIRhOYS+N2g8y38Q3v6DthNPEK2jjV7cI6b8DmGlt8eEgfuknOcz5av8cPMzp4JT8C XBl/syWgZ4yEZbA01Ws+/EuCkiU36OmHlV1kbfldodOLC0K0x+NBA3GfKfKaXyCCkpNb Z3LPie5EOk9DezcBCxrAZkpvc5rKK966VFGZDh+REQMwXvBn9w+HZ44rQ+tEpgoMB5oB NSMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700242685; x=1700847485; 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=axwOoOonBBSOEUK14LC9WCJBGGSFNq2Khys/Gw5uTT0=; b=rkeBNogTrKZvl1CwbhaX8gaba/GKbehg6dGK5xZ7OntcRvGjKPXTik11h3cQr7Jq/k +fvPVs/c+BbS+iO08GR2lo0pljGexDqFwE6pM2asdnNzscjoXmMpLxg/BKZt3PMOMuLR iBPmm+MbQyqRGHCSg0CDvDPyIpbEDOlHZZLPDndvOxYwaqyDa8ripwOnbsoGzN3fnroq NefoUHAmA0p8P90AASmH/t1OqY3nDdTmkPeJ3HxMegfOimTXaf2hlincDFnB0um15OUc PZj3Tf3bBCWFzwo3T7Zzl8LVhIQDVG/2eyfATWbGFngtgKzwZhFIiEyjtjDa4Nfc7Lek NvjQ== X-Gm-Message-State: AOJu0Yy7sLlFdz68TQrA6qK2IwdsFvJzIxdyHOEXIH2udrOio0XIyyKp hM7LeLnZvE5xTyqIi+fhq3Iwn72AuMcTomoO15o= X-Google-Smtp-Source: AGHT+IHspIzhXYTF8P1XM+Wiof2/YRyXO3wS1Fgq7VYom5XXECiEHvEL8eNlU2hbQeWTMHinlepmtzuMozimSthjEYI= X-Received: by 2002:adf:ecc9:0:b0:32d:baf4:e572 with SMTP id s9-20020adfecc9000000b0032dbaf4e572mr13315257wro.23.1700242685229; Fri, 17 Nov 2023 09:38:05 -0800 (PST) MIME-Version: 1.0 References: <20231117153138.1510158-1-jwakely@redhat.com> In-Reply-To: From: =?UTF-8?Q?Daniel_Kr=C3=BCgler?= Date: Fri, 17 Nov 2023 18:37:53 +0100 Message-ID: Subject: Re: [committed] libstdc++: Define C++26 saturation arithmetic functions (P0543R3) To: Jonathan Wakely Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE 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: Am Fr., 17. Nov. 2023 um 18:31 Uhr schrieb Jonathan Wakely : > > On Fri, 17 Nov 2023 at 17:01, Daniel Kr=C3=BCgler wrote: > > [..] > > > + > > > +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). Ah right, thanks. - Daniel