summaryrefslogtreecommitdiff
path: root/maths.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-06-30 18:24:01 +1000
committerquou <quou@disroot.org>2024-06-30 18:27:11 +1000
commit39100e7292d3ee12d387fddfa0f0d7b712e31e1c (patch)
tree28f26b19de857868aeb9ecf23a7fecfc170addf9 /maths.c
initial commit.
Diffstat (limited to 'maths.c')
-rw-r--r--maths.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/maths.c b/maths.c
new file mode 100644
index 0000000..d0bccf3
--- /dev/null
+++ b/maths.c
@@ -0,0 +1,115 @@
+#include "maths.h"
+
+#define sin_table_count 256
+
+static const int sin_table[sin_table_count] = {
+ 0, 25, 50, 75,
+ 100, 125, 150, 175,
+ 200, 224, 249, 273,
+ 297, 321, 345, 369,
+ 392, 415, 438, 460,
+ 483, 505, 526, 548,
+ 569, 590, 610, 630,
+ 650, 669, 688, 706,
+ 724, 742, 759, 775,
+ 792, 807, 822, 837,
+ 851, 865, 878, 891,
+ 903, 915, 926, 936,
+ 946, 955, 964, 972,
+ 980, 987, 993, 999,
+ 1004, 1009, 1013, 1016,
+ 1019, 1021, 1023, 1024,
+ 1024, 1024, 1023, 1021,
+ 1019, 1016, 1013, 1009,
+ 1004, 999, 993, 987,
+ 980, 972, 964, 955,
+ 946, 936, 926, 915,
+ 903, 891, 878, 865,
+ 851, 837, 822, 807,
+ 792, 775, 759, 742,
+ 724, 706, 688, 669,
+ 650, 630, 610, 590,
+ 569, 548, 526, 505,
+ 483, 460, 438, 415,
+ 392, 369, 345, 321,
+ 297, 273, 249, 224,
+ 200, 175, 150, 125,
+ 100, 75, 50, 25,
+ 0, -25, -50, -75,
+ -100, -125, -150, -175,
+ -200, -224, -249, -273,
+ -297, -321, -345, -369,
+ -392, -415, -438, -460,
+ -483, -505, -526, -548,
+ -569, -590, -610, -630,
+ -650, -669, -688, -706,
+ -724, -742, -759, -775,
+ -792, -807, -822, -837,
+ -851, -865, -878, -891,
+ -903, -915, -926, -936,
+ -946, -955, -964, -972,
+ -980, -987, -993, -999,
+ -1004, -1009, -1013, -1016,
+ -1019, -1021, -1023, -1024,
+ -1024, -1024, -1023, -1021,
+ -1019, -1016, -1013, -1009,
+ -1004, -999, -993, -987,
+ -980, -972, -964, -955,
+ -946, -936, -926, -915,
+ -903, -891, -878, -865,
+ -851, -837, -822, -807,
+ -792, -775, -759, -742,
+ -724, -706, -688, -669,
+ -650, -630, -610, -590,
+ -569, -548, -526, -505,
+ -483, -460, -438, -415,
+ -392, -369, -345, -321,
+ -297, -273, -249, -224,
+ -200, -175, -150, -125,
+ -100, -75, -50, -25
+};
+
+int fsqrt(int n) {
+ int lo, hi, mid, i, mm;
+ if (n <= 0) { return -1; }
+ lo = mini(1, n);
+ hi = maxi(1, n);
+ while ((100 << fbits * lo * lo) >> fbits * 2)
+ lo *= (10 << fbits) >> fbits;
+ while ((5 * hi * hi) >> fbits * 2 > n)
+ hi *= (1 << fbits) / 10;
+ for (i = 0; i < 100; i++) {
+ mid = ((lo + hi) * (1 << fbits)) / (2 << fbits);
+ mm = mid * mid >> fbits;
+ if (mm == n) return mid;
+ if (mm > n) hi = mid;
+ else lo = mid;
+ }
+ return mid;
+}
+
+int absolute(int x) {
+ return x >= 0 ? x : -x;
+}
+
+int fsin(int t) {
+ int index, sign;
+ sign = t >= 0 ? 1 : -1;
+ t = absolute(t);
+ index = (t * (sin_table_count) / ((1 << fbits) * 2));
+ index = index % sin_table_count;
+ return (sign * sin_table[index]) / 2;
+}
+
+int fcos(int t) {
+ return fsin((1 << fbits) / 2 - t);
+}
+
+int ftan(int t) {
+ return (fsin(t) << fbits) / fcos(t);
+}
+
+int flerp(int a, int b, int t) {
+ return a + ((t * (b - a)) >> fbits);
+}
+