Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 161 additions & 15 deletions GenSource/genlib_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************************************************/

/*******************************************************************************************************************
Parts of this file are from the gen~ export of Max 9 (genlib_ops.h, Copyright (c) 2023 Cycling '74): wrap() (result
range fix), cubic_interp(), fastcubic_interp(), spline6_interp(), genlib_fasteratan2(), genlib_fastertanh(),
Delay::read_fastcubic(), Delay::read_spline6() and Noise. That code is dual-licensed by Cycling '74 under the Cycling
'74 License for Max-Generated Code for Export or the GNU General Public License version 3, and is used here under the
GPLv3. See https://support.cycling74.com/hc/en-us/articles/360050779193-Gen-Code-Export-Licensing-FAQ
*******************************************************************************************************************/

#ifndef GENLIB_OPS_H
#define GENLIB_OPS_H 1

Expand Down Expand Up @@ -206,8 +214,12 @@ inline t_sample wrap(t_sample v, t_sample lo1, t_sample hi1){
const t_sample range = hi - lo;
if (v >= lo && v < hi) return v;
if (range <= 0.000000001) return lo; // no point...
// long, not int64_t as in Max 9: float to 64-bit int is a library call on Cortex-M
const long numWraps = long((v-lo)/range) - (v < lo);
return v - range * t_sample(numWraps);
const t_sample result = v - range * t_sample(numWraps);
// rounding can land exactly on hi
if (result >= hi) return result - range;
return result;
}

// this version gives far better performance when wrapping is relatively rare
Expand Down Expand Up @@ -414,6 +426,39 @@ inline float genlib_fasterpow (float x, float p) {
return genlib_fasterpow2(p * genlib_fasterlog2 (x));
}

// from http://dspguru.com/dsp/tricks/fixed-point-atan2-with-self-normalization
inline float genlib_fasteratan2(float y, float x) {
const float coeff_1 = (float)(GENLIB_PI/4);
const float coeff_2 = 3 * (float)(GENLIB_PI/4);
float abs_y = fabsf(y) + 1e-10f; // kludge to prevent 0/0 condition
float r, angle;
if (x >= 0) {
r = (x - abs_y) / (x + abs_y);
angle = coeff_1 - coeff_1 * r;
}
else {
r = (x + abs_y) / (abs_y - x);
angle = coeff_2 - coeff_1 * r;
}
if (y < 0) {
return (-angle); // negate if in quad III or IV
}
else {
return (angle);
}
}

// http://math.stackexchange.com/questions/107292/rapid-approximation-of-tanhx
// The rational fit holds for x >= 0 only (Max 9 applies it as is: -4 gives +22);
// mirror it for negative x and clamp, as tanh is odd and bounded by 1.
inline float genlib_fastertanh(float x) {
const float ax = fabsf(x);
float y = (-.67436811832e-5f + (.2468149110712040f + (.583691066395175e-1f + .3357335044280075e-1f * ax) * ax) * ax) /
(.2464845986383725f + (.609347197060491e-1f + (.1086202599228572f + .2874707922475963e-1f * ax) * ax) * ax);
if (y > 1.f) y = 1.f;
return x < 0 ? -y : y;
}

////////////////////////////////////////////////////////////////

