From patchwork Fri May 26 07:36:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 70153 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA337395B02B for ; Fri, 26 May 2023 07:51:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA337395B02B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685087502; bh=gqYNDyTtjYn8pVNMQqxK2CVnHBM42a2zbd8dOa15IiI=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=EGLV+1ML8X9QjD8cmQzfzZuGJKRSs5EsX4drekST2gRUaww7aJ0uEciYpKB49suf2 UGgCj2Syu1CwWKuBnYmvX6cBYvwJpsCgt+yOxz7FMM2f7AmSqfRSrAEW0w9d8AqbCr eOiJeyytf00CBTjGOYxLXALxC2Z6bnY1Hwi1uJKk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by sourceware.org (Postfix) with ESMTPS id 42109388210C for ; Fri, 26 May 2023 07:37:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 42109388210C Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-3f6e13940daso4863935e9.0 for ; Fri, 26 May 2023 00:37:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685086619; x=1687678619; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gqYNDyTtjYn8pVNMQqxK2CVnHBM42a2zbd8dOa15IiI=; b=VghHQ/ZbWghVYdOq4o3/4o3e3PZnPDFdUz76ANRr7rJP5Jj5R4bZLpCg0aBNdB23cN XMTJOFElRPC8CMZu9O7in56oac2oCpb5/576gT7PW9e8SE91gZGaCr3ngZ11aPV4amT2 k8Ox2TpU/pHQYlrLvM/g8ifiEmTQXpDyqI7yxqMAdrnApeE9pK/XHELMCD1bl0rlObPR F0QrV+celIF1vtBpK7gJ6uAz3fcEdHcmOp0DMwJ7u0dVeMSfqHBG4EzIG2J/IIXI2+jx 6w0Uo0lCTDqkQtXw3Rwmy49FPiL2/4wdI9gDw2PQL9OwqIPeSFqVwcnXoW57VvmOjn8y RH3g== X-Gm-Message-State: AC+VfDwxwi9C5LGK8hHNoZtLZpsyhge88o0vGdQ5JaWVm1b42xLh4334 GQ8dIpy0egbYRmeTzPdUeVfzdgs2549k0c+pzBYSGQ== X-Google-Smtp-Source: ACHHUZ4l5VJvpVEACPCQji5PdOx3+XekDrRlFx/CryeY2PHwkMqheC0KkJD2POnH/R9Jx4k546TkVA== X-Received: by 2002:a7b:cb9a:0:b0:3f1:72e2:5d13 with SMTP id m26-20020a7bcb9a000000b003f172e25d13mr748209wmi.16.1685086619066; Fri, 26 May 2023 00:36:59 -0700 (PDT) Received: from localhost.localdomain ([2001:861:3382:1a90:777b:eef4:6f79:f26f]) by smtp.gmail.com with ESMTPSA id m23-20020a7bca57000000b003f5ffba9ae1sm4313882wml.24.2023.05.26.00.36.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 May 2023 00:36:58 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Use truncation for dynamic conversions from floating point to fixed point Date: Fri, 26 May 2023 09:36:57 +0200 Message-Id: <20230526073657.2069975-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" From: Eric Botcazou This changes the implementation of dynamic conversions from floating-point to ordinary fixed-point types, from rounding (to the nearest number) to truncation (toward zero), so as to make them consistent with both static conversions between these types and also the value of the Machine_Rounds attribute, which is False for all fixed-point types with GNAT. The rounding is still available under the debug switch -gnatd.N for the sake of backward compatibility with the previous implementation. gcc/ada/ * debug.adb (d.N): Document new usage. * exp_ch4.adb (Expand_N_Type_Conversion): Copy the Float_Truncate flag when rewriting a floating-point to fixed-point conversion as a floating-point to integer conversion. * exp_fixd.adb: Add with and use clauses for Debug. (Expand_Convert_Fixed_To_Fixed): Generate a truncation in all cases except if the result is explicitly rounded. (Expand_Convert_Integer_To_Fixed): Likewise. (Expand_Convert_Float_To_Fixed): Generate a truncation for all kind of fixed-point types, except if the result is explicitly rounded, or -gnatd.N is specified and the type is an ordinary fixed-point type. * sinfo.ads (Float_Truncate): Document usage for floating-point to fixed-point conversions. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/debug.adb | 6 +++++- gcc/ada/exp_ch4.adb | 7 +++++-- gcc/ada/exp_fixd.adb | 41 ++++++++++++++++++++++------------------- gcc/ada/sinfo.ads | 5 +++-- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/gcc/ada/debug.adb b/gcc/ada/debug.adb index 9566e095d1a..fd94203faf8 100644 --- a/gcc/ada/debug.adb +++ b/gcc/ada/debug.adb @@ -125,7 +125,7 @@ package body Debug is -- d.K Do not reject components in extensions overlapping with parent -- d.L Depend on back end for limited types in if and case expressions -- d.M Relaxed RM semantics - -- d.N + -- d.N Use rounding when converting from floating point to fixed point -- d.O Dump internal SCO tables -- d.P Previous (non-optimized) handling of length comparisons -- d.Q Previous (incomplete) style check for binary operators @@ -906,6 +906,10 @@ package body Debug is -- d.M Relaxed RM semantics. This flag sets Opt.Relaxed_RM_Semantics -- See Opt.Relaxed_RM_Semantics for more details. + -- d.N Use rounding instead of truncation when dynamically converting from + -- a floating-point type to an ordinary fixed-point type, for the sake + -- of compatibility with earlier versions of the compiler. + -- d.O Dump internal SCO tables. Before outputting the SCO information to -- the ALI file, the internal SCO tables (SCO_Table/SCO_Unit_Table) -- are dumped for debugging purposes. diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb index 5c9c6686c01..7be240bce0e 100644 --- a/gcc/ada/exp_ch4.adb +++ b/gcc/ada/exp_ch4.adb @@ -12150,8 +12150,12 @@ package body Exp_Ch4 is Expr_Id : constant Entity_Id := Make_Temporary (Loc, 'T', Conv); Int_Typ : constant Entity_Id := Small_Integer_Type_For (RM_Size (Btyp), Uns => False); + Trunc : constant Boolean := Float_Truncate (Conv); begin + Conv := Convert_To (Int_Typ, Expression (Conv)); + Set_Float_Truncate (Conv, Trunc); + -- Generate a temporary with the integer value. Required in the -- CCG compiler to ensure that run-time checks reference this -- integer expression (instead of the resulting fixed-point @@ -12163,8 +12167,7 @@ package body Exp_Ch4 is Defining_Identifier => Expr_Id, Object_Definition => New_Occurrence_Of (Int_Typ, Loc), Constant_Present => True, - Expression => - Convert_To (Int_Typ, Expression (Conv)))); + Expression => Conv)); -- Create integer objects for range checking of result. diff --git a/gcc/ada/exp_fixd.adb b/gcc/ada/exp_fixd.adb index 61c2f923f08..b7a996ab615 100644 --- a/gcc/ada/exp_fixd.adb +++ b/gcc/ada/exp_fixd.adb @@ -25,6 +25,7 @@ with Atree; use Atree; with Checks; use Checks; +with Debug; use Debug; with Einfo; use Einfo; with Einfo.Entities; use Einfo.Entities; with Einfo.Utils; use Einfo.Utils; @@ -1624,13 +1625,14 @@ package body Exp_Fixd is -- Fall through to use floating-point for the close result set case, -- as a result of the numerator or denominator of the small ratio not - -- being a sufficiently small integer. + -- being sufficiently small. See also Expand_Convert_Float_To_Fixed. Set_Result (N, Build_Multiply (N, Fpt_Value (Expr), Real_Literal (N, Small_Ratio)), - Rng_Check); + Rng_Check, + Trunc => not Rounded_Result (N)); end Expand_Convert_Fixed_To_Fixed; ----------------------------------- @@ -1769,23 +1771,23 @@ package body Exp_Fixd is if Small = Ureal_1 then Set_Result (N, Expr, Rng_Check, Trunc => True); - -- Normal case where multiply is required. Rounding is truncating - -- for decimal fixed point types only, see RM 4.6(29), except if the - -- conversion comes from an attribute reference 'Round (RM 3.5.10 (14)): - -- The attribute is implemented by means of a conversion that must - -- round. + -- Normal case where multiply is required. The conversion is truncating + -- for fixed-point types, see RM 4.6(29), except if the conversion comes + -- from an attribute reference 'Round (RM 3.5.10 (14)): the attribute is + -- implemented by means of a conversion that needs to round. However, if + -- the switch -gnatd.N is specified, we use rounding for ordinary fixed- + -- point types, for compatibility with earlier versions of the compiler. else - Set_Result - (N => N, - Expr => - Build_Multiply - (N => N, - L => Fpt_Value (Expr), - R => Real_Literal (N, Ureal_1 / Small)), - Rchk => Rng_Check, - Trunc => Is_Decimal_Fixed_Point_Type (Result_Type) - and not Rounded_Result (N)); + Set_Result (N, + Build_Multiply (N, + L => Fpt_Value (Expr), + R => Real_Literal (N, Ureal_1 / Small)), + Rchk => Rng_Check, + Trunc => not Rounded_Result (N) + and then not + (Debug_Flag_Dot_NN + and then Is_Ordinary_Fixed_Point_Type (Result_Type))); end if; end Expand_Convert_Float_To_Fixed; @@ -1852,13 +1854,14 @@ package body Exp_Fixd is -- Fall through to use floating-point for the close result set case, -- as a result of the numerator or denominator of the small value not - -- being a sufficiently small integer. + -- being sufficiently small. See also Expand_Convert_Float_To_Fixed. Set_Result (N, Build_Multiply (N, Fpt_Value (Expr), Real_Literal (N, Ureal_1 / Small)), - Rng_Check); + Rng_Check, + Trunc => not Rounded_Result (N)); end Expand_Convert_Integer_To_Fixed; -------------------------------- diff --git a/gcc/ada/sinfo.ads b/gcc/ada/sinfo.ads index e4fd6794f4b..0f698cd68ee 100644 --- a/gcc/ada/sinfo.ads +++ b/gcc/ada/sinfo.ads @@ -1329,8 +1329,9 @@ package Sinfo is -- to the entity for the first subtype. -- Float_Truncate - -- A flag present in type conversion nodes. This is used for float to - -- integer conversions where truncation is required rather than rounding. + -- A flag present in type conversion nodes. It is used for floating-point + -- to fixed-point or integer conversions, where truncation is required + -- rather than rounding. -- Forwards_OK -- A flag present in the N_Assignment_Statement node. It is used only