1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- Support for universal real arithmetic
34 with Types
; use Types
;
35 with Uintp
; use Uintp
;
39 ---------------------------------------
40 -- Representation of Universal Reals --
41 ---------------------------------------
43 -- A universal real value is represented by a single value (which is
44 -- an index into an internal table). These values are not hashed, so
45 -- the equality operator should not be used on Ureal values (instead
46 -- use the UR_Eq function).
48 -- A Ureal value represents an arbitrary precision universal real value,
49 -- stored internally using four components
51 -- the numerator (Uint, always non-negative)
52 -- the denominator (Uint, always non-zero, always positive if base = 0)
53 -- a real base (Nat, either zero, or in the range 2 .. 16)
54 -- a sign flag (Boolean), set if negative
56 -- If the base is zero, then the absolute value of the Ureal is simply
57 -- numerator/denominator. If the base is non-zero, then the absolute
58 -- value is num / (rbase ** den).
60 -- Negative numbers are represented by the sign of the numerator being
61 -- negative. The denominator is always positive.
63 -- A normalized Ureal value has base = 0, and numerator/denominator
64 -- reduced to lowest terms, with zero itself being represented as 0/1.
65 -- This is a canonical format, so that for normalized Ureal values it
66 -- is the case that two equal values always have the same denominator
67 -- and numerator values.
69 -- Note: a value of minus zero is legitimate, and the operations in
70 -- Urealp preserve the handling of signed zeroes in accordance with
71 -- the rules of IEEE P754 ("IEEE floating point").
73 ------------------------------
74 -- Types for Urealp Package --
75 ------------------------------
77 type Ureal
is private;
78 -- Type used for representation of universal reals
80 No_Ureal
: constant Ureal
;
81 -- Constant used to indicate missing or unset Ureal value
87 function Ureal_0
return Ureal
;
90 function Ureal_M_0
return Ureal
;
93 function Ureal_Tenth
return Ureal
;
96 function Ureal_Half
return Ureal
;
99 function Ureal_1
return Ureal
;
102 function Ureal_2
return Ureal
;
105 function Ureal_10
return Ureal
;
106 -- Returns value 10.0
108 function Ureal_100
return Ureal
;
109 -- Returns value 100.0
111 function Ureal_2_80
return Ureal
;
112 -- Returns value 2.0 ** 80
114 function Ureal_2_M_80
return Ureal
;
115 -- Returns value 2.0 ** (-80)
117 function Ureal_2_128
return Ureal
;
118 -- Returns value 2.0 ** 128
120 function Ureal_2_M_128
return Ureal
;
121 -- Returns value 2.0 ** (-128)
123 function Ureal_10_36
return Ureal
;
124 -- Returns value 10.0 ** 36
126 function Ureal_M_10_36
return Ureal
;
127 -- Returns value -(10.0
133 procedure Initialize
;
134 -- Initialize Ureal tables. Note that Initialize must not be called if
135 -- Tree_Read is used. Note also that there is no Lock routine in this
136 -- unit. These tables are among the few tables that can be expanded
137 -- during Gigi processing.
140 -- Initializes internal tables from current tree file using the relevant
141 -- Table.Tree_Read routines. Note that Initialize should not be called if
142 -- Tree_Read is used. Tree_Read includes all necessary initialization.
144 procedure Tree_Write
;
145 -- Writes out internal tables to current tree file using the relevant
146 -- Table.Tree_Write routines.
148 function Rbase
(Real
: Ureal
) return Nat
;
149 -- Return the base of the universal real
151 function Denominator
(Real
: Ureal
) return Uint
;
152 -- Return the denominator of the universal real
154 function Numerator
(Real
: Ureal
) return Uint
;
155 -- Return the numerator of the universal real
157 function Norm_Den
(Real
: Ureal
) return Uint
;
158 -- Return the denominator of the universal real after a normalization
160 function Norm_Num
(Real
: Ureal
) return Uint
;
161 -- Return the numerator of the universal real after a normalization
163 function UR_From_Uint
(UI
: Uint
) return Ureal
;
164 -- Returns real corresponding to universal integer value
166 function UR_To_Uint
(Real
: Ureal
) return Uint
;
167 -- Return integer value obtained by accurate rounding of real value.
168 -- The rounding of values half way between two integers is away from
169 -- zero, as required by normal Ada 95 rounding semantics.
171 function UR_Trunc
(Real
: Ureal
) return Uint
;
172 -- Return integer value obtained by a truncation of real towards zero
174 function UR_Ceiling
(Real
: Ureal
) return Uint
;
175 -- Return value of smallest integer not less than the given value
177 function UR_Floor
(Real
: Ureal
) return Uint
;
178 -- Return value of smallest integer not greater than the given value
180 -- Conversion table for above four functions
182 -- Input To_Uint Trunc Ceiling Floor
194 function UR_From_Components
198 Negative
: Boolean := False)
200 -- Builds real value from given numerator, denominator and base. The
201 -- value is negative if Negative is set to true, and otherwise is
204 function UR_Add
(Left
: Ureal
; Right
: Ureal
) return Ureal
;
205 function UR_Add
(Left
: Ureal
; Right
: Uint
) return Ureal
;
206 function UR_Add
(Left
: Uint
; Right
: Ureal
) return Ureal
;
207 -- Returns real sum of operands
209 function UR_Div
(Left
: Ureal
; Right
: Ureal
) return Ureal
;
210 function UR_Div
(Left
: Uint
; Right
: Ureal
) return Ureal
;
211 function UR_Div
(Left
: Ureal
; Right
: Uint
) return Ureal
;
212 -- Returns real quotient of operands. Fatal error if Right is zero
214 function UR_Mul
(Left
: Ureal
; Right
: Ureal
) return Ureal
;
215 function UR_Mul
(Left
: Uint
; Right
: Ureal
) return Ureal
;
216 function UR_Mul
(Left
: Ureal
; Right
: Uint
) return Ureal
;
217 -- Returns real product of operands
219 function UR_Sub
(Left
: Ureal
; Right
: Ureal
) return Ureal
;
220 function UR_Sub
(Left
: Uint
; Right
: Ureal
) return Ureal
;
221 function UR_Sub
(Left
: Ureal
; Right
: Uint
) return Ureal
;
222 -- Returns real difference of operands
224 function UR_Exponentiate
(Real
: Ureal
; N
: Uint
) return Ureal
;
225 -- Returns result of raising Ureal to Uint power.
226 -- Fatal error if Left is 0 and Right is negative.
228 function UR_Abs
(Real
: Ureal
) return Ureal
;
229 -- Returns abs function of real
231 function UR_Negate
(Real
: Ureal
) return Ureal
;
232 -- Returns negative of real
234 function UR_Eq
(Left
, Right
: Ureal
) return Boolean;
235 -- Compares reals for equality
237 function UR_Max
(Left
, Right
: Ureal
) return Ureal
;
238 -- Returns the maximum of two reals
240 function UR_Min
(Left
, Right
: Ureal
) return Ureal
;
241 -- Returns the minimum of two reals
243 function UR_Ne
(Left
, Right
: Ureal
) return Boolean;
244 -- Compares reals for inequality
246 function UR_Lt
(Left
, Right
: Ureal
) return Boolean;
247 -- Compares reals for less than
249 function UR_Le
(Left
, Right
: Ureal
) return Boolean;
250 -- Compares reals for less than or equal
252 function UR_Gt
(Left
, Right
: Ureal
) return Boolean;
253 -- Compares reals for greater than
255 function UR_Ge
(Left
, Right
: Ureal
) return Boolean;
256 -- Compares reals for greater than or equal
258 function UR_Is_Zero
(Real
: Ureal
) return Boolean;
259 -- Tests if real value is zero
261 function UR_Is_Negative
(Real
: Ureal
) return Boolean;
262 -- Tests if real value is negative, note that negative zero gives true
264 function UR_Is_Positive
(Real
: Ureal
) return Boolean;
265 -- Test if real value is greater than zero
267 procedure UR_Write
(Real
: Ureal
; Brackets
: Boolean := False);
268 -- Writes value of Real to standard output. Used for debugging and
269 -- tree/source output, and also for -gnatR representation output. If the
270 -- result is easily representable as a standard Ada literal, it will be
271 -- given that way, but as a result of evaluation of static expressions, it
272 -- is possible to generate constants (e.g. 1/13) which have no such
273 -- representation. In such cases (and in cases where it is too much work to
274 -- figure out the Ada literal), the string that is output is of the form
275 -- of some expression such as integer/integer, or integer*integer**integer.
276 -- In the case where an expression is output, if Brackets is set to True,
277 -- the expression is surrounded by square brackets.
279 procedure pr
(Real
: Ureal
);
280 pragma Export
(Ada
, pr
);
281 -- Writes value of Real to standard output with a terminating line return,
282 -- using UR_Write as described above. This is for use from the debugger.
284 ------------------------
285 -- Operator Renamings --
286 ------------------------
288 function "+" (Left
: Ureal
; Right
: Ureal
) return Ureal
renames UR_Add
;
289 function "+" (Left
: Uint
; Right
: Ureal
) return Ureal
renames UR_Add
;
290 function "+" (Left
: Ureal
; Right
: Uint
) return Ureal
renames UR_Add
;
292 function "/" (Left
: Ureal
; Right
: Ureal
) return Ureal
renames UR_Div
;
293 function "/" (Left
: Uint
; Right
: Ureal
) return Ureal
renames UR_Div
;
294 function "/" (Left
: Ureal
; Right
: Uint
) return Ureal
renames UR_Div
;
296 function "*" (Left
: Ureal
; Right
: Ureal
) return Ureal
renames UR_Mul
;
297 function "*" (Left
: Uint
; Right
: Ureal
) return Ureal
renames UR_Mul
;
298 function "*" (Left
: Ureal
; Right
: Uint
) return Ureal
renames UR_Mul
;
300 function "-" (Left
: Ureal
; Right
: Ureal
) return Ureal
renames UR_Sub
;
301 function "-" (Left
: Uint
; Right
: Ureal
) return Ureal
renames UR_Sub
;
302 function "-" (Left
: Ureal
; Right
: Uint
) return Ureal
renames UR_Sub
;
304 function "**" (Real
: Ureal
; N
: Uint
) return Ureal
305 renames UR_Exponentiate
;
307 function "abs" (Real
: Ureal
) return Ureal
renames UR_Abs
;
309 function "-" (Real
: Ureal
) return Ureal
renames UR_Negate
;
311 function "=" (Left
, Right
: Ureal
) return Boolean renames UR_Eq
;
313 function "<" (Left
, Right
: Ureal
) return Boolean renames UR_Lt
;
315 function "<=" (Left
, Right
: Ureal
) return Boolean renames UR_Le
;
317 function ">=" (Left
, Right
: Ureal
) return Boolean renames UR_Ge
;
319 function ">" (Left
, Right
: Ureal
) return Boolean renames UR_Gt
;
321 -----------------------------
322 -- Mark/Release Processing --
323 -----------------------------
325 -- The space used by Ureal data is not automatically reclaimed. However,
326 -- a mark-release regime is implemented which allows storage to be
327 -- released back to a previously noted mark. This is used for example
328 -- when doing comparisons, where only intermediate results get stored
329 -- that do not need to be saved for future use.
331 type Save_Mark
is private;
333 function Mark
return Save_Mark
;
334 -- Note mark point for future release
336 procedure Release
(M
: Save_Mark
);
337 -- Release storage allocated since mark was noted
339 ------------------------------------
340 -- Representation of Ureal Values --
341 ------------------------------------
345 type Ureal
is new Int
range Ureal_Low_Bound
.. Ureal_High_Bound
;
346 for Ureal
'Size use 32;
348 No_Ureal
: constant Ureal
:= Ureal
'First;
350 type Save_Mark
is new Int
;
352 pragma Inline
(Denominator
);
353 pragma Inline
(Mark
);
354 pragma Inline
(Norm_Num
);
355 pragma Inline
(Norm_Den
);
356 pragma Inline
(Numerator
);
357 pragma Inline
(Rbase
);
358 pragma Inline
(Release
);
359 pragma Inline
(Ureal_0
);
360 pragma Inline
(Ureal_M_0
);
361 pragma Inline
(Ureal_Tenth
);
362 pragma Inline
(Ureal_Half
);
363 pragma Inline
(Ureal_1
);
364 pragma Inline
(Ureal_2
);
365 pragma Inline
(Ureal_10
);
366 pragma Inline
(UR_From_Components
);