with System.Pure_Exceptions; use System.Pure_Exceptions;
with Interfaces; use Interfaces;
with Unchecked_Conversion;
package body System.Arith_64 is
pragma Suppress (Overflow_Check);
pragma Suppress (Range_Check);
subtype Uns64 is Unsigned_64;
function To_Uns is new Unchecked_Conversion (Int64, Uns64);
function To_Int is new Unchecked_Conversion (Uns64, Int64);
subtype Uns32 is Unsigned_32;
function "+" (A, B : Uns32) return Uns64;
function "+" (A : Uns64; B : Uns32) return Uns64;
pragma Inline ("+");
function "*" (A, B : Uns32) return Uns64;
pragma Inline ("*");
function "/" (A : Uns64; B : Uns32) return Uns64;
pragma Inline ("/");
function "rem" (A : Uns64; B : Uns32) return Uns64;
pragma Inline ("rem");
function "&" (Hi, Lo : Uns32) return Uns64;
pragma Inline ("&");
function Le3 (X1, X2, X3 : Uns32; Y1, Y2, Y3 : Uns32) return Boolean;
function Lo (A : Uns64) return Uns32;
pragma Inline (Lo);
function Hi (A : Uns64) return Uns32;
pragma Inline (Hi);
procedure Sub3 (X1, X2, X3 : in out Uns32; Y1, Y2, Y3 : in Uns32);
function To_Neg_Int (A : Uns64) return Int64;
function To_Pos_Int (A : Uns64) return Int64;
procedure Raise_Error;
pragma No_Return (Raise_Error);
function "&" (Hi, Lo : Uns32) return Uns64 is
begin
return Shift_Left (Uns64 (Hi), 32) or Uns64 (Lo);
end "&";
function "*" (A, B : Uns32) return Uns64 is
begin
return Uns64 (A) * Uns64 (B);
end "*";
function "+" (A, B : Uns32) return Uns64 is
begin
return Uns64 (A) + Uns64 (B);
end "+";
function "+" (A : Uns64; B : Uns32) return Uns64 is
begin
return A + Uns64 (B);
end "+";
function "/" (A : Uns64; B : Uns32) return Uns64 is
begin
return A / Uns64 (B);
end "/";
function "rem" (A : Uns64; B : Uns32) return Uns64 is
begin
return A rem Uns64 (B);
end "rem";
function Add_With_Ovflo_Check (X, Y : Int64) return Int64 is
R : constant Int64 := To_Int (To_Uns (X) + To_Uns (Y));
begin
if X >= 0 then
if Y < 0 or else R >= 0 then
return R;
end if;
else if Y > 0 or else R < 0 then
return R;
end if;
end if;
Raise_Error;
end Add_With_Ovflo_Check;
procedure Double_Divide
(X, Y, Z : Int64;
Q, R : out Int64;
Round : Boolean)
is
Xu : constant Uns64 := To_Uns (abs X);
Yu : constant Uns64 := To_Uns (abs Y);
Yhi : constant Uns32 := Hi (Yu);
Ylo : constant Uns32 := Lo (Yu);
Zu : constant Uns64 := To_Uns (abs Z);
Zhi : constant Uns32 := Hi (Zu);
Zlo : constant Uns32 := Lo (Zu);
T1, T2 : Uns64;
Du, Qu, Ru : Uns64;
Den_Pos : Boolean;
begin
if Yu = 0 or else Zu = 0 then
Raise_Error;
end if;
if Yhi /= 0 then
if Zhi /= 0 then
Q := 0;
R := X;
return;
else
T2 := Yhi * Zlo;
end if;
else
if Zhi /= 0 then
T2 := Ylo * Zhi;
else
T2 := 0;
end if;
end if;
T1 := Ylo * Zlo;
T2 := T2 + Hi (T1);
if Hi (T2) /= 0 then
Q := 0;
R := X;
return;
end if;
Du := Lo (T2) & Lo (T1);
Den_Pos := (Y < 0) = (Z < 0);
if X = Int64'First and then Du = 1 and then not Den_Pos then
Raise_Error;
end if;
Qu := Xu / Du;
Ru := Xu rem Du;
if Round and then Ru > (Du - Uns64'(1)) / Uns64'(2) then
Qu := Qu + Uns64'(1);
end if;
if X >= 0 then
R := To_Int (Ru);
if Den_Pos then
Q := To_Int (Qu);
else
Q := -To_Int (Qu);
end if;
else
R := -To_Int (Ru);
if Den_Pos then
Q := -To_Int (Qu);
else
Q := To_Int (Qu);
end if;
end if;
end Double_Divide;
function Hi (A : Uns64) return Uns32 is
begin
return Uns32 (Shift_Right (A, 32));
end Hi;
function Le3 (X1, X2, X3 : Uns32; Y1, Y2, Y3 : Uns32) return Boolean is
begin
if X1 < Y1 then
return True;
elsif X1 > Y1 then
return False;
elsif X2 < Y2 then
return True;
elsif X2 > Y2 then
return False;
else
return X3 <= Y3;
end if;
end Le3;
function Lo (A : Uns64) return Uns32 is
begin
return Uns32 (A and 16#FFFF_FFFF#);
end Lo;
function Multiply_With_Ovflo_Check (X, Y : Int64) return Int64 is
Xu : constant Uns64 := To_Uns (abs X);
Xhi : constant Uns32 := Hi (Xu);
Xlo : constant Uns32 := Lo (Xu);
Yu : constant Uns64 := To_Uns (abs Y);
Yhi : constant Uns32 := Hi (Yu);
Ylo : constant Uns32 := Lo (Yu);
T1, T2 : Uns64;
begin
if Xhi /= 0 then
if Yhi /= 0 then
Raise_Error;
else
T2 := Xhi * Ylo;
end if;
elsif Yhi /= 0 then
T2 := Xlo * Yhi;
else T2 := 0;
end if;
T1 := Xlo * Ylo;
T2 := T2 + Hi (T1);
if Hi (T2) /= 0 then
Raise_Error;
end if;
T2 := Lo (T2) & Lo (T1);
if X >= 0 then
if Y >= 0 then
return To_Pos_Int (T2);
else
return To_Neg_Int (T2);
end if;
else if Y < 0 then
return To_Pos_Int (T2);
else
return To_Neg_Int (T2);
end if;
end if;
end Multiply_With_Ovflo_Check;
procedure Raise_Error is
begin
Raise_Exception (CE, "64-bit arithmetic overflow");
end Raise_Error;
procedure Scaled_Divide
(X, Y, Z : Int64;
Q, R : out Int64;
Round : Boolean)
is
Xu : constant Uns64 := To_Uns (abs X);
Xhi : constant Uns32 := Hi (Xu);
Xlo : constant Uns32 := Lo (Xu);
Yu : constant Uns64 := To_Uns (abs Y);
Yhi : constant Uns32 := Hi (Yu);
Ylo : constant Uns32 := Lo (Yu);
Zu : Uns64 := To_Uns (abs Z);
Zhi : Uns32 := Hi (Zu);
Zlo : Uns32 := Lo (Zu);
D : array (1 .. 4) of Uns32;
Qd : array (1 .. 2) of Uns32;
S1, S2, S3 : Uns32;
Qu : Uns64;
Ru : Uns64;
Scale : Natural;
T1, T2, T3 : Uns64;
begin
T1 := Xlo * Ylo;
D (4) := Lo (T1);
D (3) := Hi (T1);
if Yhi /= 0 then
T1 := Xlo * Yhi;
T2 := D (3) + Lo (T1);
D (3) := Lo (T2);
D (2) := Hi (T1) + Hi (T2);
if Xhi /= 0 then
T1 := Xhi * Ylo;
T2 := D (3) + Lo (T1);
D (3) := Lo (T2);
T3 := D (2) + Hi (T1);
T3 := T3 + Hi (T2);
D (2) := Lo (T3);
D (1) := Hi (T3);
T1 := (D (1) & D (2)) + Uns64'(Xhi * Yhi);
D (1) := Hi (T1);
D (2) := Lo (T1);
else
D (1) := 0;
end if;
else
if Xhi /= 0 then
T1 := Xhi * Ylo;
T2 := D (3) + Lo (T1);
D (3) := Lo (T2);
D (2) := Hi (T1) + Hi (T2);
else
D (2) := 0;
end if;
D (1) := 0;
end if;
if Zhi = 0 then
if D (1) /= 0 or else D (2) >= Zlo then
Raise_Error;
else
T1 := D (2) & D (3);
T2 := Lo (T1 rem Zlo) & D (4);
Qu := Lo (T1 / Zlo) & Lo (T2 / Zlo);
Ru := T2 rem Zlo;
end if;
elsif (D (1) & D (2)) >= Zu then
Raise_Error;
else
Scale := 0;
if (Zhi and 16#FFFF0000#) = 0 then
Scale := 16;
Zu := Shift_Left (Zu, 16);
end if;
if (Hi (Zu) and 16#FF00_0000#) = 0 then
Scale := Scale + 8;
Zu := Shift_Left (Zu, 8);
end if;
if (Hi (Zu) and 16#F000_0000#) = 0 then
Scale := Scale + 4;
Zu := Shift_Left (Zu, 4);
end if;
if (Hi (Zu) and 16#C000_0000#) = 0 then
Scale := Scale + 2;
Zu := Shift_Left (Zu, 2);
end if;
if (Hi (Zu) and 16#8000_0000#) = 0 then
Scale := Scale + 1;
Zu := Shift_Left (Zu, 1);
end if;
Zhi := Hi (Zu);
Zlo := Lo (Zu);
T1 := Shift_Left (D (1) & D (2), Scale);
D (1) := Hi (T1);
T2 := Shift_Left (0 & D (3), Scale);
D (2) := Lo (T1) or Hi (T2);
T3 := Shift_Left (0 & D (4), Scale);
D (3) := Lo (T2) or Hi (T3);
D (4) := Lo (T3);
for J in 0 .. 1 loop
if D (J + 1) = Zhi then
Qd (J + 1) := 2 ** 32 - 1;
else
Qd (J + 1) := Lo ((D (J + 1) & D (J + 2)) / Zhi);
end if;
T1 := Qd (J + 1) * Zlo;
T2 := Qd (J + 1) * Zhi;
S3 := Lo (T1);
T1 := Hi (T1) + Lo (T2);
S2 := Lo (T1);
S1 := Hi (T1) + Hi (T2);
loop
exit when Le3 (S1, S2, S3, D (J + 1), D (J + 2), D (J + 3));
Qd (J + 1) := Qd (J + 1) - 1;
Sub3 (S1, S2, S3, 0, Zhi, Zlo);
end loop;
Sub3 (D (J + 1), D (J + 2), D (J + 3), S1, S2, S3);
end loop;
Qu := Qd (1) & Qd (2);
Ru := Shift_Right (D (3) & D (4), Scale);
Zu := Shift_Right (Zu, Scale);
end if;
if Round and then Ru > (Zu - Uns64'(1)) / Uns64'(2) then
Qu := Qu + Uns64 (1);
end if;
if (X >= 0 and then Y >= 0)
or else (X < 0 and then Y < 0)
then
R := To_Pos_Int (Ru);
if Z > 0 then
Q := To_Pos_Int (Qu);
else
Q := To_Neg_Int (Qu);
end if;
else
R := To_Neg_Int (Ru);
if Z > 0 then
Q := To_Neg_Int (Qu);
else
Q := To_Pos_Int (Qu);
end if;
end if;
end Scaled_Divide;
procedure Sub3 (X1, X2, X3 : in out Uns32; Y1, Y2, Y3 : in Uns32) is
begin
if Y3 > X3 then
if X2 = 0 then
X1 := X1 - 1;
end if;
X2 := X2 - 1;
end if;
X3 := X3 - Y3;
if Y2 > X2 then
X1 := X1 - 1;
end if;
X2 := X2 - Y2;
X1 := X1 - Y1;
end Sub3;
function Subtract_With_Ovflo_Check (X, Y : Int64) return Int64 is
R : constant Int64 := To_Int (To_Uns (X) - To_Uns (Y));
begin
if X >= 0 then
if Y > 0 or else R >= 0 then
return R;
end if;
else if Y <= 0 or else R < 0 then
return R;
end if;
end if;
Raise_Error;
end Subtract_With_Ovflo_Check;
function To_Neg_Int (A : Uns64) return Int64 is
R : constant Int64 := -To_Int (A);
begin
if R <= 0 then
return R;
else
Raise_Error;
end if;
end To_Neg_Int;
function To_Pos_Int (A : Uns64) return Int64 is
R : constant Int64 := To_Int (A);
begin
if R >= 0 then
return R;
else
Raise_Error;
end if;
end To_Pos_Int;
end System.Arith_64;