Anklang 0.3.0-460-gc4ef46ba
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
mathutils.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#ifndef __ASE_MATHUTILS_HH__
3#define __ASE_MATHUTILS_HH__
4
5#include <ase/cxxaux.hh>
6
7namespace Ase {
8
10constexpr const double DOUBLE_EPSILON = 1.1102230246251565404236316680908203125e-16;
11
14extern inline ASE_CONST int irintf (float f) { return __builtin_rintf (f); }
15
17extern inline double force_double (double d) { volatile double v = d; return v; }
18
20extern inline float force_float (float f) { volatile float v = f; return v; }
21
33 float v_float;
34 struct {
35#if __BYTE_ORDER == __LITTLE_ENDIAN
36 uint mantissa : 23, biased_exponent : 8, sign : 1;
37#elif __BYTE_ORDER == __BIG_ENDIAN
38 uint sign : 1, biased_exponent : 8, mantissa : 23;
39#endif
40 } mpn;
41 char chars[4];
42 static constexpr const float EPSILON = 5.9604644775390625e-08;
43 static constexpr const int BIAS = 127;
44 static constexpr const float FMAX = 3.40282347e+38;
45 static constexpr const float FMIN = 1.17549435e-38;
46 static constexpr const float SMAX = 1.17549421e-38;
47 static constexpr const float SMIN = 1.40129846e-45;
48};
49
57extern inline float fast_exp2 (float x) ASE_CONST;
58
66extern inline float fast_log2 (float x) ASE_CONST;
67
69extern inline float value2hz (float x) ASE_CONST;
70
72extern inline float hz2value (float x) ASE_CONST;
73
75extern const float *const cent_table;
76
78extern const float *const semitone_tables_265[17];
79
80// == Implementations ==
81extern inline ASE_CONST float
82fast_exp2 (float ex)
83{
84 FloatIEEE754 fp = { 0, };
85 // const int i = ex < 0 ? int (ex - 0.5) : int (ex + 0.5);
86 const int i = irintf (ex);
87 fp.mpn.biased_exponent = fp.BIAS + i;
88 const float x = ex - i;
89 float r;
90 // f=2^x; remez(1, 5, [-.5;.5], 1/f, 1e-16); // minimized relative error
91 r = x * 0.0013276471992255f;
92 r = x * (0.0096755413344448f + r);
93 r = x * (0.0555071327349880f + r);
94 r = x * (0.2402211972384019f + r);
95 r = x * (0.6931469670647601f + r);
96 r = fp.v_float * (1.0f + r);
97 return r;
98}
99
100extern inline ASE_CONST float
101fast_log2 (float value)
102{
103 // log2 (i*x) = log2 (i) + log2 (x)
104 FloatIEEE754 u { value }; // v_float = 2^(biased_exponent-127) * mantissa
105 const int i = u.mpn.biased_exponent - FloatIEEE754::BIAS; // extract exponent without bias
106 u.mpn.biased_exponent = FloatIEEE754::BIAS; // reset to 2^0 so v_float is mantissa in [1..2]
107 float r, x = u.v_float - 1.0f; // x=[0..1]; r = log2 (x + 1);
108 // h=0.0113916; // offset to reduce error at origin
109 // f=(1/log(2)) * log(x+1); dom=[0-h;1+h]; p=remez(f, 6, dom, 1);
110 // p = p - p(0); // discard non-0 offset
111 // err=p-f; plot(err,[0;1]); plot(f,p,dom); // result in sollya
112 r = x * -0.0259366993544709205147977455165000143561553284592936f;
113 r = x * (+0.122047857676447181074792747820717519424533931189428f + r);
114 r = x * (-0.27814297685064327713977752916286528359628147166014f + r);
115 r = x * (+0.45764712300320092992105460899527194244236573556309f + r);
116 r = x * (-0.71816105664624015087225994551041120290062342459945f + r);
117 r = x * (+1.44254540258782520489769598315182363877204824648687f + r);
118 return i + r; // log2 (i) + log2 (x)
119}
120
121} // Ase
122
123#endif // __ASE_MATHUTILS_HH__
The Anklang C++ API namespace.
Definition api.hh:9
float value2hz(float x)
Convert synthesizer value (Voltage) to Hertz.
const float *const cent_table
Finetune factor table with 201 entries for -100…0…+100 cent.
Definition mathtables.cc:63
float fast_exp2(float x)
Definition mathutils.hh:82
const float *const semitone_tables_265[17]
Musical Tuning Tables, to be indexed by MusicalTuning
float force_float(float f)
Force number into single precision floating point format, even with -ffast-math.
Definition mathutils.hh:20
double force_double(double d)
Force number into double precision floating point format, even with -ffast-math.
Definition mathutils.hh:17
int irintf(float f)
Definition mathutils.hh:14
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:18
float fast_log2(float x)
Definition mathutils.hh:101
constexpr const double DOUBLE_EPSILON
Double round-off error at 1.0, equals 2^-53.
Definition mathutils.hh:10
float hz2value(float x)
Convert Hertz to synthesizer value (Voltage).
static constexpr const float SMIN
0x00000001 Minimum Subnormal
Definition mathutils.hh:47
static constexpr const int BIAS
Exponent bias.
Definition mathutils.hh:43
static constexpr const float FMIN
0x00800000 Minimum Normal
Definition mathutils.hh:45
static constexpr const float SMAX
0x007fffff Maximum Subnormal
Definition mathutils.hh:46
static constexpr const float EPSILON
2^-24, round-off error at 1.0
Definition mathutils.hh:42
static constexpr const float FMAX
0x7f7fffff, 2^128 * (1 - epsilon)
Definition mathutils.hh:44