OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [ada/] [s-valrea.adb] - Blame information for rev 27

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT COMPILER COMPONENTS                         --
4
--                                                                          --
5
--                      S Y S T E M . V A L _ R E A L                       --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--          Copyright (C) 1992-2005, 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 2,  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.  See the GNU General Public License --
17
-- for  more details.  You should have  received  a copy of the GNU General --
18
-- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19
-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20
-- Boston, MA 02110-1301, USA.                                              --
21
--                                                                          --
22
-- As a special exception,  if other files  instantiate  generics from this --
23
-- unit, or you link  this unit with other files  to produce an executable, --
24
-- this  unit  does not  by itself cause  the resulting  executable  to  be --
25
-- covered  by the  GNU  General  Public  License.  This exception does not --
26
-- however invalidate  any other reasons why  the executable file  might be --
27
-- covered by the  GNU Public License.                                      --
28
--                                                                          --
29
-- GNAT was originally developed  by the GNAT team at  New York University. --
30
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
31
--                                                                          --
32
------------------------------------------------------------------------------
33
 
34
with System.Powten_Table; use System.Powten_Table;
35
with System.Val_Util;     use System.Val_Util;
36
 
37
package body System.Val_Real is
38
 
39
   ---------------
40
   -- Scan_Real --
41
   ---------------
42
 
43
   function Scan_Real
44
     (Str : String;
45
      Ptr : access Integer;
46
      Max : Integer) return Long_Long_Float
47
   is
48
      procedure Reset;
49
      pragma Import (C, Reset, "__gnat_init_float");
50
      --  We import the floating-point processor reset routine so that we can
51
      --  be sure the floating-point processor is properly set for conversion
