Skip to content

Fixed Point Arithmetic

include/math/fixed.h View source View on GitHub
fx32_32_t fx_parse(const char *str, char **endptr)
source

strtol style

fx32_32_t fx_from_int(int64_t x)
source
int64_t fx_to_int(fx32_32_t x)
source
fx32_32_t fx_pow_i32(fx32_32_t base, int exp)
source

Use two’s complement to branchlessly floor for both signs

fx32_32_t fx_floor(fx32_32_t x)
source
fx32_32_t fx_map(
    fx32_32_t value,
    fx32_32_t from_low,
    fx32_32_t from_high,
    fx32_32_t to_low,
    fx32_32_t to_high
)
source
fx32_32_t fx_round_up(fx32_32_t x, fx32_32_t multiple)
source
fx32_32_t fx_round_down(fx32_32_t x, fx32_32_t multiple)
source
fx32_32_t fx_poly_eval(fx32_32_t x, const fx32_32_t *c, int n)
source

Extended fixed-point math functions implemented in kernel/math/fixed.c

fx32_32_t fx_sin(fx32_32_t angle)
source
fx32_32_t fx_cos(fx32_32_t angle)
source
void fx_sincos(
    fx32_32_t angle,
    fx32_32_t *sin_out,
    fx32_32_t *cos_out
)
source
fx32_32_t fx_exp_series(fx32_32_t x)
source
fx32_32_t fx_ln_series(fx32_32_t x)
source
fx32_32_t fx_sin_series(fx32_32_t angle)
source
fx32_32_t fx_cos_series(fx32_32_t angle)
source
void fx_sincos_series(
    fx32_32_t angle,
    fx32_32_t *sin_out,
    fx32_32_t *cos_out
)
source
#define FX_ZERO ((fx32_32_t) 0)
source
#define FX_ONE ((fx32_32_t) (1LL << 32))
source
#define FX_HALF ((fx32_32_t) (1LL << 31))
source
#define FP_PI 3.14159265358979323846
source
#define FX_PI FX(3.14159265358979323846)
source
#define FX_E FX(2.71828182845045235360)
source
#define FX(x)
source

Cast truncates towards zero, so we need to round

Expansion
#define FX(x) ((fx32_32_t) ((x) * 4294967296.0 + ((x) < 0 ? -0.5 : 0.5)))
#define FX_FROM_RATIO(n, d)
source
Expansion
#define FX_FROM_RATIO(n, d) ((fx32_32_t) (((__int128) (n) << 32) / (d)))