public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Pierre-Marie de Rodat <derodat@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Eric Botcazou <ebotcazou@adacore.com>
Subject: [Ada] Small improvement to System.Value_R.Scan_Raw_Real
Date: Fri, 27 Nov 2020 04:18:01 -0500	[thread overview]
Message-ID: <20201127091801.GA63309@adacore.com> (raw)

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

This makes System.Value_R.Scan_Raw_Real round the extra digit it returns
to the caller, so that it is precise to the last 'Aft digits for strings
that are the exact representation of a number, i.e. not already rounded.

Tested on x86_64-pc-linux-gnu, committed on trunk

gcc/ada/

	* libgnat/s-valuer.adb (Scan_Decimal_Digits): Round Extra.
	(Scan_Integral_Digits): Likewise.

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

diff --git a/gcc/ada/libgnat/s-valuer.adb b/gcc/ada/libgnat/s-valuer.adb
--- a/gcc/ada/libgnat/s-valuer.adb
+++ b/gcc/ada/libgnat/s-valuer.adb
@@ -144,6 +144,9 @@ package body System.Value_R is
       --  Set to True if addition of a digit will cause Value to be superior
       --  to Precision_Limit.
 
+      Precision_Limit_Just_Reached : Boolean := False;
+      --  Set to True if Precision_Limit_Reached was just set to True
+
       Digit : Char_As_Digit;
       --  The current digit
 
@@ -185,11 +188,24 @@ package body System.Value_R is
 
          --  If precision limit has been reached, just ignore any remaining
          --  digits for the computation of Value and Scale, but store the
-         --  first in Extra. The scanning should continue only to assess the
-         --  validity of the string.
+         --  first in Extra and use the second to round Extra. The scanning
+         --  should continue only to assess the validity of the string.
+
+         if Precision_Limit_Reached then
+            if Precision_Limit_Just_Reached then
+               if Digit >= Base / 2 then
+                  if Extra = Base - 1 then
+                     Extra := 0;
+                     Value := Value + 1;
+                  else
+                     Extra := Extra + 1;
+                  end if;
+               end if;
 
-         if not Precision_Limit_Reached then
+               Precision_Limit_Just_Reached := False;
+            end if;
 
+         else
             --  Trailing '0' digits are ignored until a non-zero digit is found
 
             if Digit = 0 then
@@ -218,7 +234,7 @@ package body System.Value_R is
                Temp := Value * Uns (Base) + Uns (Digit);
 
                if Value <= Umax
-                   or else (Value <= UmaxB and then Temp <= Precision_Limit)
+                 or else (Value <= UmaxB and then Temp <= Precision_Limit)
                then
                   Value := Temp;
                   Scale := Scale - 1;
@@ -226,6 +242,7 @@ package body System.Value_R is
                else
                   Extra := Digit;
                   Precision_Limit_Reached := True;
+                  Precision_Limit_Just_Reached := True;
                end if;
             end if;
          end if;
@@ -289,6 +306,9 @@ package body System.Value_R is
       --  Set to True if addition of a digit will cause Value to be superior
       --  to Precision_Limit.
 
+      Precision_Limit_Just_Reached : Boolean := False;
+      --  Set to True if Precision_Limit_Reached was just set to True
+
       Digit : Char_As_Digit;
       --  The current digit
 
@@ -324,12 +344,25 @@ package body System.Value_R is
 
          --  If precision limit has been reached, just ignore any remaining
          --  digits for the computation of Value, but update Scale and store
-         --  the first in Extra. The scanning should continue only to assess
-         --  the validity of the string.
+         --  the first in Extra and use the second to round Extra. The scanning
+         --  should continue only to assess the validity of the string.
 
          if Precision_Limit_Reached then
             Scale := Scale + 1;
 
+            if Precision_Limit_Just_Reached then
+               if Digit >= Base / 2 then
+                  if Extra = Base - 1 then
+                     Extra := 0;
+                     Value := Value + 1;
+                  else
+                     Extra := Extra + 1;
+                  end if;
+               end if;
+
+               Precision_Limit_Just_Reached := False;
+            end if;
+
          else
             Temp := Value * Uns (Base) + Uns (Digit);
 
@@ -341,6 +374,7 @@ package body System.Value_R is
             else
                Extra := Digit;
                Precision_Limit_Reached := True;
+               Precision_Limit_Just_Reached := True;
                Scale := Scale + 1;
             end if;
          end if;



                 reply	other threads:[~2020-11-27  9:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20201127091801.GA63309@adacore.com \
    --to=derodat@adacore.com \
    --cc=ebotcazou@adacore.com \
    --cc=gcc-patches@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).