Debian Codegraph @

gcc/ada/libgnat/a-ztfiio__128.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . W I D E _ W I D E _ T E X T _ I O . F I X E D _ I O --
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 Interfaces;
33 with Ada.Wide_Wide_Text_IO.Fixed_Aux;
34 with Ada.Wide_Wide_Text_IO.Float_Aux;
35 with System.Img_Fixed_32; use System.Img_Fixed_32;
36 with System.Img_Fixed_64; use System.Img_Fixed_64;
37 with System.Img_Fixed_128; use System.Img_Fixed_128;
38 with System.Img_LFlt; use System.Img_LFlt;
39 with System.Val_Fixed_32; use System.Val_Fixed_32;
40 with System.Val_Fixed_64; use System.Val_Fixed_64;
41 with System.Val_Fixed_128; use System.Val_Fixed_128;
42 with System.Val_LFlt; use System.Val_LFlt;
43 with System.WCh_Con; use System.WCh_Con;
44 with System.WCh_WtS; use System.WCh_WtS;
45
46 package body Ada.Wide_Wide_Text_IO.Fixed_IO is
47
48 -- Note: we still use the floating-point I/O routines for types whose small
49 -- is not the ratio of two sufficiently small integers. This will result in
50 -- inaccuracies for fixed point types that require more precision than is
51 -- available in Long_Float.
52
53 subtype Int32 is Interfaces.Integer_32; use type Int32;
54 subtype Int64 is Interfaces.Integer_64; use type Int64;
55 subtype Int128 is Interfaces.Integer_128; use type Int128;
56
57 package Aux32 is new
58 Ada.Wide_Wide_Text_IO.Fixed_Aux (Int32, Scan_Fixed32, Set_Image_Fixed32);
59
60 package Aux64 is new
61 Ada.Wide_Wide_Text_IO.Fixed_Aux (Int64, Scan_Fixed64, Set_Image_Fixed64);
62
63 package Aux128 is new
64 Ada.Wide_Wide_Text_IO.Fixed_Aux
65 (Int128, Scan_Fixed128, Set_Image_Fixed128);
66
67 package Aux_Long_Float is new
68 Ada.Wide_Wide_Text_IO.Float_Aux
69 (Long_Float, Scan_Long_Float, Set_Image_Long_Float);
70
71 -- Throughout this generic body, we distinguish between the case where type
72 -- Int32 is OK, where type Int64 is OK and where type Int128 is OK. These
73 -- boolean constants are used to test for this, such that only code for the
74 -- relevant case is included in the instance; that's why the computation of
75 -- their value must be fully static (although it is not a static expression
76 -- in the RM sense).
77
78 OK_Get_32 : constant Boolean :=
79 Num'Base'Object_Size <= 32
80 and then
81 ((Num'Base'Small_Numerator = 1
82 and then Num'Base'Small_Denominator <= 2**31)
83 or else
84 (Num'Base'Small_Denominator = 1
85 and then Num'Base'Small_Numerator <= 2**31)
86 or else
87 (Num'Base'Small_Numerator <= 2**27
88 and then Num'Base'Small_Denominator <= 2**27));
89 -- These conditions are derived from the prerequisites of System.Value_F
90
91 OK_Put_32 : constant Boolean :=
92 Num'Base'Object_Size <= 32
93 and then
94 ((Num'Base'Small_Numerator = 1
95 and then Num'Base'Small_Denominator <= 2**31)
96 or else
97 (Num'Base'Small_Denominator = 1
98 and then Num'Base'Small_Numerator <= 2**31)
99 or else
100 (Num'Base'Small_Numerator < Num'Base'Small_Denominator
101 and then Num'Base'Small_Denominator <= 2**27)
102 or else
103 (Num'Base'Small_Denominator < Num'Base'Small_Numerator
104 and then Num'Base'Small_Numerator <= 2**25));
105 -- These conditions are derived from the prerequisites of System.Image_F
106
107 OK_Get_64 : constant Boolean :=
108 Num'Base'Object_Size <= 64
109 and then
110 ((Num'Base'Small_Numerator = 1
111 and then Num'Base'Small_Denominator <= 2**63)
112 or else
113 (Num'Base'Small_Denominator = 1
114 and then Num'Base'Small_Numerator <= 2**63)
115 or else
116 (Num'Base'Small_Numerator <= 2**59
117 and then Num'Base'Small_Denominator <= 2**59));
118 -- These conditions are derived from the prerequisites of System.Value_F
119
120 OK_Put_64 : constant Boolean :=
121 Num'Base'Object_Size <= 64
122 and then
123 ((Num'Base'Small_Numerator = 1
124 and then Num'Base'Small_Denominator <= 2**63)
125 or else
126 (Num'Base'Small_Denominator = 1
127 and then Num'Base'Small_Numerator <= 2**63)
128 or else
129 (Num'Base'Small_Numerator < Num'Base'Small_Denominator
130 and then Num'Base'Small_Denominator <= 2**59)
131 or else
132 (Num'Base'Small_Denominator < Num'Base'Small_Numerator
133 and then Num'Base'Small_Numerator <= 2**53));
134 -- These conditions are derived from the prerequisites of System.Image_F
135
136 OK_Get_128 : constant Boolean :=
137 Num'Base'Object_Size <= 128
138 and then
139 ((Num'Base'Small_Numerator = 1
140 and then Num'Base'Small_Denominator <= 2**127)
141 or else
142 (Num'Base'Small_Denominator = 1
143 and then Num'Base'Small_Numerator <= 2**127)
144 or else
145 (Num'Base'Small_Numerator <= 2**123
146 and then Num'Base'Small_Denominator <= 2**123));
147 -- These conditions are derived from the prerequisites of System.Value_F
148
149 OK_Put_128 : constant Boolean :=
150 Num'Base'Object_Size <= 128
151 and then
152 ((Num'Base'Small_Numerator = 1
153 and then Num'Base'Small_Denominator <= 2**127)
154 or else
155 (Num'Base'Small_Denominator = 1
156 and then Num'Base'Small_Numerator <= 2**127)
157 or else
158 (Num'Base'Small_Numerator < Num'Base'Small_Denominator
159 and then Num'Base'Small_Denominator <= 2**123)
160 or else
161 (Num'Base'Small_Denominator < Num'Base'Small_Numerator
162 and then Num'Base'Small_Numerator <= 2**122));
163 -- These conditions are derived from the prerequisites of System.Image_F
164
165 E : constant Natural :=
166 127 - 64 * Boolean'Pos (OK_Put_64) - 32 * Boolean'Pos (OK_Put_32);
167 -- T'Size - 1 for the selected Int{32,64,128}
168
169 F0 : constant Natural := 0;
170 F1 : constant Natural :=
171 F0 + 38 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F0) >= 1.0E+38);
172 F2 : constant Natural :=
173 F1 + 19 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F1) >= 1.0E+19);
174 F3 : constant Natural :=
175 F2 + 9 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F2) >= 1.0E+9);
176 F4 : constant Natural :=
177 F3 + 5 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F3) >= 1.0E+5);
178 F5 : constant Natural :=
179 F4 + 3 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F4) >= 1.0E+3);
180 F6 : constant Natural :=
181 F5 + 2 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F5) >= 1.0E+2);
182 F7 : constant Natural :=
183 F6 + 1 * Boolean'Pos (2.0**E * Num'Small * 10.0**(-F6) >= 1.0E+1);
184 -- Binary search for the number of digits - 1 before the decimal point of
185 -- the product 2.0**E * Num'Small.
186
187 For0 : constant Natural := 2 + F7;
188 -- Fore value for the fixed point type whose mantissa is Int{32,64,128} and
189 -- whose small is Num'Small.
190
191 ---------
192 -- Get --
193 ---------
194
195 procedure Get
196 (File : File_Type;
197 Item : out Num;
198 Width : Field := 0)
199 is
200 pragma Unsuppress (Range_Check);
201
202 begin
203 if OK_Get_32 then
204 Item := Num'Fixed_Value
205 (Aux32.Get (File, Width,
206 -Num'Small_Numerator,
207 -Num'Small_Denominator));
208 elsif OK_Get_64 then
209 Item := Num'Fixed_Value
210 (Aux64.Get (File, Width,
211 -Num'Small_Numerator,
212 -Num'Small_Denominator));
213 elsif OK_Get_128 then
214 Item := Num'Fixed_Value
215 (Aux128.Get (File, Width,
216 -Num'Small_Numerator,
217 -Num'Small_Denominator));
218 else
219 Aux_Long_Float.Get (File, Long_Float (Item), Width);
220 end if;
221
222 exception
223 when Constraint_Error => raise Data_Error;
224 end Get;
225
226 procedure Get
227 (Item : out Num;
228 Width : Field := 0)
229 is
230 begin
231 Get (Current_In, Item, Width);
232 end Get;
233
234 procedure Get
235 (From : Wide_Wide_String;
236 Item : out Num;
237 Last : out Positive)
238 is
239 pragma Unsuppress (Range_Check);
240
241 S : constant String := Wide_Wide_String_To_String (From, WCEM_Upper);
242 -- String on which we do the actual conversion. Note that the method
243 -- used for wide character encoding is irrelevant, since if there is
244 -- a character outside the Standard.Character range then the call to
245 -- Aux.Gets will raise Data_Error in any case.
246
247 begin
248 if OK_Get_32 then
249 Item := Num'Fixed_Value
250 (Aux32.Gets (S, Last,
251 -Num'Small_Numerator,
252 -Num'Small_Denominator));
253 elsif OK_Get_64 then
254 Item := Num'Fixed_Value
255 (Aux64.Gets (S, Last,
256 -Num'Small_Numerator,
257 -Num'Small_Denominator));
258 elsif OK_Get_128 then
259 Item := Num'Fixed_Value
260 (Aux128.Gets (S, Last,
261 -Num'Small_Numerator,
262 -Num'Small_Denominator));
263 else
264 Aux_Long_Float.Gets (S, Long_Float (Item), Last);
265 end if;
266
267 exception
268 when Constraint_Error => raise Data_Error;
269 end Get;
270
271 ---------
272 -- Put --
273 ---------
274
275 procedure Put
276 (File : File_Type;
277 Item : Num;
278 Fore : Field := Default_Fore;
279 Aft : Field := Default_Aft;
280 Exp : Field := Default_Exp)
281 is
282 begin
283 if OK_Put_32 then
284 Aux32.Put (File, Int32'Integer_Value (Item), Fore, Aft, Exp,
285 -Num'Small_Numerator, -Num'Small_Denominator,
286 For0, Num'Aft);
287 elsif OK_Put_64 then
288 Aux64.Put (File, Int64'Integer_Value (Item), Fore, Aft, Exp,
289 -Num'Small_Numerator, -Num'Small_Denominator,
290 For0, Num'Aft);
291 elsif OK_Put_128 then
292 Aux128.Put (File, Int128'Integer_Value (Item), Fore, Aft, Exp,
293 -Num'Small_Numerator, -Num'Small_Denominator,
294 For0, Num'Aft);
295 else
296 Aux_Long_Float.Put (File, Long_Float (Item), Fore, Aft, Exp);
297 end if;
298 end Put;
299
300 procedure Put
301 (Item : Num;
302 Fore : Field := Default_Fore;
303 Aft : Field := Default_Aft;
304 Exp : Field := Default_Exp)
305 is
306 begin
307 Put (Current_Out, Item, Fore, Aft, Exp);
308 end Put;
309
310 procedure Put
311 (To : out Wide_Wide_String;
312 Item : Num;
313 Aft : Field := Default_Aft;
314 Exp : Field := Default_Exp)
315 is
316 S : String (To'First .. To'Last);
317
318 begin
319 if OK_Put_32 then
320 Aux32.Puts (S, Int32'Integer_Value (Item), Aft, Exp,
321 -Num'Small_Numerator, -Num'Small_Denominator,
322 For0, Num'Aft);
323 elsif OK_Put_64 then
324 Aux64.Puts (S, Int64'Integer_Value (Item), Aft, Exp,
325 -Num'Small_Numerator, -Num'Small_Denominator,
326 For0, Num'Aft);
327 elsif OK_Put_128 then
328 Aux128.Puts (S, Int128'Integer_Value (Item), Aft, Exp,
329 -Num'Small_Numerator, -Num'Small_Denominator,
330 For0, Num'Aft);
331 else
332 Aux_Long_Float.Puts (S, Long_Float (Item), Aft, Exp);
333 end if;
334
335 for J in S'Range loop
336 To (J) := Wide_Wide_Character'Val (Character'Pos (S (J)));
337 end loop;
338 end Put;
339
340 end Ada.Wide_Wide_Text_IO.Fixed_IO;