52
      --  calls (see description of Reset in GNAT.Float_Control (g-flocon.ads).
53
      --  This is notably need on Windows, where calls to the operating system
54
      --  randomly reset the processor into 64-bit mode.
55
 
56
      P : Integer;
57
      --  Local copy of string pointer
58
 
59
      Base   : Long_Long_Float;
60
      --  Base value
61
 
62
      Uval : Long_Long_Float;
63
      --  Accumulated float result
64
 
65
      subtype Digs is Character range '0' .. '9';
66
      --  Used to check for decimal digit
67
 
68
      Scale : Integer := 0;
69
      --  Power of Base to multiply result by
70
 
71
      Start : Positive;
72
      --  Position of starting non-blank character
73
 
74
      Minus : Boolean;
75
      --  Set to True if minus sign is present, otherwise to False
76
 
77
      Bad_Base : Boolean := False;
78
      --  Set True if Base out of range or if out of range digit
79
 
80
      After_Point : Natural := 0;
81
      --  Set to 1 after the point
82
 
83
      Num_Saved_Zeroes : Natural := 0;
84
      --  This counts zeroes after the decimal point. A non-zero value means
85
      --  that this number of previously scanned digits are zero. if the end
86
      --  of the number is reached, these zeroes are simply discarded, which
87
      --  ensures that trailing zeroes after the point never affect the value
88
      --  (which might otherwise happen as a result of rounding). With this
89
      --  processing in place, we can ensure that, for example, we get the
90
      --  same exact result from 1.0E+49 and 1.0000000E+49. This is not
91
      --  necessarily required in a case like this where the result is not
92
      --  a machine number, but it is certainly a desirable behavior.
93
 
94
      procedure Scanf;
95
      --  Scans integer literal value starting at current character position.
96
      --  For each digit encountered, Uval is multiplied by 10.0, and the new
97
      --  digit value is incremented. In addition Scale is decremented for each
98
      --  digit encountered if we are after the point (After_Point = 1). The
99
      --  longest possible syntactically valid numeral is scanned out, and on
100
      --  return P points past the last character. On entry, the current
101
      --  character is known to be a digit, so a numeral is definitely present.
102
 
103
      -----------
104
      -- Scanf --
105
      -----------
106
 
107
      procedure Scanf is
108
         Digit : Natural;
109
 
110
      begin
111
         loop
112
            Digit := Character'Pos (Str (P)) - Character'Pos ('0');
113
            P := P + 1;
114
 
115
            --  Save up trailing zeroes after the decimal point
116
 
117
            if Digit = 0 and After_Point = 1 then
118
               Num_Saved_Zeroes := Num_Saved_Zeroes + 1;
119
 
120
            --  Here for a non-zero digit
121
 
122
            else
123
               --  First deal with any previously saved zeroes
124
 
125
               if Num_Saved_Zeroes /= 0 then
126
                  while Num_Saved_Zeroes > Maxpow loop
127
                     Uval := Uval * Powten (Maxpow);
128
                     Num_Saved_Zeroes := Num_Saved_Zeroes - Maxpow;
129
                     Scale := Scale - Maxpow;
130
                  end loop;
131
 
132
                  Uval := Uval * Powten (Num_Saved_Zeroes);
133
                  Scale := Scale - Num_Saved_Zeroes;
134
 
135
                  Num_Saved_Zeroes := 0;
136
               end if;
137
 
138
               --  Accumulate new digit
139
 
140
               Uval := Uval * 10.0 + Long_Long_Float (Digit);
141
               Scale := Scale - After_Point;
142
            end if;
143
 
144
            --  Done if end of input field
145
 
146
            if P > Max then
147
               return;
148
 
149
            --  Check next character
150
 
151
            elsif Str (P) not in Digs then
152
               if Str (P) = '_' then
153
                  Scan_Underscore (Str, P, Ptr, Max, False);
154
               else
155
                  return;
156
               end if;
157
            end if;
158
         end loop;
159
      end Scanf;
160
 
161
   --  Start of processing for System.Scan_Real
162
 
163
   begin
164
      Reset;
165
      Scan_Sign (Str, Ptr, Max, Minus, Start);
166
      P := Ptr.all;
167
      Ptr.all := Start;
168
 
169
      --  If digit, scan numeral before point
170
 
171
      if Str (P) in Digs then
172
         Uval := 0.0;
173
         Scanf;
174
 
175
      --  Initial point, allowed only if followed by digit (RM 3.5(47))
176
 
177
      elsif Str (P) = '.'
178
        and then P < Max
179
        and then Str (P + 1) in Digs
180
      then
181
         Uval := 0.0;
182
 
183
      --  Any other initial character is an error
184
 
185
      else
186
         raise Constraint_Error;
187
      end if;
188
 
189
      --  Deal with based case
190
 
191
      if P < Max and then (Str (P) = ':' or else Str (P) = '#') then
192
         declare
193
            Base_Char : constant Character := Str (P);
194
            Digit     : Natural;
195
            Fdigit    : Long_Long_Float;
196
 
197
         begin
198
            --  Set bad base if out of range, and use safe base of 16.0,
199
            --  to guard against division by zero in the loop below.
200
 
201
            if Uval < 2.0 or else Uval > 16.0 then
202
               Bad_Base := True;
203
               Uval := 16.0;
204
            end if;
205
 
206
            Base := Uval;
207
            Uval := 0.0;
208
            P := P + 1;
209
 
210
            --  Special check to allow initial point (RM 3.5(49))
211
 
212
            if Str (P) = '.' then
213
               After_Point := 1;
214
               P := P + 1;
215
            end if;
216
 
217
            --  Loop to scan digits of based number. On entry to the loop we
218
            --  must have a valid digit. If we don't, then we have an illegal
219
            --  floating-point value, and we raise Constraint_Error, note that
220
            --  Ptr at this stage was reset to the proper (Start) value.
221
 
222
            loop
223
               if P > Max then
224
                  raise Constraint_Error;
225
 
226
               elsif Str (P) in Digs then
227
                  Digit := Character'Pos (Str (P)) - Character'Pos ('0');
228
 
229
               elsif Str (P) in 'A' .. 'F' then
230
                  Digit :=
231
                    Character'Pos (Str (P)) - (Character'Pos ('A') - 10);
232
 
233
               elsif Str (P) in 'a' .. 'f' then
234
                  Digit :=
235
                    Character'Pos (Str (P)) - (Character'Pos ('a') - 10);
236
 
237
               else
238
                  raise Constraint_Error;
239
               end if;
240
 
241
               --  Save up trailing zeroes after the decimal point
242
 
243
               if Digit = 0 and After_Point = 1 then
244
                  Num_Saved_Zeroes := Num_Saved_Zeroes + 1;
245
 
246
               --  Here for a non-zero digit
247
 
248
               else
249
                  --  First deal with any previously saved zeroes
250
 
251
                  if Num_Saved_Zeroes /= 0 then
252
                     Uval := Uval * Base ** Num_Saved_Zeroes;
253
                     Scale := Scale - Num_Saved_Zeroes;
