better rad-to-theta angle conversion impl

This commit is contained in:
wgroeneveld 2020-07-10 12:06:54 +02:00
parent d74916f4c8
commit 7b1bf06223
2 changed files with 22 additions and 3 deletions

View File

@ -21,6 +21,7 @@ extern FIXED HALF;
extern FIXED ONE; extern FIXED ONE;
extern FIXED TWO; extern FIXED TWO;
#define SIN_LUT_MAX ((512 << 6) * 2) // 65536, FFFFh range
#define FIX12_SHIFT 12 #define FIX12_SHIFT 12
#define FIX12_SCALE ( 1<<FIX12_SHIFT ) #define FIX12_SCALE ( 1<<FIX12_SHIFT )
@ -53,7 +54,7 @@ INLINE float gr2rad(uint grad) {
} }
INLINE FIXED gr2lut(uint grad) { INLINE FIXED gr2lut(uint grad) {
return 65535 / (360 / (grad % 360)); return SIN_LUT_MAX / (360 / (grad % 360));
} }
INLINE FIXED rad2lut(float rad) { INLINE FIXED rad2lut(float rad) {
@ -61,8 +62,9 @@ INLINE FIXED rad2lut(float rad) {
} }
INLINE FIXED fxrad2lut(FIXED rad) { INLINE FIXED fxrad2lut(FIXED rad) {
// TODO NOT good; too much divisions, but hey, fuck it // TODO could be better without fx2float() ?
return rad2lut(fx2float(rad)); int scale = fx2float(rad) / (2*M_PI / 512);
return (scale << 6) * 2;
} }
INLINE float fx12ToFloat(FIXED fx) { INLINE float fx12ToFloat(FIXED fx) {

View File

@ -40,6 +40,23 @@ TEST_F(FpSuite, LUTValueTests) {
ASSERT_EQ(rnd(sinLutConv8), 0.995f); ASSERT_EQ(rnd(sinLutConv8), 0.995f);
} }
TEST_F(FpSuite, SinLookupTests) {
int degrees = 360;
float radians = gr2rad(degrees);
std::cout << degrees << " in rad " << radians << std::endl;
float sinhalf = sin(radians);
ASSERT_FLOAT_EQ(rnd(sinhalf), 0.0049999999);
float sinlumax = fx2float(fx12Tofx8(lu_sin(SIN_LUT_MAX)));
ASSERT_FLOAT_EQ(rnd(sinlumax), 0.0049999999);
int scale = radians / (2*M_PI / 512);
FIXED theta = (scale << 6) * 2;
float sinlu = fx2float(fx12Tofx8(lu_sin(theta)));
ASSERT_FLOAT_EQ(rnd(sinlu), 0.0049999999);
}
TEST_F(FpSuite, TanFromFixedPointRadian) { TEST_F(FpSuite, TanFromFixedPointRadian) {
// 20 degrees is 0.34906577777777775 rad // 20 degrees is 0.34906577777777775 rad
// will be stored as .8f as input val // will be stored as .8f as input val