56 lines
1.7 KiB
C
56 lines
1.7 KiB
C
/* fixed.h - Q16.16 Fixed-Point Math Library in C89 */
|
|
|
|
#ifndef FIXED_H
|
|
#define FIXED_H
|
|
|
|
#include <stdint.h>
|
|
|
|
/* Q16.16 fixed-point type */
|
|
typedef int32_t fixed_t;
|
|
|
|
/* Constants */
|
|
#define FIXED_ONE 0x00010000L /* 1.0 in Q16.16 */
|
|
#define FIXED_ZERO 0x00000000L /* 0.0 in Q16.16 */
|
|
#define FIXED_HALF 0x00008000L /* 0.5 in Q16.16 */
|
|
#define FIXED_PI 0x0003243FL /* π ≈ 3.14159 */
|
|
#define FIXED_E 0x0002B7E1L /* e ≈ 2.71828 */
|
|
#define FIXED_MAX 0x7FFFFFFFL /* Maximum positive value */
|
|
#define FIXED_MIN 0x80000000L /* Minimum negative value */
|
|
|
|
/* Conversion functions */
|
|
fixed_t int_to_fixed(int32_t i);
|
|
int32_t fixed_to_int(fixed_t f);
|
|
fixed_t float_to_fixed(float f);
|
|
float fixed_to_float(fixed_t f);
|
|
|
|
/* Basic arithmetic operations */
|
|
fixed_t fixed_add(fixed_t a, fixed_t b);
|
|
fixed_t fixed_sub(fixed_t a, fixed_t b);
|
|
fixed_t fixed_mul(fixed_t a, fixed_t b);
|
|
fixed_t fixed_div(fixed_t a, fixed_t b);
|
|
|
|
/* Comparison functions */
|
|
int fixed_eq(fixed_t a, fixed_t b);
|
|
int fixed_ne(fixed_t a, fixed_t b);
|
|
int fixed_lt(fixed_t a, fixed_t b);
|
|
int fixed_le(fixed_t a, fixed_t b);
|
|
int fixed_gt(fixed_t a, fixed_t b);
|
|
int fixed_ge(fixed_t a, fixed_t b);
|
|
|
|
/* Unary operations */
|
|
fixed_t fixed_neg(fixed_t f);
|
|
fixed_t fixed_abs(fixed_t f);
|
|
|
|
/* Advanced math functions */
|
|
fixed_t fixed_sqrt(fixed_t f);
|
|
fixed_t fixed_sin(fixed_t f); /* f in radians */
|
|
fixed_t fixed_cos(fixed_t f); /* f in radians */
|
|
fixed_t fixed_tan(fixed_t f); /* f in radians */
|
|
|
|
/* Utility functions */
|
|
fixed_t fixed_min(fixed_t a, fixed_t b);
|
|
fixed_t fixed_max(fixed_t a, fixed_t b);
|
|
fixed_t fixed_clamp(fixed_t f, fixed_t min, fixed_t max);
|
|
|
|
#endif /* FIXED_H */
|