hcl/lib/h2-scheme-bigint.adb

785 lines
23 KiB
Ada
Raw Normal View History

2014-02-20 14:51:53 +00:00
with H2.Pool;
separate (H2.Scheme)
package body Bigint is
use type System.Bit_Order;
Big_Endian : constant := Standard.Boolean'Pos (
System.Default_Bit_Order = System.High_Order_First
);
Little_Endian : constant := Standard.Boolean'Pos (
System.Default_Bit_Order = System.Low_Order_First
);
2014-02-24 15:17:57 +00:00
--Half_Word_Bits: constant := Object_Pointer_Bits / 2;
Half_Word_Bits: constant := Object_Half_Word'Size;
2014-02-20 14:51:53 +00:00
Half_Word_Bytes: constant := Half_Word_Bits / System.Storage_Unit;
type Word_Record is record
Low: Object_Half_Word;
High: Object_Half_Word;
end record;
for Word_Record use record
--Low at 0 range 0 .. Half_Word_Bits - 1;
--High at 0 range Half_Word_Bits .. Word_Bits - 1;
Low at Half_Word_Bytes * (0 * Little_Endian + 1 * Big_Endian)
range 0 .. Half_Word_Bits - 1;
High at Half_Word_Bytes * (1 * Little_Endian + 0 * Big_Endian)
range 0 .. Half_Word_Bits - 1;
end record;
for Word_Record'Size use Object_Word'Size;
--for Word_Record'Size use Object_Pointer_Bits;
--for Word_Record'Alignment use Object_Word'Alignment;
--for Word_Record'Scalar_Storage_Order use System.High_Order_First;
--for Word_Record'Bit_Order use System.High_Order_First;
--for Word_Record'Bit_Order use System.Low_Order_First;
2014-02-24 15:17:57 +00:00
-----------------------------------------------------------------------------
2014-02-20 14:51:53 +00:00
function Get_Low (W: in Object_Word) return Object_Half_Word is
R: Word_Record;
for R'Address use W'Address;
begin
return R.Low;
end Get_Low;
function Get_High (W: in Object_Word) return Object_Half_Word is
R: Word_Record;
for R'Address use W'Address;
begin
return R.High;
end Get_High;
2014-02-23 16:57:31 +00:00
function Make_Word (L: in Object_Half_Word;
H: in Object_Half_Word) return Object_Word is
W: Object_Word;
R: Word_Record;
for R'Address use W'Address;
2014-02-21 16:08:43 +00:00
begin
2014-02-23 16:57:31 +00:00
R.Low := L;
R.High := H;
return W;
end Make_Word;
2014-02-21 16:08:43 +00:00
2014-02-24 15:17:57 +00:00
-----------------------------------------------------------------------------
2014-03-03 15:36:03 +00:00
function Is_Less_Unsigned_Array (X: in Object_Half_Word_Array;
XS: in Half_Word_Object_Size;
Y: in Object_Half_Word_Array;
YS: in Half_Word_Object_Size) return Standard.Boolean is
pragma Inline (Is_Less_Unsigned_Array);
2014-02-23 16:57:31 +00:00
begin
2014-03-03 15:36:03 +00:00
if XS /= YS then
return XS < YS;
2014-02-21 16:08:43 +00:00
end if;
2014-03-03 15:36:03 +00:00
for I in reverse X'Range loop
if X(I) /= Y(I) then
return X(I) < Y(I);
end if;
2014-02-21 16:08:43 +00:00
end loop;
return Standard.False;
2014-03-03 15:36:03 +00:00
end Is_Less_Unsigned_Array;
function Is_Less_Unsigned (X: in Object_Pointer;
Y: in Object_Pointer) return Standard.Boolean is
pragma Inline (Is_Less_Unsigned);
begin
return Is_Less_Unsigned_Array(X.Half_Word_Slot, X.Size, Y.Half_Word_Slot, Y.Size);
2014-02-23 16:57:31 +00:00
end Is_Less_Unsigned;
function Is_Less (X: in Object_Pointer;
Y: in Object_Pointer) return Standard.Boolean is
begin
if X.Sign /= Y.Sign then
return X.Sign = Negative_Sign;
end if;
2014-03-03 15:36:03 +00:00
return Is_Less_Unsigned(X, Y);
2014-02-21 16:08:43 +00:00
end Is_Less;
function Is_Equal (X: in Object_Pointer;
Y: in Object_Pointer) return Standard.Boolean is
begin
return X.Sign = Y.Sign and then
X.Size = Y.Size and then
X.Half_Word_Slot = Y.Half_Word_Slot;
end Is_Equal;
2014-02-23 16:57:31 +00:00
function Is_Zero (X: in Object_Pointer) return Standard.Boolean is
2014-03-03 15:36:03 +00:00
pragma Inline (Is_Zero);
2014-02-23 16:57:31 +00:00
begin
return X.Size = 1 and then X.Half_Word_Slot(1) = 0;
end Is_Zero;
2014-03-03 15:36:03 +00:00
function Is_One_Unsigned (X: in Object_Pointer) return Standard.Boolean is
pragma Inline (Is_One_Unsigned);
begin
return X.Size = 1 and then X.Half_Word_Slot(1) = 1;
end Is_One_Unsigned;
2014-02-24 15:17:57 +00:00
-----------------------------------------------------------------------------
2014-03-03 15:36:03 +00:00
function Copy_Upto (Interp: access Interpreter_Record;
X: in Object_Pointer;
Last: in Half_Word_Object_Size) return Object_Pointer is
pragma Assert (Last <= X.Size);
2014-03-03 15:36:03 +00:00
A: aliased Object_Pointer := X;
Z: Object_Pointer;
begin
Push_Top (Interp.all, A'Unchecked_Access);
Z := Make_Bigint(Interp, Size => Last);
Pop_Tops (Interp.all, 1);
2014-03-03 15:36:03 +00:00
Z.Sign := A.Sign;
Z.Half_Word_Slot := A.Half_Word_Slot(1 .. Last);
return Z;
end Copy_Upto;
2014-02-24 15:17:57 +00:00
2014-03-03 15:36:03 +00:00
function Count_Effective_Slots (X: in Object_Pointer) return Half_Word_Object_Size is
pragma Inline (Count_Effective_Slots);
2014-02-24 15:17:57 +00:00
Last: Half_Word_Object_Size := 1;
2014-02-23 16:57:31 +00:00
begin
2014-02-24 15:17:57 +00:00
for I in reverse 1 .. X.Size loop
if X.Half_Word_Slot(I) /= 0 then
Last := I;
exit;
end if;
end loop;
2014-03-03 15:36:03 +00:00
return Last;
end Count_Effective_Slots;
2014-03-03 15:36:03 +00:00
function Normalize (Interp: access Interpreter_Record;
X: in Object_Pointer) return Object_Pointer is
Last: Half_Word_Object_Size;
begin
Last := Count_Effective_Slots(X);
2014-02-24 15:17:57 +00:00
case Last is
2014-02-23 16:57:31 +00:00
when 1 =>
if X.Sign = Negative_Sign then
return Integer_To_Pointer(-Object_Integer(X.Half_Word_Slot(1)));
else
return Integer_To_Pointer(Object_Integer(X.Half_Word_Slot(1)));
end if;
2014-03-03 15:36:03 +00:00
2014-02-23 16:57:31 +00:00
when 2 =>
declare
W: Object_Word := Make_Word(X.Half_Word_Slot(1), X.Half_Word_Slot(2));
2014-02-23 16:57:31 +00:00
begin
if X.Sign = Negative_Sign then
if W in 0 .. Object_Word(-Object_Signed_Word(Object_Integer'First)) then
return Integer_To_Pointer(-Object_Integer(W));
end if;
else
if W in 0 .. Object_Word(Object_Integer'Last) then
return Integer_To_Pointer(Object_Integer(W));
end if;
end if;
end;
when others =>
2014-02-24 15:17:57 +00:00
null;
2014-02-23 16:57:31 +00:00
end case;
2014-02-24 15:17:57 +00:00
if X.Size = Last then
2014-03-03 15:36:03 +00:00
-- No compaction is needed. return it as it is
2014-02-24 15:17:57 +00:00
return X;
end if;
2014-03-03 15:36:03 +00:00
-- Remove unneeded slots and clone meaningful contents only.
return Copy_Upto(Interp, X, Last);
2014-02-23 16:57:31 +00:00
end Normalize;
2014-02-24 15:17:57 +00:00
-----------------------------------------------------------------------------
generic
with function Operator (X: in Object_Integer;
Y: in Object_Integer) return Object_Integer;
2014-03-04 14:32:49 +00:00
procedure Plain_Integer_Op (Interp: in out Interpreter_Record;
2014-02-24 15:17:57 +00:00
X: in out Object_Pointer;
Y: in out Object_Pointer;
Z: out Object_Pointer);
2014-03-04 14:32:49 +00:00
procedure Plain_Integer_Op (Interp: in out Interpreter_Record;
2014-02-24 15:17:57 +00:00
X: in out Object_Pointer;
Y: in out Object_Pointer;
Z: out Object_Pointer) is
A: aliased Object_Pointer := X;
B: aliased Object_Pointer := Y;
begin
if Is_Integer(A) and then Is_Integer(B) then
declare
G: Object_Integer := Pointer_To_Integer(A);
H: Object_Integer := Pointer_To_Integer(B);
begin
X := A;
Y := B;
Z := Integer_To_Pointer(Operator(G, H));
return;
exception
when Constraint_Error =>
2014-03-04 14:32:49 +00:00
Push_Top (Interp, A'Unchecked_Access);
Push_Top (Interp, B'Unchecked_Access);
2014-02-24 15:17:57 +00:00
-- TODO: allocate A and B from a non-GC heap.
-- I know that pointers returned by Make_Bigint here are short-lived
-- and not needed after actual operation. non-GC heap is a better choice.
2014-03-04 14:32:49 +00:00
A := Make_Bigint(Interp.Self, Value => G);
B := Make_Bigint(Interp.Self, Value => H);
Pop_Tops (Interp, 2);
2014-02-24 15:17:57 +00:00
end;
else
2014-03-04 14:32:49 +00:00
Push_Top (Interp, A'Unchecked_Access);
Push_Top (Interp, B'Unchecked_Access);
2014-02-24 15:17:57 +00:00
if Is_Integer(A) then
2014-03-04 14:32:49 +00:00
A := Make_Bigint(Interp.Self, Value => Pointer_To_Integer(A));
2014-02-24 15:17:57 +00:00
end if;
if Is_Integer(B) then
2014-03-04 14:32:49 +00:00
B := Make_Bigint(Interp.Self, Value => Pointer_To_Integer(B));
2014-03-03 15:36:03 +00:00
end if;
2014-03-04 14:32:49 +00:00
Pop_Tops (Interp, 2);
2014-02-24 15:17:57 +00:00
end if;
X := A;
Y := B;
Z := null;
end Plain_Integer_Op;
procedure Add_Integers is new Plain_Integer_Op (Operator => "+");
procedure Subtract_Integers is new Plain_Integer_Op (Operator => "-");
procedure Multiply_Integers is new Plain_Integer_Op (Operator => "*");
procedure Divide_Integers is new Plain_Integer_Op (Operator => "/");
-----------------------------------------------------------------------------
2014-03-04 14:32:49 +00:00
procedure Add_Unsigned_Array (X: in Object_Half_Word_Array;
XS: in Half_Word_Object_Size;
Y: in Object_Half_Word_Array;
YS: in Half_Word_Object_Size;
Z: in out Object_Half_Word_Array) is
pragma Inline (Add_Unsigned_Array);
pragma Assert (XS >= YS);
W: Object_Word;
Carry: Object_Half_Word := 0;
begin
for I in 1 .. YS loop
W := Object_Word(X(I)) + Object_Word(Y(I)) + Object_Word(Carry);
Carry := Get_High(W);
Z(I) := Get_Low(W);
end loop;
for I in YS + 1 .. XS loop
W := Object_Word(X(I)) + Object_Word(Carry);
Carry := Get_High(W);
Z(I) := Get_Low(W);
end loop;
Z(XS + 1) := Carry;
end Add_Unsigned_Array;
2014-02-23 16:57:31 +00:00
function Add_Unsigned (Interp: access Interpreter_Record;
X: in Object_Pointer;
Y: in Object_Pointer) return Object_Pointer is
2014-03-03 15:36:03 +00:00
A, B: aliased Object_Pointer;
Z: Object_Pointer;
2014-02-20 14:51:53 +00:00
begin
2014-02-23 16:57:31 +00:00
if X.Size >= Y.Size then
2014-02-21 16:08:43 +00:00
A := X;
B := Y;
else
A := Y;
2014-03-03 15:36:03 +00:00
B := X;
2014-02-21 16:08:43 +00:00
end if;
2014-03-04 14:32:49 +00:00
2014-03-03 15:36:03 +00:00
Push_Top (Interp.all, A'Unchecked_Access);
Push_Top (Interp.all, B'Unchecked_Access);
2014-03-04 14:32:49 +00:00
Z := Make_Bigint (Interp.Self, A.Size + 1);
2014-03-03 15:36:03 +00:00
Pop_Tops (Interp.all, 2);
2014-02-21 16:08:43 +00:00
2014-03-04 14:32:49 +00:00
Add_Unsigned_Array (A.Half_Word_Slot, A.Size, B.Half_Word_Slot, B.Size, Z.Half_Word_Slot);
2014-02-21 16:08:43 +00:00
return Z;
2014-02-23 16:57:31 +00:00
end Add_Unsigned;
2014-03-03 15:36:03 +00:00
procedure Subtract_Unsigned_Array (X: in Object_Half_Word_Array;
XS: in Half_Word_Object_Size;
Y: in Object_Half_Word_Array;
YS: in Half_Word_Object_Size;
Z: in out Object_Half_Word_Array) is
W: Object_Word;
2014-02-25 16:10:46 +00:00
Borrowed_Word: constant Object_Word := Object_Word(Object_Half_Word'Last) + 1;
2014-03-03 15:36:03 +00:00
Borrow: Object_Half_Word := 0;
2014-02-21 16:08:43 +00:00
begin
pragma Assert (not Is_Less_Unsigned_Array(X, XS, Y, YS)); -- The caller must ensure that X >= Y
2014-03-03 15:36:03 +00:00
for I in 1 .. YS loop
W := Object_Word(Y(I)) + Object_Word(Borrow);
if Object_Word(X(I)) >= W then
Z(I) := X(I) - Object_Half_Word(W);
2014-02-21 16:08:43 +00:00
Borrow := 0;
2014-02-25 16:10:46 +00:00
else
2014-03-03 15:36:03 +00:00
Z(I) := Object_Half_Word(Borrowed_Word + Object_Word(X(I)) - W);
2014-02-25 16:10:46 +00:00
Borrow := 1;
2014-02-21 16:08:43 +00:00
end if;
end loop;
2014-03-03 15:36:03 +00:00
for I in YS + 1 .. XS loop
if X(I) >= Borrow then
Z(I) := X(I) - Object_Half_Word(Borrow);
2014-02-21 16:08:43 +00:00
Borrow := 0;
2014-02-25 16:10:46 +00:00
else
2014-03-03 15:36:03 +00:00
Z(I) := Object_Half_Word(Borrowed_Word + Object_Word(X(I)) - Object_Word(Borrow));
2014-02-25 16:10:46 +00:00
Borrow := 1;
2014-02-21 16:08:43 +00:00
end if;
end loop;
2014-02-24 15:17:57 +00:00
2014-02-25 16:10:46 +00:00
pragma Assert (Borrow = 0);
2014-03-03 15:36:03 +00:00
end Subtract_Unsigned_Array;
2014-02-23 16:57:31 +00:00
2014-03-03 15:36:03 +00:00
function Subtract_Unsigned (Interp: access Interpreter_Record;
2014-02-24 15:17:57 +00:00
X: in Object_Pointer;
Y: in Object_Pointer) return Object_Pointer is
2014-03-03 15:36:03 +00:00
pragma Inline (Subtract_Unsigned);
A: aliased Object_Pointer := X;
B: aliased Object_Pointer := Y;
Z: Object_Pointer;
2014-02-24 15:17:57 +00:00
W: Object_Word;
2014-03-03 15:36:03 +00:00
Borrowed_Word: constant Object_Word := Object_Word(Object_Half_Word'Last) + 1;
Borrow: Object_Half_Word := 0;
2014-02-24 15:17:57 +00:00
begin
2014-03-03 15:36:03 +00:00
pragma Assert (not Is_Less_Unsigned(A, B)); -- The caller must ensure that X >= Y
2014-02-24 15:17:57 +00:00
Push_Top (Interp.all, A'Unchecked_Access);
Push_Top (Interp.all, B'Unchecked_Access);
2014-03-03 15:36:03 +00:00
Z := Make_Bigint (Interp.Self, A.Size); -- Assume X.Size >= Y.Size.
Pop_Tops (Interp.all, 2);
2014-03-04 14:32:49 +00:00
2014-03-03 15:36:03 +00:00
Subtract_Unsigned_Array (A.Half_Word_Slot, A.Size, B.Half_Word_SLot, B.Size, Z.Half_Word_Slot);
return Z;
end Subtract_Unsigned;
2014-02-24 15:17:57 +00:00
2014-03-03 15:36:03 +00:00
procedure Multiply_Unsigned_Array (X: in Object_Half_Word_Array;
XS: in Half_Word_Object_Size;
Y: in Object_Half_Word_Array;
YS: in Half_Word_Object_Size;
Z: in out Object_Half_Word_Array) is
W: Object_Word;
Low, High: Object_Half_Word;
Carry: Object_Half_Word;
Index: Half_Word_Object_Size;
begin
for I in 1 .. YS loop
if Y(I) = 0 then
Z(XS + I) := 0;
2014-02-24 15:17:57 +00:00
else
2014-03-03 15:36:03 +00:00
Carry := 0;
2014-02-24 15:17:57 +00:00
2014-03-03 15:36:03 +00:00
for J in 1 .. XS loop
W := Object_Word(X(J)) * Object_Word(Y(I));
Low := Get_Low(W);
High := Get_High(W);
2014-02-24 15:17:57 +00:00
Low := Low + Carry;
if Low < Carry then
High := High + 1;
end if;
2014-03-03 15:36:03 +00:00
2014-02-24 15:17:57 +00:00
Index := J + I - 1;
2014-03-03 15:36:03 +00:00
Low := Low + Z(Index);
if Low < Z(Index) then
2014-02-24 15:17:57 +00:00
High := High + 1;
end if;
2014-03-03 15:36:03 +00:00
Z(Index) := Low;
2014-02-24 15:17:57 +00:00
Carry := High;
end loop;
2014-03-03 15:36:03 +00:00
Z(XS + I) := Carry;
2014-02-24 15:17:57 +00:00
end if;
end loop;
2014-03-03 15:36:03 +00:00
end Multiply_Unsigned_Array;
function Multiply_Unsigned (Interp: access Interpreter_Record;
X: in Object_Pointer;
Y: in Object_Pointer) return Object_Pointer is
pragma Inline (Multiply_Unsigned);
A: aliased Object_Pointer := X;
B: aliased Object_Pointer := Y;
Z: Object_Pointer;
begin
Push_Top (Interp.all, A'Unchecked_Access);
Push_Top (Interp.all, B'Unchecked_Access);
Z := Make_Bigint (Interp.Self, A.Size + B.Size);
Pop_Tops (Interp.all, 2);
2014-02-24 15:17:57 +00:00
2014-03-03 15:36:03 +00:00
Multiply_Unsigned_Array (A.Half_Word_Slot, A.Size, B.Half_Word_Slot, B.Size, Z.Half_Word_Slot);
2014-02-24 15:17:57 +00:00
return Z;
end Multiply_Unsigned;
2014-02-25 16:10:46 +00:00
2014-03-04 14:32:49 +00:00
procedure Divide_Unsigned (Interp: in out Interpreter_Record;
2014-03-03 15:36:03 +00:00
X: in Object_Pointer;
Y: in Object_Pointer;
Q: out Object_Pointer;
R: out Object_Pointer) is
A: aliased Object_Pointer := X;
B: aliased Object_Pointer := Y;
Quo: aliased Object_Pointer;
Dend: aliased Object_Pointer; -- Dividend
Sor: aliased Object_Pointer; -- Divisor
Tmp: Object_Pointer;
Diff: Half_Word_Object_Size;
Dend_Size: Half_Word_Object_Size;
Sor_Size: Half_Word_Object_Size;
Tmp_Size: Half_Word_Object_Size;
2014-03-04 14:32:49 +00:00
2014-03-03 15:36:03 +00:00
Cand_W: Object_Word;
Cand: Object_Half_Word_Array (1 .. 2);
2014-03-04 14:32:49 +00:00
Cand_Size: Half_Word_Object_Size;
2014-02-25 16:10:46 +00:00
begin
2014-03-03 15:36:03 +00:00
pragma Assert (not Is_Less_Unsigned(A, B)); -- The caller must ensure that X >= Y
2014-03-04 14:32:49 +00:00
Push_Top (Interp, A'Unchecked_Access);
Push_Top (Interp, B'Unchecked_Access);
Push_Top (Interp, Quo'Unchecked_Access);
Push_Top (Interp, Dend'Unchecked_Access);
Push_Top (Interp, Sor'Unchecked_Access);
2014-03-03 15:36:03 +00:00
Quo := Make_Bigint (Interp.Self, A.Size);
Dend := Make_Bigint (Interp.Self, A.Size);
Sor := Make_Bigint (Interp.Self, A.Size);
2014-03-04 14:32:49 +00:00
Tmp := Make_Bigint (Interp.Self, A.Size + 2); -- Is it enough? A.Size + B.Size is safer
Pop_Tops (Interp, 5);
2014-03-03 15:36:03 +00:00
Dend_Size := A.Size;
Sor_Size := A.Size;
Diff := A.Size - B.Size;
Dend.Half_Word_Slot := A.Half_Word_Slot;
Sor.Half_Word_Slot(1 + Diff .. B.Size + Diff) := B.Half_Word_Slot;
for I in reverse B.Size .. A.Size loop
2014-03-04 14:32:49 +00:00
if not Is_Less_Unsigned_Array(Dend.Half_Word_Slot, Dend_Size, Sor.Half_Word_Slot, Sor_Size) then
2014-03-03 15:36:03 +00:00
if Dend_Size > Sor_Size then
2014-03-04 14:32:49 +00:00
-- Take the 2 high digits from the dividend and
-- the highest digit from the divisor and guess the quotient digits.
2014-03-03 15:36:03 +00:00
Cand_W := Make_Word(Dend.Half_Word_Slot(Dend_Size - 1), Dend.Half_Word_Slot(Dend_Size));
Cand_W := Cand_W / Object_Word(Sor.Half_Word_Slot(Sor_Size));
Cand(1) := Get_Low(Cand_W);
Cand(2) := Get_High(Cand_W);
if Cand(2) > 0 then
Cand_Size := 2;
else
Cand_Size := 1;
end if;
else
2014-03-04 14:32:49 +00:00
-- Take the highest digit from the dividend and the divisor
-- and guess the quotient digit.
2014-03-03 15:36:03 +00:00
Cand(1) := Dend.Half_Word_Slot(Dend_Size) / Sor.Half_Word_Slot(Sor_Size);
Cand_Size := 1;
end if;
2014-03-04 14:32:49 +00:00
-- Multiply the divisor and the quotient candidate.
2014-03-03 15:36:03 +00:00
Tmp.Half_Word_Slot := (others => 0);
Multiply_Unsigned_Array (Cand, Cand_Size, Sor.Half_Word_Slot, Sor_Size, Tmp.Half_Word_Slot);
Tmp_Size := Count_Effective_Slots(Tmp);
-- Check if the dividend is less than the multiplication result. Dividend < Tmp
2014-03-03 15:36:03 +00:00
if Is_Less_Unsigned_Array(Dend.Half_Word_Slot, Dend_Size, Tmp.Half_Word_Slot, Tmp_Size) then
-- If so, decrement the candidate by 1.
Quo.Half_Word_Slot(I - B.Size + 1) := Cand(1) - 1;
-- Tmp := Tmp - Divisor
Subtract_Unsigned_Array (Tmp.Half_Word_Slot, Tmp_Size, Sor.Half_Word_Slot, Sor_Size, Tmp.Half_Word_Slot);
Tmp_Size := Count_Effective_Slots(Tmp);
2014-03-03 15:36:03 +00:00
else
2014-03-04 14:32:49 +00:00
-- If not, the candidate is the right guess.
Quo.Half_Word_Slot(I - B.Size + 1) := Cand(1);
2014-03-03 15:36:03 +00:00
end if;
-- Dividend := Dividend - Tmp
Subtract_Unsigned_Array (Dend.Half_Word_Slot, Dend_Size, Tmp.Half_Word_Slot, Tmp_Size, Dend.Half_Word_Slot);
Dend_Size := Count_Effective_Slots(Dend);
2014-03-03 15:36:03 +00:00
end if;
-- Shift the divisor right by 1 slot
pragma Assert (I = Sor_Size);
Sor_Size := Sor_Size - 1;
Sor.Half_Word_Slot(1 .. Sor_Size) := Sor.Half_Word_Slot(2 .. I);
Sor.Half_Word_Slot(I) := 0;
end loop;
Q := Quo;
2014-03-04 14:32:49 +00:00
R := Dend;
2014-02-25 16:10:46 +00:00
end Divide_Unsigned;
2014-03-03 15:36:03 +00:00
2014-02-24 15:17:57 +00:00
-----------------------------------------------------------------------------
2014-03-04 14:32:49 +00:00
procedure Add (Interp: in out Interpreter_Record;
X: in Object_Pointer;
Y: in Object_Pointer;
Z: out Object_Pointer) is
2014-02-24 15:17:57 +00:00
A: Object_Pointer := X;
B: Object_Pointer := Y;
Sign: Object_Sign;
2014-02-23 16:57:31 +00:00
begin
2014-02-24 15:17:57 +00:00
Add_Integers (Interp, A, B, Z);
2014-03-04 14:32:49 +00:00
if Z = null then
if A.Sign /= B.Sign then
if A.Sign = Negative_Sign then
Subtract (Interp, B, A, Z);
else
Subtract (Interp, A, B, Z);
end if;
2014-02-23 16:57:31 +00:00
else
2014-03-04 14:32:49 +00:00
Sign := A.Sign;
Z := Add_Unsigned (Interp.Self, A, B);
Z.Sign := Sign;
2014-02-23 16:57:31 +00:00
end if;
2014-03-04 14:32:49 +00:00
Z := Normalize(Interp.Self, Z);
2014-02-23 16:57:31 +00:00
end if;
end Add;
2014-03-04 14:32:49 +00:00
procedure Subtract (Interp: in out Interpreter_Record;
X: in Object_Pointer;
Y: in Object_Pointer;
Z: out Object_Pointer) is
2014-02-24 15:17:57 +00:00
A: Object_Pointer := X;
B: Object_Pointer := Y;
Sign: Object_Sign;
2014-02-23 16:57:31 +00:00
begin
2014-02-24 15:17:57 +00:00
Subtract_Integers (Interp, A, B, Z);
2014-03-04 14:32:49 +00:00
if Z = null then
if A.Sign /= B.Sign then
2014-02-24 15:17:57 +00:00
Sign := A.Sign;
2014-03-04 14:32:49 +00:00
Z := Add_Unsigned(Interp.Self, A, B);
2014-02-24 15:17:57 +00:00
Z.Sign := Sign;
2014-03-04 14:32:49 +00:00
else
if Is_Less_Unsigned(A, B) then
--Sign := Object_Sign'Val(not Object_Sign'Pos(A.Sign)); -- opposite A.Sign
if A.Sign = Negative_Sign then
Sign := Positive_Sign;
else
Sign := Negative_Sign;
end if;
Z := Subtract_Unsigned(Interp.Self, B, A);
Z.Sign := Sign;
else
Sign := A.Sign;
Z := Subtract_Unsigned(Interp.Self, A, B);
Z.Sign := Sign;
end if;
2014-02-23 16:57:31 +00:00
end if;
2014-03-04 14:32:49 +00:00
Z := Normalize(Interp.Self, Z);
2014-02-23 16:57:31 +00:00
end if;
end Subtract;
2014-02-24 15:17:57 +00:00
2014-03-04 14:32:49 +00:00
procedure Multiply (Interp: in out Interpreter_Record;
X: in Object_Pointer;
Y: in Object_Pointer;
Z: out Object_Pointer) is
2014-02-24 15:17:57 +00:00
A: Object_Pointer := X;
B: Object_Pointer := Y;
Sign: Object_Sign;
begin
Multiply_Integers (Interp, A, B, Z);
2014-03-04 14:32:49 +00:00
if Z = null then
-- Determine the sign earlier than any object allocation
-- to avoid GC side-effects because A and B are not pushed
-- as temporarry object pointers.
if A.Sign = B.Sign then
Sign := Positive_Sign;
else
Sign := Negative_Sign;
end if;
Z := Multiply_Unsigned (Interp.Self, A, B);
Z.Sign := Sign;
Z := Normalize(Interp.Self, Z);
2014-02-24 15:17:57 +00:00
end if;
end Multiply;
2014-03-04 14:32:49 +00:00
procedure Divide (Interp: in out Interpreter_Record;
2014-03-03 15:36:03 +00:00
X: in Object_Pointer;
Y: in Object_Pointer;
Q: out Object_Pointer;
R: out Object_Pointer) is
2014-02-25 16:10:46 +00:00
A: Object_Pointer := X;
B: Object_Pointer := Y;
2014-03-03 15:36:03 +00:00
C: aliased Object_Pointer;
D: aliased Object_Pointer;
2014-02-25 16:10:46 +00:00
Sign: Object_Sign;
begin
2014-03-03 15:36:03 +00:00
if (Is_Integer(Y) and then Pointer_To_Integer(Y) = 0) or else
(Is_Bigint(Y) and then Is_Zero(Y)) then
raise Divide_By_Zero_Error;
end if;
Divide_Integers (Interp, A, B, Q);
if Q /= null then
-- remainder operation must succeed if division was ok.
R := Integer_To_Pointer(Pointer_To_Integer(A) rem Pointer_To_Integer(B));
return;
end if;
if Is_Equal(A, B) then
Q := Integer_To_Pointer(1);
R := Integer_To_Pointer(0);
return;
elsif Is_Less_Unsigned(A, B) then
Q := Integer_To_Pointer(0);
R := A;
return;
2014-02-25 16:10:46 +00:00
end if;
-- Determine the sign earlier than any object allocation
-- to avoid GC side-effects because A and B are not pushed
-- as temporarry object pointers.
if A.Sign = B.Sign then
Sign := Positive_Sign;
else
Sign := Negative_Sign;
end if;
2014-03-03 15:36:03 +00:00
Divide_Unsigned (Interp, A, B, C, D);
2014-03-03 15:36:03 +00:00
C.Sign := Sign;
D.Sign := Sign;
2014-03-04 14:32:49 +00:00
Push_Top (Interp, C'Unchecked_Access);
Push_Top (Interp, D'Unchecked_Access);
C := Normalize(Interp.Self, C);
D := Normalize(Interp.Self, D);
Pop_Tops (Interp, 2);
2014-03-03 15:36:03 +00:00
Q := C;
R := D;
2014-02-25 16:10:46 +00:00
end Divide;
2014-03-04 14:32:49 +00:00
procedure To_String (Interp: in out Interpreter_Record;
X: in Object_Pointer;
Radix: in Object_Half_Word; -- TODO define the radix type to a subtype range 2 .. 32
2014-03-04 14:32:49 +00:00
Z: out Object_Pointer) is
A: aliased Object_Pointer;
B: aliased Object_Pointer;
R: aliased Object_Pointer;
W, V: Object_Word;
Sign: Object_Sign;
Radlen: Object_Word;
Seglen: Object_Word;
-- TODO: estimate the length of the character array and create a temporary string object instead of this array.
QQQ: Object_Character_Array (1.. X.Size * Object_Half_Word'Size);
QL: Character_Object_Size := 0;
2014-03-04 14:32:49 +00:00
begin
if Is_Integer(X) then
-- TODO: change this
ada.text_io.put_line(Object_Integer'Image(Pointer_To_Integer(X)));
return;
end if;
if X.Size <= 2 then
--TODO: sign;
if X.Size = 2 then
W := Make_Word(X.Half_Word_Slot(1), X.Half_Word_Slot(2));
else
W := Object_Word(X.Half_Word_Slot(1));
end if;
ada.text_io.put_line(Object_Word'Image(W));
return;
end if;
-- Find the largest multiple of Radix that is less than or
-- equal to Object_Word'Last.
Radlen := 1;
W := Object_Word(Radix);
loop
V := W * Object_Word(Radix);
if V = W then
Radlen := Radlen + 1;
W := V;
exit;
elsif V < W then
exit;
end if;
Radlen := Radlen + 1;
W := V;
end loop;
Push_Top (Interp, R'Unchecked_Access);
Push_Top (Interp, B'Unchecked_Access);
Push_Top (Interp, A'Unchecked_Access);
A := Copy_Upto(Interp.Self, X, X.Size);
B := Make_Bigint(Interp.Self, Size => 2);
B.Half_Word_Slot(1) := Get_Low(W);
B.Half_Word_Slot(2) := Get_High(W);
Sign := A.Sign;
A.Sign := Positive_Sign;
loop
if Is_Less_Unsigned(B, A) then
Divide_Unsigned (Interp, A, B, A, R);
A := Copy_Upto(Interp.Self, A, Count_Effective_Slots(A)); -- partial normalization
else
R := A;
end if;
if R.Size = 1 then
W := Object_Word(R.Half_Word_Slot(1));
else
W := Make_Word(R.Half_Word_Slot(1), R.Half_Word_Slot(2));
end if;
Seglen := 0;
loop
V := W rem Object_Word(Radix);
Seglen := Seglen + 1;
QL := QL + 1;
if V in 0 .. 9 then
QQQ(QL) := Object_Character'Val(Object_Character'Pos(Ch.Zero) + V);
else
QQQ(QL) := Object_Character'Val(Object_Character'Pos(Ch.UC_A) + V - 10);
end if;
W := W / Object_Word(Radix);
exit when W = 0;
end loop;
exit when R = A; -- Reached the last block
-- Fill with zeros if it's not the last block
for I in Seglen + 1 .. Radlen loop
QL := QL + 1;
QQQ(QL) := Object_Character'Val(Object_Character'Pos(Ch.Zero));
end loop;
end loop;
if Sign = Negative_Sign then
Ada.Text_IO.Put ('-');
end if;
for I in reverse 1 .. QL loop
Ada.Text_IO.Put (Standard.Character'Val(Object_Character'Pos(QQQ(I))));
end loop;
ada.text_io.new_line;
Pop_Tops (Interp, 3);
-- TODO:
--Z := Make_String_Object (...);
2014-03-04 14:32:49 +00:00
end To_String;
procedure From_String (Interp: in out Interpreter_Record;
X: in Object_Pointer;
Radix: in Object_Half_Word;
Z: out Object_Pointer) is
begin
null;
end From_String;
2014-02-20 14:51:53 +00:00
end Bigint;
2014-02-21 16:08:43 +00:00