inline t_sample fastertanfull(t_sample x) {
Expand All @@ -435,6 +480,22 @@ inline t_sample fasterexp(t_sample x) {
inline t_sample fasterpow(t_sample x, t_sample p) {
return (t_sample)genlib_fasterpow((float)x, (float)p);
}

inline t_sample fasterlog2(t_sample x) {
return (t_sample)genlib_fasterlog2((float)x);
}

inline t_sample fasterpow2(t_sample p) {
return (t_sample)genlib_fasterpow2((float)p);
}

inline t_sample fasteratan2(t_sample y, t_sample x) {
return (t_sample)genlib_fasteratan2((float)y, (float)x);
}

inline t_sample fastertanh(t_sample x) {
return (t_sample)genlib_fastertanh((float)x);
}
/****************************************************************/


Expand Down Expand Up @@ -481,7 +542,22 @@ inline t_sample cosine_interp(t_sample a, t_sample x, t_sample y) {
return(x*(t_sample(1.)-a2)+y*a2);
}

// 4-point Lagrange cubic, as in Max 9
inline t_sample cubic_interp(t_sample a, t_sample w, t_sample x, t_sample y, t_sample z) {
const t_sample a1 = t_sample(1.) + a;
const t_sample aa = a * a1;
const t_sample b = t_sample(1.) - a;
const t_sample b1 = t_sample(2.) - a;
const t_sample bb = b * b1;
const t_sample fw = t_sample(-.1666667) * bb * a;
const t_sample fx = t_sample(.5) * bb * a1;
const t_sample fy = t_sample(.5) * aa * b1;
const t_sample fz = t_sample(-.1666667) * aa * b;
return w * fw + x * fx + y * fy + z * fz;
}

// the previous cubic_interp; Max 9 keeps it as "fastcubic" and deprecates it, as it shows some instability with feedback
inline t_sample fastcubic_interp(t_sample a, t_sample w, t_sample x, t_sample y, t_sample z) {
const t_sample a2 = a*a;
const t_sample f0 = z - y - w + x;
const t_sample f1 = w - x - f0;
Expand All @@ -499,7 +575,7 @@ inline t_sample spline_interp(t_sample a, t_sample w, t_sample x, t_sample y, t_
return(f0*a*a2 + f1*a2 + f2*a + x);
}

// 6-point, 5th-order B-spline (from Max 9's genlib_ops.h, for exports that use interp mode "spline6")
// 6-point, 5th-order B-spline (from Max 9, for exports that use interp mode "spline6")
inline t_sample spline6_interp(t_sample a, t_sample y0, t_sample y1, t_sample y2, t_sample y3, t_sample y4, t_sample y5) {
// http://yehar.com/blog/wp-content/uploads/2009/08/deip.pdf
// 6-point, 5th-order B-spline (x-form)
Expand Down Expand Up @@ -723,26 +799,58 @@ struct DCBlock {
};

struct Noise {
unsigned long last;
static long uniqueTickCount(void) {
static long lasttime = 0;
long time = genlib_ticks();
uint32_t state[4];

// uses system clock to generate a random seed
// ensures distinct seeds even if creating several Noise objects at the same clock value
static uint64_t uniqueTickCount(void) {
static uint64_t lasttime = 0;
uint64_t time = genlib_ticks();
return (time <= lasttime) ? (++lasttime) : (lasttime = time);
}

Noise() { reset(); }
Noise(t_sample seed) { reset(seed); }
void reset() { last = uniqueTickCount() * uniqueTickCount(); }
void reset(t_sample seed) { last = seed; }

inline t_sample operator()() {
last = 1664525L * last + 1013904223L;
union {
uint32_t ui32;
float f;
} u = { (uint32_t)(0x3f800000 | (0x007fffff & last)) }; // type-punning
// Max 9 seeds with (uint64_t)(seed*1E6); seed from the float's bits or the tick count
// instead: float <-> 64-bit int conversions are soft-float library code on Cortex-M
void reset(t_sample seed) {
union { float f; uint32_t u; } bits = { (float)seed };
seed_state(bits.u);
}
void reset() { seed_state(uniqueTickCount()); }
void seed_state(uint64_t x) {
state[3] = splitmix32(state[2] = splitmix32(state[1] = splitmix32(state[0] = splitmix32(x))));
}

return (u.f * 2.f) - 3.f;
// adapted from the xoshiro128+ PRNG, see http://prng.di.unimi.it
/* xoshiro128+: Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
To the extent possible under law, the author has dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
See <http://creativecommons.org/publicdomain/zero/1.0/>. */
inline t_sample operator()() {
const uint32_t result = state[0] + state[3];
const uint32_t t = state[1] << 9;
state[2] ^= state[0];
state[3] ^= state[1];
state[1] ^= state[2];
state[0] ^= state[3];
state[2] ^= t;
state[3] = (state[3] << 11) | (state[3] >> 21); // rotl(s[3], 11)
// top 24 bits to 0..2 (2^-23), then -1..1
return ((result >> 8) * 1.1920928955078125e-7f) - 1.f;
}

// splitmix32 suggested by David Blackman and Sebastiano Vigna as a good seed for xoshiro:
/* This is a fixed-increment version of Java 8's SplittableRandom generator
See http://dx.doi.org/10.1145/2714064.2660195 and
http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html */
static uint32_t splitmix32(uint64_t seed) {
uint64_t z = (seed += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 33)) * 0x62a9d9ed799705f5;
z = (z ^ (z >> 28)) * 0xcb24d0a5c88c35b3;
return z >> 32;
}
};

Expand Down Expand Up @@ -967,6 +1075,23 @@ struct Delay {
return cosine_interp(a, x, y);
}

// cubic requires extra sample of compensation:
inline t_sample read_fastcubic(t_sample d) {
// min 1 sample delay for read before write (r != w)
// plus extra 1 sample compensation for 4-point interpolation
const t_sample r = t_sample(size + reader) - clamp(d, t_sample(1.)+t_sample(reader != writer), maxdelay);
long r1 = long(r);
long r2 = r1+1;
long r3 = r1+2;
long r4 = r1+3;
t_sample a = r - (t_sample)r1;
t_sample w = memory[r1 & wrap];
t_sample x = memory[r2 & wrap];
t_sample y = memory[r3 & wrap];
t_sample z = memory[r4 & wrap];
return fastcubic_interp(a, w, x, y, z);
}

// cubic requires extra sample of compensation:
inline t_sample read_cubic(t_sample d) {
// min 1 sample delay for read before write (r != w)
Expand Down Expand Up @@ -1000,6 +1125,27 @@ struct Delay {
t_sample z = memory[r4 & wrap];
return spline_interp(a, w, x, y, z);
}

// spline requires extra sample of compensation:
inline t_sample read_spline6(t_sample d) {
// min 1 sample delay for read before write (r != w)
// plus extra 1 sample compensation for 4-point interpolation
const t_sample r = t_sample(size + reader) - clamp(d, t_sample(1.)+t_sample(reader != writer), maxdelay);
long r0 = long(r);
long r1 = r0+1;
long r2 = r0+2;
long r3 = r0+3;
long r4 = r0+4;
long r5 = r0+5;
t_sample a = r - (t_sample)r0;
t_sample y0 = memory[r0 & wrap];
t_sample y1 = memory[r1 & wrap];
t_sample y2 = memory[r2 & wrap];
t_sample y3 = memory[r3 & wrap];
t_sample y4 = memory[r4 & wrap];
t_sample y5 = memory[r5 & wrap];
return spline6_interp(a, y0, y1, y2, y3, y4, y5);
}
};

template<typename T=t_sample>
Expand Down
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* gen~ noise uses Max 9's xoshiro128+ generator instead of a 32-bit LCG
* Added gen~ fastertanh, fasteratan2, fasterlog2 and fasterpow2 (Max 9)
* gen~ cubic interpolation is the 4-point Lagrange cubic of Max 9; added fastcubic (the previous cubic) and spline6 delay interpolation
* Fixed gen~ wrap returning the upper bound for inputs just below the lower bound (from Max 9)

v24.1
-----

Expand Down
109 changes: 109 additions & 0 deletions TestPatches/GenOpsTestPatch.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "TestPatch.hpp"
#include "../GenSource/genlib.cpp"

/*
* Unit test for genlib_ops.h operators taken from Max 9: wrap() range,
* cubic_interp() (4-point Lagrange) and fastcubic_interp(), Delay reads,
* the fast approximations, and Noise (xoshiro128+).
*/
class GenOpsTestPatch : public TestPatch {
public:
GenOpsTestPatch(){
{
TEST("wrap stays in range");
CHECK_CLOSE(wrap(1.5f, 0, 1), 0.5f, 1e-6);
CHECK_CLOSE(wrap(-0.25f, 0, 1), 0.75f, 1e-6);
CHECK_CLOSE(wrap(7.0f, 2, 5), 4.0f, 1e-5);
float v = wrap(-1e-8f, 0, 1); // v + 1 rounds to exactly 1 in float
CHECK(v >= 0.0f && v < 1.0f);
for(int i=-1000; i<=1000; ++i){
float x = i * 0.0137f - 1e-7f;
float w = wrap(x, -1, 1);
CHECK(w >= -1.0f && w < 1.0f);
}
}
{
TEST("cubic_interp is the Lagrange cubic");
// exact for a cubic polynomial through the 4 points at -1, 0, 1, 2
auto p = [](float t){ return t*t*t - 2*t + 0.5f; };
for(int i=0; i<=10; ++i){
float a = i * 0.1f;
CHECK_CLOSE(cubic_interp(a, p(-1), p(0), p(1), p(2)), p(a), 1e-4);
}
CHECK_EQUAL(cubic_interp(0, 1, 2, 3, 4), 2.0f);
CHECK_CLOSE(cubic_interp(1, 1, 2, 3, 4), 3.0f, 1e-6);
}
{
TEST("fastcubic_interp is the previous cubic");
CHECK_EQUAL(fastcubic_interp(0, 1, 2, 5, 3), 2.0f);
CHECK_CLOSE(fastcubic_interp(1, 1, 2, 5, 3), 5.0f, 1e-6);
// not exact for a cubic, unlike cubic_interp
auto p = [](float t){ return t*t*t; };
CHECK(fabsf(fastcubic_interp(0.5f, p(-1), p(0), p(1), p(2)) - p(0.5f)) > 0.01f);
}
{
TEST("Delay reads");
Delay d;
d.reset("delaytest", 64);
for(int i=0; i<64; ++i){
d.write(i);
d.step();
}
// the delay holds a ramp, which every interpolation except fastcubic reproduces exactly.
// As in Max 9, the 4-point reads are one sample shorter than linear,
// and the 6-point spline6 two samples shorter.
for(float t : {9.0f, 10.0f, 10.25f, 10.5f, 20.75f}){
float lin = d.read_linear(t);
CHECK_CLOSE(d.read_cubic(t), lin + 1, 1e-3);
if(t == floorf(t)) // the old cubic is not exact for a ramp between samples
CHECK_CLOSE(d.read_fastcubic(t), lin + 1, 1e-3);
CHECK_CLOSE(d.read_spline(t), lin + 1, 1e-3);
CHECK_CLOSE(d.read_spline6(t), lin + 2, 1e-3);
}
CHECK_EQUAL(d.read_step(10), d.read_linear(10));
}
{
TEST("fastertanh");
float maxerr = 0;
for(int i=-800; i<=800; ++i){
float x = i * 0.01f;
float y = fastertanh(x);
CHECK(y >= -1.0f && y <= 1.0f);
CHECK_CLOSE(fastertanh(-x), -y, 1e-4); // odd, apart from a 3e-5 offset at 0
maxerr = fmaxf(maxerr, fabsf(y - tanhf(x)));
}
CHECK(maxerr < 0.001f);
CHECK_CLOSE(fastertanh(-4), -1.0f, 0.001); // Max 9's version returns +22 here
}
{
TEST("fasteratan2");
for(int i=0; i<36; ++i){
float t = i * (float)M_PI / 18 - (float)M_PI + 0.05f;
CHECK_CLOSE(fasteratan2(sinf(t), cosf(t)), t, 0.075);
}
}
{
TEST("fasterpow2 and fasterlog2");
CHECK_CLOSE(fasterpow2(3), 8.0f, 0.5);
CHECK_CLOSE(fasterlog2(8), 3.0f, 0.1);
}
{
TEST("Noise");
Noise a(0.5), b(0.5), c(0.25);
bool same = true, differ = false;
double sum = 0, sum2 = 0;
const int n = 100000;
for(int i=0; i<n; ++i){
float x = a();
if(x != b()) same = false;
if(x != c()) differ = true;
CHECK(x >= -1.0f && x < 1.0f);
sum += x; sum2 += x*x;
}
CHECK(same); // same seed, same sequence
CHECK(differ); // different seed, different sequence
CHECK(fabs(sum / n) < 0.01); // mean 0
CHECK(fabs(sum2 / n - 1.0/3.0) < 0.01); // variance of uniform -1..1
}
}
};