Debian Codegraph @

gcc/ada/libgnat/a-wtfiio__128.adb

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