Anklang-0.3.0.dev956+gd75ac925 anklang-0.3.0.dev956+gd75ac925
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#pragma once
3
4#include <ase/cxxaux.hh>
5
6namespace Ase {
7
9constexpr const double DOUBLE_EPSILON = 1.1102230246251565404236316680908203125e-16;
10
13extern inline ASE_CONST int irintf (float f) { return __builtin_rintf (f); }
14
16extern inline double force_double (double d) { volatile double v = d; return v; }
17
19extern inline float force_float (float f) { volatile float v = f; return v; }
20
32 float v_float;
33 struct {
34#if __BYTE_ORDER == __LITTLE_ENDIAN
35 uint mantissa : 23, biased_exponent : 8, sign : 1;
36#elif __BYTE_ORDER == __BIG_ENDIAN
37 uint sign : 1, biased_exponent : 8, mantissa : 23;
38#endif
39 } mpn;
40 char chars[4];
41 static constexpr const float EPSILON = 5.9604644775390625e-08;
42 static constexpr const int BIAS = 127;
43 static constexpr const float FMAX = 3.40282347e+38;
44 static constexpr const float FMIN = 1.17549435e-38;
45 static constexpr const float SMAX = 1.17549421e-38;
46 static constexpr const float SMIN = 1.40129846e-45;
47};
48
56extern inline float fast_exp2 (float x) ASE_CONST;
57
65extern inline float fast_log2 (float x) ASE_CONST;
66
68extern inline float value2hz (float x) ASE_CONST;
69
71extern inline float hz2value (float x) ASE_CONST;
72
74extern const float *const cent_table;
75
77extern const float *const semitone_tables_265[17];
78
79// == Implementations ==
80extern inline ASE_CONST float
81fast_exp2 (float ex)
82{
83 FloatIEEE754 fp = { 0, };
84 // const int i = ex < 0 ? int (ex - 0.5) : int (ex + 0.5);
85 const int i = irintf (ex);
86 fp.mpn.biased_exponent = fp.BIAS + i;
87 const float x = ex - i;
88 float r;
89 // f=2^x; remez(1, 5, [-.5;.5], 1/f, 1e-16); // minimized relative error
90 r = x * 0.0013276471992255f;
91 r = x * (0.0096755413344448f + r);
92 r = x * (0.0555071327349880f + r);
93 r = x * (0.2402211972384019f + r);
94 r = x * (0.6931469670647601f + r);
95 r = fp.v_float * (1.0f + r);
96 return r;
97}
98
99extern inline ASE_CONST float
100fast_log2 (float value)
101{
102 // log2 (i*x) = log2 (i) + log2 (x)
103 FloatIEEE754 u { value }; // v_float = 2^(biased_exponent-127) * mantissa
104 const int i = u.mpn.biased_exponent - FloatIEEE754::BIAS; // extract exponent without bias
105 u.mpn.biased_exponent = FloatIEEE754::BIAS; // reset to 2^0 so v_float is mantissa in [1..2]
106 float r, x = u.v_float - 1.0f; // x=[0..1]; r = log2 (x + 1);
107 // h=0.0113916; // offset to reduce error at origin
108 // f=(1/log(2)) * log(x+1); dom=[0-h;1+h]; p=remez(f, 6, dom, 1);
109 // p = p - p(0); // discard non-0 offset
110 // err=p-f; plot(err,[0;1]); plot(f,p,dom); // result in sollya
111 r = x * -0.0259366993544709205147977455165000143561553284592936f;
112 r = x * (+0.122047857676447181074792747820717519424533931189428f + r);
113 r = x * (-0.27814297685064327713977752916286528359628147166014f + r);
114 r = x * (+0.45764712300320092992105460899527194244236573556309f + r);
115 r = x * (-0.71816105664624015087225994551041120290062342459945f + r);
116 r = x * (+1.44254540258782520489769598315182363877204824648687f + r);
117 return i + r; // log2 (i) + log2 (x)
118}
119
120} // Ase
121
The Anklang C++ API namespace.
Definition api.hh:8
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)
Fast approximation of 2 raised to the power of x.
Definition mathutils.hh:81
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:19
double force_double(double d)
Force number into double precision floating point format, even with -ffast-math.
Definition mathutils.hh:16
int irintf(float f)
Round float to int, using round-to-nearest Fast version of f < 0 ? int (f - 0.5) : int (f + 0....
Definition mathutils.hh:13
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:17
float fast_log2(float x)
Fast approximation of logarithm to base 2.
Definition mathutils.hh:100
constexpr const double DOUBLE_EPSILON
Double round-off error at 1.0, equals 2^-53.
Definition mathutils.hh:9
float hz2value(float x)
Convert Hertz to synthesizer value (Voltage).
Union to compartmentalize an IEEE-754 float.
Definition mathutils.hh:31
static constexpr const float SMIN
0x00000001 Minimum Subnormal
Definition mathutils.hh:46
static constexpr const int BIAS
Exponent bias.
Definition mathutils.hh:42
static constexpr const float FMIN
0x00800000 Minimum Normal
Definition mathutils.hh:44
static constexpr const float SMAX
0x007fffff Maximum Subnormal
Definition mathutils.hh:45
static constexpr const float EPSILON
2^-24, round-off error at 1.0
Definition mathutils.hh:41
static constexpr const float FMAX
0x7f7fffff, 2^128 * (1 - epsilon)
Definition mathutils.hh:43