diff options
Diffstat (limited to 'standard.c')
-rw-r--r-- | standard.c | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/standard.c b/standard.c new file mode 100644 index 0000000..801777c --- /dev/null +++ b/standard.c @@ -0,0 +1,192 @@ +#include <stdint.h> + +#include "standard.h" + +int string_equal(const char* a, const char* b) { + while (*a && *b) { + if (*a != *b) { return 0; } + a++; b++; + } + return 1; +} + +#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 INT32_MAX; } + + 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; + int 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]; +} + +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); +} + +int int_to_buf(int n, char* buf) { + int i, sign, t; + unsigned n1; + + if(n == 0) { + buf[0] = '0'; + buf[1] = '\0'; + return 1; + } + + i = 0; + sign = n < 0; + + n1 = sign ? -n : n; + + while (n1 != 0) { + buf[i++] = n1 % 10 + '0'; + n1 =n1 / 10; + } + + if(sign) { + buf[i++] = '-'; + } + + buf[i] = '\0'; + + for (t = 0; t < i / 2; t++) { + buf[t] ^= buf[i - t - 1]; + buf[i - t - 1] ^= buf[t]; + buf[t] ^= buf[i - t - 1]; + } + + return i; +} + +int f_to_buf(int f, char* buf) { + int frac, w, i; + + frac = (((f) % (1 << fbits)) * 100) >> fbits; + w = f >> fbits; + + i = 0; + + if (f < 0) { + buf[i] = '-'; + i++; + } + + i += int_to_buf(absolute(w), buf + i); + buf[i] = '.'; + i++; + i += int_to_buf(absolute(frac), buf + i); + + return i; +} |