From patchwork Wed Dec 16 13:15:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1417127 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Cwwcl3Mlpz9sT5 for ; Thu, 17 Dec 2020 00:16:23 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 59D153840C1E; Wed, 16 Dec 2020 13:15:44 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTP id 2A891386F836 for ; Wed, 16 Dec 2020 13:15:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2A891386F836 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=derodat@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 0B58B561FE; Wed, 16 Dec 2020 08:15:37 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id dLrfr3LKNZHv; Wed, 16 Dec 2020 08:15:36 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id 1291056200; Wed, 16 Dec 2020 08:15:35 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4862) id 11A96A6; Wed, 16 Dec 2020 08:15:35 -0500 (EST) Date: Wed, 16 Dec 2020 08:15:35 -0500 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Subject: [Ada] Avoid artificial underflow in System.Val_Real Message-ID: <20201216131535.GA69839@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eric Botcazou Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" The final computation now needs to be protected against artificial underflow when the value is very small. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * libgnat/s-valrea.adb (Maxexp32): New constant array. (Maxexp64): Likewise. (Maxexp80): Likewise. (Integer_to_Real): New local constants Maxexp and B. When the exponent is too negative, do the divison in two steps. diff --git a/gcc/ada/libgnat/s-valrea.adb b/gcc/ada/libgnat/s-valrea.adb --- a/gcc/ada/libgnat/s-valrea.adb +++ b/gcc/ada/libgnat/s-valrea.adb @@ -44,6 +44,27 @@ package body System.Val_Real is package Impl is new Value_R (Uns, Precision_Limit, Floating => True); + subtype Base_T is Unsigned range 2 .. 16; + + -- The following tables compute the maximum exponent of the base that can + -- fit in the given floating-point format, that is to say the element at + -- index N is the largest K such that N**K <= Num'Last. + + Maxexp32 : constant array (Base_T) of Positive := + (2 => 127, 3 => 80, 4 => 63, 5 => 55, 6 => 49, + 7 => 45, 8 => 42, 9 => 40, 10 => 38, 11 => 37, + 12 => 35, 13 => 34, 14 => 33, 15 => 32, 16 => 31); + + Maxexp64 : constant array (Base_T) of Positive := + (2 => 1023, 3 => 646, 4 => 511, 5 => 441, 6 => 396, + 7 => 364, 8 => 341, 9 => 323, 10 => 308, 11 => 296, + 12 => 285, 13 => 276, 14 => 268, 15 => 262, 16 => 255); + + Maxexp80 : constant array (Base_T) of Positive := + (2 => 16383, 3 => 10337, 4 => 8191, 5 => 7056, 6 => 6338, + 7 => 5836, 8 => 5461, 9 => 5168, 10 => 4932, 11 => 4736, + 12 => 4570, 13 => 4427, 14 => 4303, 15 => 4193, 16 => 4095); + function Integer_to_Real (Str : String; Val : Uns; @@ -69,6 +90,15 @@ package body System.Val_Real is pragma Unsuppress (Range_Check); + Maxexp : constant Positive := + (if Num'Size = 32 then Maxexp32 (Base) + elsif Num'Size = 64 then Maxexp64 (Base) + elsif Num'Machine_Mantissa = 64 then Maxexp80 (Base) + else raise Program_Error); + -- Maximum exponent of the base that can fit in Num + + B : constant Num := Num (Base); + R_Val : Num; S : Integer := Scale; @@ -86,16 +116,25 @@ package body System.Val_Real is R_Val := Num (Val); if Extra > 0 then - R_Val := R_Val * Num (Base) + Num (Extra); + R_Val := R_Val * B + Num (Extra); S := S - 1; end if; - -- Compute the final value + -- Compute the final value. When the exponent is positive, we can do the + -- computation directly because, if the exponentiation overflows, then + -- the final value overflows as well. But when the exponent is negative, + -- we may need to do it in two steps to avoid an artificial underflow. + + if S > 0 then + R_Val := R_Val * B ** S; + + elsif S < 0 then + if S < -Maxexp then + R_Val := R_Val / B ** Maxexp; + S := S + Maxexp; + end if; - if S < 0 then - R_Val := R_Val / Num (Base) ** (-S); - else - R_Val := R_Val * Num (Base) ** S; + R_Val := R_Val / B ** (-S); end if; -- Finally deal with initial minus sign, note that this processing is