28#ifndef PQ_q0_32u_tRIG_H_
29#define PQ_q0_32u_tRIG_H_
32#include "pq_fixed32_math.h"
41static inline int32_t sin32(uint32_t ph) __attribute__((unused));
42static inline int32_t sin32(uint32_t ph)
44 int32_t angle, sum, p1, p2, p3, p5, p7, p9, p11;
46 if (ph >= 0xC0000000 || ph < 0x40000000) {
49 angle = (int32_t)(0x80000000u - ph);
51 p1 = multiply_32x32_rshift32_rounded(angle, 1686629713) << 2;
52 p2 = multiply_32x32_rshift32_rounded(p1, p1) << 1;
53 p3 = multiply_32x32_rshift32_rounded(p2, p1) << 2;
54 sum = multiply_subtract_32x32_rshift32_rounded(p1, p3, 1431655765);
55 p5 = multiply_32x32_rshift32_rounded(p3, p2);
56 sum = multiply_accumulate_32x32_rshift32_rounded(sum, p5, 572662306);
57 p7 = multiply_32x32_rshift32_rounded(p5, p2);
58 sum = multiply_subtract_32x32_rshift32_rounded(sum, p7, 109078534);
59 p9 = multiply_32x32_rshift32_rounded(p7, p2);
60 sum = multiply_accumulate_32x32_rshift32_rounded(sum, p9, 12119837);
61 p11 = multiply_32x32_rshift32_rounded(p9, p2);
62 sum = multiply_subtract_32x32_rshift32_rounded(sum, p11, 881443);
67static inline int32_t cos32(uint32_t ph) __attribute__((always_inline, unused));
68static inline int32_t cos32(uint32_t ph) {
69 return sin32(ph + 0x40000000);
84static inline int16_t sin16( uint16_t theta ) __attribute__ ((unused));
95static inline int16_t sin16(uint16_t theta)
97 static const uint8_t data[] =
98 { 0, 0, 49, 0, 6393%256, 6393/256, 48, 0,
99 12539%256, 12539/256, 44, 0, 18204%256, 18204/256, 38, 0,
100 23170%256, 23170/256, 31, 0, 27245%256, 27245/256, 23, 0,
101 30273%256, 30273/256, 14, 0, 32137%256, 32137/256, 4 };
103 uint16_t offset = (theta & 0x3FFF);
115 if( theta & 0x4000 ) offset = 2047 - offset;
118 sectionX4 = offset / 256;
132 u.blo = data[ sectionX4 ];
133 u.bhi = data[ sectionX4 + 1];
134 m = data[ sectionX4 + 2];
136 uint8_t secoffset8 = (uint8_t)(offset) / 2;
138 uint16_t mx = m * secoffset8;
140 int16_t y = mx + u.b;
141 if( theta & 0x8000 ) y = -y;
154static inline int16_t sin16(uint16_t theta)
156 static const uint16_t base[] =
157 { 0, 6393, 12539, 18204, 23170, 27245, 30273, 32137 };
158 static const uint8_t slope[] =
159 { 49, 48, 44, 38, 31, 23, 14, 4 };
161 uint16_t offset = (theta & 0x3FFF) >> 3;
162 if( theta & 0x4000 ) offset = 2047 - offset;
164 uint8_t section = offset / 256;
165 uint16_t b = base[section];
166 uint8_t m = slope[section];
168 uint8_t secoffset8 = (uint8_t)(offset) / 2;
170 uint16_t mx = m * secoffset8;
173 if( theta & 0x8000 ) y = -y;
187static inline int16_t cos16(uint16_t theta) __attribute__ ((unused));
188static inline int16_t cos16(uint16_t theta)
190 return sin16( theta + 16384);