Nu:Tekt NTS-1 digital SDK  v1.1-0
Classes
Floating-Point Math

Classes

union  f32_t
 
struct  f32pair_t
 

Constants

#define M_E   2.718281828459045f
 
#define M_LOG2E   1.44269504088896f
 
#define M_LOG10E   0.4342944819032518f
 
#define M_LN2   0.6931471805599453094f
 
#define M_LN10   2.30258509299404568402f
 
#define M_PI   3.141592653589793f
 
#define M_TWOPI   6.283185307179586f
 
#define M_PI_2   1.5707963267948966f
 
#define M_PI_4   0.7853981633974483f
 
#define M_1_PI   0.3183098861837907f
 
#define M_2_PI   0.6366197723675814f
 
#define M_4_PI   1.2732395447351627f
 
#define M_1_TWOPI   0.15915494309189534f
 
#define M_2_SQRTPI   1.1283791670955126f
 
#define M_4_PI2   0.40528473456935109f
 
#define M_SQRT2   1.41421356237309504880f
 
#define M_1_SQRT2   0.7071067811865475f
 

Macros

#define F32_FRAC_MASK   ((1L<<23)-1)
 
#define F32_EXP_MASK   (((1L<<9)-1)<<23)
 
#define F32_SIGN_MASK   (0x80000000)
 
#define f32_frac_bits(f)   ((f) & F32_FRAC_MASK)
 
#define f32_exp_bits(f)   ((f) & F32_EXP_MASK)
 
#define f32_sign_bit(f)   ((f) & F32_SIGN_MASK)
 

Types

static f32pair_t f32pair (const float a, const float b)
 Make a float pair.
 

Operations

static float fsel (const float a, const float b, const float c)
 FSEL construct.
 
static uint8_t fselb (const float a)
 FSEL boolean construct.
 
static uint8_t float_is_neg (const f32_t f)
 Sign bit check.
 
static int32_t float_mantissa (f32_t f)
 Obtain mantissa.
 
static int32_t float_exponent (f32_t f)
 Obtain exponent.
 
static f32pair_t f32pair_add (const f32pair_t p0, const f32pair_t p1)
 Pair-wise addition.
 
static f32pair_t f32pair_sub (const f32pair_t p0, const f32pair_t p1)
 Pair-wise subtraction.
 
static f32pair_t f32pair_addscal (const f32pair_t p, const float scl)
 Pair-wise scalar addition.
 
static f32pair_t f32pair_mul (const f32pair_t p0, const f32pair_t p1)
 Pair-wise product.
 
static f32pair_t f32pair_mulscal (const f32pair_t p, const float scl)
 Pair-wise scalar product.
 
static f32pair_t f32pair_linint (const float fr, const f32pair_t p0, const f32pair_t p1)
 Pair-wise linear interpolation.
 
static float si_copysignf (const float x, const float y)
 Return x with sign of y applied.
 
static float si_fabsf (float x)
 Absolute value.
 
static float si_floorf (float x)
 Floor function.
 
static float si_ceilf (float x)
 Ceiling function.
 
static float si_roundf (float x)
 Round to nearest integer.
 
static float clampfsel (const float min, float x, const float max)
 
static float clampminfsel (const float min, const float x)
 
static float clampmaxfsel (const float x, const float max)
 
static float clipmaxf (const float x, const float m)
 Clip upper bound of x to m (inclusive)
 
static float clipminf (const float m, const float x)
 Clip lower bound of x to m (inclusive)
 
static float clipminmaxf (const float min, const float x, const float max)
 Clip x to min and max (inclusive)
 
static float clip0f (const float x)
 Clip lower bound of x to 0.f (inclusive)
 
static float clip1f (const float x)
 Clip upper bound of x to 1.f (inclusive)
 
static float clip01f (const float x)
 Clip x to [0.f, 1.f] (inclusive)
 
static float clipm1f (const float x)
 Clip lower bound of x to -1.f (inclusive)
 
static float clip1m1f (const float x)
 Clip x to [-1.f, 1.f] (inclusive)
 

