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 1D5743858C33 for ; Mon, 14 Aug 2023 17:15:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1D5743858C33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692033346; 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: in-reply-to:in-reply-to:references:references; bh=yCtKPjrFe+yRBqvw+94RSFvErC6pdg77ACMGJVkXxG8=; b=eeryeb0EMTxdd+VkheICj5Ta4KxtGbHU7lPtrFZBZHZInEXZj5TboxNBokqsI7p93KQp0z t5AntHU4oFpFx7TGmkhhhzjk4jBpTkddrYtUpdzKRLtgmbf/devR8i0ApfmiVkbvMDLAfo 8i2VwfQIjy7NAjlnbIzGJbeFcZmZk8I= Received: from mail-lj1-f198.google.com (mail-lj1-f198.google.com [209.85.208.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-626-XXt5iLo3MOW1blQCXeYT0g-1; Mon, 14 Aug 2023 13:15:43 -0400 X-MC-Unique: XXt5iLo3MOW1blQCXeYT0g-1 Received: by mail-lj1-f198.google.com with SMTP id 38308e7fff4ca-2b9bf493456so44376171fa.0 for ; Mon, 14 Aug 2023 10:15:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692033341; x=1692638141; h=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=yCtKPjrFe+yRBqvw+94RSFvErC6pdg77ACMGJVkXxG8=; b=ap5iSP78iUXtbjJEfXjpCUbsmDsC5Uz6KFE7BaGYdUCM0PwJ+KJevqA7eGkN7yZteQ go3um9q49zO1h36CgMshFHwGJxmRM0vCvxVf2JhAZvEFjDX2KWjikerWRco3DHXELv47 Xbb7VVu5VkmDJSN6bBl2Ms3EDzzKuw5yRwwyQhQFkavkG1VWwLsjZgVeDBn8Kbbp3IRC rbL16ugOEKs3ErMYYqH8/HE8fBQ4Ja0rE/vsY7XvLan8sW34nQKtP/hwel2Kn0Baz2od 3JbL9w4Xj2CuCI3MvM4WY+ycgQtJTTNDVOjCUtHR1z5Oo8BrYXTfvzeKn/7o+Xd8P2WA wHEg== X-Gm-Message-State: AOJu0YwBBexr0OTbg1z9gZ8BpN9pI+F51k8pFEvZBPD1iN7I8Ul00I7I HIEDMRiOF5AMHdmuSqi6CZGeFsv7Vlbe4k9ReBv53HF6E/VNPRc26McrOFn2nx353C+Lfn4isSh wv3yKSBFTNssEfsvDs/2RbROEYh6j1r8= X-Received: by 2002:a2e:8790:0:b0:2b9:cd79:8f94 with SMTP id n16-20020a2e8790000000b002b9cd798f94mr7268689lji.39.1692033341642; Mon, 14 Aug 2023 10:15:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGJsB4w2QHrbxph5MGmoTu1HED/uxbyaPO3r+JRih5/UbBCB1MORTlSwnG3uYDZ6VX5SPS0qV/IXJjvmANcnNU= X-Received: by 2002:a2e:8790:0:b0:2b9:cd79:8f94 with SMTP id n16-20020a2e8790000000b002b9cd798f94mr7268674lji.39.1692033341266; Mon, 14 Aug 2023 10:15:41 -0700 (PDT) MIME-Version: 1.0 References: <3311a355-15c8-4cdb-1644-b52d8aecbd63@pauldreik.se> In-Reply-To: <3311a355-15c8-4cdb-1644-b52d8aecbd63@pauldreik.se> From: Jonathan Wakely Date: Mon, 14 Aug 2023 18:15:30 +0100 Message-ID: Subject: Re: [PATCH] Fix for bug libstdc++/110860 To: Paul Dreik Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/alternative; boundary="000000000000412ce90602e53696" X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,HTML_MESSAGE,KAM_NUMSUBJECT,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000412ce90602e53696 Content-Type: text/plain; charset="UTF-8" On Mon, 14 Aug 2023 at 10:58, Paul Dreik via Libstdc++ < libstdc++@gcc.gnu.org> wrote: > The patch below fixes an issue with the fix already committed for > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110860 which unfortunately > was not sufficient for small magnitude floating point values. > > With the patch in place, the code now survives the fuzzing I used to > find the problem in the first place. Tested on amd64. > > I prepared the patch using git show, which should include the signoff as > instructed per the DCO. > I couldn't apply the patch directly from the email, but I'm not sure where it got mangled. I just applied it by hand instead. Pushed to trunk, thanks for the patch! I'll push it to gcc-13 shortly too. > > Thanks, Paul > > ------------------------------------------------------------------------ > commit 848b8d948787495e64ed9c55d681eccf730b74fb > Author: Paul Dreik > Date: Mon Aug 14 11:52:30 2023 +0200 > > libstdc++: Avoid problematic use of log10 in std::format [PR110860] > > If abs(__v) is smaller than one, the result will be on the > form 0.xxxxx. It is only if the magnitude is large that more digits > are needed before the decimal dot. > > This uses frexp instead of log10 which should be less expensive > and have sufficient precision for the desired purpose. > > It removes the problematic cases where log10 will be negative or not > fit in an int. > > Signed-off-by: Paul Dreik > > diff --git a/libstdc++-v3/include/std/format > b/libstdc++-v3/include/std/format > index f4520ff3f..729e3d4b9 100644 > --- a/libstdc++-v3/include/std/format > +++ b/libstdc++-v3/include/std/format > @@ -1490,14 +1490,22 @@ namespace __format > // If the buffer is too small it's probably because of a > large > // precision, or a very large value in fixed format. > size_t __guess = 8 + __prec; > - if (__fmt == chars_format::fixed && __v != 0) // +ddd.prec > + if (__fmt == chars_format::fixed) // +ddd.prec > { > - if constexpr (is_same_v<_Fp, float>) > - __guess += __builtin_log10f(__v < 0.0f ? -__v : __v); > - else if constexpr (is_same_v<_Fp, double>) > - __guess += __builtin_log10(__v < 0.0 ? -__v : __v); > - else if constexpr (is_same_v<_Fp, long double>) > - __guess += __builtin_log10l(__v < 0.0l ? -__v : __v); > + if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, > double> || > is_same_v<_Fp, long double>) > + { > + // the number of digits to the left of the decimal > point > + // is floor(log10(max(abs(__v),1)))+1 > + int __exp{}; > + if constexpr (is_same_v<_Fp, float>) > + __builtin_frexpf(__v, &__exp); > + else if constexpr (is_same_v<_Fp, double>) > + __builtin_frexp(__v, &__exp); > + else if constexpr (is_same_v<_Fp, long double>) > + __builtin_frexpl(__v, &__exp); > + if (__exp>0) > + __guess += 1U + __exp * 4004U / 13301U; // > log10(2) approx. > + } > else > __guess += numeric_limits<_Fp>::max_exponent10; > } > --000000000000412ce90602e53696--