OSDN Git Service

2007-04-20 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / urealp.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                               U R E A L P                                --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, 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 Alloc;
35 with Output;  use Output;
36 with Table;
37 with Tree_IO; use Tree_IO;
38
39 package body Urealp is
40
41    Ureal_First_Entry : constant Ureal := Ureal'Succ (No_Ureal);
42    --  First subscript allocated in Ureal table (note that we can't just
43    --  add 1 to No_Ureal, since "+" means something different for Ureals!
44
45    type Ureal_Entry is record
46       Num  : Uint;
47       --  Numerator (always non-negative)
48
49       Den  : Uint;
50       --  Denominator (always non-zero, always positive if base is zero)
51
52       Rbase : Nat;
53       --  Base value. If Rbase is zero, then the value is simply Num / Den.
54       --  If Rbase is non-zero, then the value is Num / (Rbase ** Den)
55
56       Negative : Boolean;
57       --  Flag set if value is negative
58    end record;
59
60    --  The following representation clause ensures that the above record
61    --  has no holes. We do this so that when instances of this record are
62    --  written by Tree_Gen, we do not write uninitialized values to the file.
63
64    for Ureal_Entry use record
65       Num      at  0 range 0 .. 31;
66       Den      at  4 range 0 .. 31;
67       Rbase    at  8 range 0 .. 31;
68       Negative at 12 range 0 .. 31;
69    end record;
70
71    for Ureal_Entry'Size use 16 * 8;
72    --  This ensures that we did not leave out any fields
73
74    package Ureals is new Table.Table (
75      Table_Component_Type => Ureal_Entry,
76      Table_Index_Type     => Ureal'Base,
77      Table_Low_Bound      => Ureal_First_Entry,
78      Table_Initial        => Alloc.Ureals_Initial,
79      Table_Increment      => Alloc.Ureals_Increment,
80      Table_Name           => "Ureals");
81
82    --  The following universal reals are the values returned by the constant
83    --  functions. They are initialized by the initialization procedure.
84
85    UR_0          : Ureal;
86    UR_M_0        : Ureal;
87    UR_Tenth      : Ureal;
88    UR_Half       : Ureal;
89    UR_1          : Ureal;
90    UR_2          : Ureal;
91    UR_10         : Ureal;
92    UR_10_36      : Ureal;
93    UR_M_10_36    : Ureal;
94    UR_100        : Ureal;
95    UR_2_128      : Ureal;
96    UR_2_80       : Ureal;
97    UR_2_M_128    : Ureal;
98    UR_2_M_80     : Ureal;
99
100    Num_Ureal_Constants : constant := 10;
101    --  This is used for an assertion check in Tree_Read and Tree_Write to
102    --  help remember to add values to these routines when we add to the list.
103
104    Normalized_Real : Ureal := No_Ureal;
105    --  Used to memoize Norm_Num and Norm_Den, if either of these functions
106    --  is called, this value is set and Normalized_Entry contains the result
107    --  of the normalization. On subsequent calls, this is used to avoid the
108    --  call to Normalize if it has already been made.
109
110    Normalized_Entry : Ureal_Entry;
111    --  Entry built by most recent call to Normalize
112
113    -----------------------
114    -- Local Subprograms --
115    -----------------------
116
117    function Decimal_Exponent_Hi (V : Ureal) return Int;
118    --  Returns an estimate of the exponent of Val represented as a normalized
119    --  decimal number (non-zero digit before decimal point), The estimate is
120    --  either correct, or high, but never low. The accuracy of the estimate
121    --  affects only the efficiency of the comparison routines.
122
123    function Decimal_Exponent_Lo (V : Ureal) return Int;
124    --  Returns an estimate of the exponent of Val represented as a normalized
125    --  decimal number (non-zero digit before decimal point), The estimate is
126    --  either correct, or low, but never high. The accuracy of the estimate
127    --  affects only the efficiency of the comparison routines.
128
129    function Equivalent_Decimal_Exponent (U : Ureal_Entry) return Int;
130    --  U is a Ureal entry for which the base value is non-zero, the value
131    --  returned is the equivalent decimal exponent value, i.e. the value of
132    --  Den, adjusted as though the base were base 10. The value is rounded
133    --  to the nearest integer, and so can be one off.
134
135    function Is_Integer (Num, Den : Uint) return Boolean;
136    --  Return true if the real quotient of Num / Den is an integer value
137
138    function Normalize (Val : Ureal_Entry) return Ureal_Entry;
139    --  Normalizes the Ureal_Entry by reducing it to lowest terms (with a
140    --  base value of 0).
141
142    function Same (U1, U2 : Ureal) return Boolean;
143    pragma Inline (Same);
144    --  Determines if U1 and U2 are the same Ureal. Note that we cannot use
145    --  the equals operator for this test, since that tests for equality,
146    --  not identity.
147
148    function Store_Ureal (Val : Ureal_Entry) return Ureal;
149    --  This store a new entry in the universal reals table and return
150    --  its index in the table.
151
152    -------------------------
153    -- Decimal_Exponent_Hi --
154    -------------------------
155
156    function Decimal_Exponent_Hi (V : Ureal) return Int is
157       Val : constant Ureal_Entry := Ureals.Table (V);
158
159    begin
160       --  Zero always returns zero
161
162       if UR_Is_Zero (V) then
163          return 0;
164
165       --  For numbers in rational form, get the maximum number of digits in the
166       --  numerator and the minimum number of digits in the denominator, and
167       --  subtract. For example:
168
169       --     1000 / 99 = 1.010E+1
170       --     9999 / 10 = 9.999E+2
171
172       --  This estimate may of course be high, but that is acceptable
173
174       elsif Val.Rbase = 0 then
175          return UI_Decimal_Digits_Hi (Val.Num) -
176                 UI_Decimal_Digits_Lo (Val.Den);
177
178       --  For based numbers, just subtract the decimal exponent from the
179       --  high estimate of the number of digits in the numerator and add
180       --  one to accommodate possible round off errors for non-decimal
181       --  bases. For example:
182
183       --     1_500_000 / 10**4 = 1.50E-2
184
185       else -- Val.Rbase /= 0
186          return UI_Decimal_Digits_Hi (Val.Num) -
187                 Equivalent_Decimal_Exponent (Val) + 1;
188       end if;
189    end Decimal_Exponent_Hi;
190
191    -------------------------
192    -- Decimal_Exponent_Lo --
193    -------------------------
194
195    function Decimal_Exponent_Lo (V : Ureal) return Int is
196       Val : constant Ureal_Entry := Ureals.Table (V);
197
198    begin
199       --  Zero always returns zero
200
201       if UR_Is_Zero (V) then
202          return 0;
203
204       --  For numbers in rational form, get min digits in numerator, max digits
205       --  in denominator, and subtract and subtract one more for possible loss
206       --  during the division. For example:
207
208       --     1000 / 99 = 1.010E+1
209       --     9999 / 10 = 9.999E+2
210
211       --  This estimate may of course be low, but that is acceptable
212
213       elsif Val.Rbase = 0 then
214          return UI_Decimal_Digits_Lo (Val.Num) -
215                 UI_Decimal_Digits_Hi (Val.Den) - 1;
216
217       --  For based numbers, just subtract the decimal exponent from the
218       --  low estimate of the number of digits in the numerator and subtract
219       --  one to accommodate possible round off errors for non-decimal
220       --  bases. For example:
221
222       --     1_500_000 / 10**4 = 1.50E-2
223
224       else -- Val.Rbase /= 0
225          return UI_Decimal_Digits_Lo (Val.Num) -
226                 Equivalent_Decimal_Exponent (Val) - 1;
227       end if;
228    end Decimal_Exponent_Lo;
229
230    -----------------
231    -- Denominator --
232    -----------------
233
234    function Denominator (Real : Ureal) return Uint is
235    begin
236       return Ureals.Table (Real).Den;
237    end Denominator;
238
239    ---------------------------------
240    -- Equivalent_Decimal_Exponent --
241    ---------------------------------
242
243    function Equivalent_Decimal_Exponent (U : Ureal_Entry) return Int is
244
245       --  The following table is a table of logs to the base 10
246
247       Logs : constant array (Nat range 1 .. 16) of Long_Float := (
248                 1 => 0.000000000000000,
249                 2 => 0.301029995663981,
250                 3 => 0.477121254719662,
251                 4 => 0.602059991327962,
252                 5 => 0.698970004336019,
253                 6 => 0.778151250383644,
254                 7 => 0.845098040014257,
255                 8 => 0.903089986991944,
256                 9 => 0.954242509439325,
257                10 => 1.000000000000000,
258                11 => 1.041392685158230,
259                12 => 1.079181246047620,
260                13 => 1.113943352306840,
261                14 => 1.146128035678240,
262                15 => 1.176091259055680,
263                16 => 1.204119982655920);
264
265    begin
266       pragma Assert (U.Rbase /= 0);
267       return Int (Long_Float (UI_To_Int (U.Den)) * Logs (U.Rbase));
268    end Equivalent_Decimal_Exponent;
269
270    ----------------
271    -- Initialize --
272    ----------------
273
274    procedure Initialize is
275    begin
276       Ureals.Init;
277       UR_0       := UR_From_Components (Uint_0, Uint_1,         0, False);
278       UR_M_0     := UR_From_Components (Uint_0, Uint_1,         0, True);
279       UR_Half    := UR_From_Components (Uint_1, Uint_1,         2, False);
280       UR_Tenth   := UR_From_Components (Uint_1, Uint_1,        10, False);
281       UR_1       := UR_From_Components (Uint_1, Uint_1,         0, False);
282       UR_2       := UR_From_Components (Uint_1, Uint_Minus_1,   2, False);
283       UR_10      := UR_From_Components (Uint_1, Uint_Minus_1,  10, False);
284       UR_10_36   := UR_From_Components (Uint_1, Uint_Minus_36, 10, False);
285       UR_M_10_36 := UR_From_Components (Uint_1, Uint_Minus_36, 10, True);
286       UR_100     := UR_From_Components (Uint_1, Uint_Minus_2,  10, False);
287       UR_2_128   := UR_From_Components (Uint_1, Uint_Minus_128, 2, False);
288       UR_2_M_128 := UR_From_Components (Uint_1, Uint_128,       2, False);
289       UR_2_80    := UR_From_Components (Uint_1, Uint_Minus_80,  2, False);
290       UR_2_M_80  := UR_From_Components (Uint_1, Uint_80,        2, False);
291    end Initialize;
292
293    ----------------
294    -- Is_Integer --
295    ----------------
296
297    function Is_Integer (Num, Den : Uint) return Boolean is
298    begin
299       return (Num / Den) * Den = Num;
300    end Is_Integer;
301
302    ----------
303    -- Mark --
304    ----------
305
306    function Mark return Save_Mark is
307    begin
308       return Save_Mark (Ureals.Last);
309    end Mark;
310
311    --------------
312    -- Norm_Den --
313    --------------
314
315    function Norm_Den (Real : Ureal) return Uint is
316    begin
317       if not Same (Real, Normalized_Real) then
318          Normalized_Real  := Real;
319          Normalized_Entry := Normalize (Ureals.Table (Real));
320       end if;
321
322       return Normalized_Entry.Den;
323    end Norm_Den;
324
325    --------------
326    -- Norm_Num --
327    --------------
328
329    function Norm_Num (Real : Ureal) return Uint is
330    begin
331       if not Same (Real, Normalized_Real) then
332          Normalized_Real  := Real;
333          Normalized_Entry := Normalize (Ureals.Table (Real));
334       end if;
335
336       return Normalized_Entry.Num;
337    end Norm_Num;
338
339    ---------------
340    -- Normalize --
341    ---------------
342
343    function Normalize (Val : Ureal_Entry) return Ureal_Entry is
344       J   : Uint;
345       K   : Uint;
346       Tmp : Uint;
347       Num : Uint;
348       Den : Uint;
349       M   : constant Uintp.Save_Mark := Uintp.Mark;
350
351    begin
352       --  Start by setting J to the greatest of the absolute values of the
353       --  numerator and the denominator (taking into account the base value),
354       --  and K to the lesser of the two absolute values. The gcd of Num and
355       --  Den is the gcd of J and K.
356
357       if Val.Rbase = 0 then
358          J := Val.Num;
359          K := Val.Den;
360
361       elsif Val.Den < 0 then
362          J := Val.Num * Val.Rbase ** (-Val.Den);
363          K := Uint_1;
364
365       else
366          J := Val.Num;
367          K := Val.Rbase ** Val.Den;
368       end if;
369
370       Num := J;
371       Den := K;
372
373       if K > J then
374          Tmp := J;
375          J := K;
376          K := Tmp;
377       end if;
378
379       J := UI_GCD (J, K);
380       Num := Num / J;
381       Den := Den / J;
382       Uintp.Release_And_Save (M, Num, Den);
383
384       --  Divide numerator and denominator by gcd and return result
385
386       return (Num      => Num,
387               Den      => Den,
388               Rbase    => 0,
389               Negative => Val.Negative);
390    end Normalize;
391
392    ---------------
393    -- Numerator --
394    ---------------
395
396    function Numerator (Real : Ureal) return Uint is
397    begin
398       return Ureals.Table (Real).Num;
399    end Numerator;
400
401    --------
402    -- pr --
403    --------
404
405    procedure pr (Real : Ureal) is
406    begin
407       UR_Write (Real);
408       Write_Eol;
409    end pr;
410
411    -----------
412    -- Rbase --
413    -----------
414
415    function Rbase (Real : Ureal) return Nat is
416    begin
417       return Ureals.Table (Real).Rbase;
418    end Rbase;
419
420    -------------
421    -- Release --
422    -------------
423
424    procedure Release (M : Save_Mark) is
425    begin
426       Ureals.Set_Last (Ureal (M));
427    end Release;
428
429    ----------
430    -- Same --
431    ----------
432
433    function Same (U1, U2 : Ureal) return Boolean is
434    begin
435       return Int (U1) = Int (U2);
436    end Same;
437
438    -----------------
439    -- Store_Ureal --
440    -----------------
441
442    function Store_Ureal (Val : Ureal_Entry) return Ureal is
443    begin
444       Ureals.Increment_Last;
445       Ureals.Table (Ureals.Last) := Val;
446
447       --  Normalize representation of signed values
448
449       if Val.Num < 0 then
450          Ureals.Table (Ureals.Last).Negative := True;
451          Ureals.Table (Ureals.Last).Num := -Val.Num;
452       end if;
453
454       return Ureals.Last;
455    end Store_Ureal;
456
457    ---------------
458    -- Tree_Read --
459    ---------------
460
461    procedure Tree_Read is
462    begin
463       pragma Assert (Num_Ureal_Constants = 10);
464
465       Ureals.Tree_Read;
466       Tree_Read_Int (Int (UR_0));
467       Tree_Read_Int (Int (UR_M_0));
468       Tree_Read_Int (Int (UR_Tenth));
469       Tree_Read_Int (Int (UR_Half));
470       Tree_Read_Int (Int (UR_1));
471       Tree_Read_Int (Int (UR_2));
472       Tree_Read_Int (Int (UR_10));
473       Tree_Read_Int (Int (UR_100));
474       Tree_Read_Int (Int (UR_2_128));
475       Tree_Read_Int (Int (UR_2_M_128));
476
477       --  Clear the normalization cache
478
479       Normalized_Real := No_Ureal;
480    end Tree_Read;
481
482    ----------------
483    -- Tree_Write --
484    ----------------
485
486    procedure Tree_Write is
487    begin
488       pragma Assert (Num_Ureal_Constants = 10);
489
490       Ureals.Tree_Write;
491       Tree_Write_Int (Int (UR_0));
492       Tree_Write_Int (Int (UR_M_0));
493       Tree_Write_Int (Int (UR_Tenth));
494       Tree_Write_Int (Int (UR_Half));
495       Tree_Write_Int (Int (UR_1));
496       Tree_Write_Int (Int (UR_2));
497       Tree_Write_Int (Int (UR_10));
498       Tree_Write_Int (Int (UR_100));
499       Tree_Write_Int (Int (UR_2_128));
500       Tree_Write_Int (Int (UR_2_M_128));
501    end Tree_Write;
502
503    ------------
504    -- UR_Abs --
505    ------------
506
507    function UR_Abs (Real : Ureal) return Ureal is
508       Val : constant Ureal_Entry := Ureals.Table (Real);
509
510    begin
511       return Store_Ureal (
512                (Num      => Val.Num,
513                 Den      => Val.Den,
514                 Rbase    => Val.Rbase,
515                 Negative => False));
516    end UR_Abs;
517
518    ------------
519    -- UR_Add --
520    ------------
521
522    function UR_Add (Left : Uint; Right : Ureal) return Ureal is
523    begin
524       return UR_From_Uint (Left) + Right;
525    end UR_Add;
526
527    function UR_Add (Left : Ureal; Right : Uint) return Ureal is
528    begin
529       return Left + UR_From_Uint (Right);
530    end UR_Add;
531
532    function UR_Add (Left : Ureal; Right : Ureal) return Ureal is
533       Lval : Ureal_Entry := Ureals.Table (Left);
534       Rval : Ureal_Entry := Ureals.Table (Right);
535
536       Num  : Uint;
537
538    begin
539       --  Note, in the temporary Ureal_Entry values used in this procedure,
540       --  we store the sign as the sign of the numerator (i.e. xxx.Num may
541       --  be negative, even though in stored entries this can never be so)
542
543       if Lval.Rbase /= 0 and then Lval.Rbase = Rval.Rbase then
544
545          declare
546             Opd_Min, Opd_Max   : Ureal_Entry;
547             Exp_Min, Exp_Max   : Uint;
548
549          begin
550             if Lval.Negative then
551                Lval.Num := (-Lval.Num);
552             end if;
553
554             if Rval.Negative then
555                Rval.Num := (-Rval.Num);
556             end if;
557
558             if Lval.Den < Rval.Den then
559                Exp_Min := Lval.Den;
560                Exp_Max := Rval.Den;
561                Opd_Min := Lval;
562                Opd_Max := Rval;
563             else
564                Exp_Min := Rval.Den;
565                Exp_Max := Lval.Den;
566                Opd_Min := Rval;
567                Opd_Max := Lval;
568             end if;
569
570             Num :=
571               Opd_Min.Num * Lval.Rbase ** (Exp_Max - Exp_Min) + Opd_Max.Num;
572
573             if Num = 0 then
574                return Store_Ureal (
575                         (Num      => Uint_0,
576                          Den      => Uint_1,
577                          Rbase    => 0,
578                          Negative => Lval.Negative));
579
580             else
581                return Store_Ureal (
582                         (Num      => abs Num,
583                          Den      => Exp_Max,
584                          Rbase    => Lval.Rbase,
585                          Negative => (Num < 0)));
586             end if;
587          end;
588
589       else
590          declare
591             Ln : Ureal_Entry := Normalize (Lval);
592             Rn : Ureal_Entry := Normalize (Rval);
593
594          begin
595             if Ln.Negative then
596                Ln.Num := (-Ln.Num);
597             end if;
598
599             if Rn.Negative then
600                Rn.Num := (-Rn.Num);
601             end if;
602
603             Num := (Ln.Num * Rn.Den) + (Rn.Num * Ln.Den);
604
605             if Num = 0 then
606                return Store_Ureal (
607                         (Num      => Uint_0,
608                          Den      => Uint_1,
609                          Rbase    => 0,
610                          Negative => Lval.Negative));
611
612             else
613                return Store_Ureal (
614                         Normalize (
615                           (Num      => abs Num,
616                            Den      => Ln.Den * Rn.Den,
617                            Rbase    => 0,
618                            Negative => (Num < 0))));
619             end if;
620          end;
621       end if;
622    end UR_Add;
623
624    ----------------
625    -- UR_Ceiling --
626    ----------------
627
628    function UR_Ceiling (Real : Ureal) return Uint is
629       Val : constant Ureal_Entry := Normalize (Ureals.Table (Real));
630
631    begin
632       if Val.Negative then
633          return UI_Negate (Val.Num / Val.Den);
634       else
635          return (Val.Num + Val.Den - 1) / Val.Den;
636       end if;
637    end UR_Ceiling;
638
639    ------------
640    -- UR_Div --
641    ------------
642
643    function UR_Div (Left : Uint; Right : Ureal) return Ureal is
644    begin
645       return UR_From_Uint (Left) / Right;
646    end UR_Div;
647
648    function UR_Div (Left : Ureal; Right : Uint) return Ureal is
649    begin
650       return Left / UR_From_Uint (Right);
651    end UR_Div;
652
653    function UR_Div (Left, Right : Ureal) return Ureal is
654       Lval : constant Ureal_Entry := Ureals.Table (Left);
655       Rval : constant Ureal_Entry := Ureals.Table (Right);
656       Rneg : constant Boolean     := Rval.Negative xor Lval.Negative;
657
658    begin
659       pragma Assert (Rval.Num /= Uint_0);
660
661       if Lval.Rbase = 0 then
662
663          if Rval.Rbase = 0 then
664             return Store_Ureal (
665                      Normalize (
666                        (Num      => Lval.Num * Rval.Den,
667                         Den      => Lval.Den * Rval.Num,
668                         Rbase    => 0,
669                         Negative => Rneg)));
670
671          elsif Is_Integer (Lval.Num, Rval.Num * Lval.Den) then
672             return Store_Ureal (
673                      (Num      => Lval.Num / (Rval.Num * Lval.Den),
674                       Den      => (-Rval.Den),
675                       Rbase    => Rval.Rbase,
676                       Negative => Rneg));
677
678          elsif Rval.Den < 0 then
679             return Store_Ureal (
680                      Normalize (
681                        (Num      => Lval.Num,
682                         Den      => Rval.Rbase ** (-Rval.Den) *
683                                     Rval.Num *
684                                     Lval.Den,
685                         Rbase    => 0,
686                         Negative => Rneg)));
687
688          else
689             return Store_Ureal (
690                      Normalize (
691                        (Num      => Lval.Num * Rval.Rbase ** Rval.Den,
692                         Den      => Rval.Num * Lval.Den,
693                         Rbase    => 0,
694                         Negative => Rneg)));
695          end if;
696
697       elsif Is_Integer (Lval.Num, Rval.Num) then
698
699          if Rval.Rbase = Lval.Rbase then
700             return Store_Ureal (
701                      (Num      => Lval.Num / Rval.Num,
702                       Den      => Lval.Den - Rval.Den,
703                       Rbase    => Lval.Rbase,
704                       Negative => Rneg));
705
706          elsif Rval.Rbase = 0 then
707             return Store_Ureal (
708                      (Num      => (Lval.Num / Rval.Num) * Rval.Den,
709                       Den      => Lval.Den,
710                       Rbase    => Lval.Rbase,
711                       Negative => Rneg));
712
713          elsif Rval.Den < 0 then
714             declare
715                Num, Den : Uint;
716
717             begin
718                if Lval.Den < 0 then
719                   Num := (Lval.Num / Rval.Num) * (Lval.Rbase ** (-Lval.Den));
720                   Den := Rval.Rbase ** (-Rval.Den);
721                else
722                   Num := Lval.Num / Rval.Num;
723                   Den := (Lval.Rbase ** Lval.Den) *
724                          (Rval.Rbase ** (-Rval.Den));
725                end if;
726
727                return Store_Ureal (
728                         (Num      => Num,
729                          Den      => Den,
730                          Rbase    => 0,
731                          Negative => Rneg));
732             end;
733
734          else
735             return Store_Ureal (
736                      (Num      => (Lval.Num / Rval.Num) *
737                                   (Rval.Rbase ** Rval.Den),
738                       Den      => Lval.Den,
739                       Rbase    => Lval.Rbase,
740                       Negative => Rneg));
741          end if;
742
743       else
744          declare
745             Num, Den : Uint;
746
747          begin
748             if Lval.Den < 0 then
749                Num := Lval.Num * (Lval.Rbase ** (-Lval.Den));
750                Den := Rval.Num;
751
752             else
753                Num := Lval.Num;
754                Den := Rval.Num * (Lval.Rbase ** Lval.Den);
755             end if;
756
757             if Rval.Rbase /= 0 then
758                if Rval.Den < 0 then
759                   Den := Den * (Rval.Rbase ** (-Rval.Den));
760                else
761                   Num := Num * (Rval.Rbase ** Rval.Den);
762                end if;
763
764             else
765                Num := Num * Rval.Den;
766             end if;
767
768             return Store_Ureal (
769                      Normalize (
770                        (Num      => Num,
771                         Den      => Den,
772                         Rbase    => 0,
773                         Negative => Rneg)));
774          end;
775       end if;
776    end UR_Div;
777
778    -----------
779    -- UR_Eq --
780    -----------
781
782    function UR_Eq (Left, Right : Ureal) return Boolean is
783    begin
784       return not UR_Ne (Left, Right);
785    end UR_Eq;
786
787    ---------------------
788    -- UR_Exponentiate --
789    ---------------------
790
791    function UR_Exponentiate (Real : Ureal; N : Uint) return Ureal is
792       X    : constant Uint := abs N;
793       Bas  : Ureal;
794       Val  : Ureal_Entry;
795       Neg  : Boolean;
796       IBas : Uint;
797
798    begin
799       --  If base is negative, then the resulting sign depends on whether
800       --  the exponent is even or odd (even => positive, odd = negative)
801
802       if UR_Is_Negative (Real) then
803          Neg := (N mod 2) /= 0;
804          Bas := UR_Negate (Real);
805       else
806          Neg := False;
807          Bas := Real;
808       end if;
809
810       Val := Ureals.Table (Bas);
811
812       --  If the base is a small integer, then we can return the result in
813       --  exponential form, which can save a lot of time for junk exponents.
814
815       IBas := UR_Trunc (Bas);
816
817       if IBas <= 16
818         and then UR_From_Uint (IBas) = Bas
819       then
820          return Store_Ureal (
821                  (Num      => Uint_1,
822                   Den      => -N,
823                   Rbase    => UI_To_Int (UR_Trunc (Bas)),
824                   Negative => Neg));
825
826       --  If the exponent is negative then we raise the numerator and the
827       --  denominator (after normalization) to the absolute value of the
828       --  exponent and we return the reciprocal. An assert error will happen
829       --  if the numerator is zero.
830
831       elsif N < 0 then
832          pragma Assert (Val.Num /= 0);
833          Val := Normalize (Val);
834
835          return Store_Ureal (
836                  (Num      => Val.Den ** X,
837                   Den      => Val.Num ** X,
838                   Rbase    => 0,
839                   Negative => Neg));
840
841       --  If positive, we distinguish the case when the base is not zero, in
842       --  which case the new denominator is just the product of the old one
843       --  with the exponent,
844
845       else
846          if Val.Rbase /= 0 then
847
848             return Store_Ureal (
849                     (Num      => Val.Num ** X,
850                      Den      => Val.Den * X,
851                      Rbase    => Val.Rbase,
852                      Negative => Neg));
853
854          --  And when the base is zero, in which case we exponentiate
855          --  the old denominator.
856
857          else
858             return Store_Ureal (
859                     (Num      => Val.Num ** X,
860                      Den      => Val.Den ** X,
861                      Rbase    => 0,
862                      Negative => Neg));
863          end if;
864       end if;
865    end UR_Exponentiate;
866
867    --------------
868    -- UR_Floor --
869    --------------
870
871    function UR_Floor (Real : Ureal) return Uint is
872       Val : constant Ureal_Entry := Normalize (Ureals.Table (Real));
873
874    begin
875       if Val.Negative then
876          return UI_Negate ((Val.Num + Val.Den - 1) / Val.Den);
877       else
878          return Val.Num / Val.Den;
879       end if;
880    end UR_Floor;
881
882    ------------------------
883    -- UR_From_Components --
884    ------------------------
885
886    function UR_From_Components
887      (Num      : Uint;
888       Den      : Uint;
889       Rbase    : Nat := 0;
890       Negative : Boolean := False)
891       return     Ureal
892    is
893    begin
894       return Store_Ureal (
895                (Num      => Num,
896                 Den      => Den,
897                 Rbase    => Rbase,
898                 Negative => Negative));
899    end UR_From_Components;
900
901    ------------------
902    -- UR_From_Uint --
903    ------------------
904
905    function UR_From_Uint (UI : Uint) return Ureal is
906    begin
907       return UR_From_Components
908         (abs UI, Uint_1, Negative => (UI < 0));
909    end UR_From_Uint;
910
911    -----------
912    -- UR_Ge --
913    -----------
914
915    function UR_Ge (Left, Right : Ureal) return Boolean is
916    begin
917       return not (Left < Right);
918    end UR_Ge;
919
920    -----------
921    -- UR_Gt --
922    -----------
923
924    function UR_Gt (Left, Right : Ureal) return Boolean is
925    begin
926       return (Right < Left);
927    end UR_Gt;
928
929    --------------------
930    -- UR_Is_Negative --
931    --------------------
932
933    function UR_Is_Negative (Real : Ureal) return Boolean is
934    begin
935       return Ureals.Table (Real).Negative;
936    end UR_Is_Negative;
937
938    --------------------
939    -- UR_Is_Positive --
940    --------------------
941
942    function UR_Is_Positive (Real : Ureal) return Boolean is
943    begin
944       return not Ureals.Table (Real).Negative
945         and then Ureals.Table (Real).Num /= 0;
946    end UR_Is_Positive;
947
948    ----------------
949    -- UR_Is_Zero --
950    ----------------
951
952    function UR_Is_Zero (Real : Ureal) return Boolean is
953    begin
954       return Ureals.Table (Real).Num = 0;
955    end UR_Is_Zero;
956
957    -----------
958    -- UR_Le --
959    -----------
960
961    function UR_Le (Left, Right : Ureal) return Boolean is
962    begin
963       return not (Right < Left);
964    end UR_Le;
965
966    -----------
967    -- UR_Lt --
968    -----------
969
970    function UR_Lt (Left, Right : Ureal) return Boolean is
971    begin
972       --  An operand is not less than itself
973
974       if Same (Left, Right) then
975          return False;
976
977       --  Deal with zero cases
978
979       elsif UR_Is_Zero (Left) then
980          return UR_Is_Positive (Right);
981
982       elsif UR_Is_Zero (Right) then
983          return Ureals.Table (Left).Negative;
984
985       --  Different signs are decisive (note we dealt with zero cases)
986
987       elsif Ureals.Table (Left).Negative
988         and then not Ureals.Table (Right).Negative
989       then
990          return True;
991
992       elsif not Ureals.Table (Left).Negative
993         and then Ureals.Table (Right).Negative
994       then
995          return False;
996
997       --  Signs are same, do rapid check based on worst case estimates of
998       --  decimal exponent, which will often be decisive. Precise test
999       --  depends on whether operands are positive or negative.
1000
1001       elsif Decimal_Exponent_Hi (Left) < Decimal_Exponent_Lo (Right) then
1002          return UR_Is_Positive (Left);
1003
1004       elsif Decimal_Exponent_Lo (Left) > Decimal_Exponent_Hi (Right) then
1005          return UR_Is_Negative (Left);
1006
1007       --  If we fall through, full gruesome test is required. This happens
1008       --  if the numbers are close together, or in some weird (/=10) base.
1009
1010       else
1011          declare
1012             Imrk   : constant Uintp.Save_Mark  := Mark;
1013             Rmrk   : constant Urealp.Save_Mark := Mark;
1014             Lval   : Ureal_Entry;
1015             Rval   : Ureal_Entry;
1016             Result : Boolean;
1017
1018          begin
1019             Lval := Ureals.Table (Left);
1020             Rval := Ureals.Table (Right);
1021
1022             --  An optimization. If both numbers are based, then subtract
1023             --  common value of base to avoid unnecessarily giant numbers
1024
1025             if Lval.Rbase = Rval.Rbase and then Lval.Rbase /= 0 then
1026                if Lval.Den < Rval.Den then
1027                   Rval.Den := Rval.Den - Lval.Den;
1028                   Lval.Den := Uint_0;
1029                else
1030                   Lval.Den := Lval.Den - Rval.Den;
1031                   Rval.Den := Uint_0;
1032                end if;
1033             end if;
1034
1035             Lval := Normalize (Lval);
1036             Rval := Normalize (Rval);
1037
1038             if Lval.Negative then
1039                Result := (Lval.Num * Rval.Den) > (Rval.Num * Lval.Den);
1040             else
1041                Result := (Lval.Num * Rval.Den) < (Rval.Num * Lval.Den);
1042             end if;
1043
1044             Release (Imrk);
1045             Release (Rmrk);
1046             return Result;
1047          end;
1048       end if;
1049    end UR_Lt;
1050
1051    ------------
1052    -- UR_Max --
1053    ------------
1054
1055    function UR_Max (Left, Right : Ureal) return Ureal is
1056    begin
1057       if Left >= Right then
1058          return Left;
1059       else
1060          return Right;
1061       end if;
1062    end UR_Max;
1063
1064    ------------
1065    -- UR_Min --
1066    ------------
1067
1068    function UR_Min (Left, Right : Ureal) return Ureal is
1069    begin
1070       if Left <= Right then
1071          return Left;
1072       else
1073          return Right;
1074       end if;
1075    end UR_Min;
1076
1077    ------------
1078    -- UR_Mul --
1079    ------------
1080
1081    function UR_Mul (Left : Uint; Right : Ureal) return Ureal is
1082    begin
1083       return UR_From_Uint (Left) * Right;
1084    end UR_Mul;
1085
1086    function UR_Mul (Left : Ureal; Right : Uint) return Ureal is
1087    begin
1088       return Left * UR_From_Uint (Right);
1089    end UR_Mul;
1090
1091    function UR_Mul (Left, Right : Ureal) return Ureal is
1092       Lval : constant Ureal_Entry := Ureals.Table (Left);
1093       Rval : constant Ureal_Entry := Ureals.Table (Right);
1094       Num  : Uint                 := Lval.Num * Rval.Num;
1095       Den  : Uint;
1096       Rneg : constant Boolean     := Lval.Negative xor Rval.Negative;
1097
1098    begin
1099       if Lval.Rbase = 0 then
1100          if Rval.Rbase = 0 then
1101             return Store_Ureal (
1102                      Normalize (
1103                         (Num      => Num,
1104                          Den      => Lval.Den * Rval.Den,
1105                          Rbase    => 0,
1106                          Negative => Rneg)));
1107
1108          elsif Is_Integer (Num, Lval.Den) then
1109             return Store_Ureal (
1110                      (Num      => Num / Lval.Den,
1111                       Den      => Rval.Den,
1112                       Rbase    => Rval.Rbase,
1113                       Negative => Rneg));
1114
1115          elsif Rval.Den < 0 then
1116             return Store_Ureal (
1117                      Normalize (
1118                        (Num      => Num * (Rval.Rbase ** (-Rval.Den)),
1119                         Den      => Lval.Den,
1120                         Rbase    => 0,
1121                         Negative => Rneg)));
1122
1123          else
1124             return Store_Ureal (
1125                      Normalize (
1126                        (Num      => Num,
1127                         Den      => Lval.Den * (Rval.Rbase ** Rval.Den),
1128                         Rbase    => 0,
1129                         Negative => Rneg)));
1130          end if;
1131
1132       elsif Lval.Rbase = Rval.Rbase then
1133          return Store_Ureal (
1134                   (Num      => Num,
1135                    Den      => Lval.Den + Rval.Den,
1136                    Rbase    => Lval.Rbase,
1137                    Negative => Rneg));
1138
1139       elsif Rval.Rbase = 0 then
1140          if Is_Integer (Num, Rval.Den) then
1141             return Store_Ureal (
1142                      (Num      => Num / Rval.Den,
1143                       Den      => Lval.Den,
1144                       Rbase    => Lval.Rbase,
1145                       Negative => Rneg));
1146
1147          elsif Lval.Den < 0 then
1148             return Store_Ureal (
1149                      Normalize (
1150                        (Num      => Num * (Lval.Rbase ** (-Lval.Den)),
1151                         Den      => Rval.Den,
1152                         Rbase    => 0,
1153                         Negative => Rneg)));
1154
1155          else
1156             return Store_Ureal (
1157                      Normalize (
1158                        (Num      => Num,
1159                         Den      => Rval.Den * (Lval.Rbase ** Lval.Den),
1160                         Rbase    => 0,
1161                         Negative => Rneg)));
1162          end if;
1163
1164       else
1165          Den := Uint_1;
1166
1167          if Lval.Den < 0 then
1168             Num := Num * (Lval.Rbase ** (-Lval.Den));
1169          else
1170             Den := Den * (Lval.Rbase ** Lval.Den);
1171          end if;
1172
1173          if Rval.Den < 0 then
1174             Num := Num * (Rval.Rbase ** (-Rval.Den));
1175          else
1176             Den := Den * (Rval.Rbase ** Rval.Den);
1177          end if;
1178
1179          return Store_Ureal (
1180                   Normalize (
1181                     (Num      => Num,
1182                      Den      => Den,
1183                      Rbase    => 0,
1184                      Negative => Rneg)));
1185       end if;
1186    end UR_Mul;
1187
1188    -----------
1189    -- UR_Ne --
1190    -----------
1191
1192    function UR_Ne (Left, Right : Ureal) return Boolean is
1193    begin
1194       --  Quick processing for case of identical Ureal values (note that
1195       --  this also deals with comparing two No_Ureal values).
1196
1197       if Same (Left, Right) then
1198          return False;
1199
1200       --  Deal with case of one or other operand is No_Ureal, but not both
1201
1202       elsif Same (Left, No_Ureal) or else Same (Right, No_Ureal) then
1203          return True;
1204
1205       --  Do quick check based on number of decimal digits
1206
1207       elsif Decimal_Exponent_Hi (Left) < Decimal_Exponent_Lo (Right) or else
1208             Decimal_Exponent_Lo (Left) > Decimal_Exponent_Hi (Right)
1209       then
1210          return True;
1211
1212       --  Otherwise full comparison is required
1213
1214       else
1215          declare
1216             Imrk   : constant Uintp.Save_Mark  := Mark;
1217             Rmrk   : constant Urealp.Save_Mark := Mark;
1218             Lval   : constant Ureal_Entry := Normalize (Ureals.Table (Left));
1219             Rval   : constant Ureal_Entry := Normalize (Ureals.Table (Right));
1220             Result : Boolean;
1221
1222          begin
1223             if UR_Is_Zero (Left) then
1224                return not UR_Is_Zero (Right);
1225
1226             elsif UR_Is_Zero (Right) then
1227                return not UR_Is_Zero (Left);
1228
1229             --  Both operands are non-zero
1230
1231             else
1232                Result :=
1233                   Rval.Negative /= Lval.Negative
1234                    or else Rval.Num /= Lval.Num
1235                    or else Rval.Den /= Lval.Den;
1236                Release (Imrk);
1237                Release (Rmrk);
1238                return Result;
1239             end if;
1240          end;
1241       end if;
1242    end UR_Ne;
1243
1244    ---------------
1245    -- UR_Negate --
1246    ---------------
1247
1248    function UR_Negate (Real : Ureal) return Ureal is
1249    begin
1250       return Store_Ureal (
1251                (Num      => Ureals.Table (Real).Num,
1252                 Den      => Ureals.Table (Real).Den,
1253                 Rbase    => Ureals.Table (Real).Rbase,
1254                 Negative => not Ureals.Table (Real).Negative));
1255    end UR_Negate;
1256
1257    ------------
1258    -- UR_Sub --
1259    ------------
1260
1261    function UR_Sub (Left : Uint; Right : Ureal) return Ureal is
1262    begin
1263       return UR_From_Uint (Left) + UR_Negate (Right);
1264    end UR_Sub;
1265
1266    function UR_Sub (Left : Ureal; Right : Uint) return Ureal is
1267    begin
1268       return Left + UR_From_Uint (-Right);
1269    end UR_Sub;
1270
1271    function UR_Sub (Left, Right : Ureal) return Ureal is
1272    begin
1273       return Left + UR_Negate (Right);
1274    end UR_Sub;
1275
1276    ----------------
1277    -- UR_To_Uint --
1278    ----------------
1279
1280    function UR_To_Uint (Real : Ureal) return Uint is
1281       Val : constant Ureal_Entry := Normalize (Ureals.Table (Real));
1282       Res : Uint;
1283
1284    begin
1285       Res := (Val.Num + (Val.Den / 2)) / Val.Den;
1286
1287       if Val.Negative then
1288          return UI_Negate (Res);
1289       else
1290          return Res;
1291       end if;
1292    end UR_To_Uint;
1293
1294    --------------
1295    -- UR_Trunc --
1296    --------------
1297
1298    function UR_Trunc (Real : Ureal) return Uint is
1299       Val : constant Ureal_Entry := Normalize (Ureals.Table (Real));
1300
1301    begin
1302       if Val.Negative then
1303          return -(Val.Num / Val.Den);
1304       else
1305          return Val.Num / Val.Den;
1306       end if;
1307    end UR_Trunc;
1308
1309    --------------
1310    -- UR_Write --
1311    --------------
1312
1313    procedure UR_Write (Real : Ureal) is
1314       Val : constant Ureal_Entry := Ureals.Table (Real);
1315
1316    begin
1317       --  If value is negative, we precede the constant by a minus sign
1318       --  and add an extra layer of parentheses on the outside since the
1319       --  minus sign is part of the value, not a negation operator.
1320
1321       if Val.Negative then
1322          Write_Str ("(-");
1323       end if;
1324
1325       --  Constants in base 10 can be written in normal Ada literal style
1326
1327       if Val.Rbase = 10 then
1328          UI_Write (Val.Num / 10);
1329          Write_Char ('.');
1330          UI_Write (Val.Num mod 10);
1331
1332          if Val.Den /= 0 then
1333             Write_Char ('E');
1334             UI_Write (1 - Val.Den);
1335          end if;
1336
1337       --  Constants in a base other than 10 can still be easily written
1338       --  in normal Ada literal style if the numerator is one.
1339
1340       elsif Val.Rbase /= 0 and then Val.Num = 1 then
1341          Write_Int (Val.Rbase);
1342          Write_Str ("#1.0#E");
1343          UI_Write (-Val.Den);
1344
1345       --  Other constants with a base other than 10 are written using one
1346       --  of the following forms, depending on the sign of the number
1347       --  and the sign of the exponent (= minus denominator value)
1348
1349       --    (numerator.0*base**exponent)
1350       --    (numerator.0*base**(-exponent))
1351
1352       elsif Val.Rbase /= 0 then
1353          Write_Char ('(');
1354          UI_Write (Val.Num, Decimal);
1355          Write_Str (".0*");
1356          Write_Int (Val.Rbase);
1357          Write_Str ("**");
1358
1359          if Val.Den <= 0 then
1360             UI_Write (-Val.Den, Decimal);
1361
1362          else
1363             Write_Str ("(-");
1364             UI_Write (Val.Den, Decimal);
1365             Write_Char (')');
1366          end if;
1367
1368          Write_Char (')');
1369
1370       --  Rational constants with a denominator of 1 can be written as
1371       --  a real literal for the numerator integer.
1372
1373       elsif Val.Den = 1 then
1374          UI_Write (Val.Num, Decimal);
1375          Write_Str (".0");
1376
1377       --  Non-based (rational) constants are written in (num/den) style
1378
1379       else
1380          Write_Char ('(');
1381          UI_Write (Val.Num, Decimal);
1382          Write_Str (".0/");
1383          UI_Write (Val.Den, Decimal);
1384          Write_Str (".0)");
1385       end if;
1386
1387       --  Add trailing paren for negative values
1388
1389       if Val.Negative then
1390          Write_Char (')');
1391       end if;
1392    end UR_Write;
1393
1394    -------------
1395    -- Ureal_0 --
1396    -------------
1397
1398    function Ureal_0 return Ureal is
1399    begin
1400       return UR_0;
1401    end Ureal_0;
1402
1403    -------------
1404    -- Ureal_1 --
1405    -------------
1406
1407    function Ureal_1 return Ureal is
1408    begin
1409       return UR_1;
1410    end Ureal_1;
1411
1412    -------------
1413    -- Ureal_2 --
1414    -------------
1415
1416    function Ureal_2 return Ureal is
1417    begin
1418       return UR_2;
1419    end Ureal_2;
1420
1421    --------------
1422    -- Ureal_10 --
1423    --------------
1424
1425    function Ureal_10 return Ureal is
1426    begin
1427       return UR_10;
1428    end Ureal_10;
1429
1430    ---------------
1431    -- Ureal_100 --
1432    ---------------
1433
1434    function Ureal_100 return Ureal is
1435    begin
1436       return UR_100;
1437    end Ureal_100;
1438
1439    -----------------
1440    -- Ureal_10_36 --
1441    -----------------
1442
1443    function Ureal_10_36 return Ureal is
1444    begin
1445       return UR_10_36;
1446    end Ureal_10_36;
1447
1448    ----------------
1449    -- Ureal_2_80 --
1450    ----------------
1451
1452    function Ureal_2_80 return Ureal is
1453    begin
1454       return UR_2_80;
1455    end Ureal_2_80;
1456
1457    -----------------
1458    -- Ureal_2_128 --
1459    -----------------
1460
1461    function Ureal_2_128 return Ureal is
1462    begin
1463       return UR_2_128;
1464    end Ureal_2_128;
1465
1466    -------------------
1467    -- Ureal_2_M_80 --
1468    -------------------
1469
1470    function Ureal_2_M_80 return Ureal is
1471    begin
1472       return UR_2_M_80;
1473    end Ureal_2_M_80;
1474
1475    -------------------
1476    -- Ureal_2_M_128 --
1477    -------------------
1478
1479    function Ureal_2_M_128 return Ureal is
1480    begin
1481       return UR_2_M_128;
1482    end Ureal_2_M_128;
1483
1484    ----------------
1485    -- Ureal_Half --
1486    ----------------
1487
1488    function Ureal_Half return Ureal is
1489    begin
1490       return UR_Half;
1491    end Ureal_Half;
1492
1493    ---------------
1494    -- Ureal_M_0 --
1495    ---------------
1496
1497    function Ureal_M_0 return Ureal is
1498    begin
1499       return UR_M_0;
1500    end Ureal_M_0;
1501
1502    -------------------
1503    -- Ureal_M_10_36 --
1504    -------------------
1505
1506    function Ureal_M_10_36 return Ureal is
1507    begin
1508       return UR_M_10_36;
1509    end Ureal_M_10_36;
1510
1511    -----------------
1512    -- Ureal_Tenth --
1513    -----------------
1514
1515    function Ureal_Tenth return Ureal is
1516    begin
1517       return UR_Tenth;
1518    end Ureal_Tenth;
1519
1520 end Urealp;