half-assed 3D graphics attempt underway
[regis] / sincos.c
diff --git a/sincos.c b/sincos.c
new file mode 100644 (file)
index 0000000..6ca93ce
--- /dev/null
+++ b/sincos.c
@@ -0,0 +1,85 @@
+#include <stdio.h>
+#include <math.h>
+#include "fixed.h"
+#include "sincos.h"
+
+int16_t sinlut[SINLUT_SIZE] = {
+       0, 6, 12, 18, 25, 31, 37, 43, 50, 56, 62, 68, 75, 81, 87, 93,
+       99, 106, 112, 118, 124, 130, 136, 142, 148, 154, 160, 166, 172, 178, 184, 190,
+       195, 201, 207, 213, 218, 224, 230, 235, 241, 246, 252, 257, 263, 268, 273, 279,
+       284, 289, 294, 299, 304, 310, 314, 319, 324, 329, 334, 339, 343, 348, 353, 357,
+       362, 366, 370, 375, 379, 383, 387, 391, 395, 399, 403, 407, 411, 414, 418, 422,
+       425, 429, 432, 435, 439, 442, 445, 448, 451, 454, 457, 460, 462, 465, 468, 470,
+       473, 475, 477, 479, 482, 484, 486, 488, 489, 491, 493, 495, 496, 498, 499, 500,
+       502, 503, 504, 505, 506, 507, 508, 508, 509, 510, 510, 511, 511, 511, 511, 511,
+       512, 511, 511, 511, 511, 511, 510, 510, 509, 508, 508, 507, 506, 505, 504, 503,
+       502, 500, 499, 498, 496, 495, 493, 491, 489, 488, 486, 484, 482, 479, 477, 475,
+       473, 470, 468, 465, 462, 460, 457, 454, 451, 448, 445, 442, 439, 435, 432, 429,
+       425, 422, 418, 414, 411, 407, 403, 399, 395, 391, 387, 383, 379, 375, 370, 366,
+       362, 357, 353, 348, 343, 339, 334, 329, 324, 319, 314, 310, 304, 299, 294, 289,
+       284, 279, 273, 268, 263, 257, 252, 246, 241, 235, 230, 224, 218, 213, 207, 201,
+       195, 190, 184, 178, 172, 166, 160, 154, 148, 142, 136, 130, 124, 118, 112, 106,
+       99, 93, 87, 81, 75, 68, 62, 56, 50, 43, 37, 31, 25, 18, 12, 6,
+       0, -6, -12, -18, -25, -31, -37, -43, -50, -56, -62, -68, -75, -81, -87, -93,
+       -99, -106, -112, -118, -124, -130, -136, -142, -148, -154, -160, -166, -172, -178, -184, -190,
+       -195, -201, -207, -213, -218, -224, -230, -235, -241, -246, -252, -257, -263, -268, -273, -279,
+       -284, -289, -294, -299, -304, -310, -314, -319, -324, -329, -334, -339, -343, -348, -353, -357,
+       -362, -366, -370, -375, -379, -383, -387, -391, -395, -399, -403, -407, -411, -414, -418, -422,
+       -425, -429, -432, -435, -439, -442, -445, -448, -451, -454, -457, -460, -462, -465, -468, -470,
+       -473, -475, -477, -479, -482, -484, -486, -488, -489, -491, -493, -495, -496, -498, -499, -500,
+       -502, -503, -504, -505, -506, -507, -508, -508, -509, -510, -510, -511, -511, -511, -511, -511,
+       -512, -511, -511, -511, -511, -511, -510, -510, -509, -508, -508, -507, -506, -505, -504, -503,
+       -502, -500, -499, -498, -496, -495, -493, -491, -489, -488, -486, -484, -482, -479, -477, -475,
+       -473, -470, -468, -465, -462, -460, -457, -454, -451, -448, -445, -442, -439, -435, -432, -429,
+       -425, -422, -418, -414, -411, -407, -403, -399, -395, -391, -387, -383, -379, -375, -370, -366,
+       -362, -357, -353, -348, -343, -339, -334, -329, -324, -319, -314, -310, -304, -299, -294, -289,
+       -284, -279, -273, -268, -263, -257, -252, -246, -241, -235, -230, -224, -218, -213, -207, -201,
+       -195, -190, -184, -178, -172, -166, -160, -154, -148, -142, -136, -130, -124, -118, -112, -106,
+       -99, -93, -87, -81, -75, -68, -62, -56, -50, -43, -37, -31, -25, -18, -12, -6
+};
+
+void sincos_init(void)
+{
+       int i;
+
+       printf("int16_t sinlut[SINLUT_SIZE] = {");
+
+       for(i=0; i<SINLUT_SIZE; i++) {
+               float angle = 2.0 * M_PI * ((float)i / (float)SINLUT_SIZE);
+               float val = sin(angle);
+               sinlut[i] = (int16_t)(val * SINLUT_SCALE);
+
+               if((i & 0xf) == 0) {
+                       printf("%s\t%d", i ? ",\n" : "\n", sinlut[i]);
+               } else {
+                       printf(", %d", sinlut[i]);
+               }
+       }
+       printf("\n};");
+}
+
+int16_t sin_int(int16_t norm_angle)
+{
+       norm_angle %= SINLUT_SIZE;
+       if(norm_angle < 0) {
+               norm_angle += SINLUT_SIZE;
+       }
+       return sinlut[norm_angle];
+}
+
+int16_t cos_int(int16_t norm_angle)
+{
+       return sin_int(norm_angle + SINLUT_SIZE / 4);
+}
+
+int32_t sin_x16(int32_t radians)
+{
+       int32_t na = x16div(radians, M_PI_X16 * 2);
+       return (sin_int((na * SINLUT_SIZE) >> 16) << 16) / SINLUT_SCALE;
+}
+
+int32_t cos_x16(int32_t radians)
+{
+       int32_t na = x16div(radians, M_PI_X16 * 2);
+       return (cos_int((na * SINLUT_SIZE) >> 16) << 16) / SINLUT_SCALE;
+}