From patchwork Wed Oct 20 19:27:53 2021 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: 1544027 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=yRQoFFIj; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4HZLYt0qm5z9sNH for ; Thu, 21 Oct 2021 06:40:34 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C7A8B3857C73 for ; Wed, 20 Oct 2021 19:40:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C7A8B3857C73 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634758831; bh=vWibq5FcEgwd0WumVcaRdAOYXSLV8lENoO10GWTx1GA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=yRQoFFIjBPhne3xi11YNYtOn/bjaMMG3mAPEmX1Ewu7/b+tRAC4LZchLnKhfE/MGg +O6RfQ8TcyR1e1e3385TnzzJQncHbB7+c6XEivLygfxg058FvsnWYz2w09WwpQ+IPH MSFRn7IC3bn0wH6JagI7BgkF5cLBjXwN9FNz773w= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id 2B4723857C70 for ; Wed, 20 Oct 2021 19:27:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2B4723857C70 Received: by mail-lf1-x130.google.com with SMTP id j21so675114lfe.0 for ; Wed, 20 Oct 2021 12:27:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=vWibq5FcEgwd0WumVcaRdAOYXSLV8lENoO10GWTx1GA=; b=Fx763mMWa9eWAc1LXYPYsdaABjz4fbaQA6Zrm7Mi3SVkic4r5QSx5GZNddeDVKEU1z YNKOErVumH4f/0HmBQkhMwWbJ1k1eeEInUy2a8cO/QRSCNotZC1Ei1Z+IyqL8/qwVmeQ l0q570YU94rIQqt4m1qPPQ7h1s8u4A4ZayC53inX+432U8qkMeSAXTLTnh6JuTQnMybD 5U/HLwZOiIKwD6u6T0cE8kPLKaQb5WdPTNKwAznllu3BDsEzqb1O6w2SoKlcH9hce5rd qcl/I+ojEj20jpBZG9QZPEh11Dd7y8tDo0ibJyidVWXA+mtRYIQM1F++zCFCArBZdMX6 5Cfw== X-Gm-Message-State: AOAM530H9tUMhYE67kNyon5D5u02KnLbp366s/XyzCx9jjGEazz2R4NW 5eoKp13rc4CHrS6dBgusPuYl44KBBpTEr1Wk X-Google-Smtp-Source: ABdhPJwk6HMmH/q2/h4VogoC9J9c9ZVLQG2Mqu7+qaKJCxdqggYJvjrlykFNHaHM2aisNbaNTgLtRQ== X-Received: by 2002:a19:c7cb:: with SMTP id x194mr1009914lff.555.1634758075014; Wed, 20 Oct 2021 12:27:55 -0700 (PDT) Received: from adacore.com ([2a02:2ab8:224:2ce:72b5:e8ff:feef:ee60]) by smtp.gmail.com with ESMTPSA id w16sm262089lfl.189.2021.10.20.12.27.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 12:27:54 -0700 (PDT) Date: Wed, 20 Oct 2021 19:27:53 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Factor out machine rounding operations Message-ID: <20211020192753.GA3154295@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , X-Patchwork-Original-From: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Cc: Eric Botcazou Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The RM 4.9(38/2) clause specifies that the rounding to be applied to a real static expression that is not part of a larger static expression is implementation defined, so it makes sense to have a single function implementing the operation. The change also sets the Is_Machine_Number flag more consistently. No functional changes. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_eval.ads (Machine_Number): New inline function. * sem_eval.adb (Machine_Number): New function body implementing the machine rounding operation specified by RM 4.9(38/2). (Check_Non_Static_Context): Call Machine_Number and set the Is_Machine_Number flag consistently on the resulting node. * sem_attr.adb (Eval_Attribute) : Likewise. * checks.adb (Apply_Float_Conversion_Check): Call Machine_Number. (Round_Machine): Likewise. diff --git a/gcc/ada/checks.adb b/gcc/ada/checks.adb --- a/gcc/ada/checks.adb +++ b/gcc/ada/checks.adb @@ -2171,7 +2171,7 @@ package body Checks is Lo_OK := (Ifirst > 0); else - Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Expr); + Lo := Machine_Number (Expr_Type, UR_From_Uint (Ifirst), Expr); Lo_OK := (Lo >= UR_From_Uint (Ifirst)); end if; @@ -2214,7 +2214,7 @@ package body Checks is Hi := UR_From_Uint (Ilast) + Ureal_Half; Hi_OK := (Ilast < 0); else - Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Expr); + Hi := Machine_Number (Expr_Type, UR_From_Uint (Ilast), Expr); Hi_OK := (Hi <= UR_From_Uint (Ilast)); end if; @@ -5563,7 +5563,7 @@ package body Checks is -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. function Round_Machine (B : Ureal) return Ureal; - -- B is a real bound. Round it using mode Round_Even. + -- B is a real bound. Round it to the nearest machine number. ----------------- -- OK_Operands -- @@ -5589,7 +5589,7 @@ package body Checks is function Round_Machine (B : Ureal) return Ureal is begin - return Machine (Typ, B, Round_Even, N); + return Machine_Number (Typ, B, N); end Round_Machine; -- Start of processing for Determine_Range_R diff --git a/gcc/ada/sem_attr.adb b/gcc/ada/sem_attr.adb --- a/gcc/ada/sem_attr.adb +++ b/gcc/ada/sem_attr.adb @@ -9251,14 +9251,12 @@ package body Sem_Attr is -- Machine -- ------------- - -- We use the same rounding mode as the one used for RM 4.9(38) + -- We use the same rounding as the one used for RM 4.9(38/2) when Attribute_Machine => Fold_Ureal - (N, - Eval_Fat.Machine - (P_Base_Type, Expr_Value_R (E1), Eval_Fat.Round_Even, N), - Static); + (N, Machine_Number (P_Base_Type, Expr_Value_R (E1), N), Static); + Set_Is_Machine_Number (N); ------------------ -- Machine_Emax -- diff --git a/gcc/ada/sem_eval.adb b/gcc/ada/sem_eval.adb --- a/gcc/ada/sem_eval.adb +++ b/gcc/ada/sem_eval.adb @@ -523,8 +523,8 @@ package body Sem_Eval is and then Nkind (Parent (N)) in N_Subexpr then Rewrite (N, New_Copy (N)); - Set_Realval - (N, Machine (Base_Type (T), Realval (N), Round_Even, N)); + Set_Realval (N, Machine_Number (Base_Type (T), Realval (N), N)); + Set_Is_Machine_Number (N); end if; end if; @@ -575,18 +575,7 @@ package body Sem_Eval is (N, Corresponding_Integer_Value (N) * Small_Value (T)); elsif not UR_Is_Zero (Realval (N)) then - - -- Note: even though RM 4.9(38) specifies biased rounding, this - -- has been modified by AI-100 in order to prevent confusing - -- differences in rounding between static and non-static - -- expressions. AI-100 specifies that the effect of such rounding - -- is implementation dependent, and in GNAT we round to nearest - -- even to match the run-time behavior. Note that this applies - -- to floating point literals, not fixed points ones, even though - -- their compiler representation is also as a universal real. - - Set_Realval - (N, Machine (Base_Type (T), Realval (N), Round_Even, N)); + Set_Realval (N, Machine_Number (Base_Type (T), Realval (N), N)); Set_Is_Machine_Number (N); end if; @@ -6045,6 +6034,27 @@ package body Sem_Eval is return False; end Is_Statically_Unevaluated; + -------------------- + -- Machine_Number -- + -------------------- + + -- Historical note: RM 4.9(38) originally specified biased rounding but + -- this has been modified by AI-268 to prevent confusing differences in + -- rounding between static and nonstatic expressions. This AI specifies + -- that the effect of such rounding is implementation-dependent instead, + -- and in GNAT we round to nearest even to match the run-time behavior. + -- Note that this applies to floating-point literals, not fixed-point + -- ones, even though their representation is also a universal real. + + function Machine_Number + (Typ : Entity_Id; + Val : Ureal; + N : Node_Id) return Ureal + is + begin + return Machine (Typ, Val, Round_Even, N); + end Machine_Number; + -------------------- -- Not_Null_Range -- -------------------- diff --git a/gcc/ada/sem_eval.ads b/gcc/ada/sem_eval.ads --- a/gcc/ada/sem_eval.ads +++ b/gcc/ada/sem_eval.ads @@ -486,6 +486,13 @@ package Sem_Eval is -- it cannot be determined at compile time. Flag Fixed_Int is used as in -- routine Is_In_Range above. + function Machine_Number + (Typ : Entity_Id; + Val : Ureal; + N : Node_Id) return Ureal; + -- Return the machine number of Typ corresponding to the specified Val as + -- per RM 4.9(38/2). N is a node only used to post warnings. + function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean; -- Returns True if it can guarantee that Lo .. Hi is not a null range. If -- it cannot (because the value of Lo or Hi is not known at compile time) @@ -574,5 +581,6 @@ private pragma Inline (Eval_Unchecked_Conversion); pragma Inline (Is_OK_Static_Expression); + pragma Inline (Machine_Number); end Sem_Eval;