| 1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
| 2 |
|
|
-- --
|
| 3 |
|
|
-- GNAT COMPILER COMPONENTS --
|
| 4 |
|
|
-- --
|
| 5 |
|
|
-- U R E A L P --
|
| 6 |
|
|
-- --
|
| 7 |
|
|
-- S p e c --
|
| 8 |
|
|
-- --
|
| 9 |
|
|
-- Copyright (C) 1992-2009 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 |
|
|
-- Support for universal real arithmetic
|
| 33 |
|
|
|
| 34 |
|
|
with Types; use Types;
|
| 35 |
|
|
with Uintp; use Uintp;
|
| 36 |
|
|
|
| 37 |
|
|
package Urealp is
|
| 38 |
|
|
|
| 39 |
|
|
---------------------------------------
|
| 40 |
|
|
-- Representation of Universal Reals --
|
| 41 |
|
|
---------------------------------------
|
| 42 |
|
|
|
| 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).
|
| 47 |
|
|
|
| 48 |
|
|
-- A Ureal value represents an arbitrary precision universal real value,
|
| 49 |
|
|
-- stored internally using four components
|
| 50 |
|
|
|
| 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
|
| 55 |
|
|
|
| 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).
|
| 59 |
|
|
|
| 60 |
|
|
-- Negative numbers are represented by the sign of the numerator being
|
| 61 |
|
|
-- negative. The denominator is always positive.
|
| 62 |
|
|
|
| 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.
|
| 68 |
|
|
|
| 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").
|
| 72 |
|
|
|
| 73 |
|
|
------------------------------
|
| 74 |
|
|
-- Types for Urealp Package --
|
| 75 |
|
|
------------------------------
|
| 76 |
|
|
|
| 77 |
|
|
type Ureal is private;
|
| 78 |
|
|
-- Type used for representation of universal reals
|
| 79 |
|
|
|
| 80 |
|
|
No_Ureal : constant Ureal;
|
| 81 |
|
|
-- Constant used to indicate missing or unset Ureal value
|
| 82 |
|
|
|
| 83 |
|
|
---------------------
|
| 84 |
|
|
-- Ureal Constants --
|
| 85 |
|
|
---------------------
|
| 86 |
|
|
|
| 87 |
|
|
function Ureal_0 return Ureal;
|
| 88 |
|
|
-- Returns value 0.0
|
| 89 |
|
|
|
| 90 |
|
|
function Ureal_M_0 return Ureal;
|
| 91 |
|
|
-- Returns value -0.0
|
| 92 |
|
|
|
| 93 |
|
|
function Ureal_Tenth return Ureal;
|
| 94 |
|
|
-- Returns value 0.1
|
| 95 |
|
|
|
| 96 |
|
|
function Ureal_Half return Ureal;
|
| 97 |
|
|
-- Returns value 0.5
|
| 98 |
|
|
|
| 99 |
|
|
function Ureal_1 return Ureal;
|
| 100 |
|
|
-- Returns value 1.0
|
| 101 |
|
|
|
| 102 |
|
|
function Ureal_2 return Ureal;
|
| 103 |
|
|
-- Returns value 2.0
|
| 104 |
|
|
|
| 105 |
|
|
function Ureal_10 return Ureal;
|
| 106 |
|
|
-- Returns value 10.0
|
| 107 |
|
|
|
| 108 |
|
|
function Ureal_100 return Ureal;
|
| 109 |
|
|
-- Returns value 100.0
|
| 110 |
|
|
|
| 111 |
|
|
function Ureal_2_80 return Ureal;
|
| 112 |
|
|
-- Returns value 2.0 ** 80
|
| 113 |
|
|
|
| 114 |
|
|
function Ureal_2_M_80 return Ureal;
|
| 115 |
|
|
-- Returns value 2.0 ** (-80)
|
| 116 |
|
|
|
| 117 |
|
|
function Ureal_2_128 return Ureal;
|
| 118 |
|
|
-- Returns value 2.0 ** 128
|
| 119 |
|
|
|
| 120 |
|
|
function Ureal_2_M_128 return Ureal;
|
| 121 |
|
|
-- Returns value 2.0 ** (-128)
|
| 122 |
|
|
|
| 123 |
|
|
function Ureal_10_36 return Ureal;
|
| 124 |
|
|
-- Returns value 10.0 ** 36
|
| 125 |
|
|
|
| 126 |
|
|
function Ureal_M_10_36 return Ureal;
|
| 127 |
|
|
-- Returns value -(10.0
|
| 128 |
|
|
|
| 129 |
|
|
-----------------
|
| 130 |
|
|
-- Subprograms --
|
| 131 |
|
|
-----------------
|
| 132 |
|
|
|
| 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.
|
| 138 |
|
|
|
| 139 |
|
|
procedure Tree_Read;
|
| 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.
|
| 143 |
|
|
|
| 144 |
|
|
procedure Tree_Write;
|
| 145 |
|
|
-- Writes out internal tables to current tree file using the relevant
|
| 146 |
|
|
-- Table.Tree_Write routines.
|
| 147 |
|
|
|
| 148 |
|
|
function Rbase (Real : Ureal) return Nat;
|
| 149 |
|
|
-- Return the base of the universal real
|
| 150 |
|
|
|
| 151 |
|
|
function Denominator (Real : Ureal) return Uint;
|
| 152 |
|
|
-- Return the denominator of the universal real
|
| 153 |
|
|
|
| 154 |
|
|
function Numerator (Real : Ureal) return Uint;
|
| 155 |
|
|
-- Return the numerator of the universal real
|
| 156 |
|
|
|
| 157 |
|
|
function Norm_Den (Real : Ureal) return Uint;
|
| 158 |
|
|
-- Return the denominator of the universal real after a normalization
|
| 159 |
|
|
|
| 160 |
|
|
function Norm_Num (Real : Ureal) return Uint;
|
| 161 |
|
|
-- Return the numerator of the universal real after a normalization
|
| 162 |
|
|
|
| 163 |
|
|
function UR_From_Uint (UI : Uint) return Ureal;
|
| 164 |
|
|
-- Returns real corresponding to universal integer value
|
| 165 |
|
|
|
| 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.
|
| 170 |
|
|
|
| 171 |
|
|
function UR_Trunc (Real : Ureal) return Uint;
|
| 172 |
|
|
-- Return integer value obtained by a truncation of real towards zero
|
| 173 |
|
|
|
| 174 |
|
|
function UR_Ceiling (Real : Ureal) return Uint;
|
| 175 |
|
|
-- Return value of smallest integer not less than the given value
|
| 176 |
|
|
|
| 177 |
|
|
function UR_Floor (Real : Ureal) return Uint;
|
| 178 |
|
|
-- Return value of smallest integer not greater than the given value
|
| 179 |
|
|
|
| 180 |
|
|
-- Conversion table for above four functions
|
| 181 |
|
|
|
| 182 |
|
|
-- Input To_Uint Trunc Ceiling Floor
|
| 183 |
|
|
-- 1.0 1 1 1 1
|
| 184 |
|
|
-- 1.2 1 1 2 1
|
| 185 |
|
|
-- 1.5 2 1 2 1
|
| 186 |
|
|
-- 1.7 2 1 2 1
|
| 187 |
|
|
-- 2.0 2 2 2 2
|
| 188 |
|
|
-- -1.0 -1 -1 -1 -1
|
| 189 |
|
|
-- -1.2 -1 -1 -1 -2
|
| 190 |
|
|
-- -1.5 -2 -1 -1 -2
|
| 191 |
|
|
-- -1.7 -2 -1 -1 -2
|
| 192 |
|
|
-- -2.0 -2 -2 -2 -2
|
| 193 |
|
|
|
| 194 |
|
|
function UR_From_Components
|
| 195 |
|
|
(Num : Uint;
|
| 196 |
|
|
Den : Uint;
|
| 197 |
|
|
Rbase : Nat := 0;
|
| 198 |
|
|
Negative : Boolean := False)
|
| 199 |
|
|
return Ureal;
|
| 200 |
|
|
-- Builds real value from given numerator, denominator and base. The
|
| 201 |
|
|
-- value is negative if Negative is set to true, and otherwise is
|
| 202 |
|
|
-- non-negative.
|
| 203 |
|
|
|
| 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
|
| 208 |
|
|
|
| 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
|
| 213 |
|
|
|
| 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
|
| 218 |
|
|
|
| 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
|
| 223 |
|
|
|
| 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.
|
| 227 |
|
|
|
| 228 |
|
|
function UR_Abs (Real : Ureal) return Ureal;
|
| 229 |
|
|
-- Returns abs function of real
|
| 230 |
|
|
|
| 231 |
|
|
function UR_Negate (Real : Ureal) return Ureal;
|
| 232 |
|
|
-- Returns negative of real
|
| 233 |
|
|
|
| 234 |
|
|
function UR_Eq (Left, Right : Ureal) return Boolean;
|
| 235 |
|
|
-- Compares reals for equality
|
| 236 |
|
|
|
| 237 |
|
|
function UR_Max (Left, Right : Ureal) return Ureal;
|
| 238 |
|
|
-- Returns the maximum of two reals
|
| 239 |
|
|
|
| 240 |
|
|
function UR_Min (Left, Right : Ureal) return Ureal;
|
| 241 |
|
|
-- Returns the minimum of two reals
|
| 242 |
|
|
|
| 243 |
|
|
function UR_Ne (Left, Right : Ureal) return Boolean;
|
| 244 |
|
|
-- Compares reals for inequality
|
| 245 |
|
|
|
| 246 |
|
|
function UR_Lt (Left, Right : Ureal) return Boolean;
|
| 247 |
|
|
-- Compares reals for less than
|
| 248 |
|
|
|
| 249 |
|
|
function UR_Le (Left, Right : Ureal) return Boolean;
|
| 250 |
|
|
-- Compares reals for less than or equal
|
| 251 |
|
|
|
| 252 |
|
|
function UR_Gt (Left, Right : Ureal) return Boolean;
|
| 253 |
|
|
-- Compares reals for greater than
|
| 254 |
|
|
|
| 255 |
|
|
function UR_Ge (Left, Right : Ureal) return Boolean;
|
| 256 |
|
|
-- Compares reals for greater than or equal
|
| 257 |
|
|
|
| 258 |
|
|
function UR_Is_Zero (Real : Ureal) return Boolean;
|
| 259 |
|
|
-- Tests if real value is zero
|
| 260 |
|
|
|
| 261 |
|
|
function UR_Is_Negative (Real : Ureal) return Boolean;
|
| 262 |
|
|
-- Tests if real value is negative, note that negative zero gives true
|
| 263 |
|
|
|
| 264 |
|
|
function UR_Is_Positive (Real : Ureal) return Boolean;
|
| 265 |
|
|
-- Test if real value is greater than zero
|
| 266 |
|
|
|
| 267 |
|
|
procedure UR_Write (Real : Ureal);
|
| 268 |
|
|
-- Writes value of Real to standard output. Used only for debugging and
|
| 269 |
|
|
-- tree/source output. If the result is easily representable as a standard
|
| 270 |
|
|
-- Ada literal, it will be given that way, but as a result of evaluation
|
| 271 |
|
|
-- of static expressions, it is possible to generate constants (e.g. 1/13)
|
| 272 |
|
|
-- which have no such representation. In such cases (and in cases where it
|
| 273 |
|
|
-- is too much work to figure out the Ada literal), the string that is
|
| 274 |
|
|
-- output is of the form [numerator/denominator].
|
| 275 |
|
|
|
| 276 |
|
|
procedure pr (Real : Ureal);
|
| 277 |
|
|
pragma Export (Ada, pr);
|
| 278 |
|
|
-- Writes value of Real to standard output with a terminating line return,
|
| 279 |
|
|
-- using UR_Write as described above. This is for use from the debugger.
|
| 280 |
|
|
|
| 281 |
|
|
------------------------
|
| 282 |
|
|
-- Operator Renamings --
|
| 283 |
|
|
------------------------
|
| 284 |
|
|
|
| 285 |
|
|
function "+" (Left : Ureal; Right : Ureal) return Ureal renames UR_Add;
|
| 286 |
|
|
function "+" (Left : Uint; Right : Ureal) return Ureal renames UR_Add;
|
| 287 |
|
|
function "+" (Left : Ureal; Right : Uint) return Ureal renames UR_Add;
|
| 288 |
|
|
|
| 289 |
|
|
function "/" (Left : Ureal; Right : Ureal) return Ureal renames UR_Div;
|
| 290 |
|
|
function "/" (Left : Uint; Right : Ureal) return Ureal renames UR_Div;
|
| 291 |
|
|
function "/" (Left : Ureal; Right : Uint) return Ureal renames UR_Div;
|
| 292 |
|
|
|
| 293 |
|
|
function "*" (Left : Ureal; Right : Ureal) return Ureal renames UR_Mul;
|
| 294 |
|
|
function "*" (Left : Uint; Right : Ureal) return Ureal renames UR_Mul;
|
| 295 |
|
|
function "*" (Left : Ureal; Right : Uint) return Ureal renames UR_Mul;
|
| 296 |
|
|
|
| 297 |
|
|
function "-" (Left : Ureal; Right : Ureal) return Ureal renames UR_Sub;
|
| 298 |
|
|
function "-" (Left : Uint; Right : Ureal) return Ureal renames UR_Sub;
|
| 299 |
|
|
function "-" (Left : Ureal; Right : Uint) return Ureal renames UR_Sub;
|
| 300 |
|
|
|
| 301 |
|
|
function "**" (Real : Ureal; N : Uint) return Ureal
|
| 302 |
|
|
renames UR_Exponentiate;
|
| 303 |
|
|
|
| 304 |
|
|
function "abs" (Real : Ureal) return Ureal renames UR_Abs;
|
| 305 |
|
|
|
| 306 |
|
|
function "-" (Real : Ureal) return Ureal renames UR_Negate;
|
| 307 |
|
|
|
| 308 |
|
|
function "=" (Left, Right : Ureal) return Boolean renames UR_Eq;
|
| 309 |
|
|
|
| 310 |
|
|
function "<" (Left, Right : Ureal) return Boolean renames UR_Lt;
|
| 311 |
|
|
|
| 312 |
|
|
function "<=" (Left, Right : Ureal) return Boolean renames UR_Le;
|
| 313 |
|
|
|
| 314 |
|
|
function ">=" (Left, Right : Ureal) return Boolean renames UR_Ge;
|
| 315 |
|
|
|
| 316 |
|
|
function ">" (Left, Right : Ureal) return Boolean renames UR_Gt;
|
| 317 |
|
|
|
| 318 |
|
|
-----------------------------
|
| 319 |
|
|
-- Mark/Release Processing --
|
| 320 |
|
|
-----------------------------
|
| 321 |
|
|
|
| 322 |
|
|
-- The space used by Ureal data is not automatically reclaimed. However,
|
| 323 |
|
|
-- a mark-release regime is implemented which allows storage to be
|
| 324 |
|
|
-- released back to a previously noted mark. This is used for example
|
| 325 |
|
|
-- when doing comparisons, where only intermediate results get stored
|
| 326 |
|
|
-- that do not need to be saved for future use.
|
| 327 |
|
|
|
| 328 |
|
|
type Save_Mark is private;
|
| 329 |
|
|
|
| 330 |
|
|
function Mark return Save_Mark;
|
| 331 |
|
|
-- Note mark point for future release
|
| 332 |
|
|
|
| 333 |
|
|
procedure Release (M : Save_Mark);
|
| 334 |
|
|
-- Release storage allocated since mark was noted
|
| 335 |
|
|
|
| 336 |
|
|
------------------------------------
|
| 337 |
|
|
-- Representation of Ureal Values --
|
| 338 |
|
|
------------------------------------
|
| 339 |
|
|
|
| 340 |
|
|
private
|
| 341 |
|
|
|
| 342 |
|
|
type Ureal is new Int range Ureal_Low_Bound .. Ureal_High_Bound;
|
| 343 |
|
|
for Ureal'Size use 32;
|
| 344 |
|
|
|
| 345 |
|
|
No_Ureal : constant Ureal := Ureal'First;
|
| 346 |
|
|
|
| 347 |
|
|
type Save_Mark is new Int;
|
| 348 |
|
|
|
| 349 |
|
|
pragma Inline (Denominator);
|
| 350 |
|
|
pragma Inline (Mark);
|
| 351 |
|
|
pragma Inline (Norm_Num);
|
| 352 |
|
|
pragma Inline (Norm_Den);
|
| 353 |
|
|
pragma Inline (Numerator);
|
| 354 |
|
|
pragma Inline (Rbase);
|
| 355 |
|
|
pragma Inline (Release);
|
| 356 |
|
|
pragma Inline (Ureal_0);
|
| 357 |
|
|
pragma Inline (Ureal_M_0);
|
| 358 |
|
|
pragma Inline (Ureal_Tenth);
|
| 359 |
|
|
pragma Inline (Ureal_Half);
|
| 360 |
|
|
pragma Inline (Ureal_1);
|
| 361 |
|
|
pragma Inline (Ureal_2);
|
| 362 |
|
|
pragma Inline (Ureal_10);
|
| 363 |
|
|
pragma Inline (UR_From_Components);
|
| 364 |
|
|
|
| 365 |
|
|
end Urealp;
|