Add initial prototype.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
with
|
||||
cached_Rotation;
|
||||
|
||||
|
||||
package body any_Math.any_fast_Rotation
|
||||
is
|
||||
|
||||
function to_Matrix_2x2 (m11, m12,
|
||||
m21, m22 : Real) return Matrix_2x2
|
||||
is
|
||||
begin
|
||||
return (1 => (m11, m12),
|
||||
2 => (m21, m22));
|
||||
end to_Matrix_2x2;
|
||||
|
||||
|
||||
package the_Cache is new cached_Rotation (Float_type => any_Math.Real,
|
||||
Matrix_2x2_type => any_Math.Matrix_2x2,
|
||||
float_elementary_Functions => any_math.Functions,
|
||||
to_Matrix_2x2 => to_Matrix_2x2,
|
||||
slot_Count => 10_000);
|
||||
|
||||
function to_Rotation (Angle : in Real) return access constant Matrix_2x2
|
||||
is
|
||||
begin
|
||||
return the_Cache.to_Rotation (Angle);
|
||||
end to_Rotation;
|
||||
|
||||
|
||||
end any_Math.any_fast_Rotation;
|
||||
@@ -0,0 +1,11 @@
|
||||
generic
|
||||
package any_Math.any_fast_Rotation
|
||||
is
|
||||
|
||||
function to_Rotation (Angle : in Real) return access constant Matrix_2x2;
|
||||
|
||||
private
|
||||
|
||||
pragma Inline_Always (to_Rotation);
|
||||
|
||||
end any_Math.any_fast_Rotation;
|
||||
@@ -0,0 +1,10 @@
|
||||
with
|
||||
cached_Trigonometry;
|
||||
|
||||
generic
|
||||
package any_math.any_fast_Trigonometry
|
||||
is
|
||||
|
||||
package Default is new cached_Trigonometry (Float_type => any_Math.Real,
|
||||
slot_Count => 10_000);
|
||||
end any_math.any_fast_Trigonometry;
|
||||
@@ -0,0 +1,43 @@
|
||||
package body cached_Rotation
|
||||
is
|
||||
use ada.Numerics,
|
||||
float_elementary_Functions;
|
||||
|
||||
|
||||
the_Cache : array (0 .. slot_Count - 1) of aliased Matrix_2x2_type;
|
||||
|
||||
Pi_x_2 : constant := Pi * 2.0;
|
||||
last_slot_Index : constant Float_type := Float_type (slot_Count - 1);
|
||||
index_Factor : constant Float_type := last_slot_Index / Pi_x_2;
|
||||
|
||||
|
||||
|
||||
function to_Rotation (Angle : in Float_type) return access constant Matrix_2x2_type
|
||||
is
|
||||
the_Index : standard.Integer := standard.Integer (Angle * index_Factor) mod slot_Count;
|
||||
begin
|
||||
if the_Index < 0
|
||||
then
|
||||
the_index := the_Index + slot_Count;
|
||||
end if;
|
||||
|
||||
return the_Cache (the_Index)'Access;
|
||||
end to_Rotation;
|
||||
|
||||
|
||||
|
||||
begin
|
||||
for Each in the_Cache'Range
|
||||
loop
|
||||
declare
|
||||
Angle : constant Float_type := ( Float_type (Each) / Float_type (slot_Count - 1)
|
||||
* Pi_x_2);
|
||||
|
||||
C : constant Float_type := Cos (Angle);
|
||||
S : constant Float_type := Sin (Angle);
|
||||
begin
|
||||
the_Cache (Each) := to_Matrix_2x2 (C, -S,
|
||||
S, C);
|
||||
end;
|
||||
end loop;
|
||||
end cached_Rotation;
|
||||
@@ -0,0 +1,30 @@
|
||||
with
|
||||
ada.Numerics.generic_elementary_Functions;
|
||||
|
||||
|
||||
generic
|
||||
type Float_type is digits <>;
|
||||
type Matrix_2x2_type is private;
|
||||
|
||||
with package float_elementary_Functions is new ada.Numerics.generic_elementary_Functions (Float_type);
|
||||
with function to_Matrix_2x2 (m11, m12,
|
||||
m21, m22 : Float_type) return Matrix_2x2_type;
|
||||
|
||||
slot_Count : Standard.Positive;
|
||||
|
||||
package cached_Rotation
|
||||
--
|
||||
-- Caches 2x2 rotation matrices of angles for speed at the cost of precision.
|
||||
--
|
||||
is
|
||||
pragma Optimize (Time);
|
||||
|
||||
function to_Rotation (Angle : in Float_type) return access constant Matrix_2x2_type;
|
||||
|
||||
|
||||
|
||||
private
|
||||
|
||||
pragma Inline_Always (to_Rotation);
|
||||
|
||||
end cached_Rotation;
|
||||
@@ -0,0 +1,75 @@
|
||||
with
|
||||
ada.Numerics.generic_elementary_Functions;
|
||||
|
||||
package body cached_Trigonometry
|
||||
is
|
||||
Sin_Cache : array (0 .. slot_Count - 1) of Float_Type;
|
||||
Cos_Cache : array (0 .. slot_Count - 1) of Float_Type;
|
||||
|
||||
Pi_x_2 : constant := ada.Numerics.Pi * 2.0;
|
||||
last_slot_Index : constant Float_Type := Float_Type (slot_Count - 1);
|
||||
index_Factor : constant Float_Type := last_slot_Index / Pi_x_2;
|
||||
|
||||
|
||||
|
||||
function Cos (Angle : in Float_Type) return Float_Type
|
||||
is
|
||||
Index : standard.Integer := standard.Integer (Angle * index_Factor) mod slot_Count;
|
||||
begin
|
||||
if Index < 0 then
|
||||
Index := Index + slot_Count;
|
||||
end if;
|
||||
|
||||
return Cos_Cache (Index);
|
||||
end Cos;
|
||||
|
||||
|
||||
|
||||
function Sin (Angle : in Float_Type) return Float_Type
|
||||
is
|
||||
Index : standard.Integer := standard.Integer (Angle * index_Factor) mod slot_Count;
|
||||
begin
|
||||
if Index < 0 then
|
||||
Index := Index + slot_Count;
|
||||
end if;
|
||||
|
||||
return Sin_Cache (Index);
|
||||
end Sin;
|
||||
|
||||
|
||||
|
||||
procedure get (Angle : in Float_Type; the_Cos : out Float_Type;
|
||||
the_Sin : out Float_Type)
|
||||
is
|
||||
Index : standard.Integer := standard.Integer (Angle * index_Factor) mod slot_Count;
|
||||
begin
|
||||
if Index < 0 then
|
||||
Index := Index + slot_Count;
|
||||
end if;
|
||||
|
||||
the_Sin := Sin_Cache (Index);
|
||||
the_Cos := Cos_Cache (Index);
|
||||
end get;
|
||||
|
||||
|
||||
|
||||
|
||||
-- TODO: Tan, arcCos, etc
|
||||
|
||||
|
||||
package Functions is new Ada.Numerics.generic_elementary_Functions (Float_Type);
|
||||
|
||||
begin
|
||||
for Each in cos_Cache'Range
|
||||
loop
|
||||
cos_Cache (Each) := Functions.cos ( Float_Type (Each) / Float_Type (slot_Count - 1)
|
||||
* Pi_x_2);
|
||||
end loop;
|
||||
|
||||
|
||||
for Each in sin_Cache'Range
|
||||
loop
|
||||
sin_Cache (Each) := Functions.sin ( Float_Type (Each) / Float_Type (slot_Count - 1)
|
||||
* Pi_x_2);
|
||||
end loop;
|
||||
end cached_Trigonometry;
|
||||
@@ -0,0 +1,29 @@
|
||||
generic
|
||||
type Float_type is digits <>;
|
||||
|
||||
slot_Count : standard.Positive;
|
||||
|
||||
package cached_Trigonometry
|
||||
--
|
||||
-- Caches trig functions for speed at the cost of precision.
|
||||
--
|
||||
is
|
||||
pragma Optimize (Time);
|
||||
|
||||
function Cos (Angle : in Float_type) return Float_type;
|
||||
function Sin (Angle : in Float_type) return Float_type;
|
||||
|
||||
|
||||
procedure get (Angle : in Float_type; the_Cos : out Float_type;
|
||||
the_Sin : out Float_type);
|
||||
|
||||
-- TODO: tan, arccos, etc
|
||||
|
||||
|
||||
private
|
||||
|
||||
pragma Inline_Always (Cos);
|
||||
pragma Inline_Always (Sin);
|
||||
pragma Inline_Always (Get);
|
||||
|
||||
end cached_Trigonometry;
|
||||
Reference in New Issue
Block a user