1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . A R I T H _ 6 4 --
10 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 with GNAT
.Exceptions
; use GNAT
.Exceptions
;
37 with Interfaces
; use Interfaces
;
38 with Unchecked_Conversion
;
40 package body System
.Arith_64
is
42 pragma Suppress
(Overflow_Check
);
43 pragma Suppress
(Range_Check
);
45 subtype Uns64
is Unsigned_64
;
46 function To_Uns
is new Unchecked_Conversion
(Int64
, Uns64
);
47 function To_Int
is new Unchecked_Conversion
(Uns64
, Int64
);
49 subtype Uns32
is Unsigned_32
;
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
55 function "+" (A
, B
: Uns32
) return Uns64
;
56 function "+" (A
: Uns64
; B
: Uns32
) return Uns64
;
58 -- Length doubling additions
60 function "-" (A
: Uns64
; B
: Uns32
) return Uns64
;
62 -- Length doubling subtraction
64 function "*" (A
, B
: Uns32
) return Uns64
;
66 -- Length doubling multiplication
68 function "/" (A
: Uns64
; B
: Uns32
) return Uns64
;
70 -- Length doubling division
72 function "rem" (A
: Uns64
; B
: Uns32
) return Uns64
;
73 pragma Inline
("rem");
74 -- Length doubling remainder
76 function "&" (Hi
, Lo
: Uns32
) return Uns64
;
78 -- Concatenate hi, lo values to form 64-bit result
80 function Lo
(A
: Uns64
) return Uns32
;
82 -- Low order half of 64-bit value
84 function Hi
(A
: Uns64
) return Uns32
;
86 -- High order half of 64-bit value
88 function To_Neg_Int
(A
: Uns64
) return Int64
;
89 -- Convert to negative integer equivalent. If the input is in the range
90 -- 0 .. 2 ** 63, then the corresponding negative signed integer (obtained
91 -- by negating the given value) is returned, otherwise constraint error
94 function To_Pos_Int
(A
: Uns64
) return Int64
;
95 -- Convert to positive integer equivalent. If the input is in the range
96 -- 0 .. 2 ** 63-1, then the corresponding non-negative signed integer is
97 -- returned, otherwise constraint error is raised.
99 procedure Raise_Error
;
100 pragma No_Return
(Raise_Error
);
101 -- Raise constraint error with appropriate message
107 function "&" (Hi
, Lo
: Uns32
) return Uns64
is
109 return Shift_Left
(Uns64
(Hi
), 32) or Uns64
(Lo
);
116 function "*" (A
, B
: Uns32
) return Uns64
is
118 return Uns64
(A
) * Uns64
(B
);
125 function "+" (A
, B
: Uns32
) return Uns64
is
127 return Uns64
(A
) + Uns64
(B
);
130 function "+" (A
: Uns64
; B
: Uns32
) return Uns64
is
132 return A
+ Uns64
(B
);
139 function "-" (A
: Uns64
; B
: Uns32
) return Uns64
is
141 return A
- Uns64
(B
);
148 function "/" (A
: Uns64
; B
: Uns32
) return Uns64
is
150 return A
/ Uns64
(B
);
157 function "rem" (A
: Uns64
; B
: Uns32
) return Uns64
is
159 return A
rem Uns64
(B
);
162 --------------------------
163 -- Add_With_Ovflo_Check --
164 --------------------------
166 function Add_With_Ovflo_Check
(X
, Y
: Int64
) return Int64
is
167 R
: constant Int64
:= To_Int
(To_Uns
(X
) + To_Uns
(Y
));
171 if Y
< 0 or else R
>= 0 then
176 if Y
> 0 or else R
< 0 then
182 end Add_With_Ovflo_Check
;
188 procedure Double_Divide
193 Xu
: constant Uns64
:= To_Uns
(abs X
);
194 Yu
: constant Uns64
:= To_Uns
(abs Y
);
196 Yhi
: constant Uns32
:= Hi
(Yu
);
197 Ylo
: constant Uns32
:= Lo
(Yu
);
199 Zu
: constant Uns64
:= To_Uns
(abs Z
);
200 Zhi
: constant Uns32
:= Hi
(Zu
);
201 Zlo
: constant Uns32
:= Lo
(Zu
);
208 if Yu
= 0 or else Zu
= 0 then
212 -- Compute Y * Z. Note that if the result overflows 64 bits unsigned,
213 -- then the rounded result is clearly zero (since the dividend is at
214 -- most 2**63 - 1, the extra bit of precision is nice here!)
242 Du
:= Lo
(T2
) & Lo
(T1
);
246 -- Deal with rounding case
248 if Round
and then Ru
> (Du
- Uns64
'(1)) / Uns64'(2) then
249 Qu
:= Qu
+ Uns64
'(1);
252 -- Set final signs (RM 4.5.5(27-30))
254 Den_Pos := (Y < 0) = (Z < 0);
256 -- Case of dividend (X) sign positive
267 -- Case of dividend (X) sign negative
284 function Hi (A : Uns64) return Uns32 is
286 return Uns32 (Shift_Right (A, 32));
293 function Lo (A : Uns64) return Uns32 is
295 return Uns32 (A and 16#FFFF_FFFF#);
298 -------------------------------
299 -- Multiply_With_Ovflo_Check --
300 -------------------------------
302 function Multiply_With_Ovflo_Check (X, Y : Int64) return Int64 is
303 Xu : constant Uns64 := To_Uns (abs X);
304 Xhi : constant Uns32 := Hi (Xu);
305 Xlo : constant Uns32 := Lo (Xu);
307 Yu : constant Uns64 := To_Uns (abs Y);
308 Yhi : constant Uns32 := Hi (Yu);
309 Ylo : constant Uns32 := Lo (Yu);
324 else -- Yhi = Xhi = 0
328 -- Here we have T2 set to the contribution to the upper half
329 -- of the result from the upper halves of the input values.
338 T2 := Lo (T2) & Lo (T1);
342 return To_Pos_Int (T2);
344 return To_Neg_Int (T2);
348 return To_Pos_Int (T2);
350 return To_Neg_Int (T2);
354 end Multiply_With_Ovflo_Check;
360 procedure Raise_Error is
362 Raise_Exception (CE, "64-bit arithmetic overflow");
369 procedure Scaled_Divide
374 Xu : constant Uns64 := To_Uns (abs X);
375 Xhi : constant Uns32 := Hi (Xu);
376 Xlo : constant Uns32 := Lo (Xu);
378 Yu : constant Uns64 := To_Uns (abs Y);
379 Yhi : constant Uns32 := Hi (Yu);
380 Ylo : constant Uns32 := Lo (Yu);
382 Zu : Uns64 := To_Uns (abs Z);
383 Zhi : Uns32 := Hi (Zu);
384 Zlo : Uns32 := Lo (Zu);
386 D1, D2, D3, D4 : Uns32;
387 -- The dividend, four digits (D1 is high order)
390 -- The quotient, two digits (Q1 is high order)
393 -- Value to subtract, three digits (S1 is high order)
397 -- Unsigned quotient and remainder
400 -- Scaling factor used for multiple-precision divide. Dividend and
401 -- Divisor are multiplied by 2 ** Scale, and the final remainder
402 -- is divided by the scaling factor. The reason for this scaling
403 -- is to allow more accurate estimation of quotient digits.
409 -- First do the multiplication, giving the four digit dividend
419 D2 := Hi (T1) + Hi (T2);
430 T1 := (D1 & D2) + Uns64'(Xhi
* Yhi
);
443 D2
:= Hi
(T1
) + Hi
(T2
);
452 -- Now it is time for the dreaded multiple precision division. First
453 -- an easy case, check for the simple case of a one digit divisor.
456 if D1
/= 0 or else D2
>= Zlo
then
459 -- Here we are dividing at most three digits by one digit
463 T2
:= Lo
(T1
rem Zlo
) & D4
;
465 Qu
:= Lo
(T1
/ Zlo
) & Lo
(T2
/ Zlo
);
469 -- If divisor is double digit and too large, raise error
471 elsif (D1
& D2
) >= Zu
then
474 -- This is the complex case where we definitely have a double digit
475 -- divisor and a dividend of at least three digits. We use the classical
476 -- multiple division algorithm (see section (4.3.1) of Knuth's "The Art
477 -- of Computer Programming", Vol. 2 for a description (algorithm D).
480 -- First normalize the divisor so that it has the leading bit on.
481 -- We do this by finding the appropriate left shift amount.
485 if (Zhi
and 16#FFFF0000#
) = 0 then
487 Zu
:= Shift_Left
(Zu
, 16);
490 if (Hi
(Zu
) and 16#FF00_0000#
) = 0 then
492 Zu
:= Shift_Left
(Zu
, 8);
495 if (Hi
(Zu
) and 16#F000_0000#
) = 0 then
497 Zu
:= Shift_Left
(Zu
, 4);
500 if (Hi
(Zu
) and 16#C000_0000#
) = 0 then
502 Zu
:= Shift_Left
(Zu
, 2);
505 if (Hi
(Zu
) and 16#
8000_0000#
) = 0 then
507 Zu
:= Shift_Left
(Zu
, 1);
513 -- Note that when we scale up the dividend, it still fits in four
514 -- digits, since we already tested for overflow, and scaling does
515 -- not change the invariant that (D1 & D2) >= Zu.
517 T1
:= Shift_Left
(D1
& D2
, Scale
);
519 T2
:= Shift_Left
(0 & D3
, Scale
);
520 D2
:= Lo
(T1
) or Hi
(T2
);
521 T3
:= Shift_Left
(0 & D4
, Scale
);
522 D3
:= Lo
(T2
) or Hi
(T3
);
525 -- Compute first quotient digit. We have to divide three digits by
526 -- two digits, and we estimate the quotient by dividing the leading
527 -- two digits by the leading digit. Given the scaling we did above
528 -- which ensured the first bit of the divisor is set, this gives an
529 -- estimate of the quotient that is at most two too high.
534 Q1
:= Lo
((D1
& D2
) / Zhi
);
537 -- Compute amount to subtract
542 T1
:= Hi
(T1
) + Lo
(T2
);
544 S1
:= Hi
(T1
) + Hi
(T2
);
546 -- Adjust quotient digit if it was too high
561 T1
:= (S2
& S3
) - Zlo
;
563 T1
:= (S1
& S2
) - Zhi
;
568 -- Subtract from dividend (note: do not bother to set D1 to
569 -- zero, since it is no longer needed in the calculation).
571 T1
:= (D2
& D3
) - S3
;
573 T1
:= (D1
& Hi
(T1
)) - S2
;
576 -- Compute second quotient digit in same manner
581 Q2
:= Lo
((D2
& D3
) / Zhi
);
587 T1
:= Hi
(T1
) + Lo
(T2
);
589 S1
:= Hi
(T1
) + Hi
(T2
);
604 T1
:= (S2
& S3
) - Zlo
;
606 T1
:= (S1
& S2
) - Zhi
;
611 T1
:= (D3
& D4
) - S3
;
613 T1
:= (D2
& Hi
(T1
)) - S2
;
616 -- The two quotient digits are now set, and the remainder of the
617 -- scaled division is in (D3 & D4). To get the remainder for the
618 -- original unscaled division, we rescale this dividend.
619 -- We rescale the divisor as well, to make the proper comparison
620 -- for rounding below.
623 Ru
:= Shift_Right
(D3
& D4
, Scale
);
624 Zu
:= Shift_Right
(Zu
, Scale
);
627 -- Deal with rounding case
629 if Round
and then Ru
> (Zu
- Uns64
'(1)) / Uns64'(2) then
630 Qu
:= Qu
+ Uns64
(1);
633 -- Set final signs (RM 4.5.5(27-30))
635 -- Case of dividend (X * Y) sign positive
637 if (X
>= 0 and then Y
>= 0)
638 or else (X
< 0 and then Y
< 0)
640 R
:= To_Pos_Int
(Ru
);
643 Q
:= To_Pos_Int
(Qu
);
645 Q
:= To_Neg_Int
(Qu
);
648 -- Case of dividend (X * Y) sign negative
651 R
:= To_Neg_Int
(Ru
);
654 Q
:= To_Neg_Int
(Qu
);
656 Q
:= To_Pos_Int
(Qu
);
662 -------------------------------
663 -- Subtract_With_Ovflo_Check --
664 -------------------------------
666 function Subtract_With_Ovflo_Check
(X
, Y
: Int64
) return Int64
is
667 R
: constant Int64
:= To_Int
(To_Uns
(X
) - To_Uns
(Y
));
671 if Y
> 0 or else R
>= 0 then
676 if Y
<= 0 or else R
< 0 then
682 end Subtract_With_Ovflo_Check
;
688 function To_Neg_Int
(A
: Uns64
) return Int64
is
689 R
: constant Int64
:= -To_Int
(A
);
703 function To_Pos_Int
(A
: Uns64
) return Int64
is
704 R
: constant Int64
:= To_Int
(A
);