1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . A R I T H _ 6 4 --
9 -- Copyright (C) 1992-2004 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 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with System
.Pure_Exceptions
; use System
.Pure_Exceptions
;
36 with Interfaces
; use Interfaces
;
37 with Unchecked_Conversion
;
39 package body System
.Arith_64
is
41 pragma Suppress
(Overflow_Check
);
42 pragma Suppress
(Range_Check
);
44 subtype Uns64
is Unsigned_64
;
45 function To_Uns
is new Unchecked_Conversion
(Int64
, Uns64
);
46 function To_Int
is new Unchecked_Conversion
(Uns64
, Int64
);
48 subtype Uns32
is Unsigned_32
;
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
54 function "+" (A
, B
: Uns32
) return Uns64
;
55 function "+" (A
: Uns64
; B
: Uns32
) return Uns64
;
57 -- Length doubling additions
59 function "*" (A
, B
: Uns32
) return Uns64
;
61 -- Length doubling multiplication
63 function "/" (A
: Uns64
; B
: Uns32
) return Uns64
;
65 -- Length doubling division
67 function "rem" (A
: Uns64
; B
: Uns32
) return Uns64
;
68 pragma Inline
("rem");
69 -- Length doubling remainder
71 function "&" (Hi
, Lo
: Uns32
) return Uns64
;
73 -- Concatenate hi, lo values to form 64-bit result
75 function Le3
(X1
, X2
, X3
: Uns32
; Y1
, Y2
, Y3
: Uns32
) return Boolean;
76 -- Determines if 96 bit value X1&X2&X3 <= Y1&Y2&Y3
78 function Lo
(A
: Uns64
) return Uns32
;
80 -- Low order half of 64-bit value
82 function Hi
(A
: Uns64
) return Uns32
;
84 -- High order half of 64-bit value
86 procedure Sub3
(X1
, X2
, X3
: in out Uns32
; Y1
, Y2
, Y3
: in Uns32
);
87 -- Computes X1&X2&X3 := X1&X2&X3 - Y1&Y1&Y3 with mod 2**96 wrap
89 function To_Neg_Int
(A
: Uns64
) return Int64
;
90 -- Convert to negative integer equivalent. If the input is in the range
91 -- 0 .. 2 ** 63, then the corresponding negative signed integer (obtained
92 -- by negating the given value) is returned, otherwise constraint error
95 function To_Pos_Int
(A
: Uns64
) return Int64
;
96 -- Convert to positive integer equivalent. If the input is in the range
97 -- 0 .. 2 ** 63-1, then the corresponding non-negative signed integer is
98 -- returned, otherwise constraint error is raised.
100 procedure Raise_Error
;
101 pragma No_Return
(Raise_Error
);
102 -- Raise constraint error with appropriate message
108 function "&" (Hi
, Lo
: Uns32
) return Uns64
is
110 return Shift_Left
(Uns64
(Hi
), 32) or Uns64
(Lo
);
117 function "*" (A
, B
: Uns32
) return Uns64
is
119 return Uns64
(A
) * Uns64
(B
);
126 function "+" (A
, B
: Uns32
) return Uns64
is
128 return Uns64
(A
) + Uns64
(B
);
131 function "+" (A
: Uns64
; B
: Uns32
) return Uns64
is
133 return A
+ Uns64
(B
);
140 function "/" (A
: Uns64
; B
: Uns32
) return Uns64
is
142 return A
/ Uns64
(B
);
149 function "rem" (A
: Uns64
; B
: Uns32
) return Uns64
is
151 return A
rem Uns64
(B
);
154 --------------------------
155 -- Add_With_Ovflo_Check --
156 --------------------------
158 function Add_With_Ovflo_Check
(X
, Y
: Int64
) return Int64
is
159 R
: constant Int64
:= To_Int
(To_Uns
(X
) + To_Uns
(Y
));
163 if Y
< 0 or else R
>= 0 then
168 if Y
> 0 or else R
< 0 then
174 end Add_With_Ovflo_Check
;
180 procedure Double_Divide
185 Xu
: constant Uns64
:= To_Uns
(abs X
);
186 Yu
: constant Uns64
:= To_Uns
(abs Y
);
188 Yhi
: constant Uns32
:= Hi
(Yu
);
189 Ylo
: constant Uns32
:= Lo
(Yu
);
191 Zu
: constant Uns64
:= To_Uns
(abs Z
);
192 Zhi
: constant Uns32
:= Hi
(Zu
);
193 Zlo
: constant Uns32
:= Lo
(Zu
);
200 if Yu
= 0 or else Zu
= 0 then
204 -- Compute Y * Z. Note that if the result overflows 64 bits unsigned,
205 -- then the rounded result is clearly zero (since the dividend is at
206 -- most 2**63 - 1, the extra bit of precision is nice here!)
234 Du
:= Lo
(T2
) & Lo
(T1
);
238 -- Deal with rounding case
240 if Round
and then Ru
> (Du
- Uns64
'(1)) / Uns64'(2) then
241 Qu
:= Qu
+ Uns64
'(1);
244 -- Set final signs (RM 4.5.5(27-30))
246 Den_Pos := (Y < 0) = (Z < 0);
248 -- Case of dividend (X) sign positive
259 -- Case of dividend (X) sign negative
276 function Hi (A : Uns64) return Uns32 is
278 return Uns32 (Shift_Right (A, 32));
285 function Le3 (X1, X2, X3 : Uns32; Y1, Y2, Y3 : Uns32) return Boolean is
304 function Lo (A : Uns64) return Uns32 is
306 return Uns32 (A and 16#FFFF_FFFF#);
309 -------------------------------
310 -- Multiply_With_Ovflo_Check --
311 -------------------------------
313 function Multiply_With_Ovflo_Check (X, Y : Int64) return Int64 is
314 Xu : constant Uns64 := To_Uns (abs X);
315 Xhi : constant Uns32 := Hi (Xu);
316 Xlo : constant Uns32 := Lo (Xu);
318 Yu : constant Uns64 := To_Uns (abs Y);
319 Yhi : constant Uns32 := Hi (Yu);
320 Ylo : constant Uns32 := Lo (Yu);
335 else -- Yhi = Xhi = 0
339 -- Here we have T2 set to the contribution to the upper half
340 -- of the result from the upper halves of the input values.
349 T2 := Lo (T2) & Lo (T1);
353 return To_Pos_Int (T2);
355 return To_Neg_Int (T2);
359 return To_Pos_Int (T2);
361 return To_Neg_Int (T2);
365 end Multiply_With_Ovflo_Check;
371 procedure Raise_Error is
373 Raise_Exception (CE, "64-bit arithmetic overflow");
380 procedure Scaled_Divide
385 Xu : constant Uns64 := To_Uns (abs X);
386 Xhi : constant Uns32 := Hi (Xu);
387 Xlo : constant Uns32 := Lo (Xu);
389 Yu : constant Uns64 := To_Uns (abs Y);
390 Yhi : constant Uns32 := Hi (Yu);
391 Ylo : constant Uns32 := Lo (Yu);
393 Zu : Uns64 := To_Uns (abs Z);
394 Zhi : Uns32 := Hi (Zu);
395 Zlo : Uns32 := Lo (Zu);
397 D : array (1 .. 4) of Uns32;
398 -- The dividend, four digits (D(1) is high order)
400 Qd : array (1 .. 2) of Uns32;
401 -- The quotient digits, two digits (Qd(1) is high order)
404 -- Value to subtract, three digits (S1 is high order)
408 -- Unsigned quotient and remainder
411 -- Scaling factor used for multiple-precision divide. Dividend and
412 -- Divisor are multiplied by 2 ** Scale, and the final remainder
413 -- is divided by the scaling factor. The reason for this scaling
414 -- is to allow more accurate estimation of quotient digits.
420 -- First do the multiplication, giving the four digit dividend
428 T2 := D (3) + Lo (T1);
430 D (2) := Hi (T1) + Hi (T2);
434 T2 := D (3) + Lo (T1);
436 T3 := D (2) + Hi (T1);
441 T1 := (D (1) & D (2)) + Uns64'(Xhi
* Yhi
);
452 T2
:= D
(3) + Lo
(T1
);
454 D
(2) := Hi
(T1
) + Hi
(T2
);
463 -- Now it is time for the dreaded multiple precision division. First
464 -- an easy case, check for the simple case of a one digit divisor.
467 if D
(1) /= 0 or else D
(2) >= Zlo
then
470 -- Here we are dividing at most three digits by one digit
474 T2
:= Lo
(T1
rem Zlo
) & D
(4);
476 Qu
:= Lo
(T1
/ Zlo
) & Lo
(T2
/ Zlo
);
480 -- If divisor is double digit and too large, raise error
482 elsif (D
(1) & D
(2)) >= Zu
then
485 -- This is the complex case where we definitely have a double digit
486 -- divisor and a dividend of at least three digits. We use the classical
487 -- multiple division algorithm (see section (4.3.1) of Knuth's "The Art
488 -- of Computer Programming", Vol. 2 for a description (algorithm D).
491 -- First normalize the divisor so that it has the leading bit on.
492 -- We do this by finding the appropriate left shift amount.
496 if (Zhi
and 16#FFFF0000#
) = 0 then
498 Zu
:= Shift_Left
(Zu
, 16);
501 if (Hi
(Zu
) and 16#FF00_0000#
) = 0 then
503 Zu
:= Shift_Left
(Zu
, 8);
506 if (Hi
(Zu
) and 16#F000_0000#
) = 0 then
508 Zu
:= Shift_Left
(Zu
, 4);
511 if (Hi
(Zu
) and 16#C000_0000#
) = 0 then
513 Zu
:= Shift_Left
(Zu
, 2);
516 if (Hi
(Zu
) and 16#
8000_0000#
) = 0 then
518 Zu
:= Shift_Left
(Zu
, 1);
524 -- Note that when we scale up the dividend, it still fits in four
525 -- digits, since we already tested for overflow, and scaling does
526 -- not change the invariant that (D (1) & D (2)) >= Zu.
528 T1
:= Shift_Left
(D
(1) & D
(2), Scale
);
530 T2
:= Shift_Left
(0 & D
(3), Scale
);
531 D
(2) := Lo
(T1
) or Hi
(T2
);
532 T3
:= Shift_Left
(0 & D
(4), Scale
);
533 D
(3) := Lo
(T2
) or Hi
(T3
);
536 -- Loop to compute quotient digits, runs twice for Qd(1) and Qd(2).
540 -- Compute next quotient digit. We have to divide three digits by
541 -- two digits. We estimate the quotient by dividing the leading
542 -- two digits by the leading digit. Given the scaling we did above
543 -- which ensured the first bit of the divisor is set, this gives
544 -- an estimate of the quotient that is at most two too high.
546 if D
(J
+ 1) = Zhi
then
547 Qd
(J
+ 1) := 2 ** 32 - 1;
549 Qd
(J
+ 1) := Lo
((D
(J
+ 1) & D
(J
+ 2)) / Zhi
);
552 -- Compute amount to subtract
554 T1
:= Qd
(J
+ 1) * Zlo
;
555 T2
:= Qd
(J
+ 1) * Zhi
;
557 T1
:= Hi
(T1
) + Lo
(T2
);
559 S1
:= Hi
(T1
) + Hi
(T2
);
561 -- Adjust quotient digit if it was too high
564 exit when Le3
(S1
, S2
, S3
, D
(J
+ 1), D
(J
+ 2), D
(J
+ 3));
565 Qd
(J
+ 1) := Qd
(J
+ 1) - 1;
566 Sub3
(S1
, S2
, S3
, 0, Zhi
, Zlo
);
569 -- Now subtract S1&S2&S3 from D1&D2&D3 ready for next step
571 Sub3
(D
(J
+ 1), D
(J
+ 2), D
(J
+ 3), S1
, S2
, S3
);
574 -- The two quotient digits are now set, and the remainder of the
575 -- scaled division is in D3&D4. To get the remainder for the
576 -- original unscaled division, we rescale this dividend.
578 -- We rescale the divisor as well, to make the proper comparison
579 -- for rounding below.
581 Qu
:= Qd
(1) & Qd
(2);
582 Ru
:= Shift_Right
(D
(3) & D
(4), Scale
);
583 Zu
:= Shift_Right
(Zu
, Scale
);
586 -- Deal with rounding case
588 if Round
and then Ru
> (Zu
- Uns64
'(1)) / Uns64'(2) then
589 Qu
:= Qu
+ Uns64
(1);
592 -- Set final signs (RM 4.5.5(27-30))
594 -- Case of dividend (X * Y) sign positive
596 if (X
>= 0 and then Y
>= 0)
597 or else (X
< 0 and then Y
< 0)
599 R
:= To_Pos_Int
(Ru
);
602 Q
:= To_Pos_Int
(Qu
);
604 Q
:= To_Neg_Int
(Qu
);
607 -- Case of dividend (X * Y) sign negative
610 R
:= To_Neg_Int
(Ru
);
613 Q
:= To_Neg_Int
(Qu
);
615 Q
:= To_Pos_Int
(Qu
);
624 procedure Sub3
(X1
, X2
, X3
: in out Uns32
; Y1
, Y2
, Y3
: in Uns32
) is
644 -------------------------------
645 -- Subtract_With_Ovflo_Check --
646 -------------------------------
648 function Subtract_With_Ovflo_Check
(X
, Y
: Int64
) return Int64
is
649 R
: constant Int64
:= To_Int
(To_Uns
(X
) - To_Uns
(Y
));
653 if Y
> 0 or else R
>= 0 then
658 if Y
<= 0 or else R
< 0 then
664 end Subtract_With_Ovflo_Check
;
670 function To_Neg_Int
(A
: Uns64
) return Int64
is
671 R
: constant Int64
:= -To_Int
(A
);
685 function To_Pos_Int
(A
: Uns64
) return Int64
is
686 R
: constant Int64
:= To_Int
(A
);