]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Reduce the likelihood of overflow in Tanh
authorTonu Naks <naks@adacore.com>
Mon, 19 May 2025 17:20:41 +0000 (17:20 +0000)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Tue, 1 Jul 2025 08:29:43 +0000 (10:29 +0200)
gcc/ada/ChangeLog:

* libgnat/a-ngelfu.adb: conditional computation of X^2

gcc/ada/libgnat/a-ngelfu.adb

index 7ce2a4c87cefa83e82c70d75a62bf76001f537c5..d7b6c0c47f09bdacc01c92edd77da059cd17b6c6 100644 (file)
@@ -965,7 +965,6 @@ is
 
       P, Q, R : Float_Type'Base;
       Y : constant Float_Type'Base := abs X;
-      G : constant Float_Type'Base := Y * Y;
 
       Float_Type_Digits_15_Or_More : constant Boolean :=
         Float_Type'Digits > 14;
@@ -983,10 +982,14 @@ is
       elsif Y < Half_Ln3
         and then Float_Type_Digits_15_Or_More
       then
-         P := (P2 * G + P1) * G + P0;
-         Q := ((Q3 * G + Q2) * G + Q1) * G + Q0;
-         R := G * (P / Q);
-         return X + X * R;
+         declare
+            G : constant Float_Type'Base := Y * Y;
+         begin
+            P := (P2 * G + P1) * G + P0;
+            Q := ((Q3 * G + Q2) * G + Q1) * G + Q0;
+            R := G * (P / Q);
+            return X + X * R;
+         end;
 
       else
          return Aux.Tanh (X);