1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- S Y S T E M . V A L U E _ D --
-- --
-- B o d y --
-- --
-- Copyright (C) 2020-2022, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
with System.Unsigned_Types; use System.Unsigned_Types;
with System.Val_Util; use System.Val_Util;
with System.Value_R;
package body System.Value_D is
pragma Assert (Int'Size <= Uns'Size);
-- We need an unsigned type large enough to represent the mantissa
package Impl is new Value_R (Uns, 2**(Int'Size - 1), Round => False);
-- We do not use the Extra digit for decimal fixed-point types
function Integer_to_Decimal
(Str : String;
Val : Uns;
Base : Unsigned;
ScaleB : Integer;
Minus : Boolean;
Scale : Integer) return Int;
-- Convert the real value from integer to decimal representation
------------------------
-- Integer_to_Decimal --
------------------------
function Integer_to_Decimal
(Str : String;
Val : Uns;
Base : Unsigned;
ScaleB : Integer;
Minus : Boolean;
Scale : Integer) return Int
is
function Safe_Expont
(Base : Int;
Exp : in out Natural;
Factor : Int) return Int;
-- Return (Base ** Exp) * Factor if the computation does not overflow,
-- or else the number of the form (Base ** K) * Factor with the largest
-- magnitude if the former computation overflows. In both cases, Exp is
-- updated to contain the remaining power in the computation. Note that
-- Factor is expected to be positive in this context.
function Unsigned_To_Signed (Val : Uns) return Int;
-- Convert an integer value from unsigned to signed representation
-----------------
-- Safe_Expont --
-----------------
function Safe_Expont
(Base : Int;
Exp : in out Natural;
Factor : Int) return Int
is
pragma Assert (Base /= 0 and then Factor > 0);
Max : constant Int := Int'Last / Base;
Result : Int := Factor;
begin
while Exp > 0 and then Result <= Max loop
Result := Result * Base;
Exp := Exp - 1;
end loop;
return Result;
end Safe_Expont;
------------------------
-- Unsigned_To_Signed --
------------------------
function Unsigned_To_Signed (Val : Uns) return Int is
begin
-- Deal with overflow cases, and also with largest negative number
if Val > Uns (Int'Last) then
if Minus and then Val = Uns (-(Int'First)) then
return Int'First;
else
Bad_Value (Str);
end if;
-- Negative values
elsif Minus then
return -(Int (Val));
-- Positive values
else
return Int (Val);
end if;
end Unsigned_To_Signed;
begin
-- If the base of the value is 10 or its scaling factor is zero, then
-- add the scales (they are defined in the opposite sense) and apply
-- the result to the value, checking for overflow in the process.
if Base = 10 or else ScaleB = 0 then
declare
S : Integer := ScaleB + Scale;
V : Uns := Val;
begin
while S < 0 loop
V := V / 10;
S := S + 1;
end loop;
while S > 0 loop
if V <= Uns'Last / 10 then
V := V * 10;
S := S - 1;
else
Bad_Value (Str);
end if;
end loop;
return Unsigned_To_Signed (V);
end;
-- If the base of the value is not 10, use a scaled divide operation
-- to compute Val * (Base ** ScaleB) * (10 ** Scale).
else
declare
B : constant Int := Int (Base);
S : constant Integer := ScaleB;
V : Uns := Val;
Y, Z, Q, R : Int;
begin
-- If S is too negative, then drop trailing digits
if S < 0 then
declare
LS : Integer := -S;
begin
Y := 10 ** Integer'Max (0, Scale);
Z := Safe_Expont (B, LS, 10 ** Integer'Max (0, -Scale));
for J in 1 .. LS loop
V := V / Uns (B);
end loop;
end;
-- If S is too positive, then scale V up, which may then overflow
elsif S > 0 then
declare
LS : Integer := S;
begin
Y := Safe_Expont (B, LS, 10 ** Integer'Max (0, Scale));
Z := 10 ** Integer'Max (0, -Scale);
for J in 1 .. LS loop
if V <= Uns'Last / Uns (B) then
V := V * Uns (B);
else
Bad_Value (Str);
end if;
end loop;
end;
-- The case S equal to zero should have been handled earlier
else
raise Program_Error;
end if;
-- Perform a scale divide operation with rounding to match 'Image
Scaled_Divide (Unsigned_To_Signed (V), Y, Z, Q, R, Round => True);
return Q;
end;
end if;
exception
when Constraint_Error => Bad_Value (Str);
end Integer_to_Decimal;
------------------
-- Scan_Decimal --
------------------
function Scan_Decimal
(Str : String;
Ptr : not null access Integer;
Max : Integer;
Scale : Integer) return Int
is
Base : Unsigned;
ScaleB : Integer;
Extra : Unsigned;
Minus : Boolean;
Val : Uns;
begin
Val := Impl.Scan_Raw_Real (Str, Ptr, Max, Base, ScaleB, Extra, Minus);
return Integer_to_Decimal (Str, Val, Base, ScaleB, Minus, Scale);
end Scan_Decimal;
-------------------
-- Value_Decimal --
-------------------
function Value_Decimal (Str : String; Scale : Integer) return Int is
Base : Unsigned;
ScaleB : Integer;
Extra : Unsigned;
Minus : Boolean;
Val : Uns;
begin
Val := Impl.Value_Raw_Real (Str, Base, ScaleB, Extra, Minus);
return Integer_to_Decimal (Str, Val, Base, ScaleB, Minus, Scale);
end Value_Decimal;
end System.Value_D;
|