Fast Math C is a lightweight C library for fast evaluation of mathematical functions using precomputed lookup tables and linear interpolation. It is designed to accelerate repeated function calls (such as tanh, inv, etc.) over a specified range, trading a small amount of memory for significant speed improvements.
- Precompute function values over a user-defined range and sample count.
- Fast evaluation using linear interpolation.
- Supports any single-argument float function.
- Simple API for initialization and querying.
#include "fast_math.h"
#include <math.h>
// Define your function
float inv(float x) {
if (x == 0.f) return 0.f;
return 1.f / x;
}
int main() {
// Initialize lookup for inv(x) over [-3, 3] with 2137 samples
fast_math_function_t inv_struct = fast_math_init(inv, -3.f, 3.f, 2137);
printf("1 / 0.1 = %f\n", fast_math_get(0.1f, &inv_struct));
// Initialize lookup for tanh(x) over [-4, 4] with 4096 samples
fast_math_function_t tanh_struct = fast_math_init(tanhf, -4.f, 4.f, 4096);
printf("tanh(0.1) = %f\n", fast_math_get(0.1f, &tanh_struct));
}fast_math_function_t fast_math_init(float (*target_function)(float), float start_value, float end_value, int32_t samples_n);- Initializes a lookup table for the given function.
float fast_math_get(float x, fast_math_function_t* fast_math_struct);- Retrieves the interpolated function value for
x.
- Retrieves the interpolated function value for
fast_math.c/fast_math.h: Library implementation and API.main.c: Example usage.
MIT