Full face stack that runs entirely in the browser. Detection, 576-point 3D mesh, recognition, anti-spoof, smile — all WebAssembly, zero server. Apache 2.0.
C avx-512avx2biometricsc99computer-visioncpu-inferencedeep-learningedge-aiembedded-aiface-embeddingface-recognitionface-verificationlow-latencyonnx-runtimeproduction-readysimdzero-dependencies
/*
* fast_exp.h — Vectorized expf() for AVX-512, accurate to float precision.
*
* Based on Cephes approach: exp(x) = 2^n * P(f) where n=round(x/ln2), f=x-n*ln2.
* P(f) is a degree-6 polynomial on [-ln2/2, ln2/2].
* Measured error on a 2^20-point grid in [-80, 80]: at most 1 ULP.
*
* ~15 SIMD instructions per call. 3-4x faster than scalar expf loop.
*/
#ifndef FAST_EXP_H
#define FAST_EXP_H
#include <immintrin.h>
#ifdef __AVX512F__
static inline __m512 _mm512_exp_fast(__m512 x)
{
/* Constants */
const __m512 log2e = _mm512_set1_ps(1.44269504088896341f); /* 1/ln(2) */
const __m512 ln2_hi = _mm512_set1_ps(0.693145751953125f);
const __m512 ln2_lo = _mm512_set1_ps(1.42860682030941723212e-6f);
const __m512 half = _mm512_set1_ps(0.5f);
/* Degree-six Chebyshev interpolant of exp on the reduced interval,
* converted to power basis and rounded to float. */
const __m512 c1 = _mm512_set1_ps(1.0f);
const __m512 c2 = _mm512_set1_ps(0.5f);
const __m512 c3 = _mm512_set1_ps(0.16666416f);
const __m512 c4 = _mm512_set1_ps(0.04166635f);
const __m512 c5 = _mm512_set1_ps(0.00837513f);
const __m512 c6 = _mm512_set1_ps(0.00139411f);
/* Clamp to prevent overflow/underflow */
x = _mm512_max_ps(x, _mm512_set1_ps(-87.3f));
x = _mm512_min_ps(x, _mm512_set1_ps(88.7f));
/* n = round(x / ln2) */
__m512 n = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), 0);
/* f = x - n * ln2 (exact via two-part subtraction) */
__m512 f = _mm512_fnmadd_ps(n, ln2_hi, x);
f = _mm512_fnmadd_ps(n, ln2_lo, f);
/* P(f) = 1 + f + f²/2 + f³/6 + ... (Horner's method) */
__m512 p = c6;
p = _mm512_fmadd_ps(p, f, c5);
p = _mm512_fmadd_ps(p, f, c4);
p = _mm512_fmadd_ps(p, f, c3);
p = _mm512_fmadd_ps(p, f, c2);
p = _mm512_fmadd_ps(p, f, c1);
p = _mm512_fmadd_ps(p, f, c1);
/* result = 2^n * P(f). Construct 2^n via integer exponent manipulation. */
__m512i ni = _mm512_cvtps_epi32(n);
ni = _mm512_add_epi32(ni, _mm512_set1_epi32(127)); /* add float bias */
ni = _mm512_slli_epi32(ni, 23); /* shift to exponent position */
__m512 pow2n = _mm512_castsi512_ps(ni);
return _mm512_mul_ps(p, pow2n);
}
/* Vectorized sigmoid: 1 / (1 + exp(-x)), using rcp14 instead of div */
static inline __m512 _mm512_sigmoid_fast(__m512 x)
{
__m512 one = _mm512_set1_ps(1.0f);
__m512 neg_x = _mm512_sub_ps(_mm512_setzero_ps(), x);
__m512 denom = _mm512_add_ps(one, _mm512_exp_fast(neg_x));
/* Use div for accuracy — sigmoid needs full precision */
return _mm512_div_ps(_mm512_set1_ps(1.0f), denom);
}
/* Vectorized SiLU: x * sigmoid(x), float-accurate */
static inline __m512 _mm512_silu_fast(__m512 x)
{
return _mm512_mul_ps(x, _mm512_sigmoid_fast(x));
}
#endif /* __AVX512F__ */
#endif /* FAST_EXP_H */