2014-02-20 14:51:53 +00:00
|
|
|
with H2.Pool;
|
|
|
|
|
|
|
|
separate (H2.Scheme)
|
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
-- The code here assumes that Half_Word_Slot'First is 1.
|
|
|
|
-- The code breaks if you change the array range to something else,
|
|
|
|
|
2014-02-20 14:51:53 +00:00
|
|
|
package body Bigint is
|
|
|
|
|
|
|
|
use type System.Bit_Order;
|
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
Big_Endian: constant := Standard.Boolean'Pos (
|
2014-02-20 14:51:53 +00:00
|
|
|
System.Default_Bit_Order = System.High_Order_First
|
|
|
|
);
|
2014-03-07 17:58:01 +00:00
|
|
|
Little_Endian: constant := Standard.Boolean'Pos (
|
2014-02-20 14:51:53 +00:00
|
|
|
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-03-07 17:58:01 +00:00
|
|
|
type Half_Word_Bit_Array is array(1 .. Half_Word_Bits) of Object_Bit;
|
|
|
|
pragma Pack (Half_Word_Bit_Array);
|
|
|
|
for Half_Word_Bit_Array'Size use Half_Word_Bits;
|
|
|
|
|
2014-03-09 18:01:38 +00:00
|
|
|
type Block_Divisor_Record is record
|
|
|
|
Low: Object_Half_Word; -- low half-word of divisor
|
|
|
|
High: Object_Half_Word; -- high half-word of divisor
|
|
|
|
Length: Object_Size; -- number of digits
|
|
|
|
end record;
|
|
|
|
Block_Divisors: array (Object_Radix) of Block_Divisor_Record;
|
|
|
|
Block_Divisors_Initialized: Standard.Boolean := Standard.False;
|
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
-------------------------------------------------------------------------
|
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;
|
2014-03-09 18:01:38 +00:00
|
|
|
for R'Address use W'Address;
|
2014-02-20 14:51:53 +00:00
|
|
|
begin
|
|
|
|
return R.Low;
|
|
|
|
end Get_Low;
|
|
|
|
|
|
|
|
function Get_High (W: in Object_Word) return Object_Half_Word is
|
|
|
|
R: Word_Record;
|
2014-03-09 18:01:38 +00:00
|
|
|
for R'Address use W'Address;
|
2014-02-20 14:51:53 +00:00
|
|
|
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;
|
2014-03-09 18:01:38 +00:00
|
|
|
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-03-07 17:58:01 +00:00
|
|
|
function Decode_To_Word (X: in Object_Pointer;
|
|
|
|
Word: access Object_Word;
|
|
|
|
Sign: access Object_Sign) return Standard.Boolean is
|
|
|
|
begin
|
|
|
|
if Is_Integer(X) then
|
|
|
|
declare
|
|
|
|
I: Object_Integer := Pointer_To_Integer(X);
|
|
|
|
begin
|
|
|
|
if I < 0 then
|
|
|
|
-- Convert the negative number to a positive word.
|
|
|
|
Word.all := Object_Word(-(I + 1)) + 1;
|
|
|
|
Sign.all := Negative_Sign;
|
|
|
|
else
|
|
|
|
Word.all := Object_Word(I);
|
|
|
|
Sign.all := Positive_Sign;
|
|
|
|
end if;
|
|
|
|
end;
|
|
|
|
else
|
|
|
|
case X.Size is
|
|
|
|
when 1 =>
|
|
|
|
Word.all := Object_Word(X.Half_Word_Slot(1));
|
|
|
|
when 2 =>
|
2014-03-25 16:43:46 +00:00
|
|
|
Word.all := Make_Word(X.Half_Word_Slot(1), X.Half_Word_Slot(2));
|
2014-03-07 17:58:01 +00:00
|
|
|
when others =>
|
|
|
|
return Standard.False;
|
|
|
|
end case;
|
|
|
|
Sign.all := X.Sign;
|
|
|
|
end if;
|
|
|
|
return Standard.True;
|
|
|
|
end Decode_To_Word;
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
procedure Convert_Word_To_Text (Word: in Object_Word;
|
|
|
|
Radix: in Object_Radix;
|
|
|
|
Buffer: in out Object_Character_Array;
|
|
|
|
Length: out Object_Size) is
|
|
|
|
V: Object_Word;
|
|
|
|
W: Object_Word := Word;
|
|
|
|
Len: Object_Size := 0;
|
|
|
|
begin
|
|
|
|
loop
|
|
|
|
V := W rem Object_Word(Radix);
|
|
|
|
|
|
|
|
if V in 0 .. 9 then
|
|
|
|
Buffer(Buffer'First + Len) := Object_Character'Val(Object_Character'Pos(Ch.Zero) + V);
|
|
|
|
else
|
|
|
|
Buffer(Buffer'First + Len) := Object_Character'Val(Object_Character'Pos(Ch.UC_A) + V - 10);
|
|
|
|
end if;
|
|
|
|
Len := Len + 1;
|
|
|
|
|
|
|
|
W := W / Object_Word(Radix);
|
|
|
|
exit when W <= 0;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
Length := Len;
|
|
|
|
end Convert_Word_To_Text;
|
2014-03-25 16:43:46 +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-07 17:58:01 +00:00
|
|
|
for I in reverse 1 .. XS loop
|
2014-03-03 15:36:03 +00:00
|
|
|
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
|
2014-03-05 18:06:54 +00:00
|
|
|
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-03-25 16:43:46 +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
|
2014-03-05 18:06:54 +00:00
|
|
|
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);
|
2014-03-05 18:06:54 +00:00
|
|
|
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-07 17:58:01 +00:00
|
|
|
function Count_Effective_Array_Slots (X: in Object_Half_Word_Array;
|
|
|
|
XS: in Half_Word_Object_Size) return Half_Word_Object_Size is
|
|
|
|
pragma Inline (Count_Effective_Array_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-03-07 17:58:01 +00:00
|
|
|
for I in reverse 1 .. XS loop
|
|
|
|
if X(I) /= 0 then
|
2014-02-24 15:17:57 +00:00
|
|
|
Last := I;
|
|
|
|
exit;
|
|
|
|
end if;
|
|
|
|
end loop;
|
2014-03-03 15:36:03 +00:00
|
|
|
return Last;
|
2014-03-07 17:58:01 +00:00
|
|
|
end Count_Effective_Array_Slots;
|
|
|
|
|
|
|
|
function Count_Effective_Slots (X: in Object_Pointer) return Half_Word_Object_Size is
|
|
|
|
pragma Inline (Count_Effective_Slots);
|
|
|
|
begin
|
2014-03-09 18:01:38 +00:00
|
|
|
return Count_Effective_Array_Slots(X.Half_Word_Slot, X.Size);
|
2014-03-03 15:36:03 +00:00
|
|
|
end Count_Effective_Slots;
|
2014-03-05 18:06:54 +00:00
|
|
|
|
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
|
2014-03-05 18:06:54 +00:00
|
|
|
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-03-25 16:43:46 +00:00
|
|
|
-------------------------------------------------------------------------
|
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
|
2014-03-25 16:43:46 +00:00
|
|
|
pragma Unsuppress (Range_Check);
|
|
|
|
pragma Unsuppress (Overflow_Check);
|
|
|
|
|
2014-02-24 15:17:57 +00:00
|
|
|
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-25 16:43:46 +00:00
|
|
|
-- TODO: don't count on Constraint_Error exception.
|
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-09 18:01:38 +00:00
|
|
|
A := Make_Bigint(Interp.Self, Value => G);
|
2014-03-04 14:32:49 +00:00
|
|
|
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-25 16:43:46 +00:00
|
|
|
-------------------------------------------------------------------------
|
2014-02-24 15:17:57 +00:00
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
function Half_Word_Bit_Position (Pos: in Standard.Positive) return Standard.Natural is
|
|
|
|
pragma Inline (Half_Word_Bit_Position);
|
|
|
|
begin
|
|
|
|
return (Pos * Little_Endian) + ((Half_Word_Bits - Pos + 1) * Big_Endian);
|
|
|
|
end Half_Word_Bit_Position;
|
|
|
|
|
|
|
|
function Get_Half_Word_Bit (X: in Object_Half_Word;
|
|
|
|
Pos: in Standard.Positive) return Object_Bit is
|
|
|
|
pragma Inline (Get_Half_Word_Bit);
|
|
|
|
BA: Half_Word_Bit_Array;
|
|
|
|
for BA'Address use X'Address;
|
|
|
|
begin
|
|
|
|
return BA(Half_Word_Bit_Position(Pos));
|
|
|
|
end Get_Half_Word_Bit;
|
|
|
|
|
|
|
|
procedure Set_Half_Word_Bit (X: in out Object_Half_Word;
|
|
|
|
Pos: in Standard.Positive;
|
|
|
|
Bit: in Object_Bit) is
|
|
|
|
pragma Inline (Set_Half_Word_Bit);
|
|
|
|
BA: Half_Word_Bit_Array;
|
|
|
|
for BA'Address use X'Address;
|
|
|
|
begin
|
|
|
|
BA(Half_Word_Bit_Position(Pos)) := Bit;
|
|
|
|
end Set_Half_Word_Bit;
|
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
-------------------------------------------------------------------------
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
function Shift_Half_Word_Left (W: in Object_Half_Word;
|
|
|
|
Bits: in Standard.Natural) return Object_Half_Word is
|
|
|
|
pragma Inline (Shift_Half_Word_Left);
|
|
|
|
begin
|
|
|
|
--if Bits >= W'Size then
|
|
|
|
-- return 0;
|
|
|
|
--end if;
|
|
|
|
return W * (2 ** Bits);
|
|
|
|
end Shift_Half_Word_Left;
|
|
|
|
|
|
|
|
function Shift_Half_Word_Right (W: in Object_Half_Word;
|
|
|
|
Bits: in Standard.Natural) return Object_Half_Word is
|
|
|
|
pragma Inline (Shift_Half_Word_Right);
|
|
|
|
begin
|
|
|
|
if Bits >= W'Size then
|
|
|
|
-- prevent divide-by-zero in case 2 ** Bits becomes 0
|
|
|
|
-- for overflow.
|
|
|
|
return 0;
|
|
|
|
end if;
|
|
|
|
return W / (2 ** Bits);
|
|
|
|
end Shift_Half_Word_Right;
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
-------------------------------------------------------------------------
|
2014-03-07 17:58:01 +00:00
|
|
|
|
|
|
|
procedure Shift_Left_Unsigned_Array (X: in out Object_Half_Word_Array;
|
|
|
|
XS: in Half_Word_Object_Size;
|
|
|
|
Bits: in Object_Size) is
|
|
|
|
Word_Shifts: Object_Size; -- half-word shift count
|
|
|
|
Bit_Shifts: Standard.Natural; -- bit shift count
|
|
|
|
Bit_Shifts_Right: Standard.Natural;
|
|
|
|
SI: Half_Word_Object_Size;
|
|
|
|
begin
|
|
|
|
-- This function doesn't grow/shrink the array. Shifting is performed
|
|
|
|
-- within the given array size only.
|
|
|
|
|
|
|
|
-- Get how many half-words to shift.
|
|
|
|
Word_Shifts := Bits / Half_Word_Bits;
|
|
|
|
if Word_Shifts >= XS then
|
|
|
|
X(1 .. XS) := (others => 0);
|
|
|
|
return;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
-- Get how many remaining bits to shift
|
|
|
|
Bit_Shifts := Standard.Natural(Bits rem Half_Word_Bits);
|
|
|
|
Bit_Shifts_Right := Half_Word_Bits - Bit_Shifts;
|
|
|
|
|
|
|
|
-- Shift words and bits
|
|
|
|
SI := XS - Word_Shifts;
|
|
|
|
X(XS) := Shift_Half_Word_Left(X(SI), Bit_Shifts);
|
|
|
|
for DI in reverse Object_Size(Word_Shifts) + 1 .. XS - 1 loop
|
|
|
|
SI := DI - Word_Shifts; -- Source Index
|
|
|
|
X(DI + 1) := X(DI + 1) or Shift_Half_Word_Right(X(SI), Bit_Shifts_Right);
|
|
|
|
X(DI) := Shift_Half_Word_Left(X(SI), Bit_Shifts);
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
-- Fill the remaining part with zeros
|
|
|
|
X(1 .. Object_Size(Word_Shifts)) := (others => 0);
|
|
|
|
end Shift_Left_Unsigned_Array;
|
|
|
|
|
|
|
|
procedure Shift_Right_Unsigned_Array (X: in out Object_Half_Word_Array;
|
|
|
|
XS: in Half_Word_Object_Size;
|
|
|
|
Bits: in Object_Size) is
|
|
|
|
|
|
|
|
Word_Shifts: Object_Size; -- half-word shift count
|
|
|
|
Bit_Shifts: Standard.Natural; -- bit shift count
|
|
|
|
Bit_Shifts_Left: Standard.Natural;
|
|
|
|
SI: Half_Word_Object_Size;
|
|
|
|
begin
|
|
|
|
-- This function doesn't grow/shrink the array. Shifting is performed
|
|
|
|
-- within the given array size only.
|
|
|
|
|
|
|
|
-- Get how many half-words to shift.
|
|
|
|
Word_Shifts := Bits / Half_Word_Bits;
|
|
|
|
if Word_Shifts >= XS then
|
|
|
|
X(1 .. XS) := (others => 0);
|
|
|
|
return;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
-- Get how many remaining bits to shift
|
|
|
|
Bit_Shifts := Standard.Natural(Bits rem Half_Word_Bits);
|
|
|
|
Bit_Shifts_Left := Half_Word_Bits - Bit_Shifts;
|
|
|
|
|
|
|
|
-- Shift words and bits
|
|
|
|
SI := 1 + Word_Shifts;
|
|
|
|
X(1) := Shift_Half_Word_Right(X(SI), Bit_Shifts);
|
|
|
|
for DI in 2 .. XS - 1 loop
|
|
|
|
SI := DI + Word_Shifts; -- Source Index
|
|
|
|
X(DI - 1) := X(DI - 1) or Shift_Half_Word_Right(X(SI), Bit_Shifts_Left);
|
|
|
|
X(DI) := Shift_Half_Word_Right(X(SI), Bit_Shifts);
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
-- Fill the remaining part with zeros
|
|
|
|
X(XS - Half_Word_Object_Size(Word_Shifts) + 1 .. XS) := (others => 0);
|
|
|
|
end Shift_Right_Unsigned_Array;
|
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
-------------------------------------------------------------------------
|
2014-03-07 17:58:01 +00:00
|
|
|
|
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
|
2014-03-05 18:06:54 +00:00
|
|
|
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
|
|
|
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-25 16:43:46 +00:00
|
|
|
|
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-07 17:58:01 +00:00
|
|
|
procedure Divide_Unsigned_Array (X: in Object_Half_Word_Array;
|
|
|
|
XS: in Half_Word_Object_Size;
|
|
|
|
Y: in out Object_Half_Word_Array;
|
|
|
|
YS: in Half_Word_Object_Size;
|
|
|
|
Q: in out Object_Half_Word_Array;
|
|
|
|
R: in out Object_Half_Word_Array) is
|
|
|
|
Bits: constant Object_Size := XS * Half_Word_Bits;
|
|
|
|
Word_Pos: Object_Size;
|
|
|
|
Bit_Pos: Standard.Positive;
|
|
|
|
RS: Half_Word_Object_Size;
|
|
|
|
begin
|
2014-03-09 18:01:38 +00:00
|
|
|
-- Perform binary long division.
|
|
|
|
-- http://en.wikipedia.org/wiki/Division_algorithm
|
|
|
|
--Q := 0 initialize quotient and remainder to zero
|
|
|
|
--R := 0
|
|
|
|
--for i = n-1...0 do where n is number of bits in N
|
|
|
|
-- R := R << 1 left-shift R by 1 bit
|
|
|
|
-- R(0) := X(i) set the least-significant bit of R equal to bit i of the numerator
|
|
|
|
-- if R >= Y then
|
|
|
|
-- R = R - Y
|
|
|
|
-- Q(i) := 1
|
|
|
|
-- end
|
|
|
|
--end
|
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
Q := (others => 0);
|
|
|
|
R := (others => 0);
|
|
|
|
|
|
|
|
for I in reverse 1 .. Bits loop
|
|
|
|
Word_Pos := (I - 1) / Half_Word_Bits + 1;
|
|
|
|
Bit_Pos := Standard.Positive((I - 1) rem Half_Word_Bits + 1);
|
|
|
|
|
|
|
|
Shift_Left_Unsigned_Array (R, XS, 1);
|
|
|
|
Set_Half_Word_Bit (R(1), 1, Get_Half_Word_Bit(X(Word_Pos), Bit_Pos));
|
|
|
|
|
|
|
|
RS := Count_Effective_Array_Slots (R, XS);
|
|
|
|
if not Is_Less_Unsigned_Array(R, RS, Y, YS) then
|
|
|
|
Subtract_Unsigned_Array (R, RS, Y, YS, R);
|
|
|
|
Set_Half_Word_Bit (Q(Word_Pos), Bit_Pos, 1);
|
|
|
|
end if;
|
|
|
|
end loop;
|
|
|
|
end Divide_Unsigned_Array;
|
|
|
|
|
|
|
|
|
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;
|
2014-03-07 17:58:01 +00:00
|
|
|
C: aliased Object_Pointer;
|
|
|
|
D: aliased Object_Pointer;
|
|
|
|
begin
|
|
|
|
pragma Assert (not Is_Less_Unsigned(A, B)); -- The caller must ensure that X >= Y
|
|
|
|
|
|
|
|
Push_Top (Interp, A'Unchecked_Access);
|
|
|
|
Push_Top (Interp, B'Unchecked_Access);
|
|
|
|
Push_Top (Interp, C'Unchecked_Access);
|
|
|
|
Push_Top (Interp, D'Unchecked_Access);
|
|
|
|
C := Make_Bigint(Interp.Self, Size => A.Size);
|
|
|
|
D := Make_Bigint(Interp.Self, Size => A.Size);
|
|
|
|
Pop_Tops (Interp, 4);
|
|
|
|
|
|
|
|
Divide_Unsigned_Array (A.Half_Word_Slot, A.Size, B.Half_Word_Slot, B.Size, C.Half_Word_Slot, D.Half_Word_Slot);
|
|
|
|
|
|
|
|
Q := C;
|
|
|
|
R := D;
|
|
|
|
end Divide_Unsigned;
|
|
|
|
|
|
|
|
procedure Divide_Unsigned_2 (Interp: in out Interpreter_Record;
|
|
|
|
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;
|
2014-03-03 15:36:03 +00:00
|
|
|
|
|
|
|
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-07 17:58:01 +00:00
|
|
|
-- TODO: Optimize the alogrighm further. the adjustment loop may take very long.
|
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);
|
2014-03-05 18:06:54 +00:00
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
-- Adjust down the guess while the dividend is less than the multiplication result.
|
|
|
|
while Is_Less_Unsigned_Array(Dend.Half_Word_Slot, Dend_Size, Tmp.Half_Word_Slot, Tmp_Size) loop
|
|
|
|
Cand(1) := Cand(1) - 1;
|
2014-03-05 18:06:54 +00:00
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
-- Tmp := Tmp - Divisor
|
2014-03-05 18:06:54 +00:00
|
|
|
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-07 17:58:01 +00:00
|
|
|
end loop;
|
|
|
|
|
|
|
|
-- Set the guess to the quotient.
|
|
|
|
Quo.Half_Word_Slot(I - B.Size + 1) := Cand(1);
|
2014-03-05 18:06:54 +00:00
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
-- Dividend := Dividend - Tmp
|
2014-03-05 18:06:54 +00:00
|
|
|
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-03-07 17:58:01 +00:00
|
|
|
end Divide_Unsigned_2;
|
2014-03-03 15:36:03 +00:00
|
|
|
|
2014-03-25 16:43:46 +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
|
2014-03-09 18:01:38 +00:00
|
|
|
Sign := Object_Sign'Val(Object_Bit(Object_Sign'Pos(A.Sign)) + 1); -- opposite A.Sign
|
2014-03-04 14:32:49 +00:00
|
|
|
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
|
2014-03-07 17:58:01 +00:00
|
|
|
-- Remainder operation must succeed if division was ok.
|
2014-03-03 15:36:03 +00:00
|
|
|
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-05 18:06:54 +00:00
|
|
|
|
2014-03-03 15:36:03 +00:00
|
|
|
Divide_Unsigned (Interp, A, B, C, D);
|
2014-03-05 18:06:54 +00:00
|
|
|
|
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-25 16:43:46 +00:00
|
|
|
-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function Compare_Bigint_And_Bigint (Interp: access Interpreter_Record;
|
|
|
|
X: in Object_Pointer;
|
|
|
|
Y: in Object_Pointer) return Standard.Integer is
|
|
|
|
begin
|
|
|
|
if Is_Equal(X, Y) then
|
|
|
|
return 0;
|
|
|
|
elsif Is_Less(X, Y) then
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
end if;
|
|
|
|
end Compare_Bigint_And_Bigint;
|
|
|
|
|
|
|
|
function Compare_Bigint_And_Integer (Interp: access Interpreter_Record;
|
|
|
|
X: in Object_Pointer;
|
|
|
|
Y: in Object_Pointer) return Standard.Integer is
|
|
|
|
YW: Object_Word := Object_Word(Pointer_To_Integer(Y));
|
|
|
|
Size: Object_Size;
|
|
|
|
begin
|
|
|
|
if YW > Object_Word(Object_Half_Word'Last) then
|
|
|
|
Size := 2;
|
|
|
|
else
|
|
|
|
Size := 1;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
declare
|
|
|
|
YY: aliased Object_Record (Kind => Half_Word_Object, Size => Size);
|
|
|
|
begin
|
|
|
|
YY.Tag := Bigint_Object;
|
|
|
|
YY.Half_Word_Slot(1) := Get_Low(YW);
|
|
|
|
if YY.Size >= 2 then
|
|
|
|
YY.Half_Word_Slot(2) := Get_High(YW);
|
|
|
|
end if;
|
|
|
|
return Compare_Bigint_And_Bigint (Interp, X, YY'Unchecked_Access);
|
|
|
|
end;
|
|
|
|
end Compare_Bigint_And_Integer;
|
|
|
|
|
|
|
|
function Compare (Interp: access Interpreter_Record;
|
|
|
|
X: in Object_Pointer;
|
|
|
|
Y: in Object_Pointer) return Standard.Integer is
|
|
|
|
begin
|
|
|
|
if Is_Bigint(X) then
|
|
|
|
if Is_Bigint(Y) then
|
|
|
|
return Compare_Bigint_And_Bigint (Interp, X, Y);
|
|
|
|
else
|
|
|
|
return Compare_Bigint_And_Integer (Interp, X, Y);
|
|
|
|
end if;
|
|
|
|
else
|
|
|
|
if Is_Bigint(Y) then
|
|
|
|
return -Compare_Bigint_And_Integer (Interp, Y, X);
|
|
|
|
else
|
|
|
|
if Pointer_To_Integer(X) = Pointer_To_Integer(Y) then
|
|
|
|
return 0;
|
|
|
|
elsif Pointer_To_Integer(X) < Pointer_To_Integer(Y) then
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
end Compare;
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function To_String (Interp: access Interpreter_Record;
|
|
|
|
X: in Object_Pointer;
|
|
|
|
Radix: in Object_Radix) return Object_Pointer is
|
2014-03-07 17:58:01 +00:00
|
|
|
W: aliased Object_Word;
|
|
|
|
Sign: aliased Object_Sign;
|
2014-03-04 14:32:49 +00:00
|
|
|
begin
|
2014-03-07 17:58:01 +00:00
|
|
|
-- Perform simple conversion if the object can be decoded
|
|
|
|
-- to a single word.
|
|
|
|
if Decode_To_Word(X, W'Access, Sign'Access) then
|
|
|
|
declare
|
|
|
|
-- Use a static buffer for simple conversion as the largest
|
|
|
|
-- size is known. The largest buffer is required for radix 2.
|
|
|
|
-- For a binary conversion(radix 2), the number of bits is
|
|
|
|
-- the maximum number of digits that can be produced. +1 is
|
|
|
|
-- needed for the sign.
|
|
|
|
Buf: Object_Character_Array (1 .. Object_Word'Size + 1);
|
|
|
|
Len: Object_Size;
|
|
|
|
begin
|
|
|
|
Convert_Word_To_Text (W, Radix, Buf, Len);
|
|
|
|
if Sign = Negative_Sign then
|
|
|
|
Len := Len + 1;
|
|
|
|
Buf(Len) := Ch.Minus_Sign;
|
|
|
|
end if;
|
2014-03-25 16:43:46 +00:00
|
|
|
return Make_String(Interp, Source => Buf(1 .. Len), Invert => Standard.True);
|
2014-03-07 17:58:01 +00:00
|
|
|
end;
|
2014-03-05 18:06:54 +00:00
|
|
|
end if;
|
|
|
|
|
2014-03-09 18:01:38 +00:00
|
|
|
-- Otherwise, do it in a hard way.
|
2014-03-07 17:58:01 +00:00
|
|
|
declare
|
2014-03-25 16:43:46 +00:00
|
|
|
B: aliased Object_Record (Kind => Half_Word_Object, Size => 2);
|
2014-03-07 17:58:01 +00:00
|
|
|
A: aliased Object_Pointer;
|
|
|
|
R: aliased Object_Pointer;
|
|
|
|
Q: aliased Object_Pointer;
|
2014-03-25 16:43:46 +00:00
|
|
|
Z: Object_Pointer;
|
2014-03-07 17:58:01 +00:00
|
|
|
|
|
|
|
-- TODO: optimize the buffer size depending on the radix value.
|
|
|
|
subtype Static_Buffer is Object_Character_Array (1 .. 16 * Half_Word_Bits + 1);
|
|
|
|
subtype Dynamic_Buffer is Object_Character_Array (1 .. X.Size * Half_Word_Bits + 1);
|
|
|
|
type Static_Buffer_Pointer is access all Static_Buffer;
|
|
|
|
type Dynamic_Buffer_Pointer is access all Dynamic_Buffer;
|
|
|
|
package Pool is new H2.Pool (Dynamic_Buffer, Dynamic_Buffer_Pointer, Interp.Storage_Pool);
|
|
|
|
Static_Buf: aliased Static_Buffer;
|
|
|
|
Dynamic_Buf: Dynamic_Buffer_Pointer;
|
|
|
|
Buf: Thin_Object_Character_Array_Pointer;
|
2014-03-09 18:01:38 +00:00
|
|
|
|
|
|
|
Totlen: Object_Size := 0; -- Length of total conversion
|
|
|
|
Seglen: Object_Size; -- Length of each word conversion
|
|
|
|
AS: Half_Word_Object_Size;
|
|
|
|
|
|
|
|
-- BD is the largest multiple of Radix that is less than or
|
|
|
|
-- equal to Object_Word'Last.
|
|
|
|
--BD: constant Block_Divisor_Record := Get_Block_Divisor(Radix);
|
|
|
|
BD: Block_Divisor_Record renames Block_Divisors(Radix);
|
2014-03-07 17:58:01 +00:00
|
|
|
begin
|
2014-03-09 18:01:38 +00:00
|
|
|
if X.Size <= 16 then
|
2014-03-07 17:58:01 +00:00
|
|
|
declare
|
|
|
|
function Conv is new Ada.Unchecked_Conversion (Static_Buffer_Pointer, Thin_Object_Character_Array_Pointer);
|
|
|
|
begin
|
|
|
|
Buf := Conv(Static_Buf'Access);
|
|
|
|
end;
|
2014-03-05 18:06:54 +00:00
|
|
|
else
|
2014-03-07 17:58:01 +00:00
|
|
|
-- TODO: move this dynamic buffer to Interpreter_Record and let it sustained during the lifetime of Interpreer
|
|
|
|
declare
|
|
|
|
function Conv is new Ada.Unchecked_Conversion (Dynamic_Buffer_Pointer, Thin_Object_Character_Array_Pointer);
|
|
|
|
begin
|
|
|
|
Dynamic_Buf := Pool.Allocate;
|
|
|
|
Buf := Conv(Dynamic_Buf);
|
|
|
|
end;
|
2014-03-05 18:06:54 +00:00
|
|
|
end if;
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
-- Create a block divisor object.
|
|
|
|
B.Tag := Bigint_Object;
|
|
|
|
B.Half_Word_Slot := (1 => BD.Low, 2 => BD.High);
|
|
|
|
|
|
|
|
Push_Top (Interp.all, Q'Unchecked_Access);
|
|
|
|
Push_Top (Interp.all, R'Unchecked_Access);
|
|
|
|
Push_Top (Interp.all, A'Unchecked_Access);
|
2014-03-07 17:58:01 +00:00
|
|
|
|
|
|
|
-- Clone the value to convert
|
2014-03-25 16:43:46 +00:00
|
|
|
A := Copy_Upto(Interp, X, X.Size);
|
2014-03-07 17:58:01 +00:00
|
|
|
|
|
|
|
-- Remember the sign to produce the sign symbol later
|
|
|
|
Sign := A.Sign;
|
|
|
|
A.Sign := Positive_Sign;
|
|
|
|
AS := A.Size;
|
2014-03-25 16:43:46 +00:00
|
|
|
|
|
|
|
Q := Make_Bigint(Interp, Size => A.Size);
|
|
|
|
R := Make_Bigint(Interp, Size => A.Size);
|
2014-03-05 18:06:54 +00:00
|
|
|
|
|
|
|
loop
|
2014-03-25 16:43:46 +00:00
|
|
|
ada.text_io.put ("A => ");
|
|
|
|
print (interp.all, A);
|
|
|
|
ada.text_io.put ("B => ");
|
|
|
|
print (interp.all, B'Unchecked_Access);
|
2014-03-07 17:58:01 +00:00
|
|
|
-- Get a word block to convert
|
|
|
|
if Is_Less_Unsigned_Array (B.Half_Word_Slot, B.Size, A.Half_Word_Slot, AS) then
|
|
|
|
Divide_Unsigned_Array (A.Half_Word_Slot, AS, B.Half_Word_Slot, B.Size, Q.Half_Word_Slot, R.Half_Word_Slot);
|
|
|
|
A.Half_Word_Slot := Q.Half_Word_Slot;
|
|
|
|
AS := Count_Effective_Slots(A);
|
2014-03-05 18:06:54 +00:00
|
|
|
else
|
2014-03-07 17:58:01 +00:00
|
|
|
R := A; -- The last block
|
2014-03-05 18:06:54 +00:00
|
|
|
end if;
|
2014-03-25 16:43:46 +00:00
|
|
|
ada.text_io.put ("R => ");
|
|
|
|
print (interp.all, R);
|
2014-03-05 18:06:54 +00:00
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
-- Translate up to 2 half-words to a full word.
|
|
|
|
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;
|
2014-03-25 16:43:46 +00:00
|
|
|
ada.text_io.put_line ("WORD => " & w'img);
|
2014-03-07 17:58:01 +00:00
|
|
|
Convert_Word_To_Text (W, Radix, Buf(Totlen + 1 .. Buf'Last), Seglen);
|
|
|
|
Totlen := Totlen + Seglen;
|
2014-03-05 18:06:54 +00:00
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
exit when R = A; -- Reached the last block
|
2014-03-05 18:06:54 +00:00
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
-- Fill unfilled leading digits with zeros if it's not the last block
|
2014-03-09 18:01:38 +00:00
|
|
|
--for I in Seglen + 1 .. Block_Divisors(Radix).Length loop
|
|
|
|
for I in Seglen + 1 .. BD.Length loop
|
2014-03-07 17:58:01 +00:00
|
|
|
Totlen := Totlen + 1;
|
|
|
|
Buf(Totlen) := Object_Character'Val(Object_Character'Pos(Ch.Zero));
|
|
|
|
end loop;
|
2014-03-05 18:06:54 +00:00
|
|
|
end loop;
|
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
Pop_Tops (Interp.all, 3);
|
2014-03-07 17:58:01 +00:00
|
|
|
|
|
|
|
if Sign = Negative_Sign then
|
|
|
|
Totlen := Totlen + 1;
|
|
|
|
Buf(Totlen) := Ch.Minus_Sign;
|
|
|
|
end if;
|
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
Z := Make_String(Interp.Self, Source => Buf(1 .. Totlen), Invert => Standard.True);
|
2014-03-05 18:06:54 +00:00
|
|
|
|
2014-03-09 18:01:38 +00:00
|
|
|
-- TODO: Move dynamic_buf to interpreter_Record.
|
|
|
|
if Dynamic_Buf /= null then
|
2014-03-07 17:58:01 +00:00
|
|
|
Pool.Deallocate (Dynamic_Buf);
|
|
|
|
end if;
|
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
return Z;
|
|
|
|
|
2014-03-07 17:58:01 +00:00
|
|
|
exception
|
|
|
|
when others =>
|
|
|
|
if Dynamic_Buf /= null then
|
|
|
|
Pool.Deallocate (Dynamic_Buf);
|
|
|
|
end if;
|
|
|
|
raise;
|
|
|
|
end;
|
2014-03-04 14:32:49 +00:00
|
|
|
end To_String;
|
2014-03-05 18:06:54 +00:00
|
|
|
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
function From_String (Interp: access Interpreter_Record;
|
|
|
|
X: in Object_Character_Array;
|
|
|
|
Radix: in Object_Radix) return Object_Pointer is
|
|
|
|
|
|
|
|
|
2014-03-09 18:01:38 +00:00
|
|
|
function Get_Digit_Value (C: in Object_Character) return Object_Integer is
|
|
|
|
Pos: Object_Integer;
|
|
|
|
begin
|
|
|
|
Pos := Object_Character'Pos(C);
|
|
|
|
case Pos is
|
|
|
|
when Ch.Pos.Zero .. Ch.Pos.Nine =>
|
2014-03-25 16:43:46 +00:00
|
|
|
Pos := Pos - Ch.Pos.Zero;
|
2014-03-09 18:01:38 +00:00
|
|
|
|
|
|
|
when Ch.Pos.LC_A .. Ch.Pos.LC_Z =>
|
2014-03-25 16:43:46 +00:00
|
|
|
Pos := Pos - Ch.Pos.LC_A + 10;
|
2014-03-09 18:01:38 +00:00
|
|
|
|
|
|
|
when Ch.Pos.UC_A .. Ch.Pos.UC_Z =>
|
2014-03-25 16:43:46 +00:00
|
|
|
Pos := Pos - Ch.Pos.UC_A + 10;
|
2014-03-09 18:01:38 +00:00
|
|
|
|
|
|
|
when others =>
|
2014-03-25 16:43:46 +00:00
|
|
|
Pos := -1;
|
2014-03-09 18:01:38 +00:00
|
|
|
end case;
|
2014-03-25 16:43:46 +00:00
|
|
|
|
|
|
|
if Pos not in 0 .. Object_Integer(Radix) - 1 then
|
|
|
|
raise Numeric_String_Error;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
return Pos;
|
2014-03-09 18:01:38 +00:00
|
|
|
end Get_Digit_Value;
|
2014-03-25 16:43:46 +00:00
|
|
|
|
2014-03-09 18:01:38 +00:00
|
|
|
Sign: Object_Sign;
|
|
|
|
Idx: Object_Size;
|
|
|
|
W: Object_Word;
|
|
|
|
BDLen: Object_Size renames Block_Divisors(Radix).Length;
|
2014-03-25 16:43:46 +00:00
|
|
|
NDigits: Object_Size;
|
2014-03-09 18:01:38 +00:00
|
|
|
B: Object_Pointer;
|
2014-03-05 18:06:54 +00:00
|
|
|
begin
|
2014-03-09 18:01:38 +00:00
|
|
|
-- Find the first digit while remembering the sign
|
|
|
|
Sign := Positive_Sign;
|
|
|
|
Idx := X'First;
|
|
|
|
if Idx <= X'Last then
|
|
|
|
if X(Idx) = Ch.Plus_Sign then
|
|
|
|
Idx := Idx + 1;
|
|
|
|
elsif X(Idx) = Ch.Minus_Sign then
|
|
|
|
Idx := Idx + 1;
|
|
|
|
Sign := Negative_Sign;
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
pragma Assert (Idx <= X'Last); -- the caller ensure at least 1 digit
|
|
|
|
if Idx > X'Last then
|
2014-03-09 18:01:38 +00:00
|
|
|
-- No digits in the string.
|
2014-03-25 16:43:46 +00:00
|
|
|
--return Integer_To_Pointer(0);
|
|
|
|
raise Numeric_String_Error;
|
2014-03-09 18:01:38 +00:00
|
|
|
end if;
|
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
-- Find the first non-zero digit
|
2014-03-09 18:01:38 +00:00
|
|
|
while Idx <= X'Last loop
|
|
|
|
exit when X(Idx) /= Ch.Zero;
|
|
|
|
Idx := Idx + 1;
|
|
|
|
end loop;
|
|
|
|
if Idx > X'Last then
|
2014-03-25 16:43:46 +00:00
|
|
|
-- All digits are zeros.
|
|
|
|
return Integer_To_Pointer(0);
|
2014-03-09 18:01:38 +00:00
|
|
|
end if;
|
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
NDigits := X'Last - Idx + 1; -- number of effective digits
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
-- Attemp to perform conversion within the range of Object_Integer.
|
|
|
|
declare
|
|
|
|
OW: Object_Word;
|
|
|
|
I: Object_Integer;
|
|
|
|
begin
|
|
|
|
W := 0;
|
|
|
|
while Idx <= X'Last loop
|
|
|
|
OW := W;
|
|
|
|
W := W * Radix + Object_Word(Get_Digit_Value(X(Idx)));
|
|
|
|
|
|
|
|
-- Exit if the accumulated value can't be represented
|
|
|
|
-- in an Object_Integer.
|
|
|
|
if W > Object_Word(Object_Integer'Last) or else W <= OW then
|
|
|
|
W := OW;
|
|
|
|
goto Huge;
|
2014-03-09 18:01:38 +00:00
|
|
|
end if;
|
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
Idx := Idx + 1;
|
|
|
|
end loop;
|
|
|
|
-- Processed all digits. The value can fit
|
|
|
|
-- into an Object_Integer.
|
|
|
|
I := Object_Integer(W);
|
|
|
|
|
|
|
|
--I := 0;
|
|
|
|
--while Idx <= X'Last loop
|
|
|
|
-- begin
|
|
|
|
-- I := I * Object_Integer(Radix) + Get_Digit_Value(X(Idx));
|
|
|
|
-- exception
|
|
|
|
-- when Constraint_Error =>
|
|
|
|
-- W := Object_Word(I);
|
|
|
|
-- goto Huge;
|
|
|
|
-- end;
|
|
|
|
-- Idx := Idx + 1;
|
|
|
|
--end loop;
|
|
|
|
|
|
|
|
if Sign = Negative_Sign then
|
|
|
|
I := -I;
|
|
|
|
end if;
|
|
|
|
return Integer_To_Pointer(I);
|
|
|
|
end;
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
<<Huge>>
|
|
|
|
-- TODO: Optimizations if Radix 2, 4, 16. For there radix, conversion can be done in chunk.
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
-- The input string is too large to be converted to an Object_Integer.
|
|
|
|
B := Make_Bigint(Interp, Size => ((NDigits + BDLen - 1) / BDLen) * 2 + 1); -- TODO: is it the right size?
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
declare
|
|
|
|
C: Object_Pointer;
|
|
|
|
RB: aliased Object_Record (Kind => Half_Word_Object, Size => 1);
|
|
|
|
begin
|
|
|
|
RB.Tag := Bigint_Object;
|
|
|
|
RB.Half_Word_Slot(1) := Object_Half_Word(Radix);
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
C := Make_Bigint(Interp, Size => B.Size);
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
B.Half_Word_Slot(1) := Get_Low(W);
|
|
|
|
B.Half_Word_Slot(2) := Get_High(W);
|
2014-03-09 18:01:38 +00:00
|
|
|
|
2014-03-25 16:43:46 +00:00
|
|
|
while Idx <= X'Last loop
|
|
|
|
declare
|
|
|
|
DVB: aliased Object_Record (Kind => Half_Word_Object, Size => 1);
|
|
|
|
begin
|
|
|
|
DVB.Tag := Bigint_Object;
|
|
|
|
DVB.Half_Word_Slot(1) := Object_Half_Word(Get_Digit_Value(X(Idx)));
|
|
|
|
|
|
|
|
ada.text_io.put ("B =>");
|
|
|
|
print (interp.all, B);
|
|
|
|
ada.text_io.put ("RB =>");
|
|
|
|
print (interp.all, RB'Unchecked_Access);
|
|
|
|
Multiply_Unsigned_Array (B.Half_Word_Slot, Count_Effective_Array_Slots(B.Half_Word_Slot, B.Size), RB.Half_Word_Slot, RB.Size, C.Half_Word_Slot);
|
|
|
|
B.Half_Word_Slot := (others => 0);
|
|
|
|
Add_Unsigned_Array (C.Half_Word_Slot, Count_Effective_Array_Slots(C.Half_Word_Slot, B.Size), DVB.Half_Word_Slot, DVB.Size, B.Half_Word_Slot);
|
|
|
|
C.Half_Word_Slot := (others => 0);
|
|
|
|
end;
|
|
|
|
print (interp.all, B);
|
|
|
|
Idx := Idx + 1;
|
|
|
|
end loop;
|
|
|
|
end;
|
2014-03-09 18:01:38 +00:00
|
|
|
|
|
|
|
B.Sign := Sign;
|
2014-03-25 16:43:46 +00:00
|
|
|
return Normalize(Interp.Self, B);
|
2014-03-05 18:06:54 +00:00
|
|
|
end From_String;
|
2014-03-25 16:43:46 +00:00
|
|
|
|
|
|
|
-------------------------------------------------------------------------
|
|
|
|
|
2014-03-09 18:01:38 +00:00
|
|
|
function Get_Block_Divisor (Radix: in Object_Radix) return Block_Divisor_Record is
|
|
|
|
V, W: Object_Word;
|
|
|
|
Len: Object_Size;
|
|
|
|
begin
|
|
|
|
Len := 1;
|
|
|
|
W := Object_Word(Radix);
|
|
|
|
|
|
|
|
loop
|
|
|
|
V := W * Object_Word(Radix);
|
2014-03-25 16:43:46 +00:00
|
|
|
--if V = W then
|
|
|
|
-- Len := Len + 1;
|
|
|
|
-- W := V;
|
|
|
|
-- exit;
|
|
|
|
--elsif V < W then
|
|
|
|
-- -- Overflow
|
|
|
|
-- exit;
|
|
|
|
--end if;
|
|
|
|
exit when V <= W;
|
2014-03-09 18:01:38 +00:00
|
|
|
|
|
|
|
Len := Len + 1;
|
|
|
|
W := V;
|
2014-03-25 16:43:46 +00:00
|
|
|
if Radix = 10 then
|
|
|
|
ada.text_io.put_line ("BLOCK_DIVISOR XX=> " & w'img);
|
|
|
|
end if;
|
|
|
|
|
2014-03-09 18:01:38 +00:00
|
|
|
end loop;
|
2014-03-25 16:43:46 +00:00
|
|
|
|
|
|
|
if Radix = 10 then
|
|
|
|
ada.text_io.put_line ("BLOCK_DIVISOR => " & w'img);
|
|
|
|
end if;
|
2014-03-09 18:01:38 +00:00
|
|
|
return (Low => Get_Low(W), High => Get_High(W), Length => Len);
|
|
|
|
end Get_Block_Divisor;
|
|
|
|
|
|
|
|
procedure Initialize is
|
|
|
|
begin
|
|
|
|
-- Initialize block divisors table
|
|
|
|
if not Block_Divisors_Initialized then
|
|
|
|
for Radix in Object_Radix'Range loop
|
|
|
|
Block_Divisors(Radix) := Get_Block_Divisor(Radix);
|
|
|
|
end loop;
|
|
|
|
Block_Divisors_Initialized := Standard.True;
|
|
|
|
end if;
|
|
|
|
end Initialize;
|
2014-02-21 16:08:43 +00:00
|
|
|
|
2014-03-09 18:01:38 +00:00
|
|
|
begin
|
|
|
|
Initialize;
|
|
|
|
end Bigint;
|