rendering.c: fast cos from sin calculation, with lossy fast_sqrt()

This commit is contained in:
2024-07-28 14:39:23 +03:00
parent c59708d619
commit ea4d12212c
3 changed files with 154 additions and 129 deletions

View File

@@ -154,5 +154,28 @@ void tick_ftimer(float *value);
/* returns true if value was cycled */
bool repeat_ftimer(float *value, float at);
/* http://www.azillionmonkeys.com/qed/sqroot.html */
static inline float fast_sqrt(float x)
{
union {
float f;
uint32_t u;
} pun = {.f = x};
pun.u += 127 << 23;
pun.u >>= 1;
return pun.f;
}
static inline t_fvec2 fast_cossine(float a) {
const float s = sinf(a);
return (t_fvec2){
.x = fast_sqrt(1.0f - s * s) * (a >= (float)M_PI_2 && a < (float)(M_PI + M_PI_2) ? -1 : 1),
.y = s
};
}
#endif