FastLED 3.9.15
Loading...
Searching...
No Matches
sin32.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stdint.h"
4
5#include "fl/int.h"
6#include "force_inline.h"
7#include "namespace.h"
8
9namespace fl {
10
11// fast and accurate sin and cos approximations
12// they differ only 0.01 % from actual sinf and cosf funtions
13// sin32 and cos32 use 24 bit unsigned integer arguments
14// 0 to 16777216 is one cycle
15// they output an integer between -2147418112 and 2147418112
16// that's 32767 * 65536
17// this is because I use i16 look up table to interpolate the results
18//
19// sin16 and cos16 are faster and more accurate funtions that take u16 as
20// arguments and return i16 as output they can replace the older
21// implementation and are 62 times more accurate and twice as fast the downside
22// with these funtions is that they use 640 bytes for the look up table thats a
23// lot for old microcontrollers but nothing for modern ones
24//
25// sin32 and cos32 take about 13 cyces to execute on an esp32
26// they're 32 times faster than sinf and cosf
27// you can use choose to use these new by writing
28// #define USE_SIN_32 before #include "FastLED.h"
29
30extern const i16 *sinArray;
31
32extern const i16 *cosArray;
33
34// 0 to 16777216 is a full circle
35// output is between -2147418112 and 2147418112
36FASTLED_FORCE_INLINE static i32 sin32(u32 angle) {
37 u8 angle256 = angle / 65536;
38 i32 subAngle = angle % 65536;
39 return sinArray[angle256] * (65536 - subAngle) +
40 sinArray[angle256 + 1] * subAngle;
41}
42
43// 0 to 16777216 is a full circle
44// output is between -2147418112 and 2147418112
45FASTLED_FORCE_INLINE static i32 cos32(u32 angle) {
46 u8 angle256 = angle / 65536;
47 i32 subAngle = angle % 65536;
48 return cosArray[angle256] * (65536 - subAngle) +
49 cosArray[angle256 + 1] * subAngle;
50}
51
52// 0 to 65536 is a full circle
53// output is between -32767 and 32767
54FASTLED_FORCE_INLINE static i16 sin16lut(u16 angle) {
55 u8 angle256 = angle / 256;
56 i32 subAngle = angle % 256;
57 return (sinArray[angle256] * (256 - subAngle) +
58 sinArray[angle256 + 1] * subAngle) /
59 256;
60}
61
62// 0 to 65536 is a full circle
63// output is between -32767 and 32767
64FASTLED_FORCE_INLINE static i16 cos16lut(u16 angle) {
65 u8 angle256 = angle / 256;
66 i32 subAngle = angle % 256;
67 return (cosArray[angle256] * (256 - subAngle) +
68 cosArray[angle256 + 1] * subAngle) /
69 256;
70}
71
72
73} // namespace fl
#define FASTLED_FORCE_INLINE
Definition force_inline.h:6
Implements the FastLED namespace macros.
unsigned char u8
Definition int.h:17
const i16 * cosArray
Definition sin32.cpp:48
const i16 * sinArray
Definition sin32.cpp:46
static FASTLED_FORCE_INLINE i32 cos32(u32 angle)
Definition sin32.h:45
static FASTLED_FORCE_INLINE i32 sin32(u32 angle)
Definition sin32.h:36
static FASTLED_FORCE_INLINE i16 cos16lut(u16 angle)
Definition sin32.h:64
static FASTLED_FORCE_INLINE i16 sin16lut(u16 angle)
Definition sin32.h:54
IMPORTANT!
Definition crgb.h:20