File syn_qmath.c¶
FileList > src > syntropic > util > syn_qmath.c
Go to the source code of this file
Q16.16 fixed-point transcendental functions and string I/O. More...
#include "../common/syn_defs.h"#include "syn_qmath.h"
Public Functions¶
| Type | Name |
|---|---|
| q16_t | q16_acos (q16_t x) Arc-cosine: π/2 − asin(x). |
| q16_t | q16_asin (q16_t x) Arc-sine (5th-order Chebyshev polynomial). |
| q16_t | q16_atan2 (q16_t y, q16_t x) Four-quadrant arctangent (minimax polynomial). |
| q16_t | q16_ceil (q16_t x) Fixed-point ceil: smallest integer >= x. |
| q16_t | q16_cos (q16_t x) Cosine approximation via sin(x + π/2). |
| q16_t | q16_cos_fast (q16_t x) Ultra-fast Parabolic/Chebyshev Cosine approximation (<20 CPU cycles). |
| q16_t | q16_exp (q16_t x) Fixed-point exponential e^x (range reduction + minimax polynomial). |
| q16_t | q16_exp_fast (q16_t x) Ultra-fast hybrid LUT exponential e^x (~15 CPU cycles). |
| q16_t | q16_floor (q16_t x) Fixed-point floor: largest integer <= x. |
| size_t | q16_from_str (const char * str, q16_t * out) Parse a decimal string (e.g. "-3.1415") into Q16. |
| q16_t | q16_hypot (q16_t x, q16_t y) Overflow-safe hypotenuse: √(x² + y²). |
| q16_t | q16_inv (q16_t x) Fast reciprocal 1/x in Q16.16 using Newton-Raphson iterations. |
| q16_t | q16_log (q16_t x) Fixed-point natural logarithm ln(x) (CLZ + minimax polynomial). |
| q16_t | q16_log_fast (q16_t x) Ultra-fast hybrid LUT natural logarithm ln(x) (~15 CPU cycles). |
| q16_t | q16_poly_eval (const q16_t * coeffs, uint8_t n, q16_t x) Evaluate polynomial P(x) = c0 + c1*x + c2*x^2 + ... + cn*x^n via Horner's method. |
| q16_t | q16_pow (q16_t base, q16_t exp) Fixed-point power: base^exp = exp(exp * log(base)). |
| q16_t | q16_round (q16_t x) Fixed-point round: nearest integer (half rounded up). |
| q16_t | q16_rsqrt (q16_t x) Fast inverse square root 1/√x in Q16.16 (Fast Quake/Newton-Raphson algorithm). |
| q16_t | q16_sin (q16_t x) Sine approximation (5th-order Taylor series). |
| q16_t | q16_sin_fast (q16_t x) Ultra-fast Parabolic/Chebyshev Sine approximation (<20 CPU cycles). |
| void | q16_sincos_fast (q16_t x, q16_t * sin_out, q16_t * cos_out) Simultaneous ultra-fast sine and cosine calculation (<30 CPU cycles). |
| q16_t | q16_sqrt (q16_t x) Fixed-point square root (binary restoring algorithm). |
| q16_t | q16_tan (q16_t x) Tangent: sin(x) / cos(x). |
| size_t | q16_to_str (q16_t val, char * buf, size_t buf_len, uint8_t decimals) Format a Q16 value as a decimal string (e.g. "-12.345"). |
| uint32_t | syn_isqrt32 (uint32_t n) Compute floor of 32-bit unsigned integer square root: ⌊√n⌋. |
| uint64_t | syn_isqrt64 (uint64_t n) Compute floor of 64-bit unsigned integer square root: ⌊√n⌋. |
Public Static Functions¶
| Type | Name |
|---|---|
| q16_t | atan_core (q16_t x) Core atan approximation for x in [0, 1] (Q16.16). |
Detailed Description¶
All functions use 64-bit intermediates for multiply chains. No floating point, no heap, no division by zero. Optimized for 32-bit ARM Cortex-M with hardware multiply; functional on 8-bit AVR.
Public Functions Documentation¶
function q16_acos¶
Arc-cosine: π/2 − asin(x).
Parameters:
xInput in Q16, must be in [−1.0, 1.0].
Returns:
Angle in Q16 radians [0, π].
function q16_asin¶
Arc-sine (5th-order Chebyshev polynomial).
Parameters:
xInput in Q16, must be in [−1.0, 1.0].
Returns:
Angle in Q16 radians [−π/2, π/2].
function q16_atan2¶
Four-quadrant arctangent (minimax polynomial).
Parameters:
yY coordinate in Q16.xX coordinate in Q16.
Returns:
Angle in Q16 radians [−π, π].
function q16_ceil¶
Fixed-point ceil: smallest integer >= x.
Parameters:
xInput in Q16.
Returns:
Ceil value in Q16.
function q16_cos¶
Cosine approximation via sin(x + π/2).
Parameters:
xAngle in Q16 radians.
Returns:
cos(x) in Q16.
function q16_cos_fast¶
Ultra-fast Parabolic/Chebyshev Cosine approximation (<20 CPU cycles).
Parameters:
xAngle in Q16 radians.
Returns:
cos(x) in Q16.
function q16_exp¶
Fixed-point exponential e^x (range reduction + minimax polynomial).
Parameters:
xExponent in Q16.
Returns:
e^x in Q16. Returns INT32_MAX on overflow.
function q16_exp_fast¶
Ultra-fast hybrid LUT exponential e^x (~15 CPU cycles).
Parameters:
xExponent in Q16.
Returns:
e^x in Q16. Returns INT32_MAX on overflow.
function q16_floor¶
Fixed-point floor: largest integer <= x.
Parameters:
xInput in Q16.
Returns:
Floor value in Q16.
function q16_from_str¶
Parse a decimal string (e.g. "-3.1415") into Q16.
Handles optional sign, integer part, optional decimal point and fractional digits. Stops at first non-numeric character.
Parameters:
strInput string (NUL-terminated).outOutput Q16 value.
Returns:
Number of characters consumed, or 0 on parse error.
function q16_hypot¶
Overflow-safe hypotenuse: √(x² + y²).
Parameters:
xFirst coordinate in Q16.ySecond coordinate in Q16.
Returns:
Magnitude in Q16.
function q16_inv¶
Fast reciprocal 1/x in Q16.16 using Newton-Raphson iterations.
Parameters:
xInput in Q16 (must be > 0).
Returns:
1/x in Q16. Returns INT32_MAX on x == 0.
function q16_log¶
Fixed-point natural logarithm ln(x) (CLZ + minimax polynomial).
Parameters:
xInput in Q16 (must be > 0).
Returns:
ln(x) in Q16.
function q16_log_fast¶
Ultra-fast hybrid LUT natural logarithm ln(x) (~15 CPU cycles).
Parameters:
xInput in Q16 (must be > 0).
Returns:
ln(x) in Q16.
function q16_poly_eval¶
Evaluate polynomial P(x) = c0 + c1*x + c2*x^2 + ... + cn*x^n via Horner's method.
Parameters:
coeffsArray of polynomial coefficients [c0, c1, ..., cn].nNumber of coefficients (order = n - 1).xEvaluation point in Q16.
Returns:
P(x) in Q16.
function q16_pow¶
Fixed-point power: base^exp = exp(exp * log(base)).
Parameters:
baseBase in Q16 (must be > 0).expExponent in Q16.
Returns:
base^exp in Q16.
function q16_round¶
Fixed-point round: nearest integer (half rounded up).
Parameters:
xInput in Q16.
Returns:
Rounded value in Q16.
function q16_rsqrt¶
Fast inverse square root 1/√x in Q16.16 (Fast Quake/Newton-Raphson algorithm).
Parameters:
xInput in Q16 (must be > 0).
Returns:
1/√x in Q16. Returns 0 if x <= 0.
function q16_sin¶
Sine approximation (5th-order Taylor series).
Parameters:
xAngle in Q16 radians.
Returns:
sin(x) in Q16.
function q16_sin_fast¶
Ultra-fast Parabolic/Chebyshev Sine approximation (<20 CPU cycles).
Parameters:
xAngle in Q16 radians.
Returns:
sin(x) in Q16.
function q16_sincos_fast¶
Simultaneous ultra-fast sine and cosine calculation (<30 CPU cycles).
Parameters:
xAngle in Q16 radians.sin_outOutput sin(x) in Q16.cos_outOutput cos(x) in Q16.
function q16_sqrt¶
Fixed-point square root (binary restoring algorithm).
Parameters:
xInput in Q16 (must be ≥ 0).
Returns:
√x in Q16.
function q16_tan¶
Tangent: sin(x) / cos(x).
Parameters:
xAngle in Q16 radians. Must not be near ±π/2.
Returns:
tan(x) in Q16.
function q16_to_str¶
Format a Q16 value as a decimal string (e.g. "-12.345").
No heap allocation. The caller provides the output buffer.
Parameters:
valValue to format.bufOutput buffer (must be large enough).buf_lenSize of output buffer in bytes.decimalsNumber of fractional digits (0–4).
Returns:
Number of characters written (excluding NUL), or 0 on error.
function syn_isqrt32¶
Compute floor of 32-bit unsigned integer square root: ⌊√n⌋.
Parameters:
nUnsigned 32-bit integer.
Returns:
⌊√n⌋.
function syn_isqrt64¶
Compute floor of 64-bit unsigned integer square root: ⌊√n⌋.
Parameters:
nUnsigned 64-bit integer.
Returns:
⌊√n⌋.
Public Static Functions Documentation¶
function atan_core¶
Core atan approximation for x in [0, 1] (Q16.16).
Uses a 7th-order minimax polynomial: atan(x) ≈ c1*x + c3*x³ + c5*x⁵ + c7*x⁷
Max error < 0.0003 rad over [0, 1]. High precision for Q16.16.
Parameters:
xInput value in Q16.16 (must be in [0, Q16_ONE]).
Returns:
Arctangent in Q16.16.
The documentation for this class was generated from the following file src/syntropic/util/syn_qmath.c