| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . I M A G E _ F -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2020-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 System.Image_I; |
| 33 |
with System.Img_Util; use System.Img_Util; |
| 34 |
with System.Value_I_Spec; |
| 35 |
with System.Value_U_Spec; |
| 36 |
|
| 37 |
package body System.Image_F is |
| 38 |
|
| 39 |
-- Contracts, ghost code, loop invariants and assertions in this unit are |
| 40 |
-- meant for analysis only, not for run-time checking, as it would be too |
| 41 |
-- costly otherwise. This is enforced by setting the assertion policy to |
| 42 |
-- Ignore. |
| 43 |
|
| 44 |
pragma Assertion_Policy (Assert => Ignore, |
| 45 |
Assert_And_Cut => Ignore, |
| 46 |
Contract_Cases => Ignore, |
| 47 |
Ghost => Ignore, |
| 48 |
Loop_Invariant => Ignore, |
| 49 |
Pre => Ignore, |
| 50 |
Post => Ignore, |
| 51 |
Subprogram_Variant => Ignore); |
| 52 |
|
| 53 |
Maxdigs : constant Natural := Int'Width - 2; |
| 54 |
-- Maximum number of decimal digits that can be represented in an Int. |
| 55 |
-- The "-2" accounts for the sign and one extra digit, since we need the |
| 56 |
-- maximum number of 9's that can be represented, e.g. for the 64-bit case, |
| 57 |
-- Integer_64'Width is 20 since the maximum value is approximately 9.2E+18 |
| 58 |
-- and has 19 digits, but the maximum number of 9's that can be represented |
| 59 |
-- in Integer_64 is only 18. |
| 60 |
|
| 61 |
-- The first prerequisite of the implementation is that the first scaled |
| 62 |
-- divide does not overflow, which means that the absolute value of the |
| 63 |
-- input X must always be smaller than 10**Maxdigs * 2**(Int'Size - 1). |
| 64 |
-- Otherwise Constraint_Error is raised by the scaled divide operation. |
| 65 |
|
| 66 |
-- The second prerequisite of the implementation is that the computation |
| 67 |
-- of the operands does not overflow when the small is neither an integer |
| 68 |
-- nor the reciprocal of an integer, which means that its numerator and |
| 69 |
-- denominator must be smaller than 10**(2*Maxdigs-1) / 2**(Int'Size - 1) |
| 70 |
-- if the small is larger than 1, and smaller than 2**(Int'Size - 1) / 10 |
| 71 |
-- if the small is smaller than 1. |
| 72 |
|
| 73 |
package Uns_Spec is new System.Value_U_Spec (Uns); |
| 74 |
package Int_Spec is new System.Value_I_Spec (Int, Uns, Uns_Spec); |
| 75 |
|
| 76 |
package Image_I is new System.Image_I |
| 77 |
(Int => Int, |
| 78 |
Uns => Uns, |
| 79 |
U_Spec => Uns_Spec, |
| 80 |
I_Spec => Int_Spec); |
| 81 |
|
| 82 |
procedure Set_Image_Integer |
| 83 |
(V : Int; |
| 84 |
S : in out String; |
| 85 |
P : in out Natural) |
| 86 |
renames Image_I.Set_Image_Integer; |
| 87 |
|
| 88 |
-- The following section describes a specific implementation choice for |
| 89 |
-- performing base conversions needed for output of values of a fixed |
| 90 |
-- point type T with small T'Small. The goal is to be able to output |
| 91 |
-- all values of fixed point types with a precision of 64 bits and a |
| 92 |
-- small in the range 2.0**(-63) .. 2.0**63. The reasoning can easily |
| 93 |
-- be adapted to fixed point types with a precision of 32 or 128 bits. |
| 94 |
|
| 95 |
-- The chosen algorithm uses fixed precision integer arithmetic for |
| 96 |
-- reasons of simplicity and efficiency. It is important to understand |
| 97 |
-- in what ways the most simple and accurate approach to fixed point I/O |
| 98 |
-- is limiting, before considering more complicated schemes. |
| 99 |
|
| 100 |
-- Without loss of generality assume T has a range (-2.0**63) * T'Small |
| 101 |
-- .. (2.0**63 - 1) * T'Small, and is output with Aft digits after the |
| 102 |
-- decimal point and T'Fore - 1 before. If T'Small is integer, or |
| 103 |
-- 1.0 / T'Small is integer, let S = T'Small. |
| 104 |
|
| 105 |
-- The idea is to convert a value X * S of type T to a 64-bit integer value |
| 106 |
-- Q equal to 10.0**D * (X * S) rounded to the nearest integer, using only |
| 107 |
-- a scaled integer divide of the form |
| 108 |
|
| 109 |
-- Q = (X * Y) / Z, |
| 110 |
|
| 111 |
-- where the variables X, Y, Z are 64-bit integers, and both multiplication |
| 112 |
-- and division are done using full intermediate precision. Then the final |
| 113 |
-- decimal value to be output is |
| 114 |
|
| 115 |
-- Q * 10**(-D) |
| 116 |
|
| 117 |
-- This value can be written to the output file or to the result string |
| 118 |
-- according to the format described in RM A.10.9. The details of this |
| 119 |
-- operation are omitted here. |
| 120 |
|
| 121 |
-- A 64-bit value can represent all integers with 18 decimal digits, but |
| 122 |
-- not all with 19 decimal digits. If the total number of requested output |
| 123 |
-- digits (Fore - 1) + Aft is greater than 18 then, for purposes of the |
| 124 |
-- conversion, Aft is adjusted to 18 - (Fore - 1). In that case, trailing |
| 125 |
-- zeros can complete the output after writing the first 18 significant |
| 126 |
-- digits, or the technique described in the next section can be used. |
| 127 |
-- In addition, D cannot be smaller than -18, in order for 10.0**(-D) to |
| 128 |
-- fit in a 64-bit integer. |
| 129 |
|
| 130 |
-- The final expression for D is |
| 131 |
|
| 132 |
-- D = Integer'Max (-18, Integer'Min (Aft, 18 - (Fore - 1))); |
| 133 |
|
| 134 |
-- For Y and Z the following expressions can be derived: |
| 135 |
|
| 136 |
-- Q = X * S * (10.0**D) = (X * Y) / Z |
| 137 |
|
| 138 |
-- If S is an integer greater than or equal to one, then Fore must be at |
| 139 |
-- least 20 in order to print T'First, which is at most -2.0**63. This |
| 140 |
-- means that D < 0, so use |
| 141 |
|
| 142 |
-- (1) Y = -S and Z = -10**(-D) |
| 143 |
|
| 144 |
-- If 1.0 / S is an integer greater than one, use |
| 145 |
|
| 146 |
-- (2) Y = -10**D and Z = -(1.0 / S), for D >= 0 |
| 147 |
|
| 148 |
-- or |
| 149 |
|
| 150 |
-- (3) Y = -1 and Z = -(1.0 / S) * 10**(-D), for D < 0 |
| 151 |
|
| 152 |
-- Negative values are used for nominator Y and denominator Z, so that S |
| 153 |
-- can have a maximum value of 2.0**63 and a minimum of 2.0**(-63). For |
| 154 |
-- -(1.0 / S) in -1 .. -9, Fore will still be 20, and D will be negative, |
| 155 |
-- as (-2.0**63) / -9 is greater than 10**18. In these cases there is room |
| 156 |
-- in the denominator for the extra decimal scaling required, so case (3) |
| 157 |
-- will not overflow. |
| 158 |
|
| 159 |
-- In fact this reasoning can be generalized to most S which are the ratio |
| 160 |
-- of two integers with bounded magnitude. Let S = Num / Den and rewrite |
| 161 |
-- case (1) above where Den = 1 into |
| 162 |
|
| 163 |
-- (1b) Y = -Num and Z = -Den * 10**(-D) |
| 164 |
|
| 165 |
-- Suppose that Num corresponds to the maximum value of -D, i.e. 18 and |
| 166 |
-- 10**(-D) = 10**18. If you change Den into 10, then S becomes 10 times |
| 167 |
-- smaller and, therefore, Fore is decremented by 1, which means that -D |
| 168 |
-- is as well, provided that Fore was initially not larger than 37, so the |
| 169 |
-- multiplication for Z still does not overflow. But you need to reach 10 |
| 170 |
-- to trigger this effect, which means that a leeway of 10 is required, so |
| 171 |
-- let's restrict this to a Num for which 10**(-D) <= 10**17. To summarize |
| 172 |
-- this case, if S is the ratio of two integers with |
| 173 |
|
| 174 |
-- Den < Num <= B1 |
| 175 |
|
| 176 |
-- where B1 is a fixed limit, then case (1b) does not overflow. B1 can be |
| 177 |
-- taken as the largest integer Small such that D = -17, i.e. Fore = 36, |
| 178 |
-- which means that B1 * 2.0**63 must be smaller than 10**35. |
| 179 |
|
| 180 |
-- Let's continue and rewrite case (2) above when Num = 1 into |
| 181 |
|
| 182 |
-- (2b) Y = -Num * 10**D and Z = -Den, for D >= 0 |
| 183 |
|
| 184 |
-- Note that D <= 18 - (Fore - 1) and Fore >= 2 so D <= 17, thus you can |
| 185 |
-- safely change Num into 10 in the product, but then S becomes 10 times |
| 186 |
-- larger and, therefore, Fore is incremented by 1, which means that D is |
| 187 |
-- decremented by 1 so you again have a product lesser or equal to 10**17. |
| 188 |
-- To sum up, if S is the ratio of two integers with |
| 189 |
|
| 190 |
-- Num <= Den * S0 |
| 191 |
|
| 192 |
-- where S0 is the largest Small such that D >= 0, then case (2b) does not |
| 193 |
-- overflow. |
| 194 |
|
| 195 |
-- Let's conclude and rewrite case (3) above when Num = 1 into |
| 196 |
|
| 197 |
-- (3b) Y = -Num and Z = -Den * 10**(-D), for D < 0 |
| 198 |
|
| 199 |
-- As explained above, this occurs only if both S0 < S < 1 and D = -1 and |
| 200 |
-- is preserved if you scale up Num and Den simultaneously, what you can |
| 201 |
-- do until Den * 10 tops the upper bound. To sum up, if S is the ratio of |
| 202 |
-- two integers with |
| 203 |
|
| 204 |
-- Den * S0 < Num < Den <= B2 |
| 205 |
|
| 206 |
-- where B2 is a fixed limit, then case (3b) does not overflow. B2 can be |
| 207 |
-- taken as the largest integer such that B2 * 10 is smaller than 2.0**63. |
| 208 |
|
| 209 |
-- The conclusion is that the algorithm works if the small is the ratio of |
| 210 |
-- two integers in the range 1 .. 2**63 if either is equal to 1, or of two |
| 211 |
-- integers in the range 1 .. B1 if the small is larger than 1, or of two |
| 212 |
-- integers in the range 1 .. B2 if the small is smaller than 1. |
| 213 |
|
| 214 |
-- Using a scaled divide which truncates and returns a remainder R, |
| 215 |
-- another K trailing digits can be calculated by computing the value |
| 216 |
-- (R * (10.0**K)) / Z using another scaled divide. This procedure |
| 217 |
-- can be repeated to compute an arbitrary number of digits in linear |
| 218 |
-- time and storage. The last scaled divide should be rounded, with |
| 219 |
-- a possible carry propagating to the more significant digits, to |
| 220 |
-- ensure correct rounding of the unit in the last place. |
| 221 |
|
| 222 |
----------------- |
| 223 |
-- Image_Fixed -- |
| 224 |
----------------- |
| 225 |
|
| 226 |
procedure Image_Fixed |
| 227 |
(V : Int; |
| 228 |
S : in out String; |
| 229 |
P : out Natural; |
| 230 |
Num : Int; |
| 231 |
Den : Int; |
| 232 |
For0 : Natural; |
| 233 |
Aft0 : Natural) |
| 234 |
is |
| 235 |
pragma Assert (S'First = 1); |
| 236 |
|
| 237 |
begin |
| 238 |
-- Add space at start for non-negative numbers |
| 239 |
|
| 240 |
if V >= 0 then |
| 241 |
S (1) := ' '; |
| 242 |
P := 1; |
| 243 |
else |
| 244 |
P := 0; |
| 245 |
end if; |
| 246 |
|
| 247 |
Set_Image_Fixed (V, S, P, Num, Den, For0, Aft0, 1, Aft0, 0); |
| 248 |
end Image_Fixed; |
| 249 |
|
| 250 |
--------------------- |
| 251 |
-- Set_Image_Fixed -- |
| 252 |
--------------------- |
| 253 |
|
| 254 |
procedure Set_Image_Fixed |
| 255 |
(V : Int; |
| 256 |
S : in out String; |
| 257 |
P : in out Natural; |
| 258 |
Num : Int; |
| 259 |
Den : Int; |
| 260 |
For0 : Natural; |
| 261 |
Aft0 : Natural; |
| 262 |
Fore : Natural; |
| 263 |
Aft : Natural; |
| 264 |
Exp : Natural) |
| 265 |
is |
| 266 |
pragma Assert (Num < 0 and then Den < 0); |
| 267 |
-- Accept only negative numbers to allow -2**(Int'Size - 1) |
| 268 |
|
| 269 |
A : constant Natural := |
| 270 |
Boolean'Pos (Exp > 0) * Aft0 + Natural'Max (Aft, 1) + 1; |
| 271 |
-- Number of digits after the decimal point to be computed. If Exp is |
| 272 |
-- positive, we need to compute Aft decimal digits after the first non |
| 273 |
-- zero digit and we are guaranteed there is at least one in the first |
| 274 |
-- Aft0 digits (unless V is zero). In both cases, we compute one more |
| 275 |
-- digit than requested so that Set_Decimal_Digits can round at Aft. |
| 276 |
|
| 277 |
-- Aft0 is bounded by the 'Aft of a type with delta 1/2**(Int'Size - 1) |
| 278 |
-- which is N = ceil ((Int'Siz - 1) * log2 / log10). Aft lies in the |
| 279 |
-- range of type Field declared in Ada.Text_IO so is bounded by 255. |
| 280 |
-- Thus A is bounded by 256 + ceil ((Int'Siz - 1) * log2 / log10). |
| 281 |
|
| 282 |
D : constant Integer := |
| 283 |
Integer'Max (-Maxdigs, Integer'Min (A, Maxdigs - (For0 - 1))); |
| 284 |
-- D lies in the range -Maxdigs .. A |
| 285 |
|
| 286 |
Y : constant Int := Num * 10**Integer'Max (0, D); |
| 287 |
Z : constant Int := Den * 10**Integer'Max (0, -D); |
| 288 |
-- See the description of the algorithm above |
| 289 |
|
| 290 |
AF : constant Natural := A - D; |
| 291 |
-- Number of remaining digits to be computed after the first round. It |
| 292 |
-- is larger than A if the first round does not compute all the digits |
| 293 |
-- before the decimal point, i.e. (For0 - 1) larger than Maxdigs. |
| 294 |
|
| 295 |
-- AF is bounded by 256 + Maxdigs + ceil ((Int'Siz - 1) * log2 / log10) |
| 296 |
|
| 297 |
N : constant Natural := 1 + (AF + Maxdigs - 1) / Maxdigs; |
| 298 |
-- Number of rounds of scaled divide to be performed |
| 299 |
|
| 300 |
Q : Int; |
| 301 |
-- Quotient of the scaled divide in this round. Only the first round may |
| 302 |
-- yield more than Maxdigs digits and has a significant sign. |
| 303 |
|
| 304 |
Buf : String (1 .. Maxdigs); |
| 305 |
Len : Natural; |
| 306 |
-- Buffer for the image of the quotient |
| 307 |
|
| 308 |
Digs : String (1 .. 2 + N * Maxdigs); |
| 309 |
Ndigs : Natural; |
| 310 |
-- Concatenated image of the successive quotients |
| 311 |
|
| 312 |
Scale : Integer := 0; |
| 313 |
-- Exponent such that the result is Digs (1 .. NDigs) * 10**(-Scale) |
| 314 |
|
| 315 |
XX : Int := V; |
| 316 |
YY : Int := Y; |
| 317 |
-- First two operands of the scaled divide |
| 318 |
|
| 319 |
J : Natural; |
| 320 |
-- Loop index |
| 321 |
|
| 322 |
begin |
| 323 |
-- Set the first character like Image |
| 324 |
|
| 325 |
if V >= 0 then |
| 326 |
Digs (1) := ' '; |
| 327 |
Ndigs := 1; |
| 328 |
else |
| 329 |
Ndigs := 0; |
| 330 |
end if; |
| 331 |
|
| 332 |
-- First round of scaled divide |
| 333 |
|
| 334 |
if XX /= 0 then |
| 335 |
Scaled_Divide (XX, YY, Z, Q, R => XX, Round => False); |
| 336 |
if Q /= 0 then |
| 337 |
Set_Image_Integer (Q, Digs, Ndigs); |
| 338 |
end if; |
| 339 |
|
| 340 |
Scale := Scale + D; |
| 341 |
|
| 342 |
-- Prepare for next round, if any |
| 343 |
|
| 344 |
YY := 10**Maxdigs; |
| 345 |
end if; |
| 346 |
|
| 347 |
J := 2; |
| 348 |
while J <= N and then XX /= 0 loop |
| 349 |
Scaled_Divide (XX, YY, Z, Q, R => XX, Round => False); |
| 350 |
|
| 351 |
pragma Assert (-10**Maxdigs < Q and then Q < 10**Maxdigs); |
| 352 |
|
| 353 |
Len := 0; |
| 354 |
Set_Image_Integer (abs Q, Buf, Len); |
| 355 |
|
| 356 |
pragma Assert (1 <= Len and then Len <= Maxdigs); |
| 357 |
|
| 358 |
-- If no character but the space has been written, write the |
| 359 |
-- minus if need be, since Set_Image_Integer did not do it. |
| 360 |
|
| 361 |
if Ndigs <= 1 then |
| 362 |
if Q /= 0 then |
| 363 |
if Ndigs = 0 then |
| 364 |
Digs (1) := '-'; |
| 365 |
end if; |
| 366 |
|
| 367 |
Digs (2 .. Len + 1) := Buf (1 .. Len); |
| 368 |
Ndigs := Len + 1; |
| 369 |
end if; |
| 370 |
|
| 371 |
-- Or else pad the output with zeroes up to Maxdigs |
| 372 |
|
| 373 |
else |
| 374 |
for K in 1 .. Maxdigs - Len loop |
| 375 |
Digs (Ndigs + K) := '0'; |
| 376 |
end loop; |
| 377 |
|
| 378 |
for K in 1 .. Len loop |
| 379 |
Digs (Ndigs + Maxdigs - Len + K) := Buf (K); |
| 380 |
end loop; |
| 381 |
|
| 382 |
Ndigs := Ndigs + Maxdigs; |
| 383 |
end if; |
| 384 |
|
| 385 |
Scale := Scale + Maxdigs; |
| 386 |
J := J + 1; |
| 387 |
end loop; |
| 388 |
|
| 389 |
-- If no digit was output, this is zero |
| 390 |
|
| 391 |
if Ndigs <= 1 then |
| 392 |
Digs (1 .. 2) := " 0"; |
| 393 |
Ndigs := 2; |
| 394 |
end if; |
| 395 |
pragma Annotate (CodePeer, False_Positive, "test always true", |
| 396 |
"no digits were output for zero"); |
| 397 |
|
| 398 |
Set_Decimal_Digits (Digs, Ndigs, S, P, Scale, Fore, Aft, Exp); |
| 399 |
end Set_Image_Fixed; |
| 400 |
|
| 401 |
end System.Image_F; |