Skip to content
Verified contractExactMIT

Marfa1997PsychA4Family

Address0x29141a0e1ce4ad84d2fde488dbf5dc9726ffbf0e
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.37

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.37+commit.f401782d
Language
solidity
Optimization
Yes, 200 runs
EVM version
osaka
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,246,306
Creation transaction
0xa7224500a5…c7f5926277

src/generated/Marfa1997PsychA4Family.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {IMarfa1997Family} from "../interfaces/IMarfa1997Family.sol";
import {Marfa1997Genome} from "../Marfa1997Genome.sol";
import {Marfa1997Catalog} from "./Marfa1997Catalog.sol";
import {Marfa1997PsychA4} from "../families/Marfa1997PsychA4.sol";

/// @notice Thumbnail family contract for Marfa1997PsychA4.
contract Marfa1997PsychA4Family is IMarfa1997Family {
    function draw(bytes32 seed, bytes calldata pal) external pure returns (bytes memory, bytes memory) {
        Marfa1997Genome.G memory g = Marfa1997Genome.genome(seed);
        g.pal = pal;
        return Marfa1997PsychA4.draw(g, seed, Marfa1997Catalog.thumbP(g.first));
    }
}

src/Marfa1997Genome.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {Marfa1997Catalog} from "./generated/Marfa1997Catalog.sol";

/// @notice Seed-derived genome. A xoshiro128** stream seeded from the 32-byte seed yields, in order: the opening
///         slot, KNOBS knob values, two music words, the first segment's length in seconds, a skipped fork(0x5E0),
///         and fork(0x51A), whose first four words seed the per-segment simulation streams.
library Marfa1997Genome {
    struct Rng {
        uint32 s0;
        uint32 s1;
        uint32 s2;
        uint32 s3;
    }

    struct G {
        uint256 first;
        int256[6] knobs;
        uint32 music0;
        uint32 music1;
        uint256 secs0;
        uint32[4] simWords;
        bytes pal; // palette table; set by the family wrapper, empty from genome()
    }

    function rotl(uint32 x, uint32 k) internal pure returns (uint32) {
        unchecked {
            return (x << k) | (x >> (32 - k));
        }
    }

    /// @dev xoshiro128** state; an all-zero state is replaced by s0 = 1.
    function make(uint32 a, uint32 b, uint32 c, uint32 d) internal pure returns (Rng memory r) {
        r = Rng(a, b, c, d);
        if (a | b | c | d == 0) r.s0 = 1;
    }

    /// @dev State = big-endian words 0..3 XOR words 4..7 of the seed.
    function fromSeed(bytes32 seed) internal pure returns (Rng memory) {
        uint256 s = uint256(seed);
        uint32[8] memory w;
        for (uint256 i; i < 8; ++i) w[i] = uint32(s >> (224 - 32 * i));
        return make(w[0] ^ w[4], w[1] ^ w[5], w[2] ^ w[6], w[3] ^ w[7]);
    }

    /// @dev Next xoshiro128** output.
    function u32(Rng memory r) internal pure returns (uint32 res) {
        unchecked {
            uint32 s1 = r.s1;
            res = rotl(s1 * 5, 7) * 9;
            uint32 t = s1 << 9;
            r.s2 ^= r.s0;
            r.s3 ^= r.s1;
            r.s1 ^= r.s2;
            r.s0 ^= r.s3;
            r.s2 ^= t;
            r.s3 = rotl(r.s3, 11);
        }
    }

    /// @dev lo + u32 % (hi - lo + 1); consumes exactly one draw.
    function draw(Rng memory r, int256 lo, int256 hi) internal pure returns (int256) {
        uint32 x = u32(r);
        return lo + int256(uint256(x) % uint256(hi - lo + 1));
    }

    /// @dev A new stream from the next four draws, the last XORed with `tag`.
    function fork(Rng memory r, uint32 tag) internal pure returns (Rng memory) {
        uint32 a = u32(r);
        uint32 b = u32(r);
        uint32 c = u32(r);
        uint32 d = u32(r);
        return make(a, b, c, d ^ tag);
    }

    /// @dev Inclusive range of knob `i` for `slot` (two big-endian int16).
    function knobRange(uint256 slot, uint256 i) internal pure returns (int256 lo, int256 hi) {
        bytes memory t = Marfa1997Catalog.KNOB_RANGES;
        uint256 o = (slot * Marfa1997Catalog.KNOBS + i) * 4;
        lo = int256(int16(uint16(uint8(t[o])) << 8 | uint16(uint8(t[o + 1]))));
        hi = int256(int16(uint16(uint8(t[o + 2])) << 8 | uint16(uint8(t[o + 3]))));
    }

    /// @notice The genome for `seed`.
    function genome(bytes32 seed) internal pure returns (G memory g) {
        Rng memory root = fromSeed(seed);
        g.first = uint256(u32(root)) % Marfa1997Catalog.N;
        for (uint256 i; i < Marfa1997Catalog.KNOBS; ++i) {
            (int256 lo, int256 hi) = knobRange(g.first, i);
            g.knobs[i] = draw(root, lo, hi);
        }
        g.music0 = u32(root);
        g.music1 = u32(root);
        g.secs0 = uint256(draw(root, 45, 75));
        fork(root, 0x5e0);
        Rng memory sim = fork(root, 0x51a);
        for (uint256 i; i < 4; ++i) g.simWords[i] = u32(sim);
    }

    /// @notice Simulation stream of segment `k`.
    function simStream(G memory g, uint32 k) internal pure returns (Rng memory) {
        unchecked {
            return make(g.simWords[0], g.simWords[1] ^ ((k + 1) * 0x9e3779b9), g.simWords[2], g.simWords[3] ^ k);
        }
    }

    /// @notice Stream for the thumbnail's decorative draws: fork(0x7B) of the seed's stream.
    function thumbRng(bytes32 seed) internal pure returns (Rng memory) {
        Rng memory r = fromSeed(seed);
        return fork(r, 0x7b);
    }
}

src/families/Marfa1997PsychA4.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {Marfa1997Genome} from "../Marfa1997Genome.sol";
import {Marfa1997PsychCore as C} from "./Marfa1997PsychCore.sol";

/// @notice Psych thumbnail family for presets 1, 7; p[1] is the preset's index in that list.
library Marfa1997PsychA4 {
    /// @dev Four u16 section lengths, then IDX (per preset: vector offset u16, length u8, bind offset u16, count u8,
    ///      palette stop offset u16, count u8), VEC (int16), BIND (knob u8, slot u8, mul int16), PAL (entry, r, g, b).
    bytes internal constant DATA = hex"001200cc0040000000004300000a000000004323000a060000000000000201040000000000000023002d00080000000200000001000300010002000000010001001400460003000200960190000000010008000000010004000000240000000c000000000002002800000002000300010002000000030002001c005f00060001002802bc000100060010fa24000100040000001c00000016000000460005009600000002010400000000000000140023000a000000020004000000060000000e000000000000000000010001002800020004000100010000000f002800000076000400c8000000000001010f0001012b0001020d0001022900010336ffff04100001042c0002051500010531ffff0000000101180002020f00080317000104150001051c000f";

    function draw(Marfa1997Genome.G memory g, bytes32, int256[8] memory p) internal pure returns (bytes memory, bytes memory) {
        if (p[1] < 0 || p[1] >= 2) return ("", "");
        return C.runN(g, uint256(p[1]), ops(), C.unpack(DATA));
    }

    function ops() private pure returns (C.Ops memory o) {
        o.pal = C.palTab;
        o.budCyc = C.budCyc;
        o.attrCyc = C.attrCyc;
        o.cycDefs = C.cycDefs;
        o.cycInit = C.cycInit;
        o.cycAdv = C.cycAdv;
        o.emitCyc = C.emitCycN;
        o.tiles = C.tiles;
        o.budBez = C.budBez;
        o.attrBez = C.attrBez;
        o.bezInit = C.bezInit;
        o.emitBez = C.emitBezN;
        o.pattern = C.patC;
    }
}

src/families/Marfa1997PsychCore.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {Marfa1997Genome} from "../Marfa1997Genome.sol";