Faster direct approximations of common trigonometric functions

Note
Use with care. Depending on optimizations and targets these can provide little benefit over libc versions.
static float fastsinf (float x)
 "Fast" sine approximation, valid for x in [-M_PI, M_PI] More...
 
static float fastersinf (float x)
 "Faster" sine approximation, valid for x in [-M_PI, M_PI] More...
 
static float fastsinfullf (float x)
 "Fast" sine approximation, valid on full x domain More...
 
static float fastersinfullf (float x)
 "Faster" sine approximation, valid on full x domain More...
 
static float fastcosf (float x)
 "Fast" cosine approximation, valid for x in [-M_PI, M_PI] More...
 
static float fastercosf (float x)
 "Faster" cosine approximation, valid for x in [-M_PI, M_PI] More...
 
static float fastcosfullf (float x)
 "Fast" cosine approximation, valid on full x domain More...
 
static float fastercosfullf (float x)
 "Faster" cosine approximation, valid on full x domain More...
 
static float fasttanf (float x)
 "Fast" tangent approximation, valid for x in [-M_PI_2, M_PI_2] More...
 
static float fastertanf (float x)
 "Faster" tangent approximation, valid for x in [-M_PI_2, M_PI_2] More...
 
static float fasttanfullf (float x)
 "Fast" tangent approximation, valid on full x domain, except where tangent diverges. More...
 
static float fastertanfullf (float x)
 "Faster" tangent approximation, valid on full x domain, except where tangent diverges. More...
 
static float fastlog2f (float x)
 "Fast" log base 2 approximation, valid for positive x as precision allows. More...
 
static float fasterlog2f (float x)
 "Faster" log base 2 approximation, valid for positive x as precision allows. More...
 
static float fastlogf (float x)
 "Fast" natural logarithm approximation, valid for positive x as precision allows. More...
 
static float fasterlogf (float x)
 "Fast" natural logarithm approximation, valid for positive x as precision allows. More...
 
