/* Doom-inspired Q15.16 format used for most of everything. https://hackmd.io/9uRB9YbTSBW4Qz_2b8TyDw#Examples-2-DOOM */ #ifndef COM_FIXED_H #define COM_FIXED_H #include #define COM_FIXED_FRACBITS 16 #define COM_FIXED_FRACUNIT (1 << COM_FIXED_FRACBITS) typedef int32_t com_fixed_t; static inline com_fixed_t com_fixed_add(com_fixed_t a, com_fixed_t b) { return a + b; } static inline com_fixed_t com_fixed_sub(com_fixed_t a, com_fixed_t b) { return a - b; } static inline com_fixed_t com_fixed_mul(com_fixed_t a, com_fixed_t b) { return (com_fixed_t)(((int64_t)a * (int64_t)b) >> COM_FIXED_FRACBITS); } /* Note: this does not clamp for over/underflow cases */ static inline com_fixed_t com_fixed_div(com_fixed_t a, com_fixed_t b) { return (com_fixed_t)(((int64_t)a << COM_FIXED_FRACBITS) / (int64_t)b); } /* Approximate square root using Newton-Raphson in 2 iterations over small LUT*/ com_fixed_t com_fixed_sqrt(com_fixed_t a); void com_fixed_print(com_fixed_t a); #endif