lace_math_0.1.0_3ab67197/source/generic/pure/geometry/trigonometry/cached_trigonometry.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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;