static float fastpow2f (float p)
 "Fast" power of 2 approximation, valid for x in [ -126, ... More...
 
static float fasterpow2f (float p)
 "Faster" power of 2 approximation, valid for x in [ -126, ... More...
 
static float fastpowf (float x, float p)
 "Fast" x to the power of p approximation More...
 
static float fasterpowf (float x, float p)
 "Faster" x to the power of p approximation More...
 
static float fastexpf (float p)
 "Fast" exponential approximation, valid for x in [ ~ -87, ... More...
 
static float fasterexpf (float p)
 "Faster" exponential approximation, valid for x in [ ~ -87, ... More...
 
static float fasteratan2f (float y, float x)
 atan2 approximation More...
 
static float fastertanhf (float x)
 Hyperbolic tangent approximation. More...
 

Useful Conversions

Note
These can be very slow, use with caution. Should use table lookups in performance critical sections.
static float ampdbf (const float amp)
 Amplitude to dB. More...
 
static float fasterampdbf (const float amp)
 "Faster" Amplitude to dB
 
static float dbampf (const float db)
 dB to ampltitude
 
static float fasterdbampf (const float db)
 "Faster" dB to ampltitude
 

Interpolations

Todo:
Add cubic/spline interpolations
static float linintf (const float fr, const float x0, const float x1)
 Linear interpolation.
 
static float cosintf (const float fr, const float x0, const float x1)
 Cosine interpolation.
 

Detailed Description

Function Documentation

◆ ampdbf()

static float ampdbf ( const float  amp)
inlinestatic

Amplitude to dB.

Note
Will remove low boundary check in future version

Definition at line 784 of file float_math.h.

784  {
785  return (amp < 0.f) ? -999.f : 20.f*log10f(amp);
786 }

◆ fastcosf()

static float fastcosf ( float  x)
inlinestatic

"Fast" cosine approximation, valid for x in [-M_PI, M_PI]

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 559 of file float_math.h.

559  {
560  const float halfpiminustwopi = -4.7123889803846899f;
561  float offset = (x > M_PI_2) ? halfpiminustwopi : M_PI_2;
562  return fastsinf(x + offset);
563 }

◆ fastcosfullf()

static float fastcosfullf ( float  x)
inlinestatic

"Fast" cosine approximation, valid on full x domain

Note
Adapted from Paul Mineiro's FastFloat
Warning: can be slower than libc version!

Definition at line 582 of file float_math.h.

582  {
583  return fastersinfullf(x + M_PI_2);
584 }

◆ fasteratan2f()

static float fasteratan2f ( float  y,
float  x 
)
inlinestatic

atan2 approximation

Note
Adapted from http://dspguru.com/dsp/tricks/fixed-point-atan2-with-self-normalization

Definition at line 739 of file float_math.h.

739  {
740  const float coeff_1 = M_PI_4;
741  const float coeff_2 = 3 * coeff_1;
742  float abs_y = si_fabsf(y) + 1e-10f; // kludge to prevent 0/0 condition
743  float r, angle;
744  if (x >= 0) {
745  r = (x - abs_y) / (x + abs_y);
746  angle = coeff_1 - coeff_1 * r;
747  }
748  else {
749  r = (x + abs_y) / (abs_y - x);
750  angle = coeff_2 - coeff_1 * r;
751  }
752  return (y < 0) ? -angle : angle; // negate if in quad III or IV
753 }

◆ fastercosf()

static float fastercosf ( float  x)
inlinestatic

"Faster" cosine approximation, valid for x in [-M_PI, M_PI]

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 569 of file float_math.h.

569  {
570  static const float p = 0.54641335845679634f;
571  union { float f; uint32_t i; } vx = { x };
572  vx.i &= 0x7FFFFFFF;
573  const float qpprox = 1.0f - M_2_PI * vx.f;
574  return qpprox + p * qpprox * (1.0f - qpprox * qpprox);
575 }

◆ fastercosfullf()

static float fastercosfullf ( float  x)
inlinestatic

"Faster" cosine approximation, valid on full x domain

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 590 of file float_math.h.

590  {
591  return fastersinfullf(x + M_PI_2);
592 }

◆ fasterexpf()

static float fasterexpf ( float  p)
inlinestatic

"Faster" exponential approximation, valid for x in [ ~ -87, ...

as precision allows.

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 729 of file float_math.h.

729  {
730  return fasterpow2f(1.442695040f * p);
731 }

◆ fasterlog2f()

static float fasterlog2f ( float  x)
inlinestatic

"Faster" log base 2 approximation, valid for positive x as precision allows.

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 652 of file float_math.h.

652  {
653  union { float f; uint32_t i; } vx = { x };
654  float y = (float)(vx.i);
655  y *= 1.1920928955078125e-7f;
656  return y - 126.94269504f;
657 }

◆ fasterlogf()

static float fasterlogf ( float  x)
inlinestatic

"Fast" natural logarithm approximation, valid for positive x as precision allows.

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 671 of file float_math.h.

671  {
672  return 0.69314718f * fasterlog2f(x);
673 }

◆ fasterpow2f()

static float fasterpow2f ( float  p)
inlinestatic

"Faster" power of 2 approximation, valid for x in [ -126, ...

as precision allows.

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 694 of file float_math.h.

694  {
695  float clipp = (p < -126) ? -126.0f : p;
696  union { uint32_t i; float f; } v = { (uint32_t)( (1 << 23) * (clipp + 126.94269504f) ) };
697  return v.f;
698 }

◆ fasterpowf()

static float fasterpowf ( float  x,
float  p 
)
inlinestatic

"Faster" x to the power of p approximation

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 713 of file float_math.h.

713  {
714  return fasterpow2f(p * fasterlog2f(x));
715 }

◆ fastersinf()

static float fastersinf ( float  x)
inlinestatic

"Faster" sine approximation, valid for x in [-M_PI, M_PI]

Note
Adapted from Paul Mineiro's FastFloat
Warning: can be slower than libc version!

Definition at line 524 of file float_math.h.

524  {
525  static const float q = 0.77633023248007499f;
526  union { float f; uint32_t i; } p = { 0.22308510060189463f };
527  union { float f; uint32_t i; } vx = { x };
528  const uint32_t sign = vx.i & 0x80000000;
529  vx.i &= 0x7FFFFFFF;
530  const float qpprox = M_4_PI * x - M_4_PI2 * x * vx.f;
531  p.i |= sign;
532  return qpprox * (q + p.f * qpprox);
533 }

◆ fastersinfullf()

static float fastersinfullf ( float  x)
inlinestatic

"Faster" sine approximation, valid on full x domain

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 549 of file float_math.h.

549  {
550  const int32_t k = (int32_t)(x * M_1_TWOPI);
551  const float half = (x < 0) ? -0.5f : 0.5f;
552  return fastersinf((half + k) * M_TWOPI - x);
553 }

◆ fastertanf()

static float fastertanf ( float  x)
inlinestatic

"Faster" tangent approximation, valid for x in [-M_PI_2, M_PI_2]

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 606 of file float_math.h.

606  {
607  return fastcosf(x) / fastercosf(x);
608 }

◆ fastertanfullf()

static float fastertanfullf ( float  x)
inlinestatic

"Faster" tangent approximation, valid on full x domain, except where tangent diverges.

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 626 of file float_math.h.

626  {
627  const int32_t k = (int32_t)(x * M_1_TWOPI);
628  const float half = (x < 0) ? -0.5f : 0.5f;
629  const float xnew = x - (half + k) * M_TWOPI;
630  return fastersinf(xnew)/fastercosf(xnew);
631 }

◆ fastertanhf()

static float fastertanhf ( float  x)
inlinestatic

Hyperbolic tangent approximation.

Note
Adapted from http://math.stackexchange.com/questions/107292/rapid-approximation-of-tanhx

Definition at line 759 of file float_math.h.

759  {
760  return (-0.67436811832e-5f +
761  (0.2468149110712040f +
762  (0.583691066395175e-1f + 0.3357335044280075e-1f * x) * x) * x) /
763  (0.2464845986383725f +
764  (0.609347197060491e-1f +
765  (0.1086202599228572f + 0.2874707922475963e-1f * x) * x) * x);
766 }

◆ fastexpf()

static float fastexpf ( float  p)
inlinestatic

"Fast" exponential approximation, valid for x in [ ~ -87, ...

as precision allows.

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 721 of file float_math.h.

721  {
722  return fastpow2f(1.442695040f * p);
723 }

◆ fastlog2f()

static float fastlog2f ( float  x)
inlinestatic

"Fast" log base 2 approximation, valid for positive x as precision allows.

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 637 of file float_math.h.

637  {
638  union { float f; uint32_t i; } vx = { x };
639  union { uint32_t i; float f; } mx = { (vx.i & 0x007FFFFF) | 0x3f000000 };
640  float y = vx.i;
641  y *= 1.1920928955078125e-7f;
642 
643  return y - 124.22551499f
644  - 1.498030302f * mx.f
645  - 1.72587999f / (0.3520887068f + mx.f);
646 }

◆ fastlogf()

static float fastlogf ( float  x)
inlinestatic

"Fast" natural logarithm approximation, valid for positive x as precision allows.

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 663 of file float_math.h.

663  {
664  return 0.69314718f * fastlog2f(x);
665 }

◆ fastpow2f()

static float fastpow2f ( float  p)
inlinestatic

"Fast" power of 2 approximation, valid for x in [ -126, ...

as precision allows.

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 679 of file float_math.h.

679  {
680  float clipp = (p < -126) ? -126.0f : p;
681  int w = clipp;
682  float z = clipp - w + 1.f;
683  union { uint32_t i; float f; } v = { (uint32_t) ( (1 << 23) *
684  (clipp + 121.2740575f + 27.7280233f / (4.84252568f - z) - 1.49012907f * z)
685  ) };
686 
687  return v.f;
688 }

◆ fastpowf()

static float fastpowf ( float  x,
float  p 
)
inlinestatic

"Fast" x to the power of p approximation

Note
Adapted from Paul Mineiro's FastFloat
Warning: Seems to have divergent segments with discontinuities for some base/exponent combinations

Definition at line 705 of file float_math.h.

705  {
706  return fastpow2f(p * fastlog2f(x));
707 }

◆ fastsinf()

static float fastsinf ( float  x)
inlinestatic

"Fast" sine approximation, valid for x in [-M_PI, M_PI]

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 499 of file float_math.h.

499  {
500  static const float q = 0.78444488374548933f;
501  union { float f; uint32_t i; } p = { 0.20363937680730309f };
502  union { float f; uint32_t i; } r = { 0.015124940802184233f };
503  union { float f; uint32_t i; } s = { -0.0032225901625579573f };
504 
505  union { float f; uint32_t i; } vx = { x };
506  uint32_t sign = vx.i & 0x80000000;
507  vx.i = vx.i & 0x7FFFFFFF;
508 
509  float qpprox = M_4_PI * x - M_4_PI2 * x * vx.f;
510  float qpproxsq = qpprox * qpprox;
511 
512  p.i |= sign;
513  r.i |= sign;
514  s.i ^= sign;
515 
516  return q * qpprox + qpproxsq * (p.f + qpproxsq * (r.f + qpproxsq * s.f));
517 }

◆ fastsinfullf()

static float fastsinfullf ( float  x)
inlinestatic

"Fast" sine approximation, valid on full x domain

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 539 of file float_math.h.

539  {
540  const int32_t k = (int32_t)(x * M_1_TWOPI);
541  const float half = (x < 0) ? -0.5f : 0.5f;
542  return fastsinf((half + k) * M_TWOPI - x);
543 }

◆ fasttanf()

static float fasttanf ( float  x)
inlinestatic

"Fast" tangent approximation, valid for x in [-M_PI_2, M_PI_2]

Note
Adapted from Paul Mineiro's FastFloat

Definition at line 598 of file float_math.h.

598  {
599  return fastsinf(x) / fastsinf(x + M_PI_2);
600 }

◆ fasttanfullf()

static float fasttanfullf ( float  x)
inlinestatic

"Fast" tangent approximation, valid on full x domain, except where tangent diverges.

Note
Adapted from Paul Mineiro's FastFloat
Warning: can be slower than libc version!

Definition at line 615 of file float_math.h.

615  {
616  const int32_t k = (int32_t)(x * M_1_TWOPI);
617  const float half = (x < 0) ? -0.5f : 0.5f;
618  const float xnew = x - (half + k) * M_TWOPI;
619  return fastsinf(xnew)/fastcosf(xnew);
620 }
fastersinfullf
static float fastersinfullf(float x)
"Faster" sine approximation, valid on full x domain
Definition: float_math.h:549
fastsinf
static float fastsinf(float x)
"Fast" sine approximation, valid for x in [-M_PI, M_PI]
Definition: float_math.h:499
fastercosf
static float fastercosf(float x)
"Faster" cosine approximation, valid for x in [-M_PI, M_PI]
Definition: float_math.h:569
fasterpow2f
static float fasterpow2f(float p)
"Faster" power of 2 approximation, valid for x in [ -126, ...
Definition: float_math.h:694
fastpow2f
static float fastpow2f(float p)
"Fast" power of 2 approximation, valid for x in [ -126, ...
Definition: float_math.h:679
fastlog2f
static float fastlog2f(float x)
"Fast" log base 2 approximation, valid for positive x as precision allows.
Definition: float_math.h:637
fastcosf
static float fastcosf(float x)
"Fast" cosine approximation, valid for x in [-M_PI, M_PI]
Definition: float_math.h:559
fastersinf
static float fastersinf(float x)
"Faster" sine approximation, valid for x in [-M_PI, M_PI]
Definition: float_math.h:524
si_fabsf
static float si_fabsf(float x)
Absolute value.
Definition: float_math.h:284
fasterlog2f
static float fasterlog2f(float x)
"Faster" log base 2 approximation, valid for positive x as precision allows.
Definition: float_math.h:652