From patchwork Mon Nov 30 14:17:12 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: 1408330 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=8.43.85.97; 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 (unknown [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 ozlabs.org (Postfix) with ESMTPS id 4Cl6lk0BgTz9s0b for ; Tue, 1 Dec 2020 01:18:26 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 875563896835; Mon, 30 Nov 2020 14:17:25 +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 255F23892026 for ; Mon, 30 Nov 2020 14:17:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 255F23892026 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 232D4561F0; Mon, 30 Nov 2020 09:17:12 -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 8oTHQBpZBOIE; Mon, 30 Nov 2020 09:17:12 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id 04507561E4; Mon, 30 Nov 2020 09:17:12 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4862) id 03857CB; Mon, 30 Nov 2020 09:17:12 -0500 (EST) Date: Mon, 30 Nov 2020 09:17:12 -0500 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Subject: [Ada] Reimplement Ada.Numerics.Big_Numbers.Big_Reals.Fixed_Conversions Message-ID: <20201130141711.GA117852@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-11.2 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" This reimplements the aforementioned generic package according to the requirements of the Ada 2020 RM, namely that To_Big_Real be exact and that From_Big_Real use the common conversion rules. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * libgnat/a-nbnbre.adb (Float_Conversions): Instantiate Conv package only once in the body. (Fixed_Conversions.Float_Aux): New instance. (Fixed_Conversions.Conv_I): Likewise. (Fixed_Conversions.Conv_U): Likewise. (Fixed_Conversions.LLLI): New subtype. (Fixed_Conversions.LLLU): Likewise. (Fixed_Conversions.Too_Large): New constant. (Fixed_Conversions.To_Big_Real): Reimplement. (Fixed_Conversions.From_Big_Real): Likewise. diff --git a/gcc/ada/libgnat/a-nbnbre.adb b/gcc/ada/libgnat/a-nbnbre.adb --- a/gcc/ada/libgnat/a-nbnbre.adb +++ b/gcc/ada/libgnat/a-nbnbre.adb @@ -118,6 +118,9 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is package body Float_Conversions is + package Conv is new + Big_Integers.Unsigned_Conversions (Long_Long_Unsigned); + ----------------- -- To_Big_Real -- ----------------- @@ -130,9 +133,6 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is function To_Big_Real (Arg : Num) return Valid_Big_Real is - package Conv is new - Big_Integers.Unsigned_Conversions (Long_Long_Unsigned); - A : constant Num'Base := abs (Arg); E : constant Integer := Num'Exponent (A); F : constant Num'Base := Num'Fraction (A); @@ -182,9 +182,6 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is function From_Big_Real (Arg : Big_Real) return Num is - package Conv is new - Big_Integers.Unsigned_Conversions (Long_Long_Unsigned); - M : constant Natural := Num'Machine_Mantissa; One : constant Big_Real := To_Real (1); Two : constant Big_Real := To_Real (2); @@ -310,22 +307,78 @@ package body Ada.Numerics.Big_Numbers.Big_Reals is package body Fixed_Conversions is + package Float_Aux is new Float_Conversions (Long_Long_Float); + + subtype LLLI is Long_Long_Long_Integer; + subtype LLLU is Long_Long_Long_Unsigned; + + Too_Large : constant Boolean := + Num'Small_Numerator > LLLU'Last + or else Num'Small_Denominator > LLLU'Last; + -- True if the Small is too large for Long_Long_Long_Unsigned, in which + -- case we convert to/from Long_Long_Float as an intermediate step. + + package Conv_I is new Big_Integers.Signed_Conversions (LLLI); + package Conv_U is new Big_Integers.Unsigned_Conversions (LLLU); + ----------------- -- To_Big_Real -- ----------------- + -- We just compute V * N / D where V is the mantissa value of the fixed + -- point number, and N resp. D is the numerator resp. the denominator of + -- the Small of the fixed-point type. + function To_Big_Real (Arg : Num) return Valid_Big_Real is + N, D, V : Big_Integer; + begin - return From_String (Arg'Image); + if Too_Large then + return Float_Aux.To_Big_Real (Long_Long_Float (Arg)); + end if; + + N := Conv_U.To_Big_Integer (Num'Small_Numerator); + D := Conv_U.To_Big_Integer (Num'Small_Denominator); + V := Conv_I.To_Big_Integer (LLLI'Integer_Value (Arg)); + + return V * N / D; end To_Big_Real; ------------------- -- From_Big_Real -- ------------------- + -- We first compute A / B = Arg * D / N where N resp. D is the numerator + -- resp. the denominator of the Small of the fixed-point type. Then we + -- divide A by B and convert the result to the mantissa value. + function From_Big_Real (Arg : Big_Real) return Num is + N, D, A, B, Q, X : Big_Integer; + begin - return Num'Value (To_String (Arg)); + if Too_Large then + return Num (Float_Aux.From_Big_Real (Arg)); + end if; + + N := Conv_U.To_Big_Integer (Num'Small_Numerator); + D := Conv_U.To_Big_Integer (Num'Small_Denominator); + A := Numerator (Arg) * D; + B := Denominator (Arg) * N; + + Q := A / B; + + -- Round to nearest, ties to away, by comparing twice the remainder + + X := (A - Q * B) * To_Big_Integer (2); + + if X >= B then + Q := Q + To_Big_Integer (1); + + elsif X <= -B then + Q := Q - To_Big_Integer (1); + end if; + + return Num'Fixed_Value (Conv_I.From_Big_Integer (Q)); end From_Big_Real; end Fixed_Conversions;