| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . W I D T H _ I -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 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 |
with Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 33 |
use Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 34 |
|
| 35 |
function System.Width_I (Lo, Hi : Int) return Natural is |
| 36 |
|
| 37 |
-- Ghost code, loop invariants and assertions in this unit are meant for |
| 38 |
-- analysis only, not for run-time checking, as it would be too costly |
| 39 |
-- otherwise. This is enforced by setting the assertion policy to Ignore. |
| 40 |
|
| 41 |
pragma Assertion_Policy (Ghost => Ignore, |
| 42 |
Loop_Invariant => Ignore, |
| 43 |
Assert => Ignore); |
| 44 |
|
| 45 |
----------------------- |
| 46 |
-- Local Subprograms -- |
| 47 |
----------------------- |
| 48 |
|
| 49 |
package Signed_Conversion is new Signed_Conversions (Int => Int); |
| 50 |
|
| 51 |
function Big (Arg : Int) return Big_Integer renames |
| 52 |
Signed_Conversion.To_Big_Integer; |
| 53 |
|
| 54 |
-- Maximum value of exponent for 10 that fits in Uns'Base |
| 55 |
function Max_Log10 return Natural is |
| 56 |
(case Int'Base'Size is |
| 57 |
when 8 => 2, |
| 58 |
when 16 => 4, |
| 59 |
when 32 => 9, |
| 60 |
when 64 => 19, |
| 61 |
when 128 => 38, |
| 62 |
when others => raise Program_Error) |
| 63 |
with Ghost; |
| 64 |
|
| 65 |
------------------ |
| 66 |
-- Local Lemmas -- |
| 67 |
------------------ |
| 68 |
|
| 69 |
procedure Lemma_Lower_Mult (A, B, C : Big_Natural) |
| 70 |
with |
| 71 |
Ghost, |
| 72 |
Pre => A <= B, |
| 73 |
Post => A * C <= B * C; |
| 74 |
|
| 75 |
procedure Lemma_Div_Commutation (X, Y : Int) |
| 76 |
with |
| 77 |
Ghost, |
| 78 |
Pre => X >= 0 and Y > 0, |
| 79 |
Post => Big (X) / Big (Y) = Big (X / Y); |
| 80 |
|
| 81 |
procedure Lemma_Div_Twice (X : Big_Natural; Y, Z : Big_Positive) |
| 82 |
with |
| 83 |
Ghost, |
| 84 |
Post => X / Y / Z = X / (Y * Z); |
| 85 |
|
| 86 |
---------------------- |
| 87 |
-- Lemma_Lower_Mult -- |
| 88 |
---------------------- |
| 89 |
|
| 90 |
procedure Lemma_Lower_Mult (A, B, C : Big_Natural) is null; |
| 91 |
|
| 92 |
--------------------------- |
| 93 |
-- Lemma_Div_Commutation -- |
| 94 |
--------------------------- |
| 95 |
|
| 96 |
procedure Lemma_Div_Commutation (X, Y : Int) is null; |
| 97 |
|
| 98 |
--------------------- |
| 99 |
-- Lemma_Div_Twice -- |
| 100 |
--------------------- |
| 101 |
|
| 102 |
procedure Lemma_Div_Twice (X : Big_Natural; Y, Z : Big_Positive) is |
| 103 |
XY : constant Big_Natural := X / Y; |
| 104 |
YZ : constant Big_Natural := Y * Z; |
| 105 |
XYZ : constant Big_Natural := X / Y / Z; |
| 106 |
R : constant Big_Natural := (XY rem Z) * Y + (X rem Y); |
| 107 |
begin |
| 108 |
pragma Assert (X = XY * Y + (X rem Y)); |
| 109 |
pragma Assert (XY = XY / Z * Z + (XY rem Z)); |
| 110 |
pragma Assert (X = XYZ * YZ + R); |
| 111 |
pragma Assert ((XY rem Z) * Y <= (Z - 1) * Y); |
| 112 |
pragma Assert (R <= YZ - 1); |
| 113 |
pragma Assert (X / YZ = (XYZ * YZ + R) / YZ); |
| 114 |
pragma Assert (X / YZ = XYZ + R / YZ); |
| 115 |
end Lemma_Div_Twice; |
| 116 |
|
| 117 |
-- Local variables |
| 118 |
|
| 119 |
W : Natural; |
| 120 |
T : Int; |
| 121 |
|
| 122 |
-- Local ghost variables |
| 123 |
|
| 124 |
Max_W : constant Natural := Max_Log10 with Ghost; |
| 125 |
Big_10 : constant Big_Integer := Big (10) with Ghost; |
| 126 |
|
| 127 |
Pow : Big_Integer := 1 with Ghost; |
| 128 |
T_Init : constant Int := |
| 129 |
Int'Max (abs Int'Max (Lo, Int'First + 1), |
| 130 |
abs Int'Max (Hi, Int'First + 1)) |
| 131 |
with Ghost; |
| 132 |
|
| 133 |
-- Start of processing for System.Width_I |
| 134 |
|
| 135 |
begin |
| 136 |
if Lo > Hi then |
| 137 |
return 0; |
| 138 |
|
| 139 |
else |
| 140 |
-- Minimum value is 2, one for sign, one for digit |
| 141 |
|
| 142 |
W := 2; |
| 143 |
|
| 144 |
-- Get max of absolute values, but avoid bomb if we have the maximum |
| 145 |
-- negative number (note that First + 1 has same digits as First) |
| 146 |
|
| 147 |
T := Int'Max ( |
| 148 |
abs Int'Max (Lo, Int'First + 1), |
| 149 |
abs Int'Max (Hi, Int'First + 1)); |
| 150 |
|
| 151 |
-- Increase value if more digits required |
| 152 |
|
| 153 |
while T >= 10 loop |
| 154 |
Lemma_Div_Commutation (T, 10); |
| 155 |
Lemma_Div_Twice (Big (T_Init), Big_10 ** (W - 2), Big_10); |
| 156 |
|
| 157 |
T := T / 10; |
| 158 |
W := W + 1; |
| 159 |
Pow := Pow * 10; |
| 160 |
|
| 161 |
pragma Loop_Invariant (T >= 0); |
| 162 |
pragma Loop_Invariant (W in 3 .. Max_W + 3); |
| 163 |
pragma Loop_Invariant (Pow = Big_10 ** (W - 2)); |
| 164 |
pragma Loop_Invariant (Big (T) = Big (T_Init) / Pow); |
| 165 |
pragma Loop_Variant (Decreases => T); |
| 166 |
end loop; |
| 167 |
|
| 168 |
declare |
| 169 |
F : constant Big_Positive := Big_10 ** (W - 2) with Ghost; |
| 170 |
Q : constant Big_Natural := Big (T_Init) / F with Ghost; |
| 171 |
R : constant Big_Natural := Big (T_Init) rem F with Ghost; |
| 172 |
begin |
| 173 |
pragma Assert (Q < Big_10); |
| 174 |
pragma Assert (Big (T_Init) = Q * F + R); |
| 175 |
Lemma_Lower_Mult (Q, Big (9), F); |
| 176 |
pragma Assert (Big (T_Init) <= Big (9) * F + F - 1); |
| 177 |
pragma Assert (Big (T_Init) < Big_10 * F); |
| 178 |
pragma Assert (Big_10 * F = Big_10 ** (W - 1)); |
| 179 |
end; |
| 180 |
|
| 181 |
-- This is an expression of the functional postcondition for Width_I, |
| 182 |
-- which cannot be expressed readily as a postcondition as this would |
| 183 |
-- require making the instantiation Signed_Conversion and function Big |
| 184 |
-- available from the spec. |
| 185 |
|
| 186 |
pragma Assert (Big (Int'Max (Lo, Int'First + 1)) < Big_10 ** (W - 1)); |
| 187 |
pragma Assert (Big (Int'Max (Hi, Int'First + 1)) < Big_10 ** (W - 1)); |
| 188 |
|
| 189 |
return W; |
| 190 |
end if; |
| 191 |
|
| 192 |
end System.Width_I; |