From 17feec8147349f995afd1b34402b07c3bc8aa6e5 Mon Sep 17 00:00:00 2001 From: Martin Klang Date: Sat, 28 Nov 2026 04:49:19 +0100 Subject: [PATCH 1/6] genlib_ops.h: note the code taken from Max 9's genlib and its licence Max 9's gen~ export (Copyright 2023 Cycling '74) is dual-licensed: Cycling '74 License for Max-Generated Code for Export, or GPLv3. OwlProgram is GPLv3. spline6_interp() was already taken from it; the following commits take more. --- GenSource/genlib_ops.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/GenSource/genlib_ops.h b/GenSource/genlib_ops.h index ac27432c..41dd43fe 100644 --- a/GenSource/genlib_ops.h +++ b/GenSource/genlib_ops.h @@ -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 @@ -499,7 +507,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) From efe4e8a35b68408edaa679189b8a479e0cde03b9 Mon Sep 17 00:00:00 2001 From: Martin Klang Date: Sat, 28 Nov 2026 04:49:33 +0100 Subject: [PATCH 2/6] genlib: wrap() never returns hi From Max 9: v - range*numWraps can round to exactly hi, e.g. wrap(-1e-8, 0, 1) returned 1. Keeps long (not Max 9's int64_t) for the wrap count: float to int64 is a library call (__aeabi_f2lz, __aeabi_l2f) on Cortex-M4/M7. --- GenSource/genlib_ops.h | 6 +++++- HISTORY.md | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/GenSource/genlib_ops.h b/GenSource/genlib_ops.h index 41dd43fe..0630935e 100644 --- a/GenSource/genlib_ops.h +++ b/GenSource/genlib_ops.h @@ -214,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 diff --git a/HISTORY.md b/HISTORY.md index 614aae06..1f146b4f 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,5 @@ +* Fixed gen~ wrap returning the upper bound for inputs just below the lower bound (from Max 9) + v24.1 ----- From 9c7f919ee893df13d4b01620af84bfb73fddbff1 Mon Sep 17 00:00:00 2001 From: Martin Klang Date: Sat, 28 Nov 2026 04:49:56 +0100 Subject: [PATCH 3/6] genlib: Max 9 cubic interpolation, fastcubic and spline6 delay reads cubic_interp() is now the 4-point Lagrange cubic that Max 9 uses, so cubic interpolation matches Max. The previous formula is kept as fastcubic_interp(), which Max 9 deprecates for instability with feedback. Delay::read_fastcubic() and Delay::read_spline6() let exports with delay interp modes fastcubic and spline6 compile. --- GenSource/genlib_ops.h | 53 ++++++++++++++++++++++++++++++++++++++++++ HISTORY.md | 1 + 2 files changed, 54 insertions(+) diff --git a/GenSource/genlib_ops.h b/GenSource/genlib_ops.h index 0630935e..28e69443 100644 --- a/GenSource/genlib_ops.h +++ b/GenSource/genlib_ops.h @@ -493,7 +493,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; @@ -979,6 +994,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) @@ -1012,6 +1044,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 diff --git a/HISTORY.md b/HISTORY.md index 1f146b4f..42caa2ff 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,4 @@ +* 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 From 374d9604dc8e5583442d92a193f982a42965a671 Mon Sep 17 00:00:00 2001 From: Martin Klang Date: Sat, 28 Nov 2026 04:50:31 +0100 Subject: [PATCH 4/6] genlib: add fastertanh, fasteratan2, fasterlog2 and fasterpow2 from Max 9 Exports from Max 9 that use them did not compile. genlib_fastertanh() is Max 9's rational approximation, which only holds for x >= 0 (Max 9 returns +22 for -4): it is mirrored for negative input and clamped to +-1; max error 0.0009 against tanh on [-8, 8]. --- GenSource/genlib_ops.h | 49 ++++++++++++++++++++++++++++++++++++++++++ HISTORY.md | 1 + 2 files changed, 50 insertions(+) diff --git a/GenSource/genlib_ops.h b/GenSource/genlib_ops.h index 28e69443..935e6fad 100644 --- a/GenSource/genlib_ops.h +++ b/GenSource/genlib_ops.h @@ -426,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) { @@ -447,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); +} /****************************************************************/ diff --git a/HISTORY.md b/HISTORY.md index 42caa2ff..57d16731 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,4 @@ +* 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) From 8ff69bcdf9e805b2f964385494fac4c84092ca5d Mon Sep 17 00:00:00 2001 From: Martin Klang Date: Sat, 28 Nov 2026 04:50:59 +0100 Subject: [PATCH 5/6] genlib: Noise with xoshiro128+, as in Max 9 Max 9's single-precision Noise: xoshiro128+ seeded with splitmix32, replacing the 32-bit linear congruential generator. Per sample it is 32-bit integer operations and one int-to-float conversion; splitmix runs only on reset. Seeds come from the tick count or the bits of a float seed, not Max 9's (uint64_t)(seed*1E6): float <-> 64-bit int conversions pulled 2.4 KB of soft-float library code into a patch. The random sequence changes. --- GenSource/genlib_ops.h | 58 ++++++++++++++++++++++++++++++++---------- HISTORY.md | 1 + 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/GenSource/genlib_ops.h b/GenSource/genlib_ops.h index 935e6fad..0931c47d 100644 --- a/GenSource/genlib_ops.h +++ b/GenSource/genlib_ops.h @@ -799,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 . */ + 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; } }; diff --git a/HISTORY.md b/HISTORY.md index 57d16731..1efca28c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,4 @@ +* 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) From 026f24b43dc44483a7a1e31c0b1c26f86f3ea983 Mon Sep 17 00:00:00 2001 From: Martin Klang Date: Sat, 28 Nov 2026 04:53:18 +0100 Subject: [PATCH 6/6] Add GenOpsTest for the Max 9 genlib operators wrap() range (including the case that rounded to hi), cubic_interp() exact for a cubic, fastcubic_interp(), Delay reads of a ramp (4-point reads are one sample shorter than linear, spline6 two, as in Max 9), fastertanh() odd, bounded and within 0.001 of tanh, fasteratan2(), fasterpow2/log2, and Noise range, mean, variance and seeding. --- TestPatches/GenOpsTestPatch.hpp | 109 ++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 TestPatches/GenOpsTestPatch.hpp diff --git a/TestPatches/GenOpsTestPatch.hpp b/TestPatches/GenOpsTestPatch.hpp new file mode 100644 index 00000000..5e56824a --- /dev/null +++ b/TestPatches/GenOpsTestPatch.hpp @@ -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= -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 + } + } +};