1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . A R I T H _ 6 4 --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with GNAT
.Exceptions
; use GNAT
.Exceptions
;
38 with Interfaces
; use Interfaces
;
39 with Unchecked_Conversion
;
41 package body System
.Arith_64
is
43 pragma Suppress
(Overflow_Check
);
44 pragma Suppress
(Range_Check
);
46 subtype Uns64
is Unsigned_64
;
47 function To_Uns
is new Unchecked_Conversion
(Int64
, Uns64
);
48 function To_Int
is new Unchecked_Conversion
(Uns64
, Int64
);
50 subtype Uns32
is Unsigned_32
;
52 -----------------------
53 -- Local Subprograms --
54 -----------------------
56 function "+" (A
, B
: Uns32
) return Uns64
;
57 function "+" (A
: Uns64
; B
: Uns32
) return Uns64
;
59 -- Length doubling additions
61 function "-" (A
: Uns64
; B
: Uns32
) return Uns64
;
63 -- Length doubling subtraction
65 function "*" (A
, B
: Uns32
) return Uns64
;
66 function "*" (A
: Uns64
; B
: Uns32
) return Uns64
;
68 -- Length doubling multiplications
70 function "/" (A
: Uns64
; B
: Uns32
) return Uns64
;
72 -- Length doubling division
74 function "rem" (A
: Uns64
; B
: Uns32
) return Uns64
;
75 pragma Inline
("rem");
76 -- Length doubling remainder
78 function "&" (Hi
, Lo
: Uns32
) return Uns64
;
80 -- Concatenate hi, lo values to form 64-bit result
82 function Lo
(A
: Uns64
) return Uns32
;
84 -- Low order half of 64-bit value
86 function Hi
(A
: Uns64
) return Uns32
;
88 -- High order half of 64-bit value
90 function To_Neg_Int
(A
: Uns64
) return Int64
;
91 -- Convert to negative integer equivalent. If the input is in the range
92 -- 0 .. 2 ** 63, then the corresponding negative signed integer (obtained
93 -- by negating the given value) is returned, otherwise constraint error
96 function To_Pos_Int
(A
: Uns64
) return Int64
;
97 -- Convert to positive integer equivalent. If the input is in the range
98 -- 0 .. 2 ** 63-1, then the corresponding non-negative signed integer is
99 -- returned, otherwise constraint error is raised.
101 procedure Raise_Error
;
102 pragma No_Return
(Raise_Error
);
103 -- Raise constraint error with appropriate message
109 function "&" (Hi
, Lo
: Uns32
) return Uns64
is
111 return Shift_Left
(Uns64
(Hi
), 32) or Uns64
(Lo
);
118 function "*" (A
, B
: Uns32
) return Uns64
is
120 return Uns64
(A
) * Uns64
(B
);
123 function "*" (A
: Uns64
; B
: Uns32
) return Uns64
is
125 return A
* Uns64
(B
);
132 function "+" (A
, B
: Uns32
) return Uns64
is
134 return Uns64
(A
) + Uns64
(B
);
137 function "+" (A
: Uns64
; B
: Uns32
) return Uns64
is
139 return A
+ Uns64
(B
);
146 function "-" (A
: Uns64
; B
: Uns32
) return Uns64
is
148 return A
- Uns64
(B
);
155 function "/" (A
: Uns64
; B
: Uns32
) return Uns64
is
157 return A
/ Uns64
(B
);
164 function "rem" (A
: Uns64
; B
: Uns32
) return Uns64
is
166 return A
rem Uns64
(B
);
169 --------------------------
170 -- Add_With_Ovflo_Check --
171 --------------------------
173 function Add_With_Ovflo_Check
(X
, Y
: Int64
) return Int64
is
174 R
: constant Int64
:= To_Int
(To_Uns
(X
) + To_Uns
(Y
));
178 if Y
< 0 or else R
>= 0 then
183 if Y
> 0 or else R
< 0 then
189 end Add_With_Ovflo_Check
;
195 procedure Double_Divide
200 Xu
: constant Uns64
:= To_Uns
(abs X
);
201 Yu
: constant Uns64
:= To_Uns
(abs Y
);
203 Yhi
: constant Uns32
:= Hi
(Yu
);
204 Ylo
: constant Uns32
:= Lo
(Yu
);
206 Zu
: constant Uns64
:= To_Uns
(abs Z
);
207 Zhi
: constant Uns32
:= Hi
(Zu
);
208 Zlo
: constant Uns32
:= Lo
(Zu
);
215 if Yu
= 0 or else Zu
= 0 then
219 -- Compute Y * Z. Note that if the result overflows 64 bits unsigned,
220 -- then the rounded result is clearly zero (since the dividend is at
221 -- most 2**63 - 1, the extra bit of precision is nice here!)
249 Du
:= Lo
(T2
) & Lo
(T1
);
253 -- Deal with rounding case
255 if Round
and then Ru
> (Du
- Uns64
'(1)) / Uns64'(2) then
256 Qu
:= Qu
+ Uns64
'(1);
259 -- Set final signs (RM 4.5.5(27-30))
261 Den_Pos := (Y < 0) = (Z < 0);
263 -- Case of dividend (X) sign positive
274 -- Case of dividend (X) sign negative
291 function Hi (A : Uns64) return Uns32 is
293 return Uns32 (Shift_Right (A, 32));
300 function Lo (A : Uns64) return Uns32 is
302 return Uns32 (A and 16#FFFF_FFFF#);
305 -------------------------------
306 -- Multiply_With_Ovflo_Check --
307 -------------------------------
309 function Multiply_With_Ovflo_Check (X, Y : Int64) return Int64 is
310 Xu : constant Uns64 := To_Uns (abs X);
311 Xhi : constant Uns32 := Hi (Xu);
312 Xlo : constant Uns32 := Lo (Xu);
314 Yu : constant Uns64 := To_Uns (abs Y);
315 Yhi : constant Uns32 := Hi (Yu);
316 Ylo : constant Uns32 := Lo (Yu);
343 T2 := Lo (T2) & Lo (T1);
347 return To_Pos_Int (T2);
349 return To_Neg_Int (T2);
353 return To_Pos_Int (T2);
355 return To_Neg_Int (T2);
359 end Multiply_With_Ovflo_Check;
365 procedure Raise_Error is
367 Raise_Exception (CE, "64-bit arithmetic overflow");
374 procedure Scaled_Divide
379 Xu : constant Uns64 := To_Uns (abs X);
380 Xhi : constant Uns32 := Hi (Xu);
381 Xlo : constant Uns32 := Lo (Xu);
383 Yu : constant Uns64 := To_Uns (abs Y);
384 Yhi : constant Uns32 := Hi (Yu);
385 Ylo : constant Uns32 := Lo (Yu);
387 Zu : Uns64 := To_Uns (abs Z);
388 Zhi : Uns32 := Hi (Zu);
389 Zlo : Uns32 := Lo (Zu);
391 D1, D2, D3, D4 : Uns32;
392 -- The dividend, four digits (D1 is high order)
395 -- The quotient, two digits (Q1 is high order)
398 -- Value to subtract, three digits (S1 is high order)
402 -- Unsigned quotient and remainder
405 -- Scaling factor used for multiple-precision divide. Dividend and
406 -- Divisor are multiplied by 2 ** Scale, and the final remainder
407 -- is divided by the scaling factor. The reason for this scaling
408 -- is to allow more accurate estimation of quotient digits.
414 -- First do the multiplication, giving the four digit dividend
424 D2 := Hi (T1) + Hi (T2);
435 T1 := (D1 & D2) + Uns64'(Xhi
* Yhi
);
448 D2
:= Hi
(T1
) + Hi
(T2
);
457 -- Now it is time for the dreaded multiple precision division. First
458 -- an easy case, check for the simple case of a one digit divisor.
461 if D1
/= 0 or else D2
>= Zlo
then
464 -- Here we are dividing at most three digits by one digit
468 T2
:= Lo
(T1
rem Zlo
) & D4
;
470 Qu
:= Lo
(T1
/ Zlo
) & Lo
(T2
/ Zlo
);
474 -- If divisor is double digit and too large, raise error
476 elsif (D1
& D2
) >= Zu
then
479 -- This is the complex case where we definitely have a double digit
480 -- divisor and a dividend of at least three digits. We use the classical
481 -- multiple division algorithm (see section (4.3.1) of Knuth's "The Art
482 -- of Computer Programming", Vol. 2 for a description (algorithm D).
485 -- First normalize the divisor so that it has the leading bit on.
486 -- We do this by finding the appropriate left shift amount.
490 if (Zhi
and 16#FFFF0000#
) = 0 then
492 Zu
:= Shift_Left
(Zu
, 16);
495 if (Hi
(Zu
) and 16#FF00_0000#
) = 0 then
497 Zu
:= Shift_Left
(Zu
, 8);
500 if (Hi
(Zu
) and 16#F000_0000#
) = 0 then
502 Zu
:= Shift_Left
(Zu
, 4);
505 if (Hi
(Zu
) and 16#C000_0000#
) = 0 then
507 Zu
:= Shift_Left
(Zu
, 2);
510 if (Hi
(Zu
) and 16#
8000_0000#
) = 0 then
512 Zu
:= Shift_Left
(Zu
, 1);
518 -- Note that when we scale up the dividend, it still fits in four
519 -- digits, since we already tested for overflow, and scaling does
520 -- not change the invariant that (D1 & D2) >= Zu.
522 T1
:= Shift_Left
(D1
& D2
, Scale
);
524 T2
:= Shift_Left
(0 & D3
, Scale
);
525 D2
:= Lo
(T1
) or Hi
(T2
);
526 T3
:= Shift_Left
(0 & D4
, Scale
);
527 D3
:= Lo
(T2
) or Hi
(T3
);
530 -- Compute first quotient digit. We have to divide three digits by
531 -- two digits, and we estimate the quotient by dividing the leading
532 -- two digits by the leading digit. Given the scaling we did above
533 -- which ensured the first bit of the divisor is set, this gives an
534 -- estimate of the quotient that is at most two too high.
539 Q1
:= Lo
((D1
& D2
) / Zhi
);
542 -- Compute amount to subtract
547 T1
:= Hi
(T1
) + Lo
(T2
);
549 S1
:= Hi
(T1
) + Hi
(T2
);
551 -- Adjust quotient digit if it was too high
566 T1
:= (S2
& S3
) - Zlo
;
568 T1
:= (S1
& S2
) - Zhi
;
573 -- Subtract from dividend (note: do not bother to set D1 to
574 -- zero, since it is no longer needed in the calculation).
576 T1
:= (D2
& D3
) - S3
;
578 T1
:= (D1
& Hi
(T1
)) - S2
;
581 -- Compute second quotient digit in same manner
586 Q2
:= Lo
((D2
& D3
) / Zhi
);
592 T1
:= Hi
(T1
) + Lo
(T2
);
594 S1
:= Hi
(T1
) + Hi
(T2
);
609 T1
:= (S2
& S3
) - Zlo
;
611 T1
:= (S1
& S2
) - Zhi
;
616 T1
:= (D3
& D4
) - S3
;
618 T1
:= (D2
& Hi
(T1
)) - S2
;
621 -- The two quotient digits are now set, and the remainder of the
622 -- scaled division is in (D3 & D4). To get the remainder for the
623 -- original unscaled division, we rescale this dividend.
624 -- We rescale the divisor as well, to make the proper comparison
625 -- for rounding below.
628 Ru
:= Shift_Right
(D3
& D4
, Scale
);
629 Zu
:= Shift_Right
(Zu
, Scale
);
632 -- Deal with rounding case
634 if Round
and then Ru
> (Zu
- Uns64
'(1)) / Uns64'(2) then
635 Qu
:= Qu
+ Uns64
(1);
638 -- Set final signs (RM 4.5.5(27-30))
640 -- Case of dividend (X * Y) sign positive
642 if (X
>= 0 and then Y
>= 0)
643 or else (X
< 0 and then Y
< 0)
645 R
:= To_Pos_Int
(Ru
);
648 Q
:= To_Pos_Int
(Qu
);
650 Q
:= To_Neg_Int
(Qu
);
653 -- Case of dividend (X * Y) sign negative
656 R
:= To_Neg_Int
(Ru
);
659 Q
:= To_Neg_Int
(Qu
);
661 Q
:= To_Pos_Int
(Qu
);
667 -------------------------------
668 -- Subtract_With_Ovflo_Check --
669 -------------------------------
671 function Subtract_With_Ovflo_Check
(X
, Y
: Int64
) return Int64
is
672 R
: constant Int64
:= To_Int
(To_Uns
(X
) - To_Uns
(Y
));
676 if Y
> 0 or else R
>= 0 then
681 if Y
<= 0 or else R
< 0 then
687 end Subtract_With_Ovflo_Check
;
693 function To_Neg_Int
(A
: Uns64
) return Int64
is
694 R
: constant Int64
:= -To_Int
(A
);
708 function To_Pos_Int
(A
: Uns64
) return Int64
is
709 R
: constant Int64
:= To_Int
(A
);