]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Add Type_Size_For function to Uintp package
authorEric Botcazou <ebotcazou@adacore.com>
Fri, 13 Sep 2024 09:53:00 +0000 (11:53 +0200)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Fri, 25 Oct 2024 09:09:01 +0000 (11:09 +0200)
It computes the size of an integer type that can accommodate the input.

gcc/ada/ChangeLog:

* uintp.ads (Type_Size_For): New function declaration.
* uintp.adb (Type_Size_For): New function body.
* exp_imgv.adb (Rewrite_Object_Image): Call Type_Size_For to get
the size of a narrower integer type.

gcc/ada/exp_imgv.adb
gcc/ada/uintp.adb
gcc/ada/uintp.ads

index 20afebc061c0201d3cacb4c368147901cfe9e7fe..c95c46adbc029d87ae41e1c4b562ca4da9bf36f0 100644 (file)
@@ -2539,13 +2539,12 @@ package body Exp_Imgv is
 
          elsif Nkind (P) = N_Integer_Literal then
             declare
-               Val  : constant Uint    := Intval (P);
-               Neg  : constant Boolean := Val < Uint_0;
-               Bits : constant Nat     := Num_Bits (Val) + Boolean'Pos (Neg);
+               Val  : constant Uint := Intval (P);
+               Siz  : constant Nat  := Type_Size_For (Val);
 
             begin
-               if Bits <= System_Max_Integer_Size then
-                  Ptyp := Integer_Type_For (UI_From_Int (Bits), not Neg);
+               if Siz <= System_Max_Integer_Size then
+                  Ptyp := Integer_Type_For (UI_From_Int (Siz), Val >= Uint_0);
                end if;
             end;
          end if;
index 1957928f7f6f4d9e61be3c246067fc959e63665d..fc548e02d57018155ac2bdb73914009c7127610a 100644 (file)
@@ -758,6 +758,19 @@ package body Uintp is
       end if;
    end Release_And_Save;
 
+   --------------------
+   --  Type_Size_For --
+   --------------------
+
+   function Type_Size_For (Input : Valid_Uint) return Nat is
+      Neg  : constant Boolean := Input < Uint_0;
+
+   begin
+      --  Num_Bits is correct only for nonnegative values
+
+      return Num_Bits (Input) + Boolean'Pos (Neg);
+   end Type_Size_For;
+
    -------------
    -- UI_Abs --
    -------------
index c80b494718549585cbe6c58681d647073a3d2d02..2676ff51d30d29fa13de314b743303fc5a7ff63f 100644 (file)
@@ -264,6 +264,9 @@ package Uintp is
    --  function is used for capacity checks, and it can be one bit off
    --  without affecting its usage.
 
+   function Type_Size_For (Input : Valid_Uint) return Nat;
+   --  Returns the size of an integer type that can accommodate Input
+
    ---------------------
    -- Output Routines --
    ---------------------