| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . I M A G E _ R -- |
| 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 System.Double_Real; |
| 33 |
with System.Float_Control; |
| 34 |
with System.Img_Util; use System.Img_Util; |
| 35 |
|
| 36 |
package body System.Image_R is |
| 37 |
|
| 38 |
-- The following defines the maximum number of digits that we can convert |
| 39 |
-- accurately. This is limited by the precision of the Num type, and also |
| 40 |
-- by the number of digits that can be held in the Uns type, which is the |
| 41 |
-- integer type we use as an intermediate in the computation. But, in both |
| 42 |
-- cases, we can work with a double value in these types. |
| 43 |
|
| 44 |
-- Note that in the following, the "-2" accounts for the space and one |
| 45 |
-- extra digit, since we need the maximum number of 9's that can be |
| 46 |
-- represented, e.g. for the 64-bit case, Long_Long_Unsigned'Width is |
| 47 |
-- 21, since the maximum value (approx 1.8E+19) has 20 digits, but the |
| 48 |
-- maximum number of 9's that can be represented is only 19. |
| 49 |
|
| 50 |
Maxdigs : constant Natural := 2 * Natural'Min (Uns'Width - 2, Num'Digits); |
| 51 |
|
| 52 |
Maxscaling : constant Natural := 5000 + Maxdigs; |
| 53 |
-- Maximum decimal scaling required during conversion of floating-point |
| 54 |
-- numbers to decimal. This is used to defend against infinite looping |
| 55 |
-- during the conversion, that could be caused by erroneous execution. |
| 56 |
-- The largest decimal exponent in absolute value used on any current |
| 57 |
-- system is 4966 (denormals of IEEE binary128) and we scale up to the |
| 58 |
-- Maxdigs exponent during the conversion. |
| 59 |
|
| 60 |
package Double_Real is new System.Double_Real (Num); |
| 61 |
use type Double_Real.Double_T; |
| 62 |
|
| 63 |
subtype Double_T is Double_Real.Double_T; |
| 64 |
-- The double floating-point type |
| 65 |
|
| 66 |
function From_Unsigned is new Double_Real.From_Unsigned (Uns); |
| 67 |
function To_Unsigned is new Double_Real.To_Unsigned (Uns); |
| 68 |
-- Convert betwwen a double Num and a single Uns |
| 69 |
|
| 70 |
function Is_Negative (V : Num) return Boolean; |
| 71 |
-- Return True if V is negative for the purpose of the output, i.e. return |
| 72 |
-- True for negative zeros only if Signed_Zeros is True. |
| 73 |
|
| 74 |
----------------------- |
| 75 |
-- Image_Fixed_Point -- |
| 76 |
----------------------- |
| 77 |
|
| 78 |
procedure Image_Fixed_Point |
| 79 |
(V : Num; |
| 80 |
S : in out String; |
| 81 |
P : out Natural; |
| 82 |
Aft : Natural) |
| 83 |
is |
| 84 |
pragma Assert (S'First = 1); |
| 85 |
|
| 86 |
begin |
| 87 |
-- Output space at start if non-negative |
| 88 |
|
| 89 |
if V >= 0.0 then |
| 90 |
S (1) := ' '; |
| 91 |
P := 1; |
| 92 |
else |
| 93 |
P := 0; |
| 94 |
end if; |
| 95 |
|
| 96 |
Set_Image_Real (V, S, P, 1, Aft, 0); |
| 97 |
end Image_Fixed_Point; |
| 98 |
|
| 99 |
-------------------------- |
| 100 |
-- Image_Floating_Point -- |
| 101 |
-------------------------- |
| 102 |
|
| 103 |
procedure Image_Floating_Point |
| 104 |
(V : Num; |
| 105 |
S : in out String; |
| 106 |
P : out Natural; |
| 107 |
Digs : Natural) |
| 108 |
is |
| 109 |
pragma Assert (S'First = 1); |
| 110 |
|
| 111 |
Fore : Natural; |
| 112 |
begin |
| 113 |
-- Decide whether a blank should be prepended before the call to |
| 114 |
-- Set_Image_Real. We generate a blank for positive values, and |
| 115 |
-- also for positive zeros. For negative zeros, we generate a |
| 116 |
-- blank only if Signed_Zeros is False (the RM only permits the |
| 117 |
-- output of -0.0 when Signed_Zeros is True). We do not generate |
| 118 |
-- a blank for positive infinity, since we output an explicit +. |
| 119 |
|
| 120 |
if not Is_Negative (V) and then V <= Num'Last then |
| 121 |
pragma Annotate (CodePeer, False_Positive, "condition predetermined", |
| 122 |
"CodePeer analysis ignores NaN and Inf values"); |
| 123 |
pragma Assert (S'Last > 1); |
| 124 |
-- The caller is responsible for S to be large enough for all |
| 125 |
-- Image_Floating_Point operation. |
| 126 |
S (1) := ' '; |
| 127 |
P := 1; |
| 128 |
Fore := 1; |
| 129 |
else |
| 130 |
P := 0; |
| 131 |
Fore := 2; |
| 132 |
end if; |
| 133 |
|
| 134 |
Set_Image_Real (V, S, P, Fore, Digs - 1, 3); |
| 135 |
end Image_Floating_Point; |
| 136 |
|
| 137 |
----------------- |
| 138 |
-- Is_Negative -- |
| 139 |
----------------- |
| 140 |
|
| 141 |
function Is_Negative (V : Num) return Boolean is |
| 142 |
begin |
| 143 |
if V < 0.0 then |
| 144 |
return True; |
| 145 |
|
| 146 |
elsif V > 0.0 then |
| 147 |
return False; |
| 148 |
|
| 149 |
elsif not Num'Signed_Zeros then |
| 150 |
return False; |
| 151 |
|
| 152 |
else |
| 153 |
return Num'Copy_Sign (1.0, V) < 0.0; |
| 154 |
end if; |
| 155 |
end Is_Negative; |
| 156 |
|
| 157 |
-------------------- |
| 158 |
-- Set_Image_Real -- |
| 159 |
-------------------- |
| 160 |
|
| 161 |
procedure Set_Image_Real |
| 162 |
(V : Num; |
| 163 |
S : in out String; |
| 164 |
P : in out Natural; |
| 165 |
Fore : Natural; |
| 166 |
Aft : Natural; |
| 167 |
Exp : Natural) |
| 168 |
is |
| 169 |
Powten : constant array (0 .. Maxpow) of Double_T; |
| 170 |
pragma Import (Ada, Powten); |
| 171 |
for Powten'Address use Powten_Address; |
| 172 |
|
| 173 |
NFrac : constant Natural := Natural'Max (Aft, 1); |
| 174 |
-- Number of digits after the decimal point |
| 175 |
|
| 176 |
Digs : String (1 .. 3 + Maxdigs); |
| 177 |
-- Array used to hold digits of converted integer value |
| 178 |
|
| 179 |
Ndigs : Natural; |
| 180 |
-- Number of digits stored in Digs (and also subscript of last digit) |
| 181 |
|
| 182 |
Scale : Integer := 0; |
| 183 |
-- Exponent such that the result is Digs (1 .. NDigs) * 10**(-Scale) |
| 184 |
|
| 185 |
X : Double_T; |
| 186 |
-- Current absolute value of the input after scaling |
| 187 |
|
| 188 |
procedure Adjust_Scale (S : Natural); |
| 189 |
-- Adjusts the value in X by multiplying or dividing by a power of |
| 190 |
-- ten so that it is in the range 10**(S-1) <= X < 10**S. Scale is |
| 191 |
-- adjusted to reflect the power of ten used to divide the result, |
| 192 |
-- i.e. one is added to the scale value for each multiplication by |
| 193 |
-- 10.0 and one is subtracted for each division by 10.0. |
| 194 |
|
| 195 |
------------------ |
| 196 |
-- Adjust_Scale -- |
| 197 |
------------------ |
| 198 |
|
| 199 |
procedure Adjust_Scale (S : Natural) is |
| 200 |
Lo, Mid, Hi : Natural; |
| 201 |
XP : Double_T; |
| 202 |
|
| 203 |
begin |
| 204 |
-- Cases where scaling up is required |
| 205 |
|
| 206 |
if X < Powten (S - 1) then |
| 207 |
|
| 208 |
-- What we are looking for is a power of ten to multiply X by |
| 209 |
-- so that the result lies within the required range. |
| 210 |
|
| 211 |
loop |
| 212 |
XP := X * Powten (Maxpow); |
| 213 |
exit when XP >= Powten (S - 1) or else Scale > Maxscaling; |
| 214 |
X := XP; |
| 215 |
Scale := Scale + Maxpow; |
| 216 |
end loop; |
| 217 |
|
| 218 |
-- The following exception is only raised in case of erroneous |
| 219 |
-- execution, where a number was considered valid but still |
| 220 |
-- fails to scale up. One situation where this can happen is |
| 221 |
-- when a system which is supposed to be IEEE-compliant, but |
| 222 |
-- has been reconfigured to flush denormals to zero. |
| 223 |
|
| 224 |
if Scale > Maxscaling then |
| 225 |
raise Constraint_Error; |
| 226 |
end if; |
| 227 |
|
| 228 |
-- Here we know that we must multiply by at least 10**1 and that |
| 229 |
-- 10**Maxpow takes us too far: binary search to find right one. |
| 230 |
|
| 231 |
-- Because of roundoff errors, it is possible for the value |
| 232 |
-- of XP to be just outside of the interval when Lo >= Hi. In |
| 233 |
-- that case we adjust explicitly by a factor of 10. This |
| 234 |
-- can only happen with a value that is very close to an |
| 235 |
-- exact power of 10. |
| 236 |
|
| 237 |
Lo := 1; |
| 238 |
Hi := Maxpow; |
| 239 |
|
| 240 |
loop |
| 241 |
Mid := (Lo + Hi) / 2; |
| 242 |
XP := X * Powten (Mid); |
| 243 |
|
| 244 |
if XP < Powten (S - 1) then |
| 245 |
|
| 246 |
if Lo >= Hi then |
| 247 |
Mid := Mid + 1; |
| 248 |
XP := XP * 10.0; |
| 249 |
exit; |
| 250 |
|
| 251 |
else |
| 252 |
Lo := Mid + 1; |
| 253 |
end if; |
| 254 |
|
| 255 |
elsif XP >= Powten (S) then |
| 256 |
|
| 257 |
if Lo >= Hi then |
| 258 |
Mid := Mid - 1; |
| 259 |
XP := XP / 10.0; |
| 260 |
exit; |
| 261 |
|
| 262 |
else |
| 263 |
Hi := Mid - 1; |
| 264 |
end if; |
| 265 |
|
| 266 |
else |
| 267 |
exit; |
| 268 |
end if; |
| 269 |
end loop; |
| 270 |
|
| 271 |
X := XP; |
| 272 |
Scale := Scale + Mid; |
| 273 |
|
| 274 |
-- Cases where scaling down is required |
| 275 |
|
| 276 |
elsif X >= Powten (S) then |
| 277 |
|
| 278 |
-- What we are looking for is a power of ten to divide X by |
| 279 |
-- so that the result lies within the required range. |
| 280 |
|
| 281 |
loop |
| 282 |
XP := X / Powten (Maxpow); |
| 283 |
exit when XP < Powten (S) or else Scale < -Maxscaling; |
| 284 |
X := XP; |
| 285 |
Scale := Scale - Maxpow; |
| 286 |
end loop; |
| 287 |
|
| 288 |
-- The following exception is only raised in case of erroneous |
| 289 |
-- execution, where a number was considered valid but still |
| 290 |
-- fails to scale up. One situation where this can happen is |
| 291 |
-- when a system which is supposed to be IEEE-compliant, but |
| 292 |
-- has been reconfigured to flush denormals to zero. |
| 293 |
|
| 294 |
if Scale < -Maxscaling then |
| 295 |
raise Constraint_Error; |
| 296 |
end if; |
| 297 |
|
| 298 |
-- Here we know that we must divide by at least 10**1 and that |
| 299 |
-- 10**Maxpow takes us too far, binary search to find right one. |
| 300 |
|
| 301 |
Lo := 1; |
| 302 |
Hi := Maxpow; |
| 303 |
|
| 304 |
loop |
| 305 |
Mid := (Lo + Hi) / 2; |
| 306 |
XP := X / Powten (Mid); |
| 307 |
|
| 308 |
if XP < Powten (S - 1) then |
| 309 |
|
| 310 |
if Lo >= Hi then |
| 311 |
XP := XP * 10.0; |
| 312 |
Mid := Mid - 1; |
| 313 |
exit; |
| 314 |
|
| 315 |
else |
| 316 |
Hi := Mid - 1; |
| 317 |
end if; |
| 318 |
|
| 319 |
elsif XP >= Powten (S) then |
| 320 |
|
| 321 |
if Lo >= Hi then |
| 322 |
XP := XP / 10.0; |
| 323 |
Mid := Mid + 1; |
| 324 |
exit; |
| 325 |
|
| 326 |
else |
| 327 |
Lo := Mid + 1; |
| 328 |
end if; |
| 329 |
|
| 330 |
else |
| 331 |
exit; |
| 332 |
end if; |
| 333 |
end loop; |
| 334 |
|
| 335 |
X := XP; |
| 336 |
Scale := Scale - Mid; |
| 337 |
|
| 338 |
-- Here we are already scaled right |
| 339 |
|
| 340 |
else |
| 341 |
null; |
| 342 |
end if; |
| 343 |
end Adjust_Scale; |
| 344 |
|
| 345 |
-- Start of processing for Set_Image_Real |
| 346 |
|
| 347 |
begin |
| 348 |
-- We call the floating-point processor reset routine so we can be sure |
| 349 |
-- that the processor is properly set for conversions. This is notably |
| 350 |
-- needed on Windows, where calls to the operating system randomly reset |
| 351 |
-- the processor into 64-bit mode. |
| 352 |
|
| 353 |
if Num'Machine_Mantissa = 64 then |
| 354 |
System.Float_Control.Reset; |
| 355 |
end if; |
| 356 |
|
| 357 |
-- Deal with invalid values first |
| 358 |
|
| 359 |
if not V'Valid then |
| 360 |
|
| 361 |
-- Note that we're taking our chances here, as V might be |
| 362 |
-- an invalid bit pattern resulting from erroneous execution |
| 363 |
-- (caused by using uninitialized variables for example). |
| 364 |
|
| 365 |
-- No matter what, we'll at least get reasonable behavior, |
| 366 |
-- converting to infinity or some other value, or causing an |
| 367 |
-- exception to be raised is fine. |
| 368 |
|
| 369 |
-- If the following two tests succeed, then we definitely have |
| 370 |
-- an infinite value, so we print +Inf or -Inf. |
| 371 |
|
| 372 |
if V > Num'Last then |
| 373 |
pragma Annotate (CodePeer, False_Positive, "dead code", |
| 374 |
"CodePeer analysis ignores NaN and Inf values"); |
| 375 |
pragma Annotate (CodePeer, False_Positive, "test always true", |
| 376 |
"CodePeer analysis ignores NaN and Inf values"); |
| 377 |
|
| 378 |
Set_Floating_Invalid_Value (Infinity, S, P, Fore, Aft, Exp); |
| 379 |
|
| 380 |
elsif V < Num'First then |
| 381 |
Set_Floating_Invalid_Value (Minus_Infinity, S, P, Fore, Aft, Exp); |
| 382 |
|
| 383 |
-- In all other cases we print NaN |
| 384 |
|
| 385 |
else |
| 386 |
Set_Floating_Invalid_Value (Not_A_Number, S, P, Fore, Aft, Exp); |
| 387 |
end if; |
| 388 |
|
| 389 |
return; |
| 390 |
end if; |
| 391 |
|
| 392 |
-- Set the first character like Image |
| 393 |
|
| 394 |
Digs (1) := (if Is_Negative (V) then '-' else ' '); |
| 395 |
Ndigs := 1; |
| 396 |
|
| 397 |
X := Double_Real.To_Double (abs V); |
| 398 |
|
| 399 |
-- If X is zero, we are done |
| 400 |
|
| 401 |
if X = 0.0 then |
| 402 |
Digs (2) := '0'; |
| 403 |
Ndigs := 2; |
| 404 |
|
| 405 |
-- Otherwise, scale X and convert it to an integer |
| 406 |
|
| 407 |
else |
| 408 |
-- In exponent notation, we need exactly NFrac + 1 digits and always |
| 409 |
-- round the last one. |
| 410 |
|
| 411 |
if Exp > 0 then |
| 412 |
Adjust_Scale (Natural'Min (NFrac + 1, Maxdigs)); |
| 413 |
X := X + 0.5; |
| 414 |
|
| 415 |
-- In straight notation, we compute the maximum number of digits and |
| 416 |
-- compare how many of them will be put after the decimal point with |
| 417 |
-- Nfrac, in order to find out whether we need to round the last one |
| 418 |
-- here or whether the rounding is performed by Set_Decimal_Digits. |
| 419 |
|
| 420 |
else |
| 421 |
Adjust_Scale (Maxdigs); |
| 422 |
if Scale <= NFrac then |
| 423 |
X := X + 0.5; |
| 424 |
end if; |
| 425 |
end if; |
| 426 |
|
| 427 |
-- If X fits in an Uns, do the conversion directly. Note that this is |
| 428 |
-- always the case for the Image attribute. |
| 429 |
|
| 430 |
if X <= Num (Uns'Last) then |
| 431 |
Set_Image_Unsigned (To_Unsigned (X), Digs, Ndigs); |
| 432 |
|
| 433 |
-- Otherwise, do the conversion in two steps |
| 434 |
|
| 435 |
else |
| 436 |
declare |
| 437 |
Halfdigs : constant Natural := Maxdigs / 2; |
| 438 |
|
| 439 |
Buf : String (1 .. Halfdigs); |
| 440 |
Len : Natural; |
| 441 |
Y : Uns; |
| 442 |
|
| 443 |
begin |
| 444 |
-- Compute upper Halfdigs stripped from leading zeros |
| 445 |
|
| 446 |
Y := To_Unsigned (X / Powten (Halfdigs)); |
| 447 |
Set_Image_Unsigned (Y, Digs, Ndigs); |
| 448 |
|
| 449 |
-- Compute lower Halfdigs stripped from leading zeros |
| 450 |
|
| 451 |
Len := 0; |
| 452 |
X := X - From_Unsigned (Y) * Powten (Halfdigs); |
| 453 |
Set_Image_Unsigned (To_Unsigned (X), Buf, Len); |
| 454 |
pragma Assert (Len <= Halfdigs); |
| 455 |
|
| 456 |
-- Concatenate unmodified upper part with zero-padded |
| 457 |
-- lower part up to Halfdigs. |
| 458 |
|
| 459 |
for J in 1 .. Halfdigs - Len loop |
| 460 |
Digs (Ndigs + J) := '0'; |
| 461 |
end loop; |
| 462 |
|
| 463 |
for J in 1 .. Len loop |
| 464 |
Digs (Ndigs + Halfdigs - Len + J) := Buf (J); |
| 465 |
end loop; |
| 466 |
|
| 467 |
Ndigs := Ndigs + Halfdigs; |
| 468 |
end; |
| 469 |
end if; |
| 470 |
end if; |
| 471 |
|
| 472 |
Set_Decimal_Digits (Digs, Ndigs, S, P, Scale, Fore, Aft, Exp); |
| 473 |
end Set_Image_Real; |
| 474 |
|
| 475 |
end System.Image_R; |