public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Paul Dreik <gccpatches@pauldreik.se>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org
Subject: [PATCH] Fix for bug libstdc++/110860
Date: Mon, 14 Aug 2023 11:57:09 +0200	[thread overview]
Message-ID: <3311a355-15c8-4cdb-1644-b52d8aecbd63@pauldreik.se> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 2777 bytes --]

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.

Thanks, Paul

------------------------------------------------------------------------
commit 848b8d948787495e64ed9c55d681eccf730b74fb
Author: Paul Dreik <gccpatches@pauldreik.se>
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 <gccpatches@pauldreik.se>

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;
  		}

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

             reply	other threads:[~2023-08-14  9:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-14  9:57 Paul Dreik [this message]
2023-08-14 17:15 ` Jonathan Wakely

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=3311a355-15c8-4cdb-1644-b52d8aecbd63@pauldreik.se \
    --to=gccpatches@pauldreik.se \
    --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).