254
                     Num_Saved_Zeroes := 0;
255
                  end if;
256
 
257
                  --  Now accumulate the new digit
258
 
259
                  Fdigit := Long_Long_Float (Digit);
260
 
261
                  if Fdigit >= Base then
262
                     Bad_Base := True;
263
                  else
264
                     Scale := Scale - After_Point;
265
                     Uval := Uval * Base + Fdigit;
266
                  end if;
267
               end if;
268
 
269
               P := P + 1;
270
 
271
               if P > Max then
272
                  raise Constraint_Error;
273
 
274
               elsif Str (P) = '_' then
275
                  Scan_Underscore (Str, P, Ptr, Max, True);
276
 
277
               else
278
                  --  Skip past period after digit. Note that the processing
279
                  --  here will permit either a digit after the period, or the
280
                  --  terminating base character, as allowed in (RM 3.5(48))
281
 
282
                  if Str (P) = '.' and then After_Point = 0 then
283
                     P := P + 1;
284
                     After_Point := 1;
285
 
286
                     if P > Max then
287
                        raise Constraint_Error;
288
                     end if;
289
                  end if;
290
 
291
                  exit when Str (P) = Base_Char;
292
               end if;
293
            end loop;
294
 
295
            --  Based number successfully scanned out (point was found)
296
 
297
            Ptr.all := P + 1;
298
         end;
299
 
300
      --  Non-based case, check for being at decimal point now. Note that
301
      --  in Ada 95, we do not insist on a decimal point being present
302
 
303
      else
304
         Base := 10.0;
305
         After_Point := 1;
306
 
307
         if P <= Max and then Str (P) = '.' then
308
            P := P + 1;
309
 
310
            --  Scan digits after point if any are present (RM 3.5(46))
311
 
312
            if P <= Max and then Str (P) in Digs then
313
               Scanf;
314
            end if;
315
         end if;
316
 
317
         Ptr.all := P;
318
      end if;
319
 
320
      --  At this point, we have Uval containing the digits of the value as
321
      --  an integer, and Scale indicates the negative of the number of digits
322
      --  after the point. Base contains the base value (an integral value in
323
      --  the range 2.0 .. 16.0). Test for exponent, must be at least one
324
      --  character after the E for the exponent to be valid.
325
 
326
      Scale := Scale + Scan_Exponent (Str, Ptr, Max, Real => True);
327
 
328
      --  At this point the exponent has been scanned if one is present and
329
      --  Scale is adjusted to include the exponent value. Uval contains the
330
      --  the integral value which is to be multiplied by Base ** Scale.
331
 
332
      --  If base is not 10, use exponentiation for scaling
333
 
334
      if Base /= 10.0 then
335
         Uval := Uval * Base ** Scale;
336
 
337
      --  For base 10, use power of ten table, repeatedly if necessary
338
 
339
      elsif Scale > 0 then
340
         while Scale > Maxpow loop
341
            Uval := Uval * Powten (Maxpow);
342
            Scale := Scale - Maxpow;
343
         end loop;
344
 
345
         if Scale > 0 then
346
            Uval := Uval * Powten (Scale);
347
         end if;
348
 
349
      elsif Scale < 0 then
350
         while (-Scale) > Maxpow loop
351
            Uval := Uval / Powten (Maxpow);
352
            Scale := Scale + Maxpow;
353
         end loop;
354
 
355
         if Scale < 0 then
356
            Uval := Uval / Powten (-Scale);
357
         end if;
358
      end if;
359
 
360
      --  Here is where we check for a bad based number
361
 
362
      if Bad_Base then
363
         raise Constraint_Error;
364
 
365
      --  If OK, then deal with initial minus sign, note that this processing
366
      --  is done even if Uval is zero, so that -0.0 is correctly interpreted.
367
 
368
      else
369
         if Minus then
370
            return -Uval;
371
         else
372
            return Uval;
373
         end if;
374
      end if;
375
   end Scan_Real;
376
 
377
   ----------------
378
   -- Value_Real --
379
   ----------------
380
 
381
   function Value_Real (Str : String) return Long_Long_Float is
382
      V : Long_Long_Float;
383
      P : aliased Integer := Str'First;
384
   begin
385
      V := Scan_Real (Str, P'Access, Str'Last);
386
      Scan_Trailing_Blanks (Str, P);
387
      return V;
388
   end Value_Real;
389
 
390
end System.Val_Real;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.