Implementation:Tencent Ncnn SimpleMath
| Knowledge Sources | |
|---|---|
| Domains | Mathematics, Embedded Systems |
| Last Updated | 2026-02-09 19:00 GMT |
Overview
Provides a standalone replacement for the C standard math library (math.h / cmath), implementing all mathematical functions needed by ncnn for platforms without native math support.
Description
The simplemath module consists of a header (simplemath.h, 98 lines) declaring function prototypes and an implementation (simplemath.cpp, 647 lines) providing the actual computations. The entire module is conditional on the NCNN_SIMPLEMATH compile flag.
All functions are declared with extern "C" linkage and NCNN_EXPORT visibility. They are organized into categories:
- Discontinuous/rounding functions: fabs, fabsf, fmod, fmodf, floor, floorf, round, roundf, ceil, ceilf, fmaxf, truncf, frac -- implemented using integer casting and conditional logic.
- Trigonometric functions: sinf, cosf, tanf, asinf, acosf, atanf, atan2f -- adapted from NVIDIA Cg reference implementations using polynomial approximations. Hyperbolic variants sinhf, coshf, tanhf, asinhf, acoshf, atanhf are computed via exponentials.
- Power functions: sqrtf, sqrt, powf -- sqrt uses the classic inverse-square-root bit manipulation trick (similar to the Quake fast inverse sqrt).
- Exponential and logarithm functions: expf, logf, log, log10f, frexp -- use IEEE-754 float bit manipulation with uint32_as_float for extracting mantissa/exponent components, combined with polynomial approximations.
- Probability functions: erf, erff, erfcf -- implemented using Horner's method polynomial evaluation.
- Utility functions: msb (most significant bit), fmaf (fused multiply-add), copysignf, fesetround, fegetround, nearbyintf.
Usage
This module is used automatically when building ncnn with NCNN_SIMPLEMATH=ON for bare-metal and embedded platforms that have no math library (libm). No user code changes are needed; the function names match the standard C math API.
Code Reference
Source Location
- Repository: Tencent_Ncnn
- File: src/simplemath.cpp
- File: src/simplemath.h
Signature
// Discontinuous / rounding functions
float fabs(float);
float fabsf(float);
float fmod(float, float);
float floor(float);
float floorf(float);
float round(float);
float roundf(float);
float ceil(float);
float ceilf(float);
float fmaxf(float, float);
float truncf(float);
float frac(float);
float fmodf(float, float);
// Trigonometric functions
float sinf(float);
float cosf(float);
float tanf(float);
float asinf(float);
float acosf(float);
float atanf(float);
float atan2f(float, float);
float sinhf(float);
float coshf(float);
float tanhf(float);
float asinhf(float);
float acoshf(float);
float atanhf(float);
// Power functions
float sqrtf(float);
float sqrt(float);
float powf(float, float);
// Exponential and logarithm functions
float expf(float);
float frexp(float, int*);
float logf(float);
float log(float);
float log10f(float);
// Probability functions
float erf(float);
float erff(float);
float erfcf(float);
// Utility functions
int msb(unsigned int);
float fmaf(float, float, float);
float copysignf(float, float);
void fesetround(int);
int fegetround();
float nearbyintf(float);
Import
#include "simplemath.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | float | Yes | Primary input value for math functions |
| y | float | For two-arg functions | Second input (e.g., base for powf, denominator for fmod) |
| exp | int* | For frexp | Pointer to receive the exponent component |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | float | Computed mathematical result |
| *exp (frexp) | int | Exponent component of the input float |
Usage Examples
Automatic Usage via Build Flag
// When NCNN_SIMPLEMATH is enabled, standard math calls are
// transparently redirected to ncnn's implementations.
// No code changes are needed in layer implementations:
float result = sqrtf(x * x + y * y);
float angle = atan2f(y, x);
float activated = tanhf(input);
Explicit Include for Bare-Metal Projects
#include "simplemath.h"
// Use as drop-in replacement for <cmath>
float val = expf(-0.5f * x * x);
float s = sinf(3.14159f * t);