µGFX  2.9
version 2.9
gmisc_trig.c
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.io/license.html
6  */
7 
8 #include "../../gfx.h"
9 
10 #if GFX_USE_GMISC
11 
12 #if GMISC_NEED_FASTTRIG
13  const double sintabledouble[] = {
14  0.000000, 0.017452, 0.034899, 0.052336, 0.069756, 0.087156, 0.104528, 0.121869,
15  0.139173, 0.156434, 0.173648, 0.190809, 0.207912, 0.224951, 0.241922, 0.258819,
16  0.275637, 0.292372, 0.309017, 0.325568, 0.342020, 0.358368, 0.374607, 0.390731,
17  0.406737, 0.422618, 0.438371, 0.453990, 0.469472, 0.484810, 0.500000, 0.515038,
18  0.529919, 0.544639, 0.559193, 0.573576, 0.587785, 0.601815, 0.615661, 0.629320,
19  0.642788, 0.656059, 0.669131, 0.681998, 0.694658, 0.707107, 0.719340, 0.731354,
20  0.743145, 0.754710, 0.766044, 0.777146, 0.788011, 0.798636, 0.809017, 0.819152,
21  0.829038, 0.838671, 0.848048, 0.857167, 0.866025, 0.874620, 0.882948, 0.891007,
22  0.898794, 0.906308, 0.913545, 0.920505, 0.927184, 0.933580, 0.939693, 0.945519,
23  0.951057, 0.956305, 0.961262, 0.965926, 0.970296, 0.974370, 0.978148, 0.981627,
24  0.984808, 0.987688, 0.990268, 0.992546, 0.994522, 0.996195, 0.997564, 0.998630,
25  0.999391, 0.999848, 1.000000, 0.999848, 0.999391, 0.998630, 0.997564, 0.996195,
26  0.994522, 0.992546, 0.990268, 0.987688, 0.984808, 0.981627, 0.978148, 0.974370,
27  0.970296, 0.965926, 0.961262, 0.956305, 0.951057, 0.945519, 0.939693, 0.933580,
28  0.927184, 0.920505, 0.913545, 0.906308, 0.898794, 0.891007, 0.882948, 0.874620,
29  0.866025, 0.857167, 0.848048, 0.838671, 0.829038, 0.819152, 0.809017, 0.798636,
30  0.788011, 0.777146, 0.766044, 0.754710, 0.743145, 0.731354, 0.719340, 0.707107,
31  0.694658, 0.681998, 0.669131, 0.656059, 0.642788, 0.629320, 0.615661, 0.601815,
32  0.587785, 0.573576, 0.559193, 0.544639, 0.529919, 0.515038, 0.500000, 0.484810,
33  0.469472, 0.453990, 0.438371, 0.422618, 0.406737, 0.390731, 0.374607, 0.358368,
34  0.342020, 0.325568, 0.309017, 0.292372, 0.275637, 0.258819, 0.241922, 0.224951,
35  0.207912, 0.190809, 0.173648, 0.156434, 0.139173, 0.121869, 0.104528, 0.087156,
36  0.069756, 0.052336, 0.034899, 0.017452, 0.000000, -0.017452, -0.034899, -0.052336,
37  -0.069756, -0.087156, -0.104528, -0.121869, -0.139173, -0.156434, -0.173648, -0.190809,
38  -0.207912, -0.224951, -0.241922, -0.258819, -0.275637, -0.292372, -0.309017, -0.325568,
39  -0.342020, -0.358368, -0.374607, -0.390731, -0.406737, -0.422618, -0.438371, -0.453990,
40  -0.469472, -0.484810, -0.500000, -0.515038, -0.529919, -0.544639, -0.559193, -0.573576,
41  -0.587785, -0.601815, -0.615661, -0.629320, -0.642788, -0.656059, -0.669131, -0.681998,
42  -0.694658, -0.707107, -0.719340, -0.731354, -0.743145, -0.754710, -0.766044, -0.777146,
43  -0.788011, -0.798636, -0.809017, -0.819152, -0.829038, -0.838671, -0.848048, -0.857167,
44  -0.866025, -0.874620, -0.882948, -0.891007, -0.898794, -0.906308, -0.913545, -0.920505,
45  -0.927184, -0.933580, -0.939693, -0.945519, -0.951057, -0.956305, -0.961262, -0.965926,
46  -0.970296, -0.974370, -0.978148, -0.981627, -0.984808, -0.987688, -0.990268, -0.992546,
47  -0.994522, -0.996195, -0.997564, -0.998630, -0.999391, -0.999848, -1.000000, -0.999848,
48  -0.999391, -0.998630, -0.997564, -0.996195, -0.994522, -0.992546, -0.990268, -0.987688,
49  -0.984808, -0.981627, -0.978148, -0.974370, -0.970296, -0.965926, -0.961262, -0.956305,
50  -0.951057, -0.945519, -0.939693, -0.933580, -0.927184, -0.920505, -0.913545, -0.906308,
51  -0.898794, -0.891007, -0.882948, -0.874620, -0.866025, -0.857167, -0.848048, -0.838671,
52  -0.829038, -0.819152, -0.809017, -0.798636, -0.788011, -0.777146, -0.766044, -0.754710,
53  -0.743145, -0.731354, -0.719340, -0.707107, -0.694658, -0.681998, -0.669131, -0.656059,
54  -0.642788, -0.629320, -0.615661, -0.601815, -0.587785, -0.573576, -0.559193, -0.544639,
55  -0.529919, -0.515038, -0.500000, -0.484810, -0.469472, -0.453990, -0.438371, -0.422618,
56  -0.406737, -0.390731, -0.374607, -0.358368, -0.342020, -0.325568, -0.309017, -0.292372,
57  -0.275637, -0.258819, -0.241922, -0.224951, -0.207912, -0.190809, -0.173648, -0.156434,
58  -0.139173, -0.121869, -0.104528, -0.087156, -0.069756, -0.052336, -0.034899, -0.017452
59  };
60 
61  double fsin(int degrees) {
62  if (degrees < 0)
63  degrees -= (degrees/360-1)*360;
64  else if (degrees >= 360)
65  degrees %= 360;
66  return sintabledouble[degrees];
67  }
68 
69  double fcos(int degrees) {
70  return fsin(degrees+90);
71  }
72 
73 #endif
74 
75 #if GMISC_NEED_FIXEDTRIG
76  const fixed sintablefixed[] = {
77  0, 1143, 2287, 3429, 4571, 5711, 6850, 7986,
78  9120, 10252, 11380, 12504, 13625, 14742, 15854, 16961,
79  18064, 19160, 20251, 21336, 22414, 23486, 24550, 25606,
80  26655, 27696, 28729, 29752, 30767, 31772, 32767, 33753,
81  34728, 35693, 36647, 37589, 38521, 39440, 40347, 41243,
82  42125, 42995, 43852, 44695, 45525, 46340, 47142, 47929,
83  48702, 49460, 50203, 50931, 51643, 52339, 53019, 53683,
84  54331, 54963, 55577, 56175, 56755, 57319, 57864, 58393,
85  58903, 59395, 59870, 60326, 60763, 61183, 61583, 61965,
86  62328, 62672, 62997, 63302, 63589, 63856, 64103, 64331,
87  64540, 64729, 64898, 65047, 65176, 65286, 65376, 65446,
88  65496, 65526, 65536, 65526, 65496, 65446, 65376, 65286,
89  65176, 65047, 64898, 64729, 64540, 64331, 64103, 63856,
90  63589, 63302, 62997, 62672, 62328, 61965, 61583, 61183,
91  60763, 60326, 59870, 59395, 58903, 58393, 57864, 57319,
92  56755, 56175, 55577, 54963, 54331, 53683, 53019, 52339,
93  51643, 50931, 50203, 49460, 48702, 47929, 47142, 46340,
94  45525, 44695, 43852, 42995, 42125, 41243, 40347, 39440,
95  38521, 37589, 36647, 35693, 34728, 33753, 32767, 31772,
96  30767, 29752, 28729, 27696, 26655, 25606, 24550, 23486,
97  22414, 21336, 20251, 19160, 18064, 16961, 15854, 14742,
98  13625, 12504, 11380, 10252, 9120, 7986, 6850, 5711,
99  4571, 3429, 2287, 1143, 0, -1143, -2287, -3429,
100  -4571, -5711, -6850, -7986, -9120, -10252, -11380, -12504,
101  -13625, -14742, -15854, -16961, -18064, -19160, -20251, -21336,
102  -22414, -23486, -24550, -25606, -26655, -27696, -28729, -29752,
103  -30767, -31772, -32768, -33753, -34728, -35693, -36647, -37589,
104  -38521, -39440, -40347, -41243, -42125, -42995, -43852, -44695,
105  -45525, -46340, -47142, -47929, -48702, -49460, -50203, -50931,
106  -51643, -52339, -53019, -53683, -54331, -54963, -55577, -56175,
107  -56755, -57319, -57864, -58393, -58903, -59395, -59870, -60326,
108  -60763, -61183, -61583, -61965, -62328, -62672, -62997, -63302,
109  -63589, -63856, -64103, -64331, -64540, -64729, -64898, -65047,
110  -65176, -65286, -65376, -65446, -65496, -65526, -65536, -65526,
111  -65496, -65446, -65376, -65286, -65176, -65047, -64898, -64729,
112  -64540, -64331, -64103, -63856, -63589, -63302, -62997, -62672,
113  -62328, -61965, -61583, -61183, -60763, -60326, -59870, -59395,
114  -58903, -58393, -57864, -57319, -56755, -56175, -55577, -54963,
115  -54331, -53683, -53019, -52339, -51643, -50931, -50203, -49460,
116  -48702, -47929, -47142, -46340, -45525, -44695, -43852, -42995,
117  -42125, -41243, -40347, -39440, -38521, -37589, -36647, -35693,
118  -34728, -33753, -32768, -31772, -30767, -29752, -28729, -27696,
119  -26655, -25606, -24550, -23486, -22414, -21336, -20251, -19160,
120  -18064, -16961, -15854, -14742, -13625, -12504, -11380, -10252,
121  -9120, -7986, -6850, -5711, -4571, -3429, -2287, -1143
122  };
123 
124  fixed ffsin(int degrees) {
125  if (degrees < 0)
126  degrees -= (degrees/360-1)*360;
127  else if (degrees >= 360)
128  degrees %= 360;
129  return sintablefixed[degrees];
130  }
131 
132  fixed ffcos(int degrees) {
133  return ffsin(degrees+90);
134  }
135 
136 #endif
137 
138 #if GMISC_NEED_INVSQRT
139  // Algorithm based on Quake code.
140  #if GMISC_INVSQRT_REAL_SLOW
141  #include <math.h>
142  float invsqrt(float n) {
143  return 1.0/sqrt(n);
144  }
145  #else
146  float invsqrt(float n) {
147  gI32 i;
148  float x2;
149 
150  x2 = n * 0.5F;
151 
152  // Convert into an int32 (no binary format conversion)
153  #if GMISC_INVSQRT_MIXED_ENDIAN
154  ((char *)&i)[0] = ((char *)&n)[3];
155  ((char *)&i)[1] = ((char *)&n)[2];
156  ((char *)&i)[2] = ((char *)&n)[1];
157  ((char *)&i)[3] = ((char *)&n)[0];
158  #else
159  i = *(gI32 *)&n;
160  #endif
161 
162  // evil floating point bit level hacking
163  i = 0x5F375A86 - (i >> 1); // The quake code used 0x5F3759DF but this is better.
164 
165  // Convert back to a float (no binary format conversion)
166  #if GMISC_INVSQRT_MIXED_ENDIAN
167  ((char *)&n)[0] = ((char *)&i)[3];
168  ((char *)&n)[1] = ((char *)&i)[2];
169  ((char *)&n)[2] = ((char *)&i)[1];
170  ((char *)&n)[3] = ((char *)&i)[0];
171  #else
172  n = *(float *)&i;
173  #endif
174 
175  n = n * (1.5F - (x2 * n * n)); // 1st iteration
176  //n = n * (1.5F - (x2 * n * n)); // 2nd iteration for extra precision, this can be removed
177  return n;
178  }
179  #endif
180 #endif
181 
182 #endif /* GFX_USE_GMISC */
gI32 fixed
The type for a fixed point type.
Definition: gmisc.h:60
fixed ffsin(int degrees)
Fast Table Based Trig functions.
double fsin(int degrees)
Fast Table Based Trig functions.
float invsqrt(float n)
Fast inverse square root function (x^-1/2)