/// @notice Psych thumbnail core: replays the first T ticks of a preset's module models with its seeded draws and
///         writes the resulting field as SVG body and defs.
/// @dev Each family library passes its preset data and an `Ops` table of the module routines its presets use;
///      only those routines are linked into the family.
library Marfa1997PsychCore {
    int256 private constant T = 500;
    /// @dev Quarter-wave sine: round(sin(2πi/1024)·10000), i = 0..256, int16 big-endian; other angles by symmetry.
    bytes private constant QS = hex"0000003d007b00b800f50133017001ad01eb0228026502a202e0031d035a039703d40411044e048b04c805050542057f05bb05f80635067106ae06ea07260763079f07db08170853088f08cb09070942097e09b909f50a300a6b0aa60ae10b1c0b570b920bcc0c060c410c7b0cb50cef0d290d630d9c0dd60e0f0e480e810eba0ef30f2b0f640f9c0fd4100c1044107c10b410eb11221159119011c711fd1234126a12a012d6130c1341137613ab13e01415144a147e14b214e6151a154d158115b415e71619164c167e16b016e217141745177617a717d8180818391869189818c818f719261955198419b219e01a0e1a3c1a691a961ac31aef1b1c1b481b741b9f1bca1bf51c201c4a1c751c9f1cc81cf21d1b1d431d6c1d941dbc1de41e0b1e321e591e7f1ea61ecb1ef11f161f3b1f601f841fa91fcc1ff0201320362058207b209d20be20e02101212121422162218121a121c021de21fd221b223922562273229022ac22c822e42300231b23352350236a2384239d23b623cf23e723ff2417242e2445245c24722488249d24b324c724dc24f025042517252a253d254f256125732584259525a625b625c625d525e425f32601260f261d262a263726442650265c26672672267d26872691269b26a426ad26b526bd26c526cc26d326da26e026e626eb26f026f526f926fd2701270427072709270b270d270e270f27102710";
    int256 private constant NB = 6;
    int256 private constant NT = 4;    // never-erased curves: their newest NT drawn steps get a group each
    int256 private constant QV = 32;   // a vortex of at most QV vertices is drawn as its flat quads
    int256 private constant M22 = 4194304;
    int256 private constant FAR = 36864;   // px16: feedback curve bands reaching beyond this are skipped
    int256 private constant RS = 30000;    // rose bands scaled over RS/1000 use the unit rose clipped to ±RC/1000
    int256 private constant RC = 25000;

    // ---- parameter indices into a module's vector ----
    uint256 private constant C_N = 0; uint256 private constant C_SYM = 1; uint256 private constant C_MIR = 2;
    uint256 private constant C_TILE = 3; uint256 private constant C_TT = 4; uint256 private constant C_PI = 5;
    uint256 private constant C_PII = 6; uint256 private constant C_SPD = 7; uint256 private constant C_RAD = 8;
    uint256 private constant C_SIDES = 9; uint256 private constant C_MODE = 10; uint256 private constant C_D = 11;
    uint256 private constant C_ROT = 12; uint256 private constant C_DEPTH = 13; uint256 private constant C_SIDES2 = 14;
    uint256 private constant C_D2 = 15; uint256 private constant C_ROT2 = 16; uint256 private constant C_SIDES3 = 17;
    uint256 private constant C_D3 = 18; uint256 private constant C_ROT3 = 19; uint256 private constant C_TRAIL = 20;
    uint256 private constant C_CRATE = 22; uint256 private constant C_CMODE = 23;
    uint256 private constant C_COFS = 24; uint256 private constant C_AUD = 25; uint256 private constant C_AMT = 26;
    uint256 private constant B_NC = 0; uint256 private constant B_V = 1; uint256 private constant B_W = 2;
    uint256 private constant B_SYM = 3; uint256 private constant B_MIR = 4; uint256 private constant B_TILE = 5;
    uint256 private constant B_TT = 6; uint256 private constant B_TRAIL = 7; uint256 private constant B_CRATE = 8;
    uint256 private constant B_CMODE = 9; uint256 private constant B_COFS = 10; uint256 private constant B_AUD = 11;
    uint256 private constant B_AMT = 12; uint256 private constant B_MARG = 13;
    uint256 private constant V_NV = 0; uint256 private constant V_COLL = 1; uint256 private constant V_AMP = 2;
    uint256 private constant V_SPAS = 3; uint256 private constant V_ARMS = 4; uint256 private constant V_CCS = 5;
    uint256 private constant V_DIR = 6; uint256 private constant V_DUR = 7; uint256 private constant V_FG = 8;
    uint256 private constant V_CX = 12; uint256 private constant V_CY = 13;
    uint256 private constant S_SYM = 0; uint256 private constant S_SIZE = 1; uint256 private constant S_RAD = 2;
    uint256 private constant S_ROSC = 3; uint256 private constant S_RRATE = 4; uint256 private constant S_ROT = 5;
    uint256 private constant S_RING = 6; uint256 private constant S_CRATE = 7; uint256 private constant S_EVERY = 8;
    uint256 private constant S_MIR = 9; uint256 private constant S_JIT = 10; uint256 private constant S_TILE = 11;
    uint256 private constant S_TT = 12; uint256 private constant S_AUD = 13; uint256 private constant S_AMT = 14;
    uint256 private constant S_RAMT = 15;
    uint256 private constant P_SHAPE = 0; uint256 private constant P_SIDES = 1; uint256 private constant P_AMP = 2;
    uint256 private constant P_DENS = 3; uint256 private constant P_C0 = 4; uint256 private constant P_ROT = 5;
    uint256 private constant P_SPIN = 6; uint256 private constant P_ALPHA = 7;

    /// @dev Append-only byte buffer; capacity doubles on growth.
    struct W {
        bytes data;
        uint256 len;
    }

    /// @dev One module: parameter offset into the vector, model state and output state.
    struct Mod {
        uint256 t;
        uint256 o;
        // Cyclic
        int256 ra; int256 rb; int256 rc; int256 c0; int256 a0; int256 a1; int256 a2; int256 R;
        int256[4] P;
        int256 n;
        int256[] cur;
        int256 curN;
        Lv L;
        int256[] so;
        // Bezier
        int256[] pts;
        // Vortex
        int256 cx; int256 cy; int256 R0; int256 D; int256 Rv; int256 Th; int256 f; bool alive; int256 idle; int256 pR; int256 pTh;
        // Sphere
        int256 jit;
        // output
        int256 lo; int256 stride; int256 gQ; int256 gS; int256 nb; int256 top; int256 need; int256 bd; bool cull; int256 vF; int256 vStep; uint256 vi;
        int256[] last;
        int256[] hc; uint256 hcn; int256[] ho; uint256 hon;
        W[6] bk;
    }

    struct Ctx {
        bytes S;
        int256[] v;
        int256[] lut;
        int256[] su;
        int256 reps; int256 off; int256 gsz;
        int256 zoom; int256 rot; int256 fade; int256 hasFx; int256 pa; int256 anyFade;
        int256[] pw; int256[] kp;
        int256 xl;     // bytes of defs the budget leaves out (the clipped rose)
        bool[] gq;
        Marfa1997Genome.Rng rng;
        int256[4] X;
        int256[4] Y;      // (band: the older image's transform)
        int256[16] BP;    // (band: a curve's control points, now and one stride older)
        E e;
        Mod[] ms;      // the modules; ms[vx]: the vortex (at most one per preset), vF / vStep / vi its timeline
        uint256 vx;
        uint256 nvt;
        Ops o;
        bytes PAL;
        bytes TAB;     // the shared palette table (Marfa1997Genome.G.pal)
    }

    /// @dev Module routines linked by a family; entries it leaves unset are never called.
    struct Ops {
        function(Ctx memory, uint256, uint256) internal pure pal;
        function(Ctx memory, Mod memory, W memory, W memory) internal pure pattern;
        function(Ctx memory, Mod memory, uint256, W memory) internal pure cycDefs;
        function(Ctx memory, Mod memory) internal pure cycInit;
        function(Ctx memory, Mod memory) internal pure cycAdv;
        function(Ctx memory, Mod memory, uint256, int256, int256[4] memory, W memory) internal pure emitCyc;
        function(Ctx memory, Mod memory) internal pure bezInit;
        function(Ctx memory, Mod memory, int256, int256[4] memory, W memory) internal pure emitBez;
        function(Ctx memory, Mod memory) internal pure vtxInit;
        function(Ctx memory, Mod memory, int256) internal pure vtxTick;
        function(Ctx memory, Mod memory, uint256, int256, W memory, W memory) internal pure vortex;
        function(Ctx memory, Mod memory) internal pure returns (bool) sphAdv;
        function(Ctx memory, Mod memory, uint256, int256[4] memory, W memory, W memory) internal pure emitSph;
        function(Ctx memory, Mod[] memory) internal pure timelines;
        function(Ctx memory, uint256, int256, int256, int256) internal pure returns (bool) hidden;
        function(uint256, uint256, int256, int256, W memory) internal pure tiles;
        function(Ctx memory, Mod memory, int256, int256) internal pure returns (int256, int256, int256) budCyc;
        function(Ctx memory, Mod memory, int256, int256) internal pure returns (int256, int256, int256) budBez;
        function(Ctx memory, Mod memory, int256, int256) internal pure returns (int256, int256, int256) budSph;
        function(Ctx memory, Mod memory, W memory, int256) internal pure returns (int256, int256, int256, int256) attrCyc;
        function(Ctx memory, Mod memory, W memory, int256) internal pure returns (int256, int256, int256, int256) attrBez;
    }

    // =============================================================================================================
    // output buffer
    function wmake(uint256 cap) internal pure returns (W memory w) {
        unchecked {
            w.data = new bytes(cap);
        }
    }

    function wres(W memory w, uint256 extra) internal pure {
        unchecked {
            uint256 need = w.len + extra;
            if (need <= w.data.length) return;
            uint256 cap = w.data.length * 2;
            if (cap < need) cap = need;
            bytes memory n = new bytes(cap);
            bytes memory old = w.data;
            uint256 len = w.len;
            assembly ("memory-safe") {
                mcopy(add(n, 32), add(old, 32), len)
            }
            w.data = n;
        }
    }

    function wb(W memory w, bytes memory s) internal pure {
        unchecked {
            uint256 l = s.length;
            if (l == 0) return;
            wres(w, l);
            bytes memory d = w.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                mcopy(add(add(d, 32), o), add(s, 32), l)
            }
            w.len = o + l;
        }
    }

    /// append a literal of ≤ 32 bytes (no zero bytes) without allocating: its length = 32 − trailing zero bytes
    function wl(W memory w, bytes32 s) internal pure {
        unchecked {
            wres(w, 32);
            bytes memory d = w.data;
            uint256 o = w.len;
            uint256 n;
            assembly ("memory-safe") {
                mstore(add(add(d, 32), o), s)
                let x := s
                let z := 0
                if iszero(and(x, 0xffffffffffffffffffffffffffffffff)) { z := 16 x := shr(128, x) }
                if iszero(and(x, 0xffffffffffffffff)) { z := add(z, 8) x := shr(64, x) }
                if iszero(and(x, 0xffffffff)) { z := add(z, 4) x := shr(32, x) }
                if iszero(and(x, 0xffff)) { z := add(z, 2) x := shr(16, x) }
                if iszero(and(x, 0xff)) { z := add(z, 1) }
                n := sub(32, z)
            }
            w.len = o + n;
        }
    }

    function wl8(W memory w, bytes8 s) internal pure {
        wl(w, bytes32(s));
    }

    function wl16(W memory w, bytes16 s) internal pure {
        wl(w, bytes32(s));
    }

    function wbuf(W memory w, W memory s) internal pure {
        unchecked {
            uint256 l = s.len;
            if (l == 0) return;
            wres(w, l);
            bytes memory d = w.data;
            bytes memory src = s.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                mcopy(add(add(d, 32), o), add(src, 32), l)
            }
            w.len = o + l;
        }
    }

    function wch(W memory w, uint8 c) internal pure {
        unchecked {
            wres(w, 1);
            bytes memory d = w.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                mstore8(add(add(d, 32), o), c)
            }
            w.len = o + 1;
        }
    }

    function done(W memory w) internal pure returns (bytes memory d) {
        unchecked {
            d = w.data;
            uint256 l = w.len;
            assembly ("memory-safe") {
                mstore(d, l)
            }
        }
    }

    /// unsigned decimal
    function wu(W memory w, uint256 v) internal pure {
        unchecked {
            wres(w, 110);
            bytes memory d = w.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                function dg9(p9, a9) -> e9 {
                    switch lt(a9, 10000)
                    case 1 {
                        let n9 := add(add(1, gt(a9, 9)), add(gt(a9, 99), gt(a9, 999)))
                        let w9 := or(or(shl(24, add(48, div(a9, 1000))), shl(16, add(48, mod(div(a9, 100), 10)))), or(shl(8, add(48, mod(div(a9, 10), 10))), add(48, mod(a9, 10))))
                        mstore(p9, shl(sub(256, shl(3, n9)), w9))
                        e9 := add(p9, n9)
                    }
                    default {
                        let n9 := 1
                        for { let z9 := div(a9, 10) } z9 { z9 := div(z9, 10) } { n9 := add(n9, 1) }
                        e9 := add(p9, n9)
                        let i9 := e9
                        for {} 1 {} {
                            i9 := sub(i9, 1)
                            mstore8(i9, add(48, mod(a9, 10)))
                            a9 := div(a9, 10)
                            if iszero(a9) { break }
                        }
                    }
                }
                o := sub(dg9(add(add(d, 32), o), v), add(d, 32))
            }
            w.len = o;
        }
    }

    /// signed integer
    function wn0(W memory w, int256 v) internal pure {
        unchecked {
            wres(w, 111);
            bytes memory d = w.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                function dg9(p9, a9) -> e9 {
                    switch lt(a9, 10000)
                    case 1 {
                        let n9 := add(add(1, gt(a9, 9)), add(gt(a9, 99), gt(a9, 999)))
                        let w9 := or(or(shl(24, add(48, div(a9, 1000))), shl(16, add(48, mod(div(a9, 100), 10)))), or(shl(8, add(48, mod(div(a9, 10), 10))), add(48, mod(a9, 10))))
                        mstore(p9, shl(sub(256, shl(3, n9)), w9))
                        e9 := add(p9, n9)
                    }
                    default {
                        let n9 := 1
                        for { let z9 := div(a9, 10) } z9 { z9 := div(z9, 10) } { n9 := add(n9, 1) }
                        e9 := add(p9, n9)
                        let i9 := e9
                        for {} 1 {} {
                            i9 := sub(i9, 1)
                            mstore8(i9, add(48, mod(a9, 10)))
                            a9 := div(a9, 10)
                            if iszero(a9) { break }
                        }
                    }
                }
                let b := add(d, 32)
                let p := add(b, o)
                let a := v
                if slt(v, 0) { mstore8(p, 45) p := add(p, 1) a := sub(0, v) }
                p := dg9(p, a)
                o := sub(p, b)
            }
            w.len = o;
        }
    }

    /// value in tenths as a decimal (at most one fractional digit)
    function wn1(W memory w, int256 t) internal pure {
        unchecked {
            wres(w, 112);
            bytes memory d = w.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                function dg9(p9, a9) -> e9 {
                    switch lt(a9, 10000)
                    case 1 {
                        let n9 := add(add(1, gt(a9, 9)), add(gt(a9, 99), gt(a9, 999)))
                        let w9 := or(or(shl(24, add(48, div(a9, 1000))), shl(16, add(48, mod(div(a9, 100), 10)))), or(shl(8, add(48, mod(div(a9, 10), 10))), add(48, mod(a9, 10))))
                        mstore(p9, shl(sub(256, shl(3, n9)), w9))
                        e9 := add(p9, n9)
                    }
                    default {
                        let n9 := 1
                        for { let z9 := div(a9, 10) } z9 { z9 := div(z9, 10) } { n9 := add(n9, 1) }
                        e9 := add(p9, n9)
                        let i9 := e9
                        for {} 1 {} {
                            i9 := sub(i9, 1)
                            mstore8(i9, add(48, mod(a9, 10)))
                            a9 := div(a9, 10)
                            if iszero(a9) { break }
                        }
                    }
                }
                let b := add(d, 32)
                let p := add(b, o)
                let a := t
                if slt(t, 0) { mstore8(p, 45) p := add(p, 1) a := sub(0, t) }
                let q := div(a, 10)
                let r := mod(a, 10)
                p := dg9(p, q)
                if r { mstore8(p, 46) mstore8(add(p, 1), add(48, r)) p := add(p, 2) }
                o := sub(p, b)
            }
            w.len = o;
        }
    }

    /// value in thousandths as a decimal
    function wf3(W memory w, int256 t) internal pure {
        unchecked {
            wres(w, 114);
            bytes memory d = w.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                function dg9(p9, a9) -> e9 {
                    switch lt(a9, 10000)
                    case 1 {
                        let n9 := add(add(1, gt(a9, 9)), add(gt(a9, 99), gt(a9, 999)))
                        let w9 := or(or(shl(24, add(48, div(a9, 1000))), shl(16, add(48, mod(div(a9, 100), 10)))), or(shl(8, add(48, mod(div(a9, 10), 10))), add(48, mod(a9, 10))))
                        mstore(p9, shl(sub(256, shl(3, n9)), w9))
                        e9 := add(p9, n9)
                    }
                    default {
                        let n9 := 1
                        for { let z9 := div(a9, 10) } z9 { z9 := div(z9, 10) } { n9 := add(n9, 1) }
                        e9 := add(p9, n9)
                        let i9 := e9
                        for {} 1 {} {
                            i9 := sub(i9, 1)
                            mstore8(i9, add(48, mod(a9, 10)))
                            a9 := div(a9, 10)
                            if iszero(a9) { break }
                        }
                    }
                }
                let b := add(d, 32)
                let p := add(b, o)
                let a := t
                if slt(t, 0) { mstore8(p, 45) p := add(p, 1) a := sub(0, t) }
                let q := div(a, 1000)
                let fr := mod(a, 1000)
                p := dg9(p, q)
                if fr {
                    mstore8(p, 46)
                    mstore8(add(p, 1), add(48, div(fr, 100)))
                    p := add(p, 2)
                    if mod(fr, 100) {
                        mstore8(p, add(48, div(mod(fr, 100), 10)))
                        p := add(p, 1)
                        if mod(fr, 10) { mstore8(p, add(48, mod(fr, 10))) p := add(p, 1) }
                    }
                }
                o := sub(p, b)
            }
            w.len = o;
        }
    }

    function hexd(uint256 v) internal pure returns (uint8) {
        unchecked {
            return uint8(v < 10 ? 48 + v : 87 + v);
        }
    }

    function wrgb(W memory w, int256 r, int256 g, int256 b) internal pure {
        unchecked {
            wres(w, 7);
            bytes memory d = w.data;
            uint256 o = w.len;
            uint256 v = (cl(r) << 16) | (cl(g) << 8) | cl(b);
            assembly ("memory-safe") {
                let p := add(add(d, 32), o)
                mstore8(p, 35)
                for { let i := 0 } lt(i, 6) { i := add(i, 1) } {
                    let h := and(shr(mul(4, sub(5, i)), v), 15)
                    mstore8(add(p, add(i, 1)), add(h, add(48, mul(39, gt(h, 9)))))
                }
            }
            w.len = o + 7;
        }
    }

    function cl(int256 x) internal pure returns (uint256) {
        unchecked {
            return x < 0 ? 0 : x > 255 ? 255 : uint256(x);
        }
    }

    /// px16 → svg tenths (P), whole units (Pi)
    function wP(W memory w, int256 v) internal pure {
        unchecked {
            wn1(w, v * 625 / 1024);
        }
    }

    /// "x y" (svg tenths)
    function wPP(W memory w, int256 x, int256 y) internal pure {
        unchecked {
            wn1(w, x * 625 / 1024);
            wch(w, 32);
            wn1(w, y * 625 / 1024);
        }
    }

    /// wPP with a single buffer reservation
    function wPPf(W memory w, int256 x, int256 y) internal pure {
        unchecked {
            x = x * 625 / 1024;
            y = y * 625 / 1024;
            wres(w, 200);
            bytes memory d = w.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                function dg9(p9, a9) -> e9 {
                    switch lt(a9, 10000)
                    case 1 {
                        let n9 := add(add(1, gt(a9, 9)), add(gt(a9, 99), gt(a9, 999)))
                        let w9 := or(or(shl(24, add(48, div(a9, 1000))), shl(16, add(48, mod(div(a9, 100), 10)))), or(shl(8, add(48, mod(div(a9, 10), 10))), add(48, mod(a9, 10))))
                        mstore(p9, shl(sub(256, shl(3, n9)), w9))
                        e9 := add(p9, n9)
                    }
                    default {
                        let n9 := 1
                        for { let z9 := div(a9, 10) } z9 { z9 := div(z9, 10) } { n9 := add(n9, 1) }
                        e9 := add(p9, n9)
                        let i9 := e9
                        for {} 1 {} {
                            i9 := sub(i9, 1)
                            mstore8(i9, add(48, mod(a9, 10)))
                            a9 := div(a9, 10)
                            if iszero(a9) { break }
                        }
                    }
                }
                function n1(p_, t_) -> e_ {
                    let a_ := t_
                    if slt(t_, 0) { mstore8(p_, 45) p_ := add(p_, 1) a_ := sub(0, t_) }
                    let q_ := div(a_, 10)
                    let r_ := mod(a_, 10)
                    p_ := dg9(p_, q_)
                    if r_ { mstore8(p_, 46) mstore8(add(p_, 1), add(48, r_)) p_ := add(p_, 2) }
                    e_ := p_
                }
                let b0_ := add(d, 32)
                let p0_ := n1(add(b0_, o), x)
                mstore8(p0_, 32)
                p0_ := n1(add(p0_, 1), y)
                o := sub(p0_, b0_)
            }
            w.len = o;
        }
    }

    /// a Cyclic stamp's <use> up to its colour: <use href="#q{i}" x="{P x}" y="{P y}" (k = 0) or
    /// <use href="#q{i}" transform="translate({P x} {P y})rotate({deg g})" (k = 1), then stroke=" / fill="
    function wStamp(W memory w, uint256 i, int256 x, int256 y, int256 g, uint256 k, bool stroke) internal pure {
        unchecked {
            x = x * 625 / 1024;
            y = y * 625 / 1024;
            if (k != 0) g = (((-g) % 65536) * 3600) / 65536;
            wres(w, 450);
            bytes memory d = w.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                function dg9(p9, a9) -> e9 {
                    switch lt(a9, 10000)
                    case 1 {
                        let n9 := add(add(1, gt(a9, 9)), add(gt(a9, 99), gt(a9, 999)))
                        let w9 := or(or(shl(24, add(48, div(a9, 1000))), shl(16, add(48, mod(div(a9, 100), 10)))), or(shl(8, add(48, mod(div(a9, 10), 10))), add(48, mod(a9, 10))))
                        mstore(p9, shl(sub(256, shl(3, n9)), w9))
                        e9 := add(p9, n9)
                    }
                    default {
                        let n9 := 1
                        for { let z9 := div(a9, 10) } z9 { z9 := div(z9, 10) } { n9 := add(n9, 1) }
                        e9 := add(p9, n9)
                        let i9 := e9
                        for {} 1 {} {
                            i9 := sub(i9, 1)
                            mstore8(i9, add(48, mod(a9, 10)))
                            a9 := div(a9, 10)
                            if iszero(a9) { break }
                        }
                    }
                }
                function n1(p_, t_) -> e_ {
                    let a_ := t_
                    if slt(t_, 0) { mstore8(p_, 45) p_ := add(p_, 1) a_ := sub(0, t_) }
                    let q_ := div(a_, 10)
                    let r_ := mod(a_, 10)
                    p_ := dg9(p_, q_)
                    if r_ { mstore8(p_, 46) mstore8(add(p_, 1), add(48, r_)) p_ := add(p_, 2) }
                    e_ := p_
                }
                let b0_ := add(d, 32)
                let p0_ := add(b0_, o)
                mstore(p0_, 0x3c75736520687265663d22237100000000000000000000000000000000000000)
                p0_ := add(p0_, 13)
                p0_ := dg9(p0_, i)
                switch k
                case 0 {
                    mstore(p0_, 0x2220783d22000000000000000000000000000000000000000000000000000000)
                    p0_ := n1(add(p0_, 5), x)
                    mstore(p0_, 0x2220793d22000000000000000000000000000000000000000000000000000000)
                    p0_ := n1(add(p0_, 5), y)
                }
                default {
                    mstore(p0_, 0x22207472616e73666f726d3d227472616e736c61746528000000000000000000)
                    p0_ := n1(add(p0_, 23), x)
                    mstore8(p0_, 32)
                    p0_ := n1(add(p0_, 1), y)
                    mstore(p0_, 0x29726f7461746528000000000000000000000000000000000000000000000000)
                    p0_ := n1(add(p0_, 8), g)
                    mstore8(p0_, 41)
                    p0_ := add(p0_, 1)
                }
                switch stroke
                case 0 { mstore(p0_, 0x222066696c6c3d22000000000000000000000000000000000000000000000000) p0_ := add(p0_, 8) }
                default { mstore(p0_, 0x22207374726f6b653d2200000000000000000000000000000000000000000000) p0_ := add(p0_, 10) }
                o := sub(p0_, b0_)
            }
            w.len = o;
        }
    }

    /// a vortex quad: M{x0},{y0}L{x1},{y1} {x2},{y2} {x3},{y3}Z in whole svg units
    function wQuad(W memory w, int256[] memory A, int256[] memory B, uint256 i) internal pure {
        unchecked {
            wres(w, 760);
            bytes memory d = w.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                function dg9(p9, a9) -> e9 {
                    switch lt(a9, 10000)
                    case 1 {
                        let n9 := add(add(1, gt(a9, 9)), add(gt(a9, 99), gt(a9, 999)))
                        let w9 := or(or(shl(24, add(48, div(a9, 1000))), shl(16, add(48, mod(div(a9, 100), 10)))), or(shl(8, add(48, mod(div(a9, 10), 10))), add(48, mod(a9, 10))))
                        mstore(p9, shl(sub(256, shl(3, n9)), w9))
                        e9 := add(p9, n9)
                    }
                    default {
                        let n9 := 1
                        for { let z9 := div(a9, 10) } z9 { z9 := div(z9, 10) } { n9 := add(n9, 1) }
                        e9 := add(p9, n9)
                        let i9 := e9
                        for {} 1 {} {
                            i9 := sub(i9, 1)
                            mstore8(i9, add(48, mod(a9, 10)))
                            a9 := div(a9, 10)
                            if iszero(a9) { break }
                        }
                    }
                }
                function n0(p_, v_) -> e_ {
                    let a_ := v_
                    if slt(v_, 0) { mstore8(p_, 45) p_ := add(p_, 1) a_ := sub(0, v_) }
                    p_ := dg9(p_, a_)
                    e_ := p_
                }
                function pi(v_) -> r_ { r_ := sdiv(mul(v_, 125), 2048) }
                let a_ := add(add(A, 32), mul(i, 64))
                let c_ := add(add(B, 32), mul(i, 64))
                let b0_ := add(d, 32)
                let p0_ := add(b0_, o)
                mstore8(p0_, 77)
                p0_ := n0(add(p0_, 1), pi(mload(a_)))
                mstore8(p0_, 44)
                p0_ := n0(add(p0_, 1), pi(mload(add(a_, 32))))
                mstore8(p0_, 76)
                p0_ := n0(add(p0_, 1), pi(mload(add(a_, 64))))
                mstore8(p0_, 44)
                p0_ := n0(add(p0_, 1), pi(mload(add(a_, 96))))
                mstore8(p0_, 32)
                p0_ := n0(add(p0_, 1), pi(mload(add(c_, 64))))
                mstore8(p0_, 44)
                p0_ := n0(add(p0_, 1), pi(mload(add(c_, 96))))
                mstore8(p0_, 32)
                p0_ := n0(add(p0_, 1), pi(mload(c_)))
                mstore8(p0_, 44)
                p0_ := n0(add(p0_, 1), pi(mload(add(c_, 32))))
                mstore8(p0_, 90)
                o := sub(add(p0_, 1), b0_)
            }
            w.len = o;
        }
    }

    function wPi(W memory w, int256 v) internal pure {
        unchecked {
            wn0(w, v * 125 / 2048);
        }
    }

    function wPi2(W memory w, int256 x, int256 y) internal pure {
        wPi2s(w, 0, x, y);
    }

    /// "x,y" in whole svg units, preceded by byte `sep` when non-zero
    function wPi2s(W memory w, uint256 sep, int256 x, int256 y) internal pure {
        unchecked {
            x = x * 125 / 2048;
            y = y * 125 / 2048;
            wres(w, 200);
            bytes memory d = w.data;
            uint256 o = w.len;
            assembly ("memory-safe") {
                function dg9(p9, a9) -> e9 {
                    switch lt(a9, 10000)
                    case 1 {
                        let n9 := add(add(1, gt(a9, 9)), add(gt(a9, 99), gt(a9, 999)))
                        let w9 := or(or(shl(24, add(48, div(a9, 1000))), shl(16, add(48, mod(div(a9, 100), 10)))), or(shl(8, add(48, mod(div(a9, 10), 10))), add(48, mod(a9, 10))))
                        mstore(p9, shl(sub(256, shl(3, n9)), w9))
                        e9 := add(p9, n9)
                    }
                    default {
                        let n9 := 1
                        for { let z9 := div(a9, 10) } z9 { z9 := div(z9, 10) } { n9 := add(n9, 1) }
                        e9 := add(p9, n9)
                        let i9 := e9
                        for {} 1 {} {
                            i9 := sub(i9, 1)
                            mstore8(i9, add(48, mod(a9, 10)))
                            a9 := div(a9, 10)
                            if iszero(a9) { break }
                        }
                    }
                }
                function n0(p_, v_) -> e_ {
                    let a_ := v_
                    if slt(v_, 0) { mstore8(p_, 45) p_ := add(p_, 1) a_ := sub(0, v_) }
                    p_ := dg9(p_, a_)
                    e_ := p_
                }
                let b0_ := add(d, 32)
                let p0_ := add(b0_, o)
                if sep { mstore8(p0_, sep) p0_ := add(p0_, 1) }
                p0_ := n0(p0_, x)
                mstore8(p0_, 44)
                p0_ := n0(add(p0_, 1), y)
                o := sub(p0_, b0_)
            }
            w.len = o;
        }
    }

    /// T16 angle → svg rotate() degrees, tenths
    function wdeg(W memory w, int256 g) internal pure {
        unchecked {
            wn1(w, (((-g) % 65536) * 3600) / 65536);
        }
    }

    // =============================================================================================================
    // integer math
    function tbl(bytes memory S, uint256 i) internal pure returns (int256 v) {
        assembly ("memory-safe") {
            let q := and(i, 511)
            if gt(q, 256) { q := sub(512, q) }
            v := signextend(1, shr(240, mload(add(add(S, 32), mul(q, 2)))))
            if gt(i, 511) { v := sub(0, v) }
        }
    }

    function sn(bytes memory S, int256 a) internal pure returns (int256 r) {
        assembly ("memory-safe") {
            function tb(S_, i_) -> v_ {
                let q_ := and(i_, 511)
                if gt(q_, 256) { q_ := sub(512, q_) }
                v_ := signextend(1, shr(240, mload(add(add(S_, 32), mul(q_, 2)))))
                if gt(i_, 511) { v_ := sub(0, v_) }
            }
            a := smod(a, 65536)
            if slt(a, 0) { a := add(a, 65536) }
            let i := shr(6, a)
            let s0 := tb(S, i)
            r := add(s0, sdiv(mul(sub(tb(S, and(add(i, 1), 1023)), s0), and(a, 63)), 64))
        }
    }

    function cs(bytes memory S, int256 a) internal pure returns (int256) {
        unchecked {
            return sn(S, a + 16384);
        }
    }

    function S22(bytes memory S, int256 a) internal pure returns (int256) {
        unchecked {
            return sn(S, a >> 6);
        }
    }

    function C22(bytes memory S, int256 a) internal pure returns (int256) {
        unchecked {
            return sn(S, (a >> 6) + 16384);
        }
    }

    function rad22(int256 s) internal pure returns (int256) {
        unchecked {
            return s * 66754 / 1000;
        }
    }

    function r16(Marfa1997Genome.Rng memory r) internal pure returns (int256) {
        unchecked {
            return int256(uint256(Marfa1997Genome.u32(r) >> 16));
        }
    }

    function absi(int256 v) internal pure returns (int256) {
        unchecked {
            return v < 0 ? -v : v;
        }
    }

    function mx(int256 a, int256 b) internal pure returns (int256) {
        unchecked {
            return a > b ? a : b;
        }
    }

    function mn(int256 a, int256 b) internal pure returns (int256) {
        unchecked {
            return a < b ? a : b;
        }
    }

    /// floor(sqrt(n)): Newton's iteration from a power of two above the root, decreasing monotonically to the floor
    function isqrt2(int256 n) internal pure returns (int256 r) {
        if (n < 2) return n;
        assembly ("memory-safe") {
            let x := n
            let z := x
            let s := 0
            if gt(z, 0xffffffffffffffffffffffffffffffff) { z := shr(128, z) s := 128 }
            if gt(z, 0xffffffffffffffff) { z := shr(64, z) s := add(s, 64) }
            if gt(z, 0xffffffff) { z := shr(32, z) s := add(s, 32) }
            if gt(z, 0xffff) { z := shr(16, z) s := add(s, 16) }
            if gt(z, 0xff) { z := shr(8, z) s := add(s, 8) }
            if gt(z, 0xf) { z := shr(4, z) s := add(s, 4) }
            if gt(z, 0x3) { z := shr(2, z) s := add(s, 2) }
            if gt(z, 0x1) { s := add(s, 1) }
            r := shl(shr(1, add(s, 2)), 1)
            for {} 1 {} {
                let y := shr(1, add(r, div(x, r)))
                if iszero(lt(y, r)) { break }
                r := y
            }
        }
    }

    function colAt(int256 mode, int256 c0, int256 rate, int256 n) internal pure returns (int256) {
        unchecked {
            int256 v = c0 * 16 + n * rate;
            if (mode != 0) return ((v % 3776) + 3776) % 3776;
            int256 w = ((v % 7520) + 7520) % 7520;
            return w > 3760 ? 7520 - w : w;
        }
    }

    function idxU(int256 i16) internal pure returns (int256) {
        unchecked {
            return i16 * 65536 / 3776;
        }
    }

    function u8(bytes memory b, uint256 i) internal pure returns (int256) {
        unchecked {
            return int256(uint256(uint8(b[i])));
        }
    }

    function i16(bytes memory b, uint256 i) internal pure returns (int256) {
        unchecked {
            return int256(int16(uint16(uint8(b[i])) << 8 | uint16(uint8(b[i + 1]))));
        }
    }

    /// palette from the family's hex stops (entry, r, g, b per stop)
    function palHex(Ctx memory c, uint256 palOff, uint256 palN) internal pure {
        unchecked {
            bytes memory pal = c.PAL;
            int256[] memory u = new int256[](palN);
            int256[] memory col = new int256[](palN * 3);
            for (uint256 i; i < palN; ++i) {
                uint256 o = (palOff + i) * 4;
                u[i] = u8(pal, o) * 65536 / 236;
                col[3 * i] = u8(pal, o + 1);
                col[3 * i + 1] = u8(pal, o + 2);
                col[3 * i + 2] = u8(pal, o + 3);
            }
            lutOf(c, u, col);
        }
    }

    /// palette v[0] % 30 of the shared table, its colours as stops evenly spaced
    function palTab(Ctx memory c, uint256, uint256) internal pure {
        unchecked {
            bytes memory R = c.TAB;
            uint256 o;
            for (uint256 k = uint256(c.v[0]) % 30; k > 0; --k) o += 1 + 3 * uint256(uint8(R[o]));
            uint256 M = uint256(uint8(R[o]));
            int256[] memory u = new int256[](M);
            int256[] memory col = new int256[](M * 3);
            for (uint256 i; i < M; ++i) {
                u[i] = int256(i) * 65536 / int256(M);
                for (uint256 ch; ch < 3; ++ch) col[3 * i + ch] = u8(R, o + 1 + 3 * i + ch);
            }
            lutOf(c, u, col);
        }
    }

    function palAny(Ctx memory c, uint256 palOff, uint256 palN) internal pure {
        if (c.v[0] < 0) palHex(c, palOff, palN);
        else palTab(c, palOff, palN);
    }

    /// stop positions and colours → the 236-entry LUT
    function lutOf(Ctx memory c, int256[] memory u, int256[] memory col) internal pure {
        unchecked {
            uint256 n = u.length;
            int256[] memory lut = new int256[](708);
            uint256 ptr;
            for (uint256 e; e < 236; ++e) {
                int256 x = int256(e) * 65536 / 236;
                while (ptr < n && u[ptr] <= x) ++ptr;
                uint256 a = ptr == 0 ? n - 1 : ptr - 1;
                uint256 b = (a + 1) % n;
                int256 ua = u[a];
                int256 ub = u[b];
                int256 da = x - ua;
                if (ub <= ua) ub += 65536;
                if (da < 0) da += 65536;
                for (uint256 ch; ch < 3; ++ch) lut[e * 3 + ch] = col[3 * a + ch] + (col[3 * b + ch] - col[3 * a + ch]) * da / (ub - ua);
            }
            c.su = u;
            c.lut = lut;
        }
    }

    function colU(Ctx memory c, W memory w, int256 u) internal pure {
        unchecked {
            int256 x = (u * c.reps) % 65536;
            if (x < 0) x += 65536;
            int256 p = x * 236;
            uint256 i = uint256(p >> 16);
            int256 f = p & 65535;
            uint256 j = (i + 1) % 236;
            int256[] memory L = c.lut;
            int256 r0; int256 g0; int256 b0; int256 r1; int256 g1; int256 b1;
            assembly ("memory-safe") {
                let a := add(add(L, 32), mul(96, i))
                let e := add(add(L, 32), mul(96, j))
                r0 := mload(a) g0 := mload(add(a, 32)) b0 := mload(add(a, 64))
                r1 := mload(e) g1 := mload(add(e, 32)) b1 := mload(add(e, 64))
            }
            wrgb(w, r0 + (r1 - r0) * f / 65536, g0 + (g1 - g0) * f / 65536, b0 + (b1 - b0) * f / 65536);
        }
    }

    function colEnd(Ctx memory c, W memory w, int256 i) internal pure {
        col(c, w, i);
        wl8(w, '"/>');
    }

    function colUEnd(Ctx memory c, W memory w, int256 u) internal pure {
        colU(c, w, u);
        wl8(w, '"/>');
    }

    function col(Ctx memory c, W memory w, int256 i) internal pure {
        unchecked {
            colU(c, w, idxU(i) + c.off);
        }
    }

    function pv(Ctx memory c, Mod memory m, uint256 k) internal pure returns (int256 r) {
        int256[] memory v = c.v;
        uint256 i = m.o + k;
        assembly ("memory-safe") {
            r := mload(add(add(v, 32), mul(i, 32)))
        }
    }

    // =============================================================================================================
    // module models
    function cycInit(Ctx memory c, Mod memory m) internal pure {
        unchecked {
            int256 spd = pv(c, m, C_SPD);
            int256 S = spd > 0 ? 51 - spd : 1 - 15 * spd;
            m.ra = 4868 * 64 * (r16(c.rng) + 13107) / S / 65536;
            m.rb = 2920 * 64 * (r16(c.rng) + 13107) / S / 65536;
            m.rc = 3651 * 64 * (r16(c.rng) + 13107) / S / 65536;
            m.c0 = int256(uint256(Marfa1997Genome.u32(c.rng) % 236));
            m.a0 = r16(c.rng);
            m.a1 = r16(c.rng);
            m.a2 = r16(c.rng);
            m.R = pv(c, m, C_RAD) * 8192 / 100;
            m.curN = -1;
            m.cur = new int256[](uint256(pv(c, m, C_N)) * 2);
            Lv memory L = m.L;
            L.n = [pv(c, m, C_SIDES), pv(c, m, C_SIDES2), pv(c, m, C_SIDES3)];
            L.d = [pv(c, m, C_D) * 16, pv(c, m, C_D2) * 16, pv(c, m, C_D3) * 16];
            L.rot = [pv(c, m, C_ROT), pv(c, m, C_ROT2), pv(c, m, C_ROT3)];
            L.depth = uint256(pv(c, m, C_DEPTH));
            m.so = new int256[](uint256(L.depth == 0 ? int256(1) : L.depth == 1 ? L.n[0] : L.n[0] * L.n[1]) * 5);
            int256 ns = L.depth == 0 ? int256(1) : L.depth == 1 ? L.n[0] : L.n[0] * L.n[1];
            m.last = new int256[](uint256(2 * (pv(c, m, C_MIR) + 1) * pv(c, m, C_N) * ns));
        }
    }

    function cycAdv(Ctx memory c, Mod memory m) internal pure {
        unchecked {
            int256 a = (m.P[0] + m.ra) % M22;
            int256 b = (m.P[1] + m.rb) % M22;
            m.P[2] = (m.P[2] + m.rc) % M22;
            m.P[3] = (m.P[3] + S22(c.S, a + b) * 6675 / 10000) % M22;
            m.P[0] = a;
            m.P[1] = b;
            m.n++;
        }
    }

    function path(bytes memory S, int256 t, int256 a, int256 b, int256 d) internal pure returns (int256, int256) {
        unchecked {
            if (t == 1) return (S22(S, a), C22(S, b));
            if (t == 2) return (S22(S, a), S22(S, a + b));
            if (t == 3) return (S22(S, a + b), S22(S, b));
            if (t == 4) return (S22(S, a + d), S22(S, b + d));
            if (t == 5) return (S22(S, a), S22(S, rad22(C22(S, b)) + d));
            if (t == 6) return (S22(S, rad22(C22(S, a)) + d), S22(S, a));
            if (t == 7) return (S22(S, d), S22(S, d + rad22(S22(S, d))));
            return (S22(S, b + d), S22(S, d));
        }
    }

    /// positions of the current step (cached)
    function cycPos(Ctx memory c, Mod memory m) internal pure {
        unchecked {
            if (m.curN == m.n) return;
            m.curN = m.n;
            bytes memory S = c.S;
            int256 d = rad22(S22(S, m.P[2]));
            (int256 ax, int256 gx) = path(S, pv(c, m, C_PI), m.P[0], m.P[1], d);
            (int256 ay, int256 gy) = path(S, pv(c, m, C_PII), m.P[0], m.P[1], d);
            int256 N = pv(c, m, C_N);
            for (int256 r; r < N; ++r) {
                int256 k = (N - 1 - r) * M22 / N + m.P[3];
                m.cur[uint256(2 * r)] = 8192 + m.R * ax / 10000 * S22(S, k * gx / 10000) / 10000;
                m.cur[uint256(2 * r + 1)] = 8192 + m.R * ay / 10000 * C22(S, k * gy / 10000) / 10000;
            }
        }
    }

    function cycCol(Ctx memory c, Mod memory m, int256 r) internal pure returns (int256) {
        unchecked {
            return (colAt(pv(c, m, C_CMODE), m.c0, pv(c, m, C_CRATE), m.n) + r * pv(c, m, C_COFS) * 16) % 3776;
        }
    }

    /// nested stamp levels of a Cyclic step: [sides, radius px16, angle] × 3
    struct Lv {
        int256[3] n;
        int256[3] d;
        int256[3] g;
        int256[3] rot;
        uint256 depth;
        uint256 k;
    }

    /// stamps of object r for the current step (one per segment): [x, y, radius, angle, sides] ×k
    function stamps(Ctx memory c, Mod memory m, int256 r) internal pure returns (uint256) {
        unchecked {
            cycPos(c, m);
            Lv memory L = m.L;
            L.g[0] = m.a0 + m.n * L.rot[0];
            L.g[1] = m.a1 + m.n * L.rot[1];
            L.g[2] = m.a2 + m.n * L.rot[2];
            L.k = 0;
            nest(c.S, L, m.so, m.cur[uint256(2 * r)], m.cur[uint256(2 * r + 1)], 0, L.depth);
            return L.k;
        }
    }

    /// the stamp at (x, y) of level l: drawn when l is the deepest, else one per vertex of the level-l polygon
    function nest(bytes memory S, Lv memory L, int256[] memory out, int256 x, int256 y, uint256 l, uint256 depth) internal pure {
        unchecked {
            if (l == depth) {
                uint256 k = L.k;
                (out[k], out[k + 1], out[k + 2], out[k + 3], out[k + 4]) = (x, y, L.d[l], L.g[l], L.n[l]);
                L.k = k + 5;
                return;
            }
            for (int256 v; v < L.n[l]; ++v) {
                int256 a = L.g[l] + v * 65536 / L.n[l];
                nest(S, L, out, x + sn(S, a) * L.d[l] / 10000, y + cs(S, a) * L.d[l] / 10000, l + 1, depth);
            }
        }
    }

    function bezInit(Ctx memory c, Mod memory m) internal pure {
        unchecked {
            uint256 n = uint256(pv(c, m, B_NC)) * 8;
            m.pts = new int256[](n * 2);
            for (uint256 i; i < n; ++i) {
                uint32 r = Marfa1997Genome.u32(c.rng);
                m.pts[2 * i] = 16 * int256(uint256(r % 1024));
                m.pts[2 * i + 1] = (r & 0x80000000) != 0 ? int256(-1) : int256(1);
            }
            m.c0 = int256(uint256(Marfa1997Genome.u32(c.rng) % 236));
        }
    }

    function refl(int256 u, int256 lo, int256 hi) internal pure returns (int256) {
        unchecked {
            int256 L = hi - lo;
            int256 P2 = 2 * L;
            int256 t = (u - lo) % P2;
            if (t < 0) t += P2;
            return t < L ? lo + t : hi - (t - L);
        }
    }

    function bezPt(Ctx memory c, Mod memory m, uint256 k, uint256 i) internal pure returns (int256) {
        unchecked {
            int256 marg = pv(c, m, B_MARG) * 16;
            uint256 j = 2 * (k * 8 + i);
            return refl(m.pts[j] + m.pts[j + 1] * pv(c, m, B_V) * m.n, -marg, 16384 + marg);
        }
    }

    function vtxInit(Ctx memory c, Mod memory m) internal pure {
        unchecked {
            m.cx = pv(c, m, V_CX) * 16384 / 100;
            m.cy = pv(c, m, V_CY) * 16384 / 100;
            int256 dx = mx(m.cx, 16384 - m.cx);
            int256 dy = mx(m.cy, 16384 - m.cy);
            m.R0 = isqrt2(dx * dx + dy * dy) + 32;
            m.c0 = int256(uint256(Marfa1997Genome.u32(c.rng) % 236));
            vtxGen(c, m);
            m.hc = new int256[](uint256(T) * 3 + 9);
            m.ho = new int256[](uint256(T) * 3 + 9);
        }
    }

    function vtxGen(Ctx memory c, Mod memory m) internal pure {
        unchecked {
            m.D = (301 - pv(c, m, V_SPAS)) * (3000 + r16(c.rng) * 1000 / 65536);
            m.Rv = m.R0;
            m.Th = r16(c.rng) * 64;
            m.f = 0;
            m.alive = true;
            m.idle = 0;
            m.pR = m.Rv;
            m.pTh = m.Th;
        }
    }

    function vtxAdv(Ctx memory c, Mod memory m) internal pure returns (bool) {
        unchecked {
            if (!m.alive) {
                if (++m.idle >= pv(c, m, V_DUR)) vtxGen(c, m);
                return false;
            }
            m.pR = m.Rv;
            m.pTh = m.Th;
            m.Rv -= pv(c, m, V_COLL);
            m.Th = (m.Th + sn(c.S, m.Rv * 651875 / m.D) * pv(c, m, V_AMP) * 2225 / 10000) % M22;
            m.f++;
            if (m.Rv + pv(c, m, V_FG) * 16 < 0) m.alive = false;
            return true;
        }
    }

    function vert(Ctx memory c, Mod memory m, int256 i, int256 R, int256 Th) internal pure returns (int256, int256) {
        unchecked {
            int256 nv = pv(c, m, V_NV);
            int256 a = (nv * 16 - 16 - i) * 65536 / (nv * 16) + (Th >> 6);
            int256 r = R < 0 ? int256(0) : R;
            return (m.cx + sn(c.S, a) * r / 10000, m.cy + cs(c.S, a) * r / 10000);
        }
    }

    function sphAdv(Ctx memory c, Mod memory m) internal pure returns (bool) {
        unchecked {
            m.n++;
            if (m.n % pv(c, m, S_EVERY) != 0) return false;
            m.jit = r16(c.rng) * pv(c, m, S_JIT) / (100 * pv(c, m, S_SYM));
            return true;
        }
    }

    function place(Ctx memory c, Mod memory m, int256 k, int256 f) internal pure returns (int256, int256) {
        unchecked {
            int256 sym = pv(c, m, S_SYM);
            int256 R = (pv(c, m, S_RAD) * 100 + pv(c, m, S_ROSC) * (sn(c.S, m.n * pv(c, m, S_RRATE)) / 100) + pv(c, m, S_RAMT) * f) * 8192 / 10000;
            int256 a = m.n * pv(c, m, S_ROT) + m.jit + (k % sym) * 65536 / sym;
            if (k >= sym) a = -a;
            return (8192 + sn(c.S, a) * R / 10000, 8192 + cs(c.S, a) * R / 10000);
        }
    }

    // =============================================================================================================
    function avg(int256 aud) internal pure returns (int256) {
        unchecked {
            return aud == 1 ? int256(64) : aud == 2 ? int256(27) : aud == 3 ? int256(45) : aud == 4 ? int256(20) : aud == 5 ? int256(18) : aud == 6 ? int256(8) : int256(0);
        }
    }

    function au(int256 v, int256 amt, int256 aud) internal pure returns (int256) {
        unchecked {
            return v * (10000 + amt * avg(aud)) / 10000;
        }
    }

    /// the level drawn by a Cyclic module: sides, radius
    function lev(Ctx memory c, Mod memory m) internal pure returns (int256, int256) {
        unchecked {
            int256 d = pv(c, m, C_DEPTH);
            if (d == 2) return (pv(c, m, C_SIDES3), pv(c, m, C_D3));
            if (d == 1) return (pv(c, m, C_SIDES2), pv(c, m, C_D2));
            return (pv(c, m, C_SIDES), pv(c, m, C_D));
        }
    }

    /// gradient stops for one palette period starting at ph
    function gstops(Ctx memory c, W memory w, int256 ph) internal pure {
        dstops(c, w, ph, 65536 / c.reps);
    }

    /// the step's disc colour bucket and radius; its gradient (defs) on first use, over the disc's palette span
    function discGrad(Ctx memory c, Mod memory m, uint256 i, W memory D_) internal pure returns (int256 qc, int256 R) {
        unchecked {
            qc = colAt(1, m.c0, pv(c, m, S_CRATE), m.n) / m.gQ;
            R = au(pv(c, m, S_SIZE) * 16, pv(c, m, S_AMT), pv(c, m, S_AUD));
            if (c.gq[i * 64 + uint256(qc)]) return (qc, R);
            c.gq[i * 64 + uint256(qc)] = true;
            int256 ph = idxU(qc * m.gQ) + c.off;
            wl(D_, '<radialGradient id="s');
            wid(D_, i, uint256(qc));
            wl8(D_, '">');
            dstops(c, D_, ph, m.gS);
            wl(D_, "</radialGradient>");
        }
    }

    /// gradient stops over the palette span [ph, ph + span]: one at each palette stop inside
    function dstops(Ctx memory c, W memory w, int256 ph, int256 span) internal pure {
        unchecked {
            int256 Pu = 65536 / c.reps;
            wl(w, '<stop offset="0" stop-color="');
            colUEnd(c, w, ph);
            for (int256 j = ph / Pu - 1; j * Pu < ph + span; ++j) {
                for (uint256 k; k < c.su.length; ++k) {
                    int256 p = j * Pu + c.su[k] / c.reps;
                    if (p > ph && p < ph + span) {
                        wl16(w, '<stop offset="');
                        wf3(w, (p - ph) * 1000 / span);
                        wl16(w, '" stop-color="');
                        colUEnd(c, w, p);
                    }
                }
            }
            wl(w, '<stop offset="1" stop-color="');
            colUEnd(c, w, ph + span);
        }
    }

    /// a family's preset data: IDX, VEC, BIND and PAL sections
    struct Data {
        bytes I;
        bytes V;
        bytes B;
        bytes P;
    }

    /// the family blob: four u16 section lengths, then IDX, VEC, BIND, PAL
    function unpack(bytes memory b) internal pure returns (Data memory d) {
        unchecked {
            uint256 o = 8;
            d.I = cut(b, o, uint256(i16(b, 0)));
            o += d.I.length;
            d.V = cut(b, o, uint256(i16(b, 2)));
            o += d.V.length;
            d.B = cut(b, o, uint256(i16(b, 4)));
            o += d.B.length;
            d.P = cut(b, o, uint256(i16(b, 6)));
        }
    }

    function cut(bytes memory b, uint256 o, uint256 n) internal pure returns (bytes memory r) {
        r = new bytes(n);
        assembly ("memory-safe") {
            mcopy(add(r, 32), add(add(b, 32), o), n)
        }
    }

    /// the preset vector (knobs applied), its modules, palette and feedback tables
    function load(Ctx memory c, Marfa1997Genome.G memory g, uint256 li, Data memory dt) internal pure returns (Mod[] memory mods) {
        unchecked {
            uint256 nm;
            uint256 palOff;
            uint256 palN;
            {
                uint256 o = li * 9;
                bytes memory I = dt.I;
                bytes memory V = dt.V;
                bytes memory Bd = dt.B;
                uint256 vo = uint256(int256(uint256(uint8(I[o])) << 8 | uint256(uint8(I[o + 1]))));
                uint256 vl = uint256(uint8(I[o + 2]));
                uint256 bo = uint256(uint8(I[o + 3])) << 8 | uint256(uint8(I[o + 4]));
                uint256 bl = uint256(uint8(I[o + 5]));
                palOff = uint256(uint8(I[o + 6])) << 8 | uint256(uint8(I[o + 7]));
                palN = uint256(uint8(I[o + 8]));
                
                c.v = new int256[](vl);
                for (uint256 i; i < vl; ++i) c.v[i] = i16(V, (vo + i) * 2);
                
                for (uint256 i; i < bl; ++i) {
                    uint256 q = (bo + i) * 4;
                    c.v[uint256(uint8(Bd[q + 1]))] += g.knobs[uint256(uint8(Bd[q]))] * i16(Bd, q + 2);
                }
                nm = uint256(c.v[10]);
                mods = new Mod[](nm);
                uint256 k = 11;
                for (uint256 i; i < nm; ++i) {
                    uint256 t = uint256(c.v[k]);
                    mods[i].t = t;
                    mods[i].o = k + 1;
                    k += 1 + (t == 0 ? 27 : t == 3 ? 16 : t == 4 ? 8 : 14);
                }
            }
            c.rng = Marfa1997Genome.fork(Marfa1997Genome.simStream(g, 0), 0x9517);
            c.reps = c.v[1];
            c.off = (T - 1) * c.v[2];
            c.o.pal(c, palOff, palN);
            c.gsz = int256(c.su.length + 2) * 42 + 90;
            c.zoom = c.v[3];
            c.rot = c.v[4];
            c.fade = c.v[5];
            c.hasFx = c.zoom != 0 || c.rot != 0 || c.fade != 0 ? int256(1) : int256(0);
            for (uint256 i; i < nm; ++i) {
                if (mods[i].t == 4 && pv(c, mods[i], P_ALPHA) > 0 && pv(c, mods[i], P_ALPHA) < 255) c.pa = pv(c, mods[i], P_ALPHA);
            }
            c.anyFade = c.fade != 0 ? c.fade : c.pa;
            c.pw = new int256[](uint256(T) + 1);
            c.kp = new int256[](uint256(T) + 1);
            c.pw[0] = 1000000;
            c.kp[0] = 1000000;
            for (uint256 a = 1; a <= uint256(T); ++a) {
                c.pw[a] = c.pw[a - 1] * (65536 + c.zoom) / 65536;
                c.kp[a] = c.kp[a - 1] * (4096 - c.fade) / 4096 * (255 - c.pa) / 255;
            }
        }
    }

    function run(Marfa1997Genome.G memory g, uint256 li, Ops memory ops, Data memory dt) internal pure returns (bytes memory body, bytes memory defs) {
        unchecked {
            Ctx memory c;
            c.o = ops;
            c.PAL = dt.P;
            c.TAB = g.pal;
            c.S = QS;
            Mod[] memory mods = load(c, g, li, dt);
            uint256 nm = mods.length;
            W memory D_ = wmake(4096);
            W memory pre = wmake(4096);
            for (uint256 i; i < nm; ++i) {
                if (mods[i].t == 4) {
                    c.o.pattern(c, mods[i], pre, D_);
                    break;
                }
            }
            // each stamp module's shape at its radius
            for (uint256 i; i < nm; ++i) if (mods[i].t == 0) c.o.cycDefs(c, mods[i], i, D_);
            for (uint256 i; i < nm; ++i) {
                Mod memory m = mods[i];
                if (m.t == 0) c.o.cycInit(c, m);
                else if (m.t == 1) c.o.bezInit(c, m);
                else if (m.t == 2) c.o.vtxInit(c, m);
                else if (m.t == 3) m.c0 = int256(uint256(Marfa1997Genome.u32(c.rng) % 236));
            }
            budget(c, mods, int256(pre.len + D_.len) - c.xl);
            if (c.hasFx != 0) fxCosts(c, mods);
            c.gq = new bool[](nm * 64);
            for (uint256 i; i < nm; ++i) if (mods[i].t == 2) { c.o.timelines(c, mods); break; }
            for (uint256 i; i < nm; ++i) {
                Mod memory m = mods[i];
                for (uint256 b; b < 6; ++b) m.bk[b] = wmake(256);
            }
            for (int256 n = 1; n <= T; ++n) {
                for (uint256 i; i < nm; ++i) tickMod(c, mods[i], i, n, D_);
            }
            for (uint256 i; i < nm; ++i) if (mods[i].t == 2) c.o.vortex(c, mods[i], i, mn(48 / pv(c, mods[i], V_ARMS), mx(12, 2 * int256(c.su.length) * c.reps)), pre, D_);
            if (c.hasFx != 0) blurDiscs(mods, D_);
            W memory B_ = wmake(16384);
            wbuf(B_, pre);
            for (uint256 b; b < 6; ++b) {
                for (uint256 i; i < nm; ++i) assemble(c, mods[i], i, b, B_, D_);
            }
            body = done(B_);
            defs = done(D_);
        }
    }

    /// (feedback) the per-step costs of rings (disc satellites, depth 1) and swept curve bands; strides again
    function fxCosts(Ctx memory c, Mod[] memory mods) internal pure {
        unchecked {
            for (uint256 i; i < mods.length; ++i) {
                Mod memory m = mods[i];
                if (m.t == 0 && pv(c, m, C_DEPTH) == 1 && pv(c, m, C_SIDES2) == 1) m.need = (T - m.lo + 1) * pv(c, m, C_N) * 96 * (1 + (pv(c, m, C_MIR) != 0 ? int256(1) : int256(0)));
                else if (m.t == 1) m.need = (T - m.lo + 1) * pv(c, m, B_NC) * 176 * (1 + (pv(c, m, B_MIR) != 0 ? int256(1) : int256(0)));
            }
            spread(mods);
        }
    }

    /// (feedback) blurs each age group of discs by its mean age
    function blurDiscs(Mod[] memory mods, W memory D_) internal pure {
        unchecked {
            bool[5] memory fl;
            for (uint256 i; i < mods.length; ++i) {
                Mod memory m = mods[i];
                if (m.t != 3) continue;
                for (uint256 b; b < 6; ++b) {
                    int256 L = mn(4, (T - (m.lo + (2 * (int256(b) - NB + m.nb) + 1) * (T - m.lo + 1) / (2 * m.nb))) / 48);
                    if (m.bk[b].len == 0 || L == 0) continue;
                    if (!fl[uint256(L)]) {
                        fl[uint256(L)] = true;
                        wl16(D_, '<filter id="fl');
                        wu(D_, uint256(L));
                        wb(D_, '"><feGaussianBlur stdDeviation="');
                        wu(D_, uint256(L == 1 ? 3 : L == 2 ? 6 : L == 3 ? 10 : 16));
                        wl16(D_, '"/></filter>');
                    }
                    W memory G = wmake(m.bk[b].len + 64);
                    wl(G, '<g filter="url(#fl');
                    wu(G, uint256(L));
                    wl8(G, ')">');
                    wbuf(G, m.bk[b]);
                    wl8(G, "</g>");
                    m.bk[b] = G;
                }
            }
        }
    }

    /// a Cyclic module's stamp shape (defs)
    function cycDefs(Ctx memory c, Mod memory m, uint256 i, W memory D_) internal pure {
        unchecked {
            (int256 n, int256 d) = lev(c, m);
            int256 R = au(d * 16, pv(c, m, C_AMT), pv(c, m, C_AUD));
            if (n == 1) {
                wl16(D_, '<circle id="q');
                wu(D_, i);
                wl8(D_, '" r="');
                wP(D_, R);
                wl8(D_, '"/>');
            } else if (n == 2) {
                wl16(D_, '<path id="q');
                wu(D_, i);
                wl8(D_, '" d="M0 ');
                wP(D_, -R);
                wl8(D_, "V");
                wP(D_, R);
                wl8(D_, '"/>');
            } else {
                wl16(D_, '<polygon id="q');
                wu(D_, i);
                wl16(D_, '" points="');
                for (int256 v; v < n; ++v) {
                    int256 a = v * 65536 / n;
                    if (v != 0) wch(D_, 32);
                    wP(D_, sn(c.S, a) * R / 10000);
                    wch(D_, 44);
                    wP(D_, cs(c.S, a) * R / 10000);
                }
                wl8(D_, '"/>');
            }
        }
    }

    /// vortex timelines (generation k starts at tick k·step, frames 1..F) and the modules they cull
    function timelines(Ctx memory c, Mod[] memory mods) internal pure {
        unchecked {
            c.ms = mods;
            for (uint256 i; i < mods.length; ++i) {
                Mod memory m = mods[i];
                if (m.t != 2) continue;
                m.vF = (m.R0 + pv(c, m, V_FG) * 16) / pv(c, m, V_COLL) + 1;
                m.vStep = m.vF + mx(1, pv(c, m, V_DUR));
                m.vi = i;
                c.vx = i;
                c.nvt = 1;
            }
            if (c.hasFx != 0) return;
            bool centred = mods[c.vx].cx == 8192 && mods[c.vx].cy == 8192;
            for (uint256 i; i < mods.length; ++i) {
                Mod memory m = mods[i];
                if (m.t == 2 || m.t == 4) continue;
                uint256 k = m.t == 0 ? C_SYM : B_SYM;
                (int256 sym, int256 mir, int256 tile) = m.t == 3 ? (int256(1), pv(c, m, S_MIR), pv(c, m, S_TILE)) : (pv(c, m, k), pv(c, m, k + 1), pv(c, m, k + 2));
                m.cull = tile < 2 && (centred || (sym == 1 && mir == 0));
            }
        }
    }

    /// is an element of module mi painted at tick n at (x, y) painted over by a later vortex frame?
    function hidden(Ctx memory c, uint256 mi, int256 n, int256 x, int256 y) internal pure returns (bool) {
        unchecked {
            Mod memory v = c.ms[c.vx];
            int256 dx = x - v.cx;
            int256 dy = y - v.cy;
            int256 r2 = dx * dx + dy * dy;
            if (r2 >= v.R0 * v.R0) return false;
            int256 coll = pv(c, v, V_COLL);
            int256 fr = (v.R0 - isqrt2(r2) + coll - 1) / coll;
            if (fr < 1 || fr > v.vF) return false;
            int256 t = n < fr ? fr : ((n - fr) / v.vStep) * v.vStep + fr;   // the last pass at or before n (or the first)
            if (t < n || (t == n && v.vi <= mi)) t += v.vStep;
            return t <= T;
        }
    }

    /// per module: the window [lo, T], the stride, the disc gradient sharing
    function budget(Ctx memory c, Mod[] memory mods, int256 used) internal pure {
        unchecked {
            int256 nd;
            int256 nv;
            for (uint256 i; i < mods.length; ++i) {
                uint256 t = mods[i].t;
                if (t == 0 || t == 1 || t == 3) nd++;
                if (t == 2) nv++;
            }
            int256 share = mx(4000, 27500 - used - nv * 9000) / mx(1, nd);
            for (uint256 i; i < mods.length; ++i) {
                Mod memory m = mods[i];
                if (m.t == 2 || m.t == 4) continue;
                m.nb = NB;
                int256 span = T;
                int256 tr = m.t == 0 ? pv(c, m, C_TRAIL) : m.t == 1 ? pv(c, m, B_TRAIL) : int256(0);
                if (tr != 0) span = tr;
                if (c.anyFade != 0) span = mn(span, c.fade != 0 ? 11522 / c.fade : 718 / c.pa);
                else if (c.zoom > 0) span = mn(span, 196608 / c.zoom);
                (int256 cost, int256 bud, int256 steps) = m.t == 0 ? c.o.budCyc(c, m, span, share) : m.t == 1 ? c.o.budBez(c, m, span, share) : c.o.budSph(c, m, span, share);
                m.lo = T - span + 1;
                m.need = steps * cost;
                m.bd = bud;
            }
            spread(mods);
        }
    }

    /// strides from each module's need and budget; the budget a module leaves unused goes to the others, evenly
    function spread(Mod[] memory mods) internal pure {
        unchecked {
            int256 spare;
            int256 short_;
            for (uint256 i; i < mods.length; ++i) {
                Mod memory m = mods[i];
                if (m.need == 0) continue;
                if (m.need < m.bd) spare += m.bd - m.need;
                else short_++;
            }
            spare /= mx(1, short_);
            for (uint256 i; i < mods.length; ++i) {
                Mod memory m = mods[i];   // (a vortex / pattern: need 0, stride 1, unused)
                int256 bb = mx(m.need >= m.bd ? m.bd + spare : m.bd, 1500);
                m.stride = mx(1, (m.need + bb - 1) / bb);
            }
        }
    }

    /// a module's byte budget left after its copies: share − 6 groups × (copies + tiles) × 50 − 6 × 60
    function budLeft(int256 share, int256 sym, int256 mir, int256 tile, int256 nb) internal pure returns (int256) {
        unchecked {
            return share - nb * (sym * (mir + 1) + tile * tile) * 50 - nb * 60;
        }
    }

    /// → (cost per drawn step, budget, steps)
    function budCyc(Ctx memory c, Mod memory m, int256 span, int256 share) internal pure returns (int256, int256, int256) {
        unchecked {
            int256 mir = pv(c, m, C_MIR) != 0 ? int256(1) : int256(0);
            int256 dep = pv(c, m, C_DEPTH);
            m.gQ = 64;
            return (pv(c, m, C_N) * (dep != 0 ? pv(c, m, C_SIDES) : int256(1)) * (dep == 2 ? pv(c, m, C_SIDES2) : int256(1)) * (78 + c.hasFx * 16) * (1 + mir * c.hasFx),
                budLeft(share, pv(c, m, C_SYM), mir, pv(c, m, C_TILE), NB), span);
        }
    }

    function budBez(Ctx memory c, Mod memory m, int256 span, int256 share) internal pure returns (int256, int256, int256) {
        unchecked {
            int256 mir = pv(c, m, B_MIR) != 0 ? int256(1) : int256(0);
            m.gQ = 64;
            m.top = c.hasFx == 0 && pv(c, m, B_TRAIL) == 0 ? NT : int256(0);
            m.nb = NB - m.top;
            return (pv(c, m, B_NC) * 82 * (1 + mir * c.hasFx), budLeft(share, pv(c, m, B_SYM), mir, pv(c, m, B_TILE), NB), span);
        }
    }

    function budSph(Ctx memory c, Mod memory m, int256 span, int256 share) internal pure returns (int256, int256, int256) {
        return budSphX(c, m, span, share, c.hasFx != 0);
    }

    /// (families without feedback)
    function budSphN(Ctx memory c, Mod memory m, int256 span, int256 share) internal pure returns (int256, int256, int256) {
        return budSphX(c, m, span, share, false);
    }

    function budSphX(Ctx memory c, Mod memory m, int256 span, int256 share, bool fx) internal pure returns (int256, int256, int256) {
        unchecked {
            // without feedback a step's discs are one disc's rotations (and mirror): one disc, copied per group
            int256 mir = pv(c, m, S_MIR) != 0 ? int256(1) : int256(0);
            int256 bud;
            if (fx) bud = budLeft(share, 1, 0, pv(c, m, S_TILE), NB);
            else {
                // fewer age groups (the newest ones) when the copies would take over a third of the share
                int256 cc = (pv(c, m, S_SYM) * (mir + 1) + pv(c, m, S_TILE) * pv(c, m, S_TILE)) * 50;
                m.nb = mx(2, mn(NB, share / (3 * cc)));
                bud = budLeft(share, pv(c, m, S_SYM), mir, pv(c, m, S_TILE), m.nb);
            }
            int256 gs = gradSize(c, m);
            int256 rg = mn(3776, span * pv(c, m, S_CRATE));
            int256 G = mx(1, mn(24, bud / (2 * gs)));
            int256 q = mx(64, (rg + G - 1) / G);
            m.gQ = q;
            return (fx ? pv(c, m, S_SYM) * (mir + 1) * 62 : int256(62), bud - (rg / q + 2) * gs, span / pv(c, m, S_EVERY));
        }
    }

    /// a disc's palette span (gS, u units) → its gradient's size (bytes, about)
    function gradSize(Ctx memory c, Mod memory m) internal pure returns (int256) {
        unchecked {
            int256 sp = au(pv(c, m, S_SIZE) * 16, pv(c, m, S_AMT), pv(c, m, S_AUD)) * pv(c, m, S_RING) * 256 / 236;
            m.gS = sp;
            return (sp * c.reps * int256(c.su.length) / 65536 + 3) * 42 + 70;
        }
    }

    /// a Vortex tick: advance, sample the rings (every 8 frames or 1/80 turn of twist)
    function vtxTick(Ctx memory c, Mod memory m, int256 n) internal pure {
        vtxSample(c, m, n, false);
    }

    /// (families with a vortex of at most QV vertices: every frame of those is kept)
    function vtxTickQ(Ctx memory c, Mod memory m, int256 n) internal pure {
        vtxSample(c, m, n, pv(c, m, V_NV) <= QV);
    }

    function vtxSample(Ctx memory c, Mod memory m, int256 n, bool every) internal pure {
        unchecked {
            if (!vtxAdv(c, m)) return;
            if (m.f == 1) {
                if (m.hcn > 3) {
                    (m.ho, m.hc) = (m.hc, m.ho);
                    m.hon = m.hcn;
                }
                m.hc[0] = 0;
                m.hc[1] = m.pR;
                m.hc[2] = m.pTh;
                m.hcn = 3;
            }
            uint256 L = m.hcn;
            int256 dT = absi((((m.Th - m.hc[L - 1] + 6291456) % 4194304) - 2097152));
            if (every || m.f - m.hc[L - 3] >= 8 || dT >= 52429 || !m.alive || n == T) {
                m.hc[L] = m.f;
                m.hc[L + 1] = m.Rv;
                m.hc[L + 2] = m.Th;
                m.hcn = L + 3;
            }
        }
    }

    function tickMod(Ctx memory c, Mod memory m, uint256 i, int256 n, W memory D_) internal pure {
        unchecked {
            if (m.t == 4) return;
            if (m.t == 2) {
                c.o.vtxTick(c, m, n);
                return;
            }
            if (m.t == 0) c.o.cycAdv(c, m);
            else if (m.t == 1) m.n++;
            else if (!c.o.sphAdv(c, m)) return;
            if (n < m.lo || ((T - n) / (m.t == 3 ? pv(c, m, S_EVERY) : int256(1))) % m.stride != 0) return;
            int256 a = T - n;
            int256[4] memory X = c.X;   // scale ×1000, angle, cos, sin
            (X[0], X[1], X[2], X[3]) = (c.pw[uint256(a)] / 1000, a * c.rot, cs(c.S, a * c.rot), sn(c.S, a * c.rot));
            int256 r = (T - n) / m.stride;   // (curves: the drawn step's rank, newest 0)
            W memory s = m.bk[uint256(r < m.top ? NB - 1 - r : NB - m.top - m.nb + mn(m.nb - 1, (n - m.lo) * m.nb / (T - m.lo + 1)))];
            int256 mirs = c.hasFx != 0 && m.t != 3 ? pv(c, m, m.t == 0 ? C_MIR : B_MIR) : int256(0);
            for (int256 mi; mi <= mirs; ++mi) {
                if (m.t == 0) c.o.emitCyc(c, m, i, mi, X, s);
                else if (m.t == 1) c.o.emitBez(c, m, mi, X, s);
                else c.o.emitSph(c, m, i, X, s, D_);
            }
        }
    }

    function tx(int256 x, int256 y, int256[4] memory X) internal pure returns (int256) {
        unchecked {
            return 8192 + ((x - 8192) * X[2] - (y - 8192) * X[3]) / 10000 * X[0] / 1000;
        }
    }

    function ty(int256 x, int256 y, int256[4] memory X) internal pure returns (int256) {
        unchecked {
            return 8192 + ((x - 8192) * X[3] + (y - 8192) * X[2]) / 10000 * X[0] / 1000;
        }
    }

    /// per-object emission state of a Cyclic step (keeps the stack small)
    struct E {
        uint256 i;
        int256 mi;
        int256 r;
        int256 kc;
        int256 N;
        int256 ns;
        int256 L0;
        int256 L1;
        bool strokeCol;
        bool streak;
        bool tube;
        int256 R;
    }

    function emitCyc(Ctx memory c, Mod memory m, uint256 i, int256 mi, int256[4] memory X, W memory s) internal pure {
        unchecked {
            if (c.hasFx != 0 && pv(c, m, C_DEPTH) == 1 && pv(c, m, C_SIDES2) == 1) return ring(c, m, mi, X, s);
            E memory e = c.e;
            (e.L0, e.L1) = lev(c, m);
            e.i = i;
            e.mi = mi;
            e.N = pv(c, m, C_N);
            e.strokeCol = pv(c, m, C_MODE) == 2 || e.L0 == 2;
            e.streak = e.L0 == 1 && (c.hasFx != 0 || e.L1 < 4);
            e.R = au(e.L1 * 16, pv(c, m, C_AMT), pv(c, m, C_AUD));
        e.tube = pv(c, m, C_MODE) != 2 && e.L0 != 2;
        for (e.r = 0; e.r < e.N; ++e.r) {
            e.kc = cycCol(c, m, e.r);
                uint256 k = stamps(c, m, e.r);
                e.ns = int256(k / 5);
                for (uint256 j; j < k; j += 5) emitStamp(c, m, e, X, s, m.so, j);
            }
        }
    }

    function emitStamp(Ctx memory c, Mod memory m, E memory e, int256[4] memory X, W memory s, int256[] memory q, uint256 j) internal pure {
        unchecked {
            int256 x = e.mi != 0 ? 16384 - q[j] : q[j];
            int256 y = q[j + 1];
            int256 X1 = tx(x, y, X);
            int256 Y1 = ty(x, y, X);
            uint256 li = uint256(((e.mi * e.N + e.r) * e.ns + int256(j / 5)) * 2);
            int256 lx = m.last[li];
            int256 ly = m.last[li + 1];
            bool far = lx != 0 && (X1 - lx) * (X1 - lx) + (Y1 - ly) * (Y1 - ly) > e.R * e.R * 36 / 25;
            m.last[li] = X1;
            m.last[li + 1] = Y1;
            if (m.cull && c.o.hidden(c, e.i, m.n, x, y)) return;
            if (e.streak) return seg(c, s, lx, ly, X1, Y1, e.R * 2 * X[0] / 1000, e.kc);
            if (e.tube && far && c.hasFx == 0) return seg(c, s, lx, ly, X1, Y1, 2 * e.R, e.kc);
            int256 g = (e.mi != 0 ? -q[j + 3] : q[j + 3]) - X[1];
            if (c.hasFx == 0) wStamp(s, e.i, x, y, g, (e.L0 == 1) ? 0 : 1, e.strokeCol);
            else {
                wl16(s, '<use href="#q');
                wu(s, e.i);
                wl(s, '" transform="translate(');
                wPP(s, X1, Y1);
                wch(s, 41);
                if (e.L0 != 1) {
                    wl8(s, "rotate(");
                    wdeg(s, g);
                    wch(s, 41);
                }
                wl8(s, "scale(");
                wf3(s, X[0]);
                wch(s, 41);
                if (e.strokeCol) wl16(s, '" stroke="');
                else wl8(s, '" fill="');
            }
            colEnd(c, s, e.kc);
        }
    }

    /// emitCyc for families without feedback (the feedback branches folded away)
    function emitCycN(Ctx memory c, Mod memory m, uint256 i, int256 mi, int256[4] memory X, W memory s) internal pure {
        unchecked {
            E memory e = c.e;
            (e.L0, e.L1) = lev(c, m);
            e.i = i;
            e.mi = mi;
            e.N = pv(c, m, C_N);
            e.strokeCol = pv(c, m, C_MODE) == 2 || e.L0 == 2;
            e.streak = e.L0 == 1 && (false || e.L1 < 4);
            e.R = au(e.L1 * 16, pv(c, m, C_AMT), pv(c, m, C_AUD));
        e.tube = pv(c, m, C_MODE) != 2 && e.L0 != 2;
        for (e.r = 0; e.r < e.N; ++e.r) {
            e.kc = cycCol(c, m, e.r);
                uint256 k = stamps(c, m, e.r);
                e.ns = int256(k / 5);
                for (uint256 j; j < k; j += 5) emitStampN(c, m, e, X, s, m.so, j);
            }
        }
    }

    function emitStampN(Ctx memory c, Mod memory m, E memory e, int256[4] memory X, W memory s, int256[] memory q, uint256 j) internal pure {
        unchecked {
            int256 x = e.mi != 0 ? 16384 - q[j] : q[j];
            int256 y = q[j + 1];
            int256 X1 = tx(x, y, X);
            int256 Y1 = ty(x, y, X);
            uint256 li = uint256(((e.mi * e.N + e.r) * e.ns + int256(j / 5)) * 2);
            int256 lx = m.last[li];
            int256 ly = m.last[li + 1];
            bool far = lx != 0 && (X1 - lx) * (X1 - lx) + (Y1 - ly) * (Y1 - ly) > e.R * e.R * 36 / 25;
            m.last[li] = X1;
            m.last[li + 1] = Y1;
            if (m.cull && c.o.hidden(c, e.i, m.n, x, y)) return;
            if (e.streak) return seg(c, s, lx, ly, X1, Y1, e.R * 2 * X[0] / 1000, e.kc);
            if (e.tube && far && true) return seg(c, s, lx, ly, X1, Y1, 2 * e.R, e.kc);
            int256 g = (e.mi != 0 ? -q[j + 3] : q[j + 3]) - X[1];
            wStamp(s, e.i, x, y, g, (e.L0 == 1) ? 0 : 1, e.strokeCol);
            colEnd(c, s, e.kc);
        }
    }

    function seg(Ctx memory c, W memory s, int256 lx, int256 ly, int256 x, int256 y, int256 w, int256 kc) internal pure {
        wl16(s, '<path d="M');
        if (lx != 0) {
            wPP(s, lx, ly);
            wch(s, 76);
        }
        wPP(s, x, y);
        if (lx == 0) wl8(s, "h0");
        wl16(s, '" stroke-width="');
        wP(s, w);
        wl16(s, '" stroke="');
        colEnd(c, s, kc);
    }

    /// (feedback) disc satellites spinning: between drawn steps they sweep their circle — a ring about each object
    function ring(Ctx memory c, Mod memory m, int256 mi, int256[4] memory X, W memory s) internal pure {
        unchecked {
            cycPos(c, m);
            int256 R = au(pv(c, m, C_D2) * 16, pv(c, m, C_AMT), pv(c, m, C_AUD));
            int256 N = pv(c, m, C_N);
            for (int256 r; r < N; ++r) {
                int256 x = m.cur[uint256(2 * r)];
                if (mi != 0) x = 16384 - x;
                int256 y = m.cur[uint256(2 * r + 1)];
                wl16(s, '<circle cx="');
                wP(s, tx(x, y, X));
                wl8(s, '" cy="');
                wP(s, ty(x, y, X));
                wl8(s, '" r="');
                wP(s, pv(c, m, C_D) * 16 * X[0] / 1000);
                wl16(s, '" stroke-width="');
                wP(s, R * 2 * X[0] / 1000);
                wl16(s, '" stroke="');
                col(c, s, cycCol(c, m, r));
                wl16(s, '" fill="none"/>');
            }
        }
    }

    function emitBez(Ctx memory c, Mod memory m, int256 mi, int256[4] memory X, W memory s) internal pure {
        unchecked {
            uint256 nc = uint256(pv(c, m, B_NC));
            if (c.hasFx != 0) return band(c, m, mi, X, s, nc);
            for (uint256 k; k < nc; ++k) {
                wl16(s, '<path d="M');
                for (uint256 v; v < 8; v += 2) {
                    if (v == 2) wch(s, 67);
                    else if (v != 0) wch(s, 32);
                    int256 x = bezPt(c, m, k, v);
                    if (mi != 0) x = 16384 - x;
                    int256 y = bezPt(c, m, k, v + 1);
                    wPP(s, tx(x, y, X), ty(x, y, X));
                }
                wl16(s, '" stroke="');
                colEnd(c, s, (colAt(pv(c, m, B_CMODE), m.c0, pv(c, m, B_CRATE), m.n) + int256(k) * pv(c, m, B_COFS) * 16) % 3776);
            }
        }
    }

    /// (feedback) a curve smears outwards as it ages: filled between its image now and its image one stride older
    function band(Ctx memory c, Mod memory m, int256 mi, int256[4] memory X, W memory s, uint256 nc) internal pure {
        unchecked {
            int256 b = mn(T, T - m.n + m.stride);
            int256[4] memory Y = c.Y;
            (Y[0], Y[1], Y[2], Y[3]) = (c.pw[uint256(b)] / 1000, b * c.rot, cs(c.S, b * c.rot), sn(c.S, b * c.rot));
            int256 marg = pv(c, m, B_MARG) * 16;
            int256 vn = pv(c, m, B_V) * m.n;
            int256[] memory pts = m.pts;
            for (uint256 k; k < nc; ++k) {
                int256[16] memory p = c.BP;   // per control point: x, y now, x, y one stride older (all 16 set below)
                bool far;
                for (uint256 v; v < 8; v += 2) {
                    uint256 j = 2 * (k * 8 + v);
                    int256 x = refl(pts[j] + pts[j + 1] * vn, -marg, 16384 + marg);
                    if (mi != 0) x = 16384 - x;
                    int256 y = refl(pts[j + 2] + pts[j + 3] * vn, -marg, 16384 + marg);
                    p[2 * v] = tx(x, y, X);
                    p[2 * v + 1] = ty(x, y, X);
                    p[2 * v + 2] = tx(x, y, Y);
                    p[2 * v + 3] = ty(x, y, Y);
                }
                for (uint256 j; j < 16; ++j) if (absi(p[j] - 8192) > FAR) far = true;
                if (far) continue;
                wl16(s, '<path d="M');
                for (uint256 k; k < 8; ++k) {
                    // k < 4: point k now (separators: none, C, space, space); k ≥ 4: point 7 − k one stride older (L, C, …)
                    uint256 v = k < 4 ? 4 * k : 4 * (7 - k) + 2;
                    uint256 sep = k < 4 ? (k == 1 ? 67 : k > 1 ? 32 : 0) : (k == 4 ? 76 : k == 5 ? 67 : 32);
                    if (sep != 0) wch(s, uint8(sep));
                    wPPf(s, p[v], p[v + 1]);
                }
                int256 kc = (colAt(pv(c, m, B_CMODE), m.c0, pv(c, m, B_CRATE), m.n) + int256(k) * pv(c, m, B_COFS) * 16) % 3776;
                wl16(s, 'Z" fill="');
                col(c, s, kc);
                wl16(s, '" stroke="');
                colEnd(c, s, kc);
            }
        }
    }

    function emitSph(Ctx memory c, Mod memory m, uint256 i, int256[4] memory X, W memory s, W memory D_) internal pure {
        unchecked {
            (int256 qc, int256 R) = discGrad(c, m, i, D_);
            if (c.hasFx != 0) R = R * (512 + mn(T - m.n, 384)) / 512;   // older discs drawn larger
            int256 nd = c.hasFx != 0 ? pv(c, m, S_SYM) * (pv(c, m, S_MIR) != 0 ? int256(2) : int256(1)) : int256(1);
            for (int256 k; k < nd; ++k) disc(c, m, i, X, s, k, R * X[0] / 1000, qc);
        }
    }

    function disc(Ctx memory c, Mod memory m, uint256 i, int256[4] memory X, W memory s, int256 k, int256 r, int256 qc) internal pure {
        unchecked {
            (int256 x, int256 y) = place(c, m, k, avg(pv(c, m, S_AUD)));
            if (m.cull && c.o.hidden(c, i, m.n, x, y)) return;
            wl16(s, '<circle cx="');
            wP(s, tx(x, y, X));
            wl8(s, '" cy="');
            wP(s, ty(x, y, X));
            wl8(s, '" r="');
            wP(s, r);
            wl16(s, '" fill="url(#s');
            wu(s, i);
            wch(s, 95);
            wu(s, uint256(qc));
            wl8(s, ')"/>');
        }
    }

    /// age group b of module i → body (with symmetry copies; tiled through defs)
    function assemble(Ctx memory c, Mod memory m, uint256 i, uint256 b, W memory B_, W memory D_) internal pure {
        unchecked {
            if (m.t == 2 || m.t == 4 || m.bk[b].len == 0) return;
            int256 a = int256(b) >= NB - m.top ? (NB - 1 - int256(b)) * m.stride : T - (m.lo + (2 * (int256(b) - NB + m.top + m.nb) + 1) * (T - m.lo + 1) / (2 * m.nb));
            W memory G = wmake(m.bk[b].len + 1024);
            wl8(G, '<g id="b');
            wid(G, i, b);
            wch(G, 34);
            if (c.anyFade != 0) {
                wl16(G, ' opacity="');
                wf3(G, mx(30, c.kp[uint256(a)] / 1000));
                wch(G, 34);
            }
            (int256 sym, int256 mir, int256 tile, int256 tt) = m.t == 0 ? c.o.attrCyc(c, m, G, a) : m.t == 1 ? c.o.attrBez(c, m, G, a) : (pv(c, m, S_SYM), pv(c, m, S_MIR), pv(c, m, S_TILE), pv(c, m, S_TT));
            wch(G, 62);
            wbuf(G, m.bk[b]);
            wl8(G, "</g>");
            if (m.t != 3 || c.hasFx == 0) copies(G, i, b, sym, c.hasFx != 0 ? int256(0) : mir);
            if (tile < 2) {
                wbuf(B_, G);
                return;
            }
            wl16(D_, '<g id="ab');
            wid(D_, i, b);
            wl8(D_, '">');
            wbuf(D_, G);
            wl8(D_, "</g>");
            c.o.tiles(i, b, tile, tt, B_);
        }
    }

    /// "<i>_<b>"
    function wid(W memory w, uint256 i, uint256 b) internal pure {
        wu(w, i);
        wch(w, 95);
        wu(w, b);
    }

    /// a Cyclic group's attributes → (sym, mir, tile, tile type)
    function attrCyc(Ctx memory c, Mod memory m, W memory G, int256) internal pure returns (int256, int256, int256, int256) {
        unchecked {
            (int256 n, ) = lev(c, m);
            if (n == 1 || (pv(c, m, C_MODE) != 2 && n != 2)) wl(G, ' stroke-linecap="round"');
            if (n == 2) wl(G, ' stroke-width="2.1"');
            else if (pv(c, m, C_MODE) == 0 && n > 2) wb(G, ' stroke="#000" stroke-width="1.6"');
            else if (pv(c, m, C_MODE) == 2 && n > 2) wl(G, ' fill="none" stroke-width="2.1"');
            // outlined single-sided discs: heavier stroke
            else if (pv(c, m, C_MODE) == 2 && n == 1) wl(G, ' stroke-width="4"');
            return (pv(c, m, C_SYM), pv(c, m, C_MIR), pv(c, m, C_TILE), pv(c, m, C_TT));
        }
    }

    function attrBez(Ctx memory c, Mod memory m, W memory G, int256 a) internal pure returns (int256, int256, int256, int256) {
        unchecked {
            wl(G, ' fill="none" stroke-width="');
            // curves never erased, drawn every stride-th step: widened to close the gaps between them
            int256 gap = c.hasFx == 0 && pv(c, m, B_TRAIL) == 0 && m.stride > 1 ? m.stride * pv(c, m, B_V) * 65 : int256(0);
            wf3(G, mx(mx(600, au(pv(c, m, B_W) * 977, pv(c, m, B_AMT), pv(c, m, B_AUD)) * (c.pw[uint256(a)] / 1000) / 1000), gap));
            wl(G, '" stroke-linecap="round"');
            return (pv(c, m, B_SYM), pv(c, m, B_MIR), pv(c, m, B_TILE), pv(c, m, B_TT));
        }
    }

    /// uses of the assembled group ab<i>_<b>, scaled into each tile cell
    function tiles(uint256 i, uint256 b, int256 tile, int256 tt, W memory B_) internal pure {
        unchecked {
            int256 k = 1000 / tile;
            for (int256 x; x < tile; ++x) {
                for (int256 y; y < tile; ++y) {
                    if ((tt == 1 && (x + y) % 2 != 0) || (tt == 2 && (x + y) % 2 == 0)) continue;
                    bool fx_ = x % 2 != 0 && tt != 3 && tt != 4;
                    bool fy_ = y % 2 != 0 && tt != 3 && tt != 5;
                    wl16(B_, '<use href="#ab');
                    wid(B_, i, b);
                    wl(B_, '" transform="matrix(');
                    wf3(B_, fx_ ? -k : k);
                    wl8(B_, " 0 0 ");
                    wf3(B_, fy_ ? -k : k);
                    wch(B_, 32);
                    wf3(B_, (x + (fx_ ? int256(1) : int256(0))) * 1000000 / tile);
                    wch(B_, 32);
                    wf3(B_, (y + (fy_ ? int256(1) : int256(0))) * 1000000 / tile);
                    wl8(B_, ')"/>');
                }
            }
        }
    }

    function copies(W memory w, uint256 i, uint256 b, int256 sym, int256 mir) internal pure {
        unchecked {
            for (int256 k; k < sym; ++k) {
                for (int256 q = k != 0 ? int256(0) : int256(1); q <= (mir != 0 ? int256(1) : int256(0)); ++q) {
                    wl16(w, '<use href="#b');
                    wid(w, i, b);
                    wl16(w, '" transform="');
                    if (k != 0) {
                        wl8(w, "rotate(");
                        wf3(w, k * 360000 / sym);
                        wl16(w, " 500 500)");
                    }
                    if (q != 0) wl(w, "matrix(-1 0 0 1 1000 0)");
                    wl8(w, '"/>');
                }
            }
        }
    }

    function pattern(Ctx memory c, Mod memory m, W memory pre, W memory D_) internal pure {
        unchecked {
            int256 rot = pv(c, m, P_ROT) + (T - 1) * pv(c, m, P_SPIN);
            int256 shape = pv(c, m, P_SHAPE);
            int256 sides = pv(c, m, P_SIDES);
            int256 dens = pv(c, m, P_DENS);
            int256 ph = idxU(pv(c, m, P_C0) * 16) + c.off;
            int256 Lp = 37760000 / (dens * 1024 * c.reps);
            if (shape == 0) {
                wb(D_, '<radialGradient id="pg" gradientUnits="userSpaceOnUse" cx="500" cy="500" r="');
                wn1(D_, Lp);
                wl(D_, '" spreadMethod="repeat">');
                gstops(c, D_, ph);
                wl(D_, "</radialGradient>");
                wb(pre, '<rect width="1000" height="1000" fill="url(#pg)"/>');
                return;
            }
            if (shape == 1) {
                int256 h = cs(c.S, 32768 / sides);
                wb(D_, '<linearGradient id="pg" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="');
                wn1(D_, Lp * h / 10000);
                wl(D_, '" spreadMethod="repeat">');
                gstops(c, D_, ph);
                wl(D_, "</linearGradient>");
                int256 w = 1500 * sn(c.S, 32768 / sides + 400) / cs(c.S, 32768 / sides + 400);
                wl(D_, '<polygon id="ps" points="0,0 ');
                wn0(D_, -w);
                wl8(D_, ",1500 ");
                wn0(D_, w);
                wl8(D_, ',1500"/>');
                wb(pre, '<g transform="translate(500 500)">');
                for (int256 k; k < sides; ++k) {
                    wl(pre, '<use href="#ps" transform="');
                    wl8(pre, "rotate(");
                    wdeg(pre, rot + 32768 / sides + k * 65536 / sides);
                    wl(pre, ')" fill="url(#pg)"/>');
                }
                wl8(pre, "</g>");
                return;
            }
            rose(c, m, rot, sides, dens, pre, D_);
        }
    }

    /// one edge of the vortex band polygon (e = 0 out along the lower colour edge, 1 back along the upper one)
    function band(Ctx memory c, Mod memory m, W memory D_, int256[] memory g, uint256 len, uint256 e, int256 de, int256 K, int256 dir, int256 ccs) internal pure {
        unchecked {
            // Vertex vi of ring (R, Th) is centre + (sin, cos)(a)·max(R, 0)/10⁴, written relative to the centre in whole
            // svg units. |R| ≤ R0 < 24,000 keeps a point within 1 + 2 × 6 + 1 bytes; the reservation adds 32 bytes for
            // the trailing word store.
            bytes memory S = c.S;
            int256 nv16 = pv(c, m, V_NV) * 16;
            int256 base = -dir * m.c0 * 16;
            wres(D_, (len / 3 + 1) * 16 + 64);
            bytes memory d = D_.data;
            uint256 o = D_.len;
            assembly ("memory-safe") {
                function tb9(sp9, i9) -> v9 {
                    let q9 := and(i9, 511)
                    if gt(q9, 256) { q9 := sub(512, q9) }
                    v9 := signextend(1, shr(240, mload(add(sp9, mul(q9, 2)))))
                    if gt(i9, 511) { v9 := sub(0, v9) }
                }
                function sn9(sp9, a9) -> r9 {
                    a9 := smod(a9, 65536)
                    if slt(a9, 0) { a9 := add(a9, 65536) }
                    let i9 := shr(6, a9)
                    let s9 := tb9(sp9, i9)
                    r9 := add(s9, sdiv(mul(sub(tb9(sp9, and(add(i9, 1), 1023)), s9), and(a9, 63)), 64))
                }
                function dg9(p9, a9) -> e9 {
                    switch lt(a9, 10000)
                    case 1 {
                        let n9 := add(add(1, gt(a9, 9)), add(gt(a9, 99), gt(a9, 999)))
                        let w9 := or(or(shl(24, add(48, div(a9, 1000))), shl(16, add(48, mod(div(a9, 100), 10)))), or(shl(8, add(48, mod(div(a9, 10), 10))), add(48, mod(a9, 10))))
                        mstore(p9, shl(sub(256, shl(3, n9)), w9))
                        e9 := add(p9, n9)
                    }
                    default {
                        let n9 := 1
                        for { let z9 := div(a9, 10) } z9 { z9 := div(z9, 10) } { n9 := add(n9, 1) }
                        e9 := add(p9, n9)
                        let i9 := e9
                        for {} 1 {} {
                            i9 := sub(i9, 1)
                            mstore8(i9, add(48, mod(a9, 10)))
                            a9 := div(a9, 10)
                            if iszero(a9) { break }
                        }
                    }
                }
                function n09(p9, v9) -> e9 {
                    if slt(v9, 0) { mstore8(p9, 45) p9 := add(p9, 1) v9 := sub(0, v9) }
                    e9 := dg9(p9, v9)
                }
                let sp := add(S, 32)
                let gp := add(g, 32)
                let b0 := add(d, 32)
                let p := add(b0, o)
                for { let qq := 0 } lt(qq, len) { qq := add(qq, 3) } {
                    let q := qq
                    if e { q := sub(sub(len, 3), qq) }
                    let gq := add(gp, mul(q, 32))
                    let vi := add(sdiv(mul(sub(base, mul(mload(gq), ccs)), 256), K), de)
                    let a := add(sdiv(mul(sub(sub(nv16, 16), vi), 65536), nv16), sar(6, mload(add(gq, 64))))
                    let r := mload(add(gq, 32))
                    if slt(r, 0) { r := 0 }
                    let dx := sdiv(mul(sn9(sp, a), r), 10000)
                    let dy := sdiv(mul(sn9(sp, add(a, 16384)), r), 10000)
                    if or(e, qq) { mstore8(p, 32) p := add(p, 1) }
                    p := n09(p, sdiv(mul(dx, 125), 2048))
                    mstore8(p, 44)
                    p := n09(add(p, 1), sdiv(mul(dy, 125), 2048))
                }
                o := sub(p, b0)
            }
            D_.len = o;
        }
    }

    function vortex(Ctx memory c, Mod memory m, uint256 mi, int256 NBv, W memory pre, W memory D_) internal pure {
        for (uint256 gi; gi < 2; ++gi) vgen(c, m, mi, NBv, gi, pre, D_);
    }

    /// (families with a vortex of at most QV vertices)
    function vortexAny(Ctx memory c, Mod memory m, uint256 mi, int256 NBv, W memory pre, W memory D_) internal pure {
        if (pv(c, m, V_NV) <= QV) vortexQ(c, m, NBv, pre);
        else vortex(c, m, mi, NBv, pre, D_);
    }

    /// a vortex of few vertices: its flat quads in NBv colour bands (one path each); the current generation's frames,
    /// then the previous one's inside them; frames merged by m.stride (colour band width m.gQ)
    function vortexQ(Ctx memory c, Mod memory m, int256 NBv, W memory pre) internal pure {
        unchecked {
            int256 Wd = 3776 / NBv;
            int256 F = (m.R0 + pv(c, m, V_FG) * 16) / pv(c, m, V_COLL) + 1;
            m.gQ = Wd;
            m.stride = mx(mx(1, Wd / mx(1, absi(pv(c, m, V_CCS)))), (pv(c, m, V_NV) * F * 30 + 7999) / 8000);
            W[] memory pb = new W[](uint256(NBv) + 1);
            int256 fc = m.hc[m.hcn - 3];
            qgen(c, m, pb, m.hc, 0, fc);
            if (m.hon > 3 && m.ho[m.hon - 3] > fc) qgen(c, m, pb, m.ho, fc, m.ho[m.hon - 3]);
            wl(pre, '<g stroke-width="1.5">');
            for (uint256 b; b <= uint256(NBv); ++b) {
                if (pb[b].len == 0) continue;
                wl16(pre, '<path fill="');
                col(c, pre, int256(b) * Wd + Wd / 2);
                wl16(pre, '" stroke="');
                col(c, pre, int256(b) * Wd + Wd / 2);
                wl8(pre, '" d="');
                wbuf(pre, pb[b]);
                wl8(pre, '"/>');
            }
            wl8(pre, "</g>");
        }
    }

    /// the quads of one generation (ring list g) from frame a to z, into the colour bands pb
    function qgen(Ctx memory c, Mod memory m, W[] memory pb, int256[] memory g, int256 a, int256 z) internal pure {
        unchecked {
            if (a >= z) return;
            int256 nv = pv(c, m, V_NV);
            int256 dir = pv(c, m, V_DIR);
            int256 arms = pv(c, m, V_ARMS);
            int256 ccs = pv(c, m, V_CCS);
            // frame rows of vertices 0..nv (x, y): A at f0, B at f1
            int256[] memory A = new int256[](uint256(2 * nv + 2));
            int256[] memory B = new int256[](uint256(2 * nv + 2));
            vrow(c, m, A, nv, g, a);
            for (int256 f0 = a; f0 < z;) {
                int256 f1 = mn(z, f0 + m.stride);
                int256 fm = (f0 + 1 + f1) / 2;
                vrow(c, m, B, nv, g, f1);
                for (int256 i; i < nv; ++i) {
                    int256 v = (m.c0 * 16 + dir * (i * arms * 3776 / nv + fm * ccs)) % 3776;
                    if (v < 0) v += 3776;
                    W memory w = pb[uint256(v / m.gQ)];
                    if (w.data.length == 0) {
                        w = wmake(1024);
                        pb[uint256(v / m.gQ)] = w;
                    }
                    wQuad(w, A, B, uint256(i));
                }
                (A, B) = (B, A);
                f0 = f1;
            }
        }
    }

    /// the vertices 0..nv of ring frame f: vert(i·16, R, Th) of the frame
    function vrow(Ctx memory c, Mod memory m, int256[] memory R, int256 nv, int256[] memory g, int256 f) internal pure {
        unchecked {
            int256 Rf = g[uint256(3 * f + 1)];
            int256 Tf = g[uint256(3 * f + 2)];
            for (int256 i; i <= nv; ++i) {
                (int256 x, int256 y) = vert(c, m, i * 16, Rf, Tf);
                R[uint256(2 * i)] = x;
                R[uint256(2 * i + 1)] = y;
            }
        }
    }

    function qpt(Ctx memory c, Mod memory m, W memory w, int256 i, int256[] memory g, int256 f) internal pure {
        unchecked {
            (int256 x, int256 y) = vert(c, m, i * 16, g[uint256(3 * f + 1)], g[uint256(3 * f + 2)]);
            wPi2(w, x, y);
        }
    }

    /// one generation's band polygon (defs) and its rotated copies, one per band (pre)
    function vgen(Ctx memory c, Mod memory m, uint256 mi, int256 NBv, uint256 gi, W memory pre, W memory D_) internal pure {
        unchecked {
            uint256 len = gi != 0 ? m.hcn : m.hon;
            if (len < 6) return;
            int256 K = pv(c, m, V_ARMS) * 3776 * 16 / pv(c, m, V_NV);
            int256 di = (3776 / NBv) * 256 / (K * pv(c, m, V_DIR));
            wl16(D_, '<polygon id="v');
            wu(D_, mi);
            wu(D_, gi);
            wl16(D_, '" points="');
            for (uint256 e; e < 2; ++e) band(c, m, D_, gi != 0 ? m.hc : m.ho, len, e, (int256(e) * di), K, pv(c, m, V_DIR), pv(c, m, V_CCS));
            wl8(D_, '"/>');
            wl(pre, '<g transform="translate(');
            wPP(pre, m.cx, m.cy);
            wl8(pre, ')">');
            int256 nb = NBv * pv(c, m, V_ARMS);
            for (int256 k; k < nb; ++k) {
                wl16(pre, '<use href="#v');
                wu(pre, mi);
                wu(pre, gi);
                wch(pre, 34);
                if (k != 0) {
                    wl(pre, ' transform="rotate(');
                    wf3(pre, k * di * 22500 / pv(c, m, V_NV));
                    wl8(pre, ')"');
                }
                wl8(pre, ' fill="');
                colEnd(c, pre, k * (3776 / NBv) + (3776 / NBv) / 2);
            }
            wl8(pre, "</g>");
        }
    }

    // ---- the same pipeline for families without feedback (zoom / rotate / fade folded away) ----
    function runN(Marfa1997Genome.G memory g, uint256 li, Ops memory ops, Data memory dt) internal pure returns (bytes memory body, bytes memory defs) {
        unchecked {
            Ctx memory c;
            c.o = ops;
            c.PAL = dt.P;
            c.TAB = g.pal;
            c.S = QS;
            Mod[] memory mods = load(c, g, li, dt);
            uint256 nm = mods.length;
            W memory D_ = wmake(4096);
            W memory pre = wmake(4096);
            for (uint256 i; i < nm; ++i) {
                if (mods[i].t == 4) {
                    c.o.pattern(c, mods[i], pre, D_);
                    break;
                }
            }
            // each stamp module's shape at its radius
            for (uint256 i; i < nm; ++i) if (mods[i].t == 0) c.o.cycDefs(c, mods[i], i, D_);
            for (uint256 i; i < nm; ++i) {
                Mod memory m = mods[i];
                if (m.t == 0) c.o.cycInit(c, m);
                else if (m.t == 1) c.o.bezInit(c, m);
                else if (m.t == 2) c.o.vtxInit(c, m);
                else if (m.t == 3) m.c0 = int256(uint256(Marfa1997Genome.u32(c.rng) % 236));
            }
            budget(c, mods, int256(pre.len + D_.len) - c.xl);
            c.gq = new bool[](nm * 64);
            for (uint256 i; i < nm; ++i) if (mods[i].t == 2) { c.o.timelines(c, mods); break; }
            for (uint256 i; i < nm; ++i) {
                Mod memory m = mods[i];
                for (uint256 b; b < 6; ++b) m.bk[b] = wmake(256);
            }
            for (int256 n = 1; n <= T; ++n) {
                for (uint256 i; i < nm; ++i) tickModN(c, mods[i], i, n, D_);
            }
            for (uint256 i; i < nm; ++i) if (mods[i].t == 2) c.o.vortex(c, mods[i], i, mn(48 / pv(c, mods[i], V_ARMS), mx(12, 2 * int256(c.su.length) * c.reps)), pre, D_);
            W memory B_ = wmake(16384);
            wbuf(B_, pre);
            for (uint256 b; b < 6; ++b) {
                for (uint256 i; i < nm; ++i) assemble(c, mods[i], i, b, B_, D_);
            }
            body = done(B_);
            defs = done(D_);
        }
    }

    function tickModN(Ctx memory c, Mod memory m, uint256 i, int256 n, W memory D_) internal pure {
        unchecked {
            if (m.t == 4) return;
            if (m.t == 2) {
                c.o.vtxTick(c, m, n);
                return;
            }
            if (m.t == 0) c.o.cycAdv(c, m);
            else if (m.t == 1) m.n++;
            else if (!c.o.sphAdv(c, m)) return;
            if (n < m.lo || ((T - n) / (m.t == 3 ? pv(c, m, S_EVERY) : int256(1))) % m.stride != 0) return;
            int256 a = T - n;
            int256[4] memory X = c.X;   // scale ×1000, angle, cos, sin
            (X[0], X[1], X[2], X[3]) = (1000, 0, 10000, 0);
            int256 r = (T - n) / m.stride;   // (curves: the drawn step's rank, newest 0)
            W memory s = m.bk[uint256(r < m.top ? NB - 1 - r : NB - m.top - m.nb + mn(m.nb - 1, (n - m.lo) * m.nb / (T - m.lo + 1)))];
            int256 mirs = false && m.t != 3 ? pv(c, m, m.t == 0 ? C_MIR : B_MIR) : int256(0);
            for (int256 mi; mi <= mirs; ++mi) {
                if (m.t == 0) c.o.emitCyc(c, m, i, mi, X, s);
                else if (m.t == 1) c.o.emitBez(c, m, mi, X, s);
                else c.o.emitSph(c, m, i, X, s, D_);
            }
        }
    }



    function emitBezN(Ctx memory c, Mod memory m, int256 mi, int256[4] memory X, W memory s) internal pure {
        unchecked {
            uint256 nc = uint256(pv(c, m, B_NC));
            for (uint256 k; k < nc; ++k) {
                wl16(s, '<path d="M');
                for (uint256 v; v < 8; v += 2) {
                    if (v == 2) wch(s, 67);
                    else if (v != 0) wch(s, 32);
                    int256 x = bezPt(c, m, k, v);
                    if (mi != 0) x = 16384 - x;
                    int256 y = bezPt(c, m, k, v + 1);
                    wPP(s, x, y);
                }
                wl16(s, '" stroke="');
                colEnd(c, s, (colAt(pv(c, m, B_CMODE), m.c0, pv(c, m, B_CRATE), m.n) + int256(k) * pv(c, m, B_COFS) * 16) % 3776);
            }
        }
    }

    function emitSphN(Ctx memory c, Mod memory m, uint256 i, int256[4] memory X, W memory s, W memory D_) internal pure {
        unchecked {
            (int256 qc, int256 R) = discGrad(c, m, i, D_);
            discN(c, m, i, X, s, 0, R * X[0] / 1000, qc);
        }
    }

    function discN(Ctx memory c, Mod memory m, uint256 i, int256[4] memory X, W memory s, int256 k, int256 r, int256 qc) internal pure {
        unchecked {
            (int256 x, int256 y) = place(c, m, k, avg(pv(c, m, S_AUD)));
            if (m.cull && c.o.hidden(c, i, m.n, x, y)) return;
            wl16(s, '<circle cx="');
            wP(s, x);
            wl8(s, '" cy="');
            wP(s, y);
            wl8(s, '" r="');
            wP(s, r);
            wl16(s, '" fill="url(#s');
            wu(s, i);
            wch(s, 95);
            wu(s, uint256(qc));
            wl8(s, ')"/>');
        }
    }


    // ---- specialised variants, selected per family through Ops ----
    function patC(Ctx memory c, Mod memory m, W memory pre, W memory D_) internal pure {
        unchecked {
            int256 rot = pv(c, m, P_ROT) + (T - 1) * pv(c, m, P_SPIN);
            int256 shape = 0;
            int256 sides = pv(c, m, P_SIDES);
            int256 dens = pv(c, m, P_DENS);
            int256 ph = idxU(pv(c, m, P_C0) * 16) + c.off;
            int256 Lp = 37760000 / (dens * 1024 * c.reps);
            if (shape == 0) {
                wb(D_, '<radialGradient id="pg" gradientUnits="userSpaceOnUse" cx="500" cy="500" r="');
                wn1(D_, Lp);
                wl(D_, '" spreadMethod="repeat">');
                gstops(c, D_, ph);
                wl(D_, "</radialGradient>");
                wb(pre, '<rect width="1000" height="1000" fill="url(#pg)"/>');
                return;
            }
            if (shape == 1) {
                int256 h = cs(c.S, 32768 / sides);
                wb(D_, '<linearGradient id="pg" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="');
                wn1(D_, Lp * h / 10000);
                wl(D_, '" spreadMethod="repeat">');
                gstops(c, D_, ph);
                wl(D_, "</linearGradient>");
                int256 w = 1500 * sn(c.S, 32768 / sides + 400) / cs(c.S, 32768 / sides + 400);
                wl(D_, '<polygon id="ps" points="0,0 ');
                wn0(D_, -w);
                wl8(D_, ",1500 ");
                wn0(D_, w);
                wl8(D_, ',1500"/>');
                wb(pre, '<g transform="translate(500 500)">');
                for (int256 k; k < sides; ++k) {
                    wl(pre, '<use href="#ps" transform="');
                    wl8(pre, "rotate(");
                    wdeg(pre, rot + 32768 / sides + k * 65536 / sides);
                    wl(pre, ')" fill="url(#pg)"/>');
                }
                wl8(pre, "</g>");
                return;
            }
            rose(c, m, rot, sides, dens, pre, D_);
        }
    }

    function patN(Ctx memory c, Mod memory m, W memory pre, W memory D_) internal pure {
        unchecked {
            int256 rot = pv(c, m, P_ROT) + (T - 1) * pv(c, m, P_SPIN);
            int256 shape = 1;
            int256 sides = pv(c, m, P_SIDES);
            int256 dens = pv(c, m, P_DENS);
            int256 ph = idxU(pv(c, m, P_C0) * 16) + c.off;
            int256 Lp = 37760000 / (dens * 1024 * c.reps);
            if (shape == 0) {
                wb(D_, '<radialGradient id="pg" gradientUnits="userSpaceOnUse" cx="500" cy="500" r="');
                wn1(D_, Lp);
                wl(D_, '" spreadMethod="repeat">');
                gstops(c, D_, ph);
                wl(D_, "</radialGradient>");
                wb(pre, '<rect width="1000" height="1000" fill="url(#pg)"/>');
                return;
            }
            if (shape == 1) {
                int256 h = cs(c.S, 32768 / sides);
                wb(D_, '<linearGradient id="pg" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="');
                wn1(D_, Lp * h / 10000);
                wl(D_, '" spreadMethod="repeat">');
                gstops(c, D_, ph);
                wl(D_, "</linearGradient>");
                int256 w = 1500 * sn(c.S, 32768 / sides + 400) / cs(c.S, 32768 / sides + 400);
                wl(D_, '<polygon id="ps" points="0,0 ');
                wn0(D_, -w);
                wl8(D_, ",1500 ");
                wn0(D_, w);
                wl8(D_, ',1500"/>');
                wb(pre, '<g transform="translate(500 500)">');
                for (int256 k; k < sides; ++k) {
                    wl(pre, '<use href="#ps" transform="');
                    wl8(pre, "rotate(");
                    wdeg(pre, rot + 32768 / sides + k * 65536 / sides);
                    wl(pre, ')" fill="url(#pg)"/>');
                }
                wl8(pre, "</g>");
                return;
            }
            rose(c, m, rot, sides, dens, pre, D_);
        }
    }

    function patR(Ctx memory c, Mod memory m, W memory pre, W memory D_) internal pure {
        unchecked {
            int256 rot = pv(c, m, P_ROT) + (T - 1) * pv(c, m, P_SPIN);
            int256 shape = 2;
            int256 sides = pv(c, m, P_SIDES);
            int256 dens = pv(c, m, P_DENS);
            int256 ph = idxU(pv(c, m, P_C0) * 16) + c.off;
            int256 Lp = 37760000 / (dens * 1024 * c.reps);
            if (shape == 0) {
                wb(D_, '<radialGradient id="pg" gradientUnits="userSpaceOnUse" cx="500" cy="500" r="');
                wn1(D_, Lp);
                wl(D_, '" spreadMethod="repeat">');
                gstops(c, D_, ph);
                wl(D_, "</radialGradient>");
                wb(pre, '<rect width="1000" height="1000" fill="url(#pg)"/>');
                return;
            }
            if (shape == 1) {
                int256 h = cs(c.S, 32768 / sides);
                wb(D_, '<linearGradient id="pg" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="');
                wn1(D_, Lp * h / 10000);
                wl(D_, '" spreadMethod="repeat">');
                gstops(c, D_, ph);
                wl(D_, "</linearGradient>");
                int256 w = 1500 * sn(c.S, 32768 / sides + 400) / cs(c.S, 32768 / sides + 400);
                wl(D_, '<polygon id="ps" points="0,0 ');
                wn0(D_, -w);
                wl8(D_, ",1500 ");
                wn0(D_, w);
                wl8(D_, ',1500"/>');
                wb(pre, '<g transform="translate(500 500)">');
                for (int256 k; k < sides; ++k) {
                    wl(pre, '<use href="#ps" transform="');
                    wl8(pre, "rotate(");
                    wdeg(pre, rot + 32768 / sides + k * 65536 / sides);
                    wl(pre, ')" fill="url(#pg)"/>');
                }
                wl8(pre, "</g>");
                return;
            }
            rose(c, m, rot, sides, dens, pre, D_);
        }
    }

    /// rose: nested copies of the unit rose (radius 100 at its widest), band width W entries (≤ 90 bands). Bands scaled
    /// over RS/1000 use the unit rose clipped to the square ±RC/1000.
    function rose(Ctx memory c, Mod memory m, int256 rot, int256 sides, int256 dens, W memory pre, W memory D_) internal pure {
        unchecked {
            int256 amp = pv(c, m, P_AMP);
            int256 NP = sides * 12;
            int256[] memory xy = new int256[](uint256(2 * NP));
            wl(D_, '<polygon id="pu" points="');
            for (int256 v; v < NP; ++v) {
                int256 a = v * 65536 / NP + rot;
                int256 rr = (10000 + amp * cs(c.S, v * 65536 * sides / NP) / 100) * 1000 / (10000 + amp * 100);
                int256 x = sn(c.S, a) * rr / 10000;
                int256 y = cs(c.S, a) * rr / 10000;
                if (v != 0) wch(D_, 32);
                wn1(D_, x);
                wch(D_, 44);
                wn1(D_, y);
                xy[uint256(2 * v)] = x * 100;
                xy[uint256(2 * v + 1)] = y * 100;
            }
            wl8(D_, '"/>');
            int256 gmin = (10000 - amp * 100) * 10000 / (10000 + amp * 100);
            int256 span = 7250000 / mx(1000, gmin) * dens / 16;
            int256 W_ = mx(236 / (int256(c.su.length) * c.reps * 3) + 1, (span + 89) / 90);
            int256 e0 = span / W_ * W_ + W_;
            if (e0 * 160000 / (dens * 1024) > RS) {
                uint256 l0 = D_.len;
                (int256[] memory q, uint256 n) = clipSq(xy, uint256(2 * NP), RC);
                wl(D_, '<polygon id="pv" points="');
                for (uint256 j; j < n; j += 2) {
                    if (j != 0) wch(D_, 32);
                    wf3(D_, q[j]);
                    wch(D_, 44);
                    wf3(D_, q[j + 1]);
                }
                wl8(D_, '"/>');
                c.xl = int256(D_.len - l0);
            }
            wb(pre, '<g transform="translate(500 500)">');
            for (int256 e = e0; e > 0; e -= W_) {
                int256 k = e * 160000 / (dens * 1024);
                wl(pre, k > RS ? bytes32('<use href="#pv" transform="') : bytes32('<use href="#pu" transform="'));
                wl8(pre, "scale(");
                wf3(pre, k);
                wl16(pre, ')" fill="');
                colUEnd(c, pre, idxU((pv(c, m, P_C0) + e) * 16 - W_ * 8) + c.off);
            }
            wl8(pre, "</g>");
        }
    }

    /// polygon p (x, y, …, thousandths; n values) clipped to the square ±c (Sutherland–Hodgman: one side after another)
    function clipSq(int256[] memory p, uint256 n, int256 c) internal pure returns (int256[] memory, uint256) {
        unchecked {
            for (uint256 k; k < 4; ++k) {
                uint256 a = k & 1;
                uint256 o = 1 - a;
                int256 g = k < 2 ? int256(1) : -1;   // keeps g · (coordinate a) ≤ c
                int256[] memory q = new int256[](2 * n);
                uint256 l;
                for (uint256 i; i < n; i += 2) {
                    uint256 j = (i + n - 2) % n;
                    bool ci = g * p[i + a] <= c;
                    if (ci != (g * p[j + a] <= c)) {
                        q[l + a] = g * c;
                        q[l + o] = p[j + o] + (p[i + o] - p[j + o]) * (g * c - p[j + a]) / (p[i + a] - p[j + a]);
                        l += 2;
                    }
                    if (ci) {
                        q[l] = p[i];
                        q[l + 1] = p[i + 1];
                        l += 2;
                    }
                }
                p = q;
                n = l;
            }
            return (p, n);
        }
    }

    function emitCycNP(Ctx memory c, Mod memory m, uint256 i, int256 mi, int256[4] memory X, W memory s) internal pure {
        unchecked {
            E memory e = c.e;
            (e.L0, e.L1) = lev(c, m);
            e.i = i;
            e.mi = mi;
            e.N = pv(c, m, C_N);
            e.strokeCol = pv(c, m, C_MODE) == 2 || e.L0 == 2;
            e.streak = e.L0 == 1 && (false || e.L1 < 4);
            e.R = au(e.L1 * 16, pv(c, m, C_AMT), pv(c, m, C_AUD));
        e.tube = pv(c, m, C_MODE) != 2 && e.L0 != 2;
        for (e.r = 0; e.r < e.N; ++e.r) {
            e.kc = cycCol(c, m, e.r);
                uint256 k = stamps(c, m, e.r);
                e.ns = int256(k / 5);
                for (uint256 j; j < k; j += 5) emitStampNP(c, m, e, X, s, m.so, j);
            }
        }
    }

    function emitStampNP(Ctx memory c, Mod memory m, E memory e, int256[4] memory X, W memory s, int256[] memory q, uint256 j) internal pure {
        unchecked {
            int256 x = e.mi != 0 ? 16384 - q[j] : q[j];
            int256 y = q[j + 1];
            int256 X1 = tx(x, y, X);
            int256 Y1 = ty(x, y, X);
            uint256 li = uint256(((e.mi * e.N + e.r) * e.ns + int256(j / 5)) * 2);
            int256 lx = m.last[li];
            int256 ly = m.last[li + 1];
            bool far = lx != 0 && (X1 - lx) * (X1 - lx) + (Y1 - ly) * (Y1 - ly) > e.R * e.R * 36 / 25;
            m.last[li] = X1;
            m.last[li + 1] = Y1;
            if (m.cull && c.o.hidden(c, e.i, m.n, x, y)) return;
            if (false) return seg(c, s, lx, ly, X1, Y1, e.R * 2 * X[0] / 1000, e.kc);
            if (e.tube && far && true) return seg(c, s, lx, ly, X1, Y1, 2 * e.R, e.kc);
            int256 g = (e.mi != 0 ? -q[j + 3] : q[j + 3]) - X[1];
            wStamp(s, e.i, x, y, g, (e.L0 == 1) ? 0 : 1, e.strokeCol);
            colEnd(c, s, e.kc);
        }
    }

    function cycDefsP(Ctx memory c, Mod memory m, uint256 i, W memory D_) internal pure {
        unchecked {
            (int256 n, int256 d) = lev(c, m);
            int256 R = au(d * 16, pv(c, m, C_AMT), pv(c, m, C_AUD));

            wl16(D_, '<polygon id="q');
            wu(D_, i);
            wl16(D_, '" points="');
            for (int256 v; v < n; ++v) {
                int256 a = v * 65536 / n;
                if (v != 0) wch(D_, 32);
                wP(D_, sn(c.S, a) * R / 10000);
                wch(D_, 44);
                wP(D_, cs(c.S, a) * R / 10000);
            }
            wl8(D_, '"/>');
        }
    }

    function emitCycNQ(Ctx memory c, Mod memory m, uint256 i, int256 mi, int256[4] memory X, W memory s) internal pure {
        unchecked {
            E memory e = c.e;
            (e.L0, e.L1) = lev(c, m);
            e.i = i;
            e.mi = mi;
            e.N = pv(c, m, C_N);
            e.strokeCol = pv(c, m, C_MODE) == 2 || e.L0 == 2;
            e.streak = e.L0 == 1 && (false || e.L1 < 4);
            e.R = au(e.L1 * 16, pv(c, m, C_AMT), pv(c, m, C_AUD));
        e.tube = pv(c, m, C_MODE) != 2 && e.L0 != 2;
        for (e.r = 0; e.r < e.N; ++e.r) {
            e.kc = cycCol(c, m, e.r);
                uint256 k = stamps(c, m, e.r);
                e.ns = int256(k / 5);
                for (uint256 j; j < k; j += 5) emitStampNQ(c, m, e, X, s, m.so, j);
            }
        }
    }

    function emitStampNQ(Ctx memory c, Mod memory m, E memory e, int256[4] memory X, W memory s, int256[] memory q, uint256 j) internal pure {
        unchecked {
            int256 x = e.mi != 0 ? 16384 - q[j] : q[j];
            int256 y = q[j + 1];
            int256 X1 = tx(x, y, X);
            int256 Y1 = ty(x, y, X);
            uint256 li = uint256(((e.mi * e.N + e.r) * e.ns + int256(j / 5)) * 2);
            int256 lx = m.last[li];
            int256 ly = m.last[li + 1];
            bool far = lx != 0 && (X1 - lx) * (X1 - lx) + (Y1 - ly) * (Y1 - ly) > e.R * e.R * 36 / 25;
            m.last[li] = X1;
            m.last[li + 1] = Y1;
            if (m.cull && c.o.hidden(c, e.i, m.n, x, y)) return;
            if (false) return seg(c, s, lx, ly, X1, Y1, e.R * 2 * X[0] / 1000, e.kc);
            if (e.tube && far && true) return seg(c, s, lx, ly, X1, Y1, 2 * e.R, e.kc);
            int256 g = (e.mi != 0 ? -q[j + 3] : q[j + 3]) - X[1];
            wStamp(s, e.i, x, y, g, (false) ? 0 : 1, e.strokeCol);
            colEnd(c, s, e.kc);
        }
    }

}

