1#ifndef __INC_LIB8TION_TRIG_H
2#define __INC_LIB8TION_TRIG_H
26#define sin16 sin16_avr
36LIB8STATIC int16_t sin16_avr(uint16_t theta) {
37 static const uint8_t data[] = {
38 0, 0, 49, 0, 6393 % 256, 6393 / 256, 48, 0,
39 12539 % 256, 12539 / 256, 44, 0, 18204 % 256, 18204 / 256, 38, 0,
40 23170 % 256, 23170 / 256, 31, 0, 27245 % 256, 27245 / 256, 23, 0,
41 30273 % 256, 30273 / 256, 14, 0, 32137 % 256, 32137 / 256, 4 };
43 uint16_t offset = (theta & 0x3FFF);
56 offset = 2047 - offset;
59 sectionX4 = offset / 256;
73 u.blo = data[sectionX4];
74 u.bhi = data[sectionX4 + 1];
75 m = data[sectionX4 + 2];
77 uint8_t secoffset8 = (uint8_t)(offset) / 2;
79 uint16_t mx = m * secoffset8;
102 static const uint16_t base[] = {0, 6393, 12539, 18204,
103 23170, 27245, 30273, 32137};
104 static const uint8_t slope[] = {49, 48, 44, 38, 31, 23, 14, 4};
106 uint16_t offset = (theta & 0x3FFF) >> 3;
108 offset = 2047 - offset;
110 uint8_t section = offset / 256;
111 uint16_t b = base[section];
112 uint8_t m = slope[section];
114 uint8_t secoffset8 = (uint8_t)(offset) / 2;
116 uint16_t mx = m * secoffset8;
135LIB8STATIC int16_t
cos16(uint16_t theta) {
return sin16(theta + 16384); }
144#if defined(__AVR__) && !defined(LIB8_ATTINY)
156LIB8STATIC uint8_t sin8_avr(uint8_t theta) {
157 uint8_t offset = theta;
159 asm volatile(
"sbrc %[theta],6 \n\t"
161 : [theta]
"+r"(theta), [offset]
"+r"(offset));
165 uint8_t secoffset = offset & 0x0F;
172 uint8_t section = offset >> 4;
173 uint8_t s2 = section * 2;
183 asm volatile(
"mul %[m16],%[secoffset] \n\t"
188 "andi %[mx],0x0F \n\t"
190 "andi %[xr1], 0xF0 \n\t"
191 "or %[mx], %[xr1] \n\t"
192 : [mx]
"=d"(mx), [xr1]
"=d"(xr1)
193 : [m16]
"d"(m16), [secoffset]
"d"(secoffset));
217LIB8STATIC uint8_t
sin8_C(uint8_t theta) {
218 uint8_t offset = theta;
220 offset = (uint8_t)255 - offset;
224 uint8_t secoffset = offset & 0x0F;
228 uint8_t section = offset >> 4;
229 uint8_t s2 = section * 2;
236 uint8_t mx = (m16 * secoffset) >> 4;
257LIB8STATIC uint8_t
cos8(uint8_t theta) {
return sin8(theta + 64); }
LIB8STATIC int16_t cos16(uint16_t theta)
Fast 16-bit approximation of cos(x).
LIB8STATIC int16_t sin16_C(uint16_t theta)
Fast 16-bit approximation of sin(x).
LIB8STATIC uint8_t cos8(uint8_t theta)
Fast 8-bit approximation of cos(x).
LIB8STATIC uint8_t sin8_C(uint8_t theta)
Fast 8-bit approximation of sin(x).
#define sin16
Platform-independent alias of the fast sin implementation.
const uint8_t b_m16_interleave[]
Pre-calculated lookup table used in sin8() and cos8() functions.
#define sin8
Platform-independent alias of the fast sin implementation.