Skip to content

genlib: fixes and operators from Max 9's gen~ export - #137

Merged
pingdynasty merged 6 commits into
masterfrom
feature/genlib-max9
Nov 28, 2026
Merged

pingdynasty merged 6 commits into
masterfrom
feature/genlib-max9

Conversation

@pingdynasty

Copy link
Copy Markdown
Collaborator

Brings genlib (GenSource/genlib_ops.h) up to the gen~ export of Max 9 where it fixes bugs or adds operators that Max 9 exports use. Delay-read fixes follow in a separate PR on top of this one.

Changes

  • wrap() never returns hi: v - range*numWraps could round to exactly hi, e.g. wrap(-1e-8, 0, 1) returned 1
  • cubic_interp() is Max 9's 4-point Lagrange cubic, so cubic interpolation matches Max. The previous formula is kept as fastcubic_interp() (Max 9 deprecates it for instability with feedback; it is not exact even for a straight line between samples). Adds Delay::read_fastcubic() and Delay::read_spline6()
  • New operators: fastertanh, fasteratan2, fasterlog2, fasterpow2; Max 9 exports using them did not compile
  • Noise is Max 9's xoshiro128+, seeded with splitmix32, instead of a 32-bit LCG
  • Licence note: the code taken from Max 9 (Copyright 2023 Cycling '74) is dual-licensed and used here under the GPLv3, as is OwlProgram; this also covers the earlier spline6_interp()

Where this differs from Max 9

  • wrap() / fold() keep long for the wrap count, not Max 9's int64_t: float to 64-bit int is a library call (__aeabi_f2lz, __aeabi_l2f) on Cortex-M4/M7. Max 9's int64 FLOAT_TO_UINT32 in fasterpow2 is not taken for the same reason
  • fastertanh(): Max 9's rational approximation only holds for x >= 0 (it returns +22 for -4, and tends to 1.17 for large x); it is mirrored for negative input and clamped to +-1, within 0.0009 of tanh on [-8, 8]
  • Noise seeds from the tick count or the bits of a float seed, not Max 9's (uint64_t)(seed*1E6), whose float <-> uint64 conversions pulled 2.4 KB of soft-float library code into a patch (now +256 B on the Gen test patch)
  • OWL's single-precision math (logf, expf, ...) is kept

Behaviour changes for existing patches

  • gen~ cubic interpolation sounds different (matches Max 9)
  • the noise sequence changes

Tests

GenOpsTest: wrap range including the rounding case, cubic_interp exact for a cubic, fastcubic_interp, Delay reads of a ramp (as in Max 9 the 4-point reads are one sample shorter than linear, spline6 two), fastertanh odd/bounded/accuracy, fasteratan2, fasterpow2/log2, Noise range, mean, variance and seeding.

Verification

  • make check passes; Gen test patch builds without warnings on OWL2 and OWL3
  • Not tested: a real Max 9 export on a device

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.
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.
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.
…ax 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].
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.
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.
@pingdynasty
pingdynasty merged commit a21f7a2 into master Nov 28, 2026
1 check passed
@pingdynasty
pingdynasty deleted the feature/genlib-max9 branch November 28, 2026 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant