| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . W I D T H _ U -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- Preconditions in this unit are meant for analysis only, not for run-time |
| 33 |
-- checking, so that the expected exceptions are raised. This is enforced by |
| 34 |
-- setting the corresponding assertion policy to Ignore. Postconditions and |
| 35 |
-- contract cases should not be executed at runtime as well, in order not to |
| 36 |
-- slow down the execution of these functions. |
| 37 |
|
| 38 |
pragma Assertion_Policy (Pre => Ignore, |
| 39 |
Post => Ignore, |
| 40 |
Contract_Cases => Ignore, |
| 41 |
Ghost => Ignore, |
| 42 |
Subprogram_Variant => Ignore); |
| 43 |
|
| 44 |
-- Compute Width attribute for non-static type derived from a modular integer |
| 45 |
-- type. The arguments Lo, Hi are the bounds of the type. |
| 46 |
|
| 47 |
with Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 48 |
|
| 49 |
generic |
| 50 |
|
| 51 |
type Uns is mod <>; |
| 52 |
|
| 53 |
package System.Width_U |
| 54 |
with Pure |
| 55 |
is |
| 56 |
package BI_Ghost renames Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 57 |
subtype Big_Integer is BI_Ghost.Big_Integer with Ghost; |
| 58 |
subtype Big_Natural is BI_Ghost.Big_Natural with Ghost; |
| 59 |
subtype Big_Positive is BI_Ghost.Big_Positive with Ghost; |
| 60 |
use type BI_Ghost.Big_Integer; |
| 61 |
|
| 62 |
package Unsigned_Conversion is |
| 63 |
new BI_Ghost.Unsigned_Conversions (Int => Uns); |
| 64 |
|
| 65 |
function Big (Arg : Uns) return Big_Integer renames |
| 66 |
Unsigned_Conversion.To_Big_Integer; |
| 67 |
|
| 68 |
Big_10 : constant Big_Integer := Big (Uns'(10)) with Ghost; |
| 69 |
|
| 70 |
-- Maximum value of exponent for 10 that fits in Uns'Base |
| 71 |
function Max_Log10 return Natural is |
| 72 |
(case Uns'Base'Size is |
| 73 |
when 8 => 2, |
| 74 |
when 16 => 4, |
| 75 |
when 32 => 9, |
| 76 |
when 64 => 19, |
| 77 |
when 128 => 38, |
| 78 |
when others => raise Program_Error) |
| 79 |
with Ghost; |
| 80 |
|
| 81 |
function Width (Lo, Hi : Uns) return Natural |
| 82 |
with |
| 83 |
Post => |
| 84 |
(declare |
| 85 |
W : constant Natural := System.Width_U.Width'Result; |
| 86 |
begin |
| 87 |
(if Lo > Hi then W = 0 |
| 88 |
else W > 0 |
| 89 |
and then W <= Max_Log10 + 2 |
| 90 |
and then Big (Lo) < Big_10 ** (W - 1) |
| 91 |
and then Big (Hi) < Big_10 ** (W - 1))); |
| 92 |
|
| 93 |
end System.Width_U; |