src/generated/Marfa1997Catalog.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

/// @notice Per-slot preset data: knob ranges, thumbnail family and static thumbnail params.
library Marfa1997Catalog {
    string internal constant SET = "A";
    uint256 internal constant N = 30;
    uint256 internal constant KNOBS = 6;
    /// per slot, per knob: lo, hi (big-endian int16)
    bytes internal constant KNOB_RANGES = hex"0000001d00020007000a0028ffd8001effe80018000100050000001d0003000700000003003c00b400000037fff100190000001d0000000100020003ffd8000afffa0003000a001a0000001d0001000400010003fffc001000010004001400280000001d0004000700050028ffce001400140028fffa00080000001d0002000600000003ffd8000f0000003c000f002d0000001d00000007001e004600010002fff800080000000c0000001dffe2001e0002000500000002fff10019000f002d0000001d00000002ffec000affd80028ffd3002d000100060000001d00000003000a001e00020003ffec0014ffe200140000001d00000007000000090000000500000005000000020000001d00000002000000030000000500000001000000030000001d00000008000000040000000500000003000000020000001d00000002000000050000000300000009000000030000001d00000005000000030000000400000004000000030000001d00000009000000040000000400020004000000050000001d00000005000000070000000400000001000000030000001d00000006000000060000000300000002000000040000001d00000007000000050000000400000003000000070000001d00000007000000040000000300000004000000040000001d00000005fffa00060000000300010006000000030000001d0000000500010004000200080023004b000000030000001d000000050003000c00020008ffd80028000000000000001d00000005000200060001000400000003000000000000001d00000005000200040008001e00010002000000000000001d000000050001000400010003000a0028000000030000001d000000050000003b001e005a00000003fffc00040000001d0000000500020004001e0050000000010028006e0000001d00000005fffa00060000000300030008000000030000001d000000050003000900010006fffc000400000001";
    /// per slot: thumbnail family index
    bytes internal constant FAMILY = hex"00000000010201010202030405060305060405060708090a0b080a0b0907";
    /// per slot: 8 static thumbnail params (big-endian int16)
    bytes internal constant THUMB_P = hex"000000000000000000000000000000000001000000000000000000000000000000020000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000002000000000000000000000000000000010000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000200000000000000000000000000000003000000000000000000000000000000040001000000000000000000000000000500010000000000000000000000000006000100000000000000000000000000070001000000000000000000000000000800020000000000000000000000000009000200000000000000000000000000000000000000000000000000000000000100000000000000000000000000000002000000000000000000000000000000030000000000000000000000000000000400000000000000000000000000000005000000000000000000000000000000060000000000000000000000000000000700000000000000000000000000000008000000000000000000000000000000090000000000000000000000000000";

    /// @notice The static thumbnail params of a slot.
    function thumbP(uint256 slot) internal pure returns (int256[8] memory p) {
        bytes memory t = THUMB_P;
        for (uint256 i; i < 8; ++i) {
            uint256 o = (slot * 8 + i) * 2;
            p[i] = int256(int16(uint16(uint8(t[o])) << 8 | uint16(uint8(t[o + 1]))));
        }
    }
}

src/interfaces/IMarfa1997Family.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

/// @notice A thumbnail family: draws the SVG body and defs for a seed.
interface IMarfa1997Family {
    /// @param pal Shared palette table, supplied by the caller.
    function draw(bytes32 seed, bytes calldata pal) external pure returns (bytes memory body, bytes memory defs);
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "osaka",
  "remappings": [
    ":forge-std/=lib/forge-std/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.