Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-kernel

Source of release 0.1.0

0.1.0 / src / lower / sources.lucb

# Generated by tools/sources.py from src/lib and luce-std; edit those, not this file.
# The files `write_root` writes into a snippet root, by path under the root.

let source_paths: str[17] = [
    "luce_kernel/lib/ORDER",
    "luce_kernel/lib/module.lucb",
    "luce_kernel/lib/interpolate.lucb",
    "luce_kernel/lib/vector.lucb",
    "luce_kernel/lib/random.lucb",
    "luce_kernel/lib/noise.lucb",
    "luce_kernel/lib/perlin.lucb",
    "luce_kernel/lib/arithmetic.lucb",
    "luce_kernel/lib/spline.lucb",
    "luce_kernel/lib/matrix.lucb",
    "luce_kernel/lib/transform.lucb",
    "luce_kernel/lib/quaternion.lucb",
    "luce_kernel/lib/gradient.lucb",
    "luce_kernel/lib/cellular.lucb",
    "luce_kernel/lib/sampling.lucb",
    "luce_std/math.lucb",
    "luce_std/math32.lucb",
]

let source_texts: str[17] = [
    "module.lucb\ninterpolate.lucb\nvector.lucb\nrandom.lucb\nnoise.lucb\nperlin.lucb\narithmetic.lucb\nspline.lucb\nmatrix.lucb\ntransform.lucb\nquaternion.lucb\ngradient.lucb\ncellular.lucb\nsampling.lucb\n",
    "#==============================================================================================\n#\n#   lib - The functions a Code node's snippet calls without importing them\n#\n#   DESCRIPTION:\n#       VEX's general library in Base spelling: vector functions over `f32[4]` (a\n#       three-component value keeps lane 3 at zero), interpolation and fitting, hash-based\n#       random numbers and Perlin noise, each in the widths it supports. f64 forms carry a\n#       `64` suffix and two-component forms a `2d` suffix, since Base has no overloading.\n#\n#       The node never calls these bodies: the lowering recognizes each name and runs the\n#       engine's column operation for it. They are the same computation lane for lane, so a\n#       snippet means what it would mean as plain Base, and the engine's tests check its\n#       columns against them.\n#\n#==============================================================================================\nfrom luce_std import math, math32\n",
    "# Interpolation, fitting and angle units.\n\n## `a` moved toward `b` by `t`: `a + (b - a) * t` (VEX `lerp`).\npub func lerp(a: f32, b: f32, t: f32) -> f32:\n    return a + (b - a) * t\n\n## `lerp` in f64.\npub func lerp64(a: f64, b: f64, t: f64) -> f64:\n    return a + (b - a) * t\n\n## `x` held to `low..high`: `low` below it, `high` above it, `x` itself (a NaN included)\n## otherwise.\npub func clamp(x: f32, low: f32, high: f32) -> f32:\n    return low if x < low else (high if x > high else x)\n\n## `clamp` in f64.\npub func clamp64(x: f64, low: f64, high: f64) -> f64:\n    return low if x < low else (high if x > high else x)\n\n## `x` clamped to `0..1` and mapped onto `new_low..new_high` (VEX `fit01`).\npub func fit01(x: f32, new_low: f32, new_high: f32) -> f32:\n    return lerp(new_low, new_high, clamp(x, 0.0, 1.0))\n\n## `fit01` in f64.\npub func fit01_64(x: f64, new_low: f64, new_high: f64) -> f64:\n    return lerp64(new_low, new_high, clamp64(x, 0.0, 1.0))\n\n## `x` clamped to the old range and mapped onto the new one (VEX `fit`). A reversed old\n## range maps in reverse; an empty one maps everything to `new_low`.\npub func fit(x: f32, old_low: f32, old_high: f32, new_low: f32, new_high: f32) -> f32:\n    if old_low == old_high:\n        return new_low\n    let held = clamp(x, old_low, old_high) if old_low < old_high else clamp(x, old_high, old_low)\n    return lerp(new_low, new_high, (held - old_low) / (old_high - old_low))\n\n## `fit` in f64.\npub func fit64(x: f64, old_low: f64, old_high: f64, new_low: f64, new_high: f64) -> f64:\n    if old_low == old_high:\n        return new_low\n    let held = clamp64(x, old_low, old_high) if old_low < old_high else clamp64(x, old_high, old_low)\n    return lerp64(new_low, new_high, (held - old_low) / (old_high - old_low))\n\n## 0 at or below `low`, 1 at or above `high`, and the smooth Hermite rise\n## `t * t * (3 - 2 * t)` between (VEX `smooth`).\npub func smooth(low: f32, high: f32, x: f32) -> f32:\n    if x <= low: return 0.0\n    if x >= high: return 1.0\n    let t = (x - low) / (high - low)\n    return t * t * (3.0 - 2.0 * t)\n\n## `smooth` in f64.\npub func smooth64(low: f64, high: f64, x: f64) -> f64:\n    if x <= low: return 0.0\n    if x >= high: return 1.0\n    let t = (x - low) / (high - low)\n    return t * t * (3.0 - 2.0 * t)\n\n## Radians to degrees.\npub func degrees(radians_value: f32) -> f32:\n    return radians_value * (180.0 / math32.pi)\n\n## `degrees` in f64.\npub func degrees64(radians_value: f64) -> f64:\n    return radians_value * (180.0 / math.pi)\n\n## Degrees to radians.\npub func radians(degrees_value: f32) -> f32:\n    return degrees_value * (math32.pi / 180.0)\n\n## `radians` in f64.\npub func radians64(degrees_value: f64) -> f64:\n    return degrees_value * (math.pi / 180.0)\n",
    "# Vector functions. An `f32[4]` holding a position, normal or color keeps lane 3 at zero,\n# so the four-lane forms are VEX's three-component ones.\n\n## The dot product of all four lanes.\npub func dot(a: f32[4], b: f32[4]) -> f32:\n    return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]\n\n## The cross product of lanes 0 to 2; lane 3 of the result is zero.\npub func cross(a: f32[4], b: f32[4]) -> f32[4]:\n    return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0], 0.0]\n\n## The length.\npub func length(a: f32[4]) -> f32:\n    return math32.sqrt(dot(a, a))\n\n## The squared length.\npub func length2(a: f32[4]) -> f32:\n    return dot(a, a)\n\n## The distance between two points.\npub func distance(a: f32[4], b: f32[4]) -> f32:\n    return length(a - b)\n\n## The squared distance between two points.\npub func distance2(a: f32[4], b: f32[4]) -> f32:\n    return length2(a - b)\n\n## `a` scaled to length 1; a zero vector stays zero.\npub func normalized(a: f32[4]) -> f32[4]:\n    let size = length(a)\n    if size == 0.0:\n        return f32[4](0.0)\n    return a * (1.0 / size)\n\n## The dot product of a two-lane vector.\npub func dot2d(a: f32[2], b: f32[2]) -> f32:\n    return a[0] * b[0] + a[1] * b[1]\n\n## The length of a two-lane vector.\npub func length2d(a: f32[2]) -> f32:\n    return math32.sqrt(dot2d(a, a))\n\n## The distance between two two-lane points.\npub func distance2d(a: f32[2], b: f32[2]) -> f32:\n    return length2d(a - b)\n\n## A two-lane vector scaled to length 1; zero stays zero.\npub func normalized2d(a: f32[2]) -> f32[2]:\n    let size = length2d(a)\n    if size == 0.0:\n        return f32[2](0.0)\n    return a * (1.0 / size)\n\n## The dot product of four f64 lanes.\npub func dot64(a: f64[4], b: f64[4]) -> f64:\n    return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]\n\n## The cross product of f64 lanes 0 to 2; lane 3 of the result is zero.\npub func cross64(a: f64[4], b: f64[4]) -> f64[4]:\n    return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0], 0.0]\n\n## The length of an f64 vector.\npub func length64(a: f64[4]) -> f64:\n    return math.sqrt(dot64(a, a))\n\n## The squared length of an f64 vector.\npub func length2_64(a: f64[4]) -> f64:\n    return dot64(a, a)\n\n## The distance between two f64 points.\npub func distance64(a: f64[4], b: f64[4]) -> f64:\n    return length64(difference64(a, b))\n\n## The squared distance between two f64 points.\npub func distance2_64(a: f64[4], b: f64[4]) -> f64:\n    return length2_64(difference64(a, b))\n\n## An f64 vector scaled to length 1; zero stays zero.\npub func normalized64(a: f64[4]) -> f64[4]:\n    let size = length64(a)\n    if size == 0.0:\n        return [0.0, 0.0, 0.0, 0.0]\n    let scale = 1.0 / size\n    return [a[0] * scale, a[1] * scale, a[2] * scale, a[3] * scale]\n\n# `a - b` lane by lane (`f64[4]` is an array, not a vector, so it has no `-`).\nfunc difference64(a: f64[4], b: f64[4]) -> f64[4]:\n    return [a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]]\n",
    "# Hash-based random numbers: pure functions of their seed, so every element, run, thread\n# count and machine gets the same values (a per-element random is `rand(index + seed)`).\n\n## A random number in `[0, 1)` for `seed` (VEX `rand`).\npub func rand(seed: i64) -> f32:\n    return rand_stream(seed, 0)\n\n## `rand` in f64, with 53 random bits.\npub func rand64(seed: i64) -> f64:\n    return rand_stream64(seed, 0)\n\n## Three independent random numbers in `[0, 1)` for `seed`, lane 3 zero (VEX `rand` of a\n## vector); lane `k` is `rand_stream(seed, k)`.\npub func rand3(seed: i64) -> f32[4]:\n    return [rand_stream(seed, 0), rand_stream(seed, 1), rand_stream(seed, 2), 0.0]\n\n## The `stream`-th independent random number in `[0, 1)` for `seed`; stream 0 is `rand`.\npub func rand_stream(seed: i64, stream: u32) -> f32:\n    return (f32)(random_bits(seed, stream) >> 40) * (1.0 / 16777216.0)\n\n## `rand_stream` in f64.\npub func rand_stream64(seed: i64, stream: u32) -> f64:\n    return (f64)(random_bits(seed, stream) >> 11) * (1.0 / 9007199254740992.0)\n\n## 64 random bits for `seed` and `stream`: the SplitMix64 finalizer over the seed and a\n## stream offset (VEX `random_ihash`).\npub func random_bits(seed: i64, stream: u32) -> u64:\n    var z = (u64)seed +% (u64)stream *% 0xD1B54A32D192ED03 +% 0x9E3779B97F4A7C15\n    z = (z ^ (z >> 30)) *% 0xBF58476D1CE4E5B9\n    z = (z ^ (z >> 27)) *% 0x94D049BB133111EB\n    return z ^ (z >> 31)\n",
    "# Perlin noise, VEX's `noise` and `snoise`: `snoise` is signed, in `-1..1`, zero on the\n# integer lattice; `noise` is `0.5 + 0.5 * snoise`, in `0..1` centered on 0.5. Each comes\n# in 1 to 4 dimensions, computed in f32 or in f64. The 3D forms take an `f32[4]` and read\n# lanes 0 to 2.\n\n## Signed 1D noise.\npub func snoise1(x: f32) -> f32:\n    return limit(perlin1_32(x))\n\n## Signed 2D noise.\npub func snoise2(p: f32[2]) -> f32:\n    return limit(perlin2_32(p[0], p[1]))\n\n## Signed 3D noise of lanes 0 to 2.\npub func snoise(p: f32[4]) -> f32:\n    return limit(perlin3_32(p[0], p[1], p[2]))\n\n## Signed 4D noise.\npub func snoise4(p: f32[4]) -> f32:\n    return limit(perlin4_32(p[0], p[1], p[2], p[3]))\n\n## 1D noise in `0..1`.\npub func noise1(x: f32) -> f32:\n    return 0.5 + 0.5 * snoise1(x)\n\n## 2D noise in `0..1`.\npub func noise2(p: f32[2]) -> f32:\n    return 0.5 + 0.5 * snoise2(p)\n\n## 3D noise in `0..1` of lanes 0 to 2.\npub func noise(p: f32[4]) -> f32:\n    return 0.5 + 0.5 * snoise(p)\n\n## 4D noise in `0..1`.\npub func noise4(p: f32[4]) -> f32:\n    return 0.5 + 0.5 * snoise4(p)\n\n## Signed 1D noise in f64.\npub func snoise1_64(x: f64) -> f64:\n    return limit64(perlin1_64(x))\n\n## Signed 2D noise in f64.\npub func snoise2_64(p: f64[2]) -> f64:\n    return limit64(perlin2_64(p[0], p[1]))\n\n## Signed 3D noise in f64 of lanes 0 to 2.\npub func snoise64(p: f64[4]) -> f64:\n    return limit64(perlin3_64(p[0], p[1], p[2]))\n\n## Signed 4D noise in f64.\npub func snoise4_64(p: f64[4]) -> f64:\n    return limit64(perlin4_64(p[0], p[1], p[2], p[3]))\n\n## 1D noise in `0..1`, in f64.\npub func noise1_64(x: f64) -> f64:\n    return 0.5 + 0.5 * snoise1_64(x)\n\n## 2D noise in `0..1`, in f64.\npub func noise2_64(p: f64[2]) -> f64:\n    return 0.5 + 0.5 * snoise2_64(p)\n\n## 3D noise in `0..1`, in f64.\npub func noise64(p: f64[4]) -> f64:\n    return 0.5 + 0.5 * snoise64(p)\n\n## 4D noise in `0..1`, in f64.\npub func noise4_64(p: f64[4]) -> f64:\n    return 0.5 + 0.5 * snoise4_64(p)\n\n# The scaled lattice sums stay within -1..1 but for rounding; this makes it exact.\nfunc limit(value: f32) -> f32:\n    return -1.0 if value < -1.0 else (1.0 if value > 1.0 else value)\n\nfunc limit64(value: f64) -> f64:\n    return -1.0 if value < -1.0 else (1.0 if value > 1.0 else value)\n",
    "# Improved Perlin noise (Perlin 2002): quintic fade, a 256-entry permutation, and\n# gradients chosen from the hash's low bits, as in Stefan Gustavson's public-domain\n# noise1234. Each dimension's sum is scaled into -1..1. The f32 and f64 forms are the\n# same computation in their own width.\n\nlet permutation: u8[256] = [\n    151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30,\n    69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94,\n    252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171,\n    168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60,\n    211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1,\n    216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86,\n    164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118,\n    126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170,\n    213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39,\n    253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34,\n    242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49,\n    192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254,\n    138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,\n]\n\n# The hash of a lattice cell: each coordinate is reduced to 0..255 first.\nfunc mix(cell: usize, next: usize) -> usize:\n    return (usize)permutation[(cell + next) & 255]\n\nfunc lattice(value: i64) -> usize:\n    return (usize)(value & 255)\n\nfunc fade32(t: f32) -> f32:\n    return t * t * t * (t * (t * 6.0 - 15.0) + 10.0)\n\nfunc fade64(t: f64) -> f64:\n    return t * t * t * (t * (t * 6.0 - 15.0) + 10.0)\n\n# Gradients: 1D slopes ±1..±8; 2D eight directions (±1, ±2) and (±2, ±1); 3D the twelve\n# cube edge directions (with four repeated); 4D the 32 directions with one zero lane.\nfunc grad1_32(hash_value: usize, x: f32) -> f32:\n    let slope = 1.0 + (f32)(hash_value & 7)\n    return -slope * x if (hash_value & 8) != 0 else slope * x\n\nfunc grad2_32(hash_value: usize, x: f32, y: f32) -> f32:\n    let h = hash_value & 7\n    let u = x if h < 4 else y\n    let v = y if h < 4 else x\n    return (-u if (h & 1) != 0 else u) + (-2.0 * v if (h & 2) != 0 else 2.0 * v)\n\nfunc grad3_32(hash_value: usize, x: f32, y: f32, z: f32) -> f32:\n    let h = hash_value & 15\n    let u = x if h < 8 else y\n    let v = y if h < 4 else (x if h == 12 or h == 14 else z)\n    return (-u if (h & 1) != 0 else u) + (-v if (h & 2) != 0 else v)\n\nfunc grad4_32(hash_value: usize, x: f32, y: f32, z: f32, t: f32) -> f32:\n    let h = hash_value & 31\n    let u = x if h < 24 else y\n    let v = y if h < 16 else z\n    let w = z if h < 8 else t\n    return (-u if (h & 1) != 0 else u) + (-v if (h & 2) != 0 else v) + (-w if (h & 4) != 0 else w)\n\nfunc grad1_64(hash_value: usize, x: f64) -> f64:\n    let slope = 1.0 + (f64)(hash_value & 7)\n    return -slope * x if (hash_value & 8) != 0 else slope * x\n\nfunc grad2_64(hash_value: usize, x: f64, y: f64) -> f64:\n    let h = hash_value & 7\n    let u = x if h < 4 else y\n    let v = y if h < 4 else x\n    return (-u if (h & 1) != 0 else u) + (-2.0 * v if (h & 2) != 0 else 2.0 * v)\n\nfunc grad3_64(hash_value: usize, x: f64, y: f64, z: f64) -> f64:\n    let h = hash_value & 15\n    let u = x if h < 8 else y\n    let v = y if h < 4 else (x if h == 12 or h == 14 else z)\n    return (-u if (h & 1) != 0 else u) + (-v if (h & 2) != 0 else v)\n\nfunc grad4_64(hash_value: usize, x: f64, y: f64, z: f64, t: f64) -> f64:\n    let h = hash_value & 31\n    let u = x if h < 24 else y\n    let v = y if h < 16 else z\n    let w = z if h < 8 else t\n    return (-u if (h & 1) != 0 else u) + (-v if (h & 2) != 0 else v) + (-w if (h & 4) != 0 else w)\n\n# One lattice coordinate: the cell (saturating for huge or NaN input) and the offset.\nfunc split32(x: f32) -> (usize, f32):\n    let floored = math32.floor(x)\n    return (lattice((i64)floored), x - floored)\n\nfunc split64(x: f64) -> (usize, f64):\n    let floored = math.floor(x)\n    return (lattice((i64)floored), x - floored)\n\nfunc perlin1_32(x: f32) -> f32:\n    let (ix, fx) = split32(x)\n    let s = fade32(fx)\n    let n0 = grad1_32(mix(ix, 0), fx)\n    let n1 = grad1_32(mix(ix + 1, 0), fx - 1.0)\n    return 0.188 * (n0 + s * (n1 - n0))\n\nfunc perlin2_32(x: f32, y: f32) -> f32:\n    let (ix, fx) = split32(x)\n    let (iy, fy) = split32(y)\n    let s = fade32(fx)\n    let t = fade32(fy)\n    let a = mix(ix, 0)\n    let b = mix(ix + 1, 0)\n    let n00 = grad2_32(mix(a, iy), fx, fy)\n    let n01 = grad2_32(mix(a, iy + 1), fx, fy - 1.0)\n    let n10 = grad2_32(mix(b, iy), fx - 1.0, fy)\n    let n11 = grad2_32(mix(b, iy + 1), fx - 1.0, fy - 1.0)\n    let n0 = n00 + t * (n01 - n00)\n    let n1 = n10 + t * (n11 - n10)\n    return 0.507 * (n0 + s * (n1 - n0))\n\nfunc perlin3_32(x: f32, y: f32, z: f32) -> f32:\n    let (ix, fx) = split32(x)\n    let (iy, fy) = split32(y)\n    let (iz, fz) = split32(z)\n    let r = fade32(fz)\n    let t = fade32(fy)\n    let s = fade32(fx)\n    let a = mix(ix, 0)\n    let b = mix(ix + 1, 0)\n    let aa = mix(a, iy)\n    let ab = mix(a, iy + 1)\n    let ba = mix(b, iy)\n    let bb = mix(b, iy + 1)\n    let x0 = blend32(r, grad3_32(mix(aa, iz), fx, fy, fz), grad3_32(mix(aa, iz + 1), fx, fy, fz - 1.0))\n    let x1 = blend32(r, grad3_32(mix(ab, iz), fx, fy - 1.0, fz), grad3_32(mix(ab, iz + 1), fx, fy - 1.0, fz - 1.0))\n    let x2 = blend32(r, grad3_32(mix(ba, iz), fx - 1.0, fy, fz), grad3_32(mix(ba, iz + 1), fx - 1.0, fy, fz - 1.0))\n    let x3 = blend32(r, grad3_32(mix(bb, iz), fx - 1.0, fy - 1.0, fz), grad3_32(mix(bb, iz + 1), fx - 1.0, fy - 1.0, fz - 1.0))\n    return 0.936 * blend32(s, blend32(t, x0, x1), blend32(t, x2, x3))\n\nfunc perlin4_32(x: f32, y: f32, z: f32, w: f32) -> f32:\n    let (ix, fx) = split32(x)\n    let (iy, fy) = split32(y)\n    let (iz, fz) = split32(z)\n    let (iw, fw) = split32(w)\n    let fade_x = fade32(fx)\n    let fade_y = fade32(fy)\n    let fade_z = fade32(fz)\n    let fade_w = fade32(fw)\n    var corners: f32[16]\n    for corner: usize in 0..<16:\n        let dx = corner & 1\n        let dy = (corner >> 1) & 1\n        let dz = (corner >> 2) & 1\n        let dw = (corner >> 3) & 1\n        let cell = mix(mix(mix(mix(ix + dx, 0), iy + dy), iz + dz), iw + dw)\n        corners[corner] = grad4_32(cell, fx - (f32)dx, fy - (f32)dy, fz - (f32)dz, fw - (f32)dw)\n    # Fold the 16 corners along w, then z, then y, then x.\n    for half: usize in 0..<8:\n        corners[half] = blend32(fade_w, corners[half], corners[half + 8])\n    for half: usize in 0..<4:\n        corners[half] = blend32(fade_z, corners[half], corners[half + 4])\n    for half: usize in 0..<2:\n        corners[half] = blend32(fade_y, corners[half], corners[half + 2])\n    return 0.87 * blend32(fade_x, corners[0], corners[1])\n\nfunc perlin1_64(x: f64) -> f64:\n    let (ix, fx) = split64(x)\n    let s = fade64(fx)\n    let n0 = grad1_64(mix(ix, 0), fx)\n    let n1 = grad1_64(mix(ix + 1, 0), fx - 1.0)\n    return 0.188 * (n0 + s * (n1 - n0))\n\nfunc perlin2_64(x: f64, y: f64) -> f64:\n    let (ix, fx) = split64(x)\n    let (iy, fy) = split64(y)\n    let s = fade64(fx)\n    let t = fade64(fy)\n    let a = mix(ix, 0)\n    let b = mix(ix + 1, 0)\n    let n00 = grad2_64(mix(a, iy), fx, fy)\n    let n01 = grad2_64(mix(a, iy + 1), fx, fy - 1.0)\n    let n10 = grad2_64(mix(b, iy), fx - 1.0, fy)\n    let n11 = grad2_64(mix(b, iy + 1), fx - 1.0, fy - 1.0)\n    let n0 = n00 + t * (n01 - n00)\n    let n1 = n10 + t * (n11 - n10)\n    return 0.507 * (n0 + s * (n1 - n0))\n\nfunc perlin3_64(x: f64, y: f64, z: f64) -> f64:\n    let (ix, fx) = split64(x)\n    let (iy, fy) = split64(y)\n    let (iz, fz) = split64(z)\n    let r = fade64(fz)\n    let t = fade64(fy)\n    let s = fade64(fx)\n    let a = mix(ix, 0)\n    let b = mix(ix + 1, 0)\n    let aa = mix(a, iy)\n    let ab = mix(a, iy + 1)\n    let ba = mix(b, iy)\n    let bb = mix(b, iy + 1)\n    let x0 = blend64(r, grad3_64(mix(aa, iz), fx, fy, fz), grad3_64(mix(aa, iz + 1), fx, fy, fz - 1.0))\n    let x1 = blend64(r, grad3_64(mix(ab, iz), fx, fy - 1.0, fz), grad3_64(mix(ab, iz + 1), fx, fy - 1.0, fz - 1.0))\n    let x2 = blend64(r, grad3_64(mix(ba, iz), fx - 1.0, fy, fz), grad3_64(mix(ba, iz + 1), fx - 1.0, fy, fz - 1.0))\n    let x3 = blend64(r, grad3_64(mix(bb, iz), fx - 1.0, fy - 1.0, fz), grad3_64(mix(bb, iz + 1), fx - 1.0, fy - 1.0, fz - 1.0))\n    return 0.936 * blend64(s, blend64(t, x0, x1), blend64(t, x2, x3))\n\nfunc perlin4_64(x: f64, y: f64, z: f64, w: f64) -> f64:\n    let (ix, fx) = split64(x)\n    let (iy, fy) = split64(y)\n    let (iz, fz) = split64(z)\n    let (iw, fw) = split64(w)\n    let fade_x = fade64(fx)\n    let fade_y = fade64(fy)\n    let fade_z = fade64(fz)\n    let fade_w = fade64(fw)\n    var corners: f64[16]\n    for corner: usize in 0..<16:\n        let dx = corner & 1\n        let dy = (corner >> 1) & 1\n        let dz = (corner >> 2) & 1\n        let dw = (corner >> 3) & 1\n        let cell = mix(mix(mix(mix(ix + dx, 0), iy + dy), iz + dz), iw + dw)\n        corners[corner] = grad4_64(cell, fx - (f64)dx, fy - (f64)dy, fz - (f64)dz, fw - (f64)dw)\n    for half: usize in 0..<8:\n        corners[half] = blend64(fade_w, corners[half], corners[half + 8])\n    for half: usize in 0..<4:\n        corners[half] = blend64(fade_z, corners[half], corners[half + 4])\n    for half: usize in 0..<2:\n        corners[half] = blend64(fade_y, corners[half], corners[half + 2])\n    return 0.87 * blend64(fade_x, corners[0], corners[1])\n\n# The lattice blend: `a` toward `b` by `t` (argument order of Perlin's reference).\nfunc blend32(t: f32, a: f32, b: f32) -> f32:\n    return a + t * (b - a)\n\nfunc blend64(t: f64, a: f64, b: f64) -> f64:\n    return a + t * (b - a)\n",
    "# VEX's arithmetic: rounding and sign, component reductions of vectors, powers and logs,\n# the half-turn trigonometry, polynomial roots and point distances. Vector forms work on\n# lanes 0 to 2 of an `f32[4]`.\n\n## `|x|` (VEX `abs`).\npub func abs(x: f32) -> f32:\n    return math32.abs(x)\n\n## Each lane's `|x|` (VEX `abs` of a vector); lane 3 is zero.\npub func abs3(v: f32[4]) -> f32[4]:\n    return [math32.abs(v[0]), math32.abs(v[1]), math32.abs(v[2]), 0.0]\n\n## -1, 0 or 1 by the sign of `x` (VEX `sign`); NaN gives 0.\npub func sign(x: f32) -> f32:\n    if x > 0.0: return 1.0\n    if x < 0.0: return -1.0\n    return 0.0\n\n## The fractional part, `x - floor(x)`, in `0..1` (VEX `frac`).\npub func frac(x: f32) -> f32:\n    return x - math32.floor(x)\n\n## `x` rounded to the nearest whole number, halves to the even one (VEX `rint`).\npub func rint(x: f32) -> f32:\n    let r = math32.floor(x + 0.5)\n    if r - x == 0.5 and math32.floor(r * 0.5) * 2.0 != r:\n        return r - 1.0\n    return r\n\n## `x` with its fraction dropped, toward zero (VEX `trunc`).\npub func trunc(x: f32) -> f32:\n    return math32.trunc(x)\n\n## The smaller of `a` and `b` (VEX `min`).\npub func min(a: f32, b: f32) -> f32:\n    return a if a < b else b\n\n## The larger of `a` and `b` (VEX `max`).\npub func max(a: f32, b: f32) -> f32:\n    return a if a > b else b\n\n## Lane-by-lane `min` of lanes 0 to 2; lane 3 is zero.\npub func min3(a: f32[4], b: f32[4]) -> f32[4]:\n    return [min(a[0], b[0]), min(a[1], b[1]), min(a[2], b[2]), 0.0]\n\n## Lane-by-lane `max` of lanes 0 to 2; lane 3 is zero.\npub func max3(a: f32[4], b: f32[4]) -> f32[4]:\n    return [max(a[0], b[0]), max(a[1], b[1]), max(a[2], b[2]), 0.0]\n\n## The smallest of lanes 0 to 2 (VEX `min(vector)`).\npub func minv(v: f32[4]) -> f32:\n    return min(v[0], min(v[1], v[2]))\n\n## The largest of lanes 0 to 2 (VEX `max(vector)`).\npub func maxv(v: f32[4]) -> f32:\n    return max(v[0], max(v[1], v[2]))\n\n## The mean of lanes 0 to 2 (VEX `avg(vector)`).\npub func avg(v: f32[4]) -> f32:\n    return (v[0] + v[1] + v[2]) * (1.0 / 3.0)\n\n## The sum of lanes 0 to 2 (VEX `sum(vector)`).\npub func sum(v: f32[4]) -> f32:\n    return v[0] + v[1] + v[2]\n\n## The product of lanes 0 to 2 (VEX `product(vector)`).\npub func product(v: f32[4]) -> f32:\n    return v[0] * v[1] * v[2]\n\n## `a` to the power `b` (VEX `pow`).\npub func pow(a: f32, b: f32) -> f32:\n    return math32.pow(a, b)\n\n## e to the power `x` (VEX `exp`).\npub func exp(x: f32) -> f32:\n    return math32.exp(x)\n\n## The natural logarithm (VEX `log`).\npub func log(x: f32) -> f32:\n    return math32.log(x)\n\n## The base-10 logarithm (VEX `log10`).\npub func log10(x: f32) -> f32:\n    return math32.log10(x)\n\n## The cube root, negative for negative `x` (VEX `cbrt`).\npub func cbrt(x: f32) -> f32:\n    return math32.cbrt(x)\n\n## `x` half turns reduced to `-1..1`, so whole and half turns stay exact.\nfunc half_turns(x: f32) -> f32:\n    return x - 2.0 * math32.floor(x * 0.5 + 0.5)\n\n## `sin(π x)`, exactly 0 at whole `x` and ±1 at halves.\npub func sinpi(x: f32) -> f32:\n    let r = half_turns(x)\n    if r == 0.0 or r == 1.0 or r == -1.0: return 0.0\n    if r == 0.5: return 1.0\n    if r == -0.5: return -1.0\n    return math32.sin(math32.pi * r)\n\n## `cos(π x)`, exactly ±1 at whole `x` and 0 at halves.\npub func cospi(x: f32) -> f32:\n    let r = half_turns(x)\n    if r == 0.5 or r == -0.5: return 0.0\n    if r == 0.0: return 1.0\n    if r == 1.0 or r == -1.0: return -1.0\n    return math32.cos(math32.pi * r)\n\n## `tan(π x)`: exactly 0 at whole `x`.\npub func tanpi(x: f32) -> f32:\n    return sinpi(x) / cospi(x)\n\n## A polynomial's real roots: `count` of them in `roots`, ascending.\npub struct Roots:\n    pub var count: i32 = 0\n    pub var roots: f32[4] = [0.0, 0.0, 0.0, 0.0]\n\n## The real roots of `a x² + b x + c` (VEX `solvequadratic`); `a` zero solves the line,\n## and a polynomial that is zero everywhere has no roots listed.\npub func solvequadratic(a: f32, b: f32, c: f32) -> Roots:\n    let a64 = (f64)a\n    let b64 = (f64)b\n    let c64 = (f64)c\n    if a64 == 0.0:\n        if b64 == 0.0:\n            return Roots()\n        return Roots(count = 1, roots = [(f32)(-c64 / b64), 0.0, 0.0, 0.0])\n    let disc = b64 * b64 - 4.0 * a64 * c64\n    if disc < 0.0:\n        return Roots()\n    if disc == 0.0:\n        return Roots(count = 1, roots = [(f32)(-b64 / (2.0 * a64)), 0.0, 0.0, 0.0])\n    # The stable pair: no cancellation between -b and the root of the discriminant.\n    let q = -0.5 * (b64 + (math.sqrt(disc) if b64 >= 0.0 else -math.sqrt(disc)))\n    let r1 = q / a64\n    let r2 = c64 / q\n    let low = r1 if r1 < r2 else r2\n    let high = r2 if r1 < r2 else r1\n    return Roots(count = 2, roots = [(f32)low, (f32)high, 0.0, 0.0])\n\n## The real roots of `a x³ + b x² + c x + d` (VEX `solvecubic`), each repeated root once;\n## `a` zero solves the quadratic.\npub func solvecubic(a: f32, b: f32, c: f32, d: f32) -> Roots:\n    if a == 0.0:\n        return solvequadratic(b, c, d)\n    let p2 = (f64)b / (f64)a\n    let p1 = (f64)c / (f64)a\n    let p0 = (f64)d / (f64)a\n    # The depressed cubic t³ + p t + q with x = t - p2 / 3.\n    let shift = p2 / 3.0\n    let p = p1 - p2 * p2 / 3.0\n    let q = 2.0 * p2 * p2 * p2 / 27.0 - p2 * p1 / 3.0 + p0\n    let disc = q * q / 4.0 + p * p * p / 27.0\n    if math.abs(disc) < 1.0e-12 * (1.0 + q * q):\n        if math.abs(p) < 1.0e-12:\n            return Roots(count = 1, roots = [(f32)(-shift), 0.0, 0.0, 0.0])\n        # A double root and a single one.\n        let single = 3.0 * q / p\n        let twice = -1.5 * q / p\n        let low = (single if single < twice else twice) - shift\n        let high = (twice if single < twice else single) - shift\n        return Roots(count = 2, roots = [(f32)low, (f32)high, 0.0, 0.0])\n    if disc > 0.0:\n        let s = math.sqrt(disc)\n        let t = math.cbrt(-q / 2.0 + s) + math.cbrt(-q / 2.0 - s)\n        return Roots(count = 1, roots = [(f32)(t - shift), 0.0, 0.0, 0.0])\n    # Three real roots: the trigonometric form, ascending.\n    let m = 2.0 * math.sqrt(-p / 3.0)\n    let angle = math.acos(math.clamp(3.0 * q / (p * m), -1.0, 1.0)) / 3.0\n    let r0 = m * math.cos(angle) - shift\n    let r1 = m * math.cos(angle - 2.0 * math.pi / 3.0) - shift\n    let r2 = m * math.cos(angle - 4.0 * math.pi / 3.0) - shift\n    let low = math.min(r0, math.min(r1, r2))\n    let high = math.max(r0, math.max(r1, r2))\n    let middle = r0 + r1 + r2 - low - high\n    return Roots(count = 3, roots = [(f32)low, (f32)middle, (f32)high, 0.0])\n\n## The distance from `point` to the infinite line through `origin` along `direction`\n## (VEX `distance_pointline`); a zero direction measures to `origin`.\npub func distance_pointline(point: f32[4], origin: f32[4], direction: f32[4]) -> f32:\n    let d = [direction[0], direction[1], direction[2], 0.0]\n    let to = [point[0] - origin[0], point[1] - origin[1], point[2] - origin[2], 0.0]\n    let size2 = dot(d, d)\n    if size2 == 0.0:\n        return length(to)\n    return length(to - d * (dot(to, d) / size2))\n\n## The distance from `point` to the segment from `a` to `b` (VEX `distance_pointsegment`).\npub func distance_pointsegment(point: f32[4], a: f32[4], b: f32[4]) -> f32:\n    let d = [b[0] - a[0], b[1] - a[1], b[2] - a[2], 0.0]\n    let to = [point[0] - a[0], point[1] - a[1], point[2] - a[2], 0.0]\n    let size2 = dot(d, d)\n    if size2 == 0.0:\n        return length(to)\n    let t = math32.clamp(dot(to, d) / size2, 0.0, 1.0)\n    return length(to - d * t)\n\n## The distance from `point` to the ray from `origin` along `direction` (VEX\n## `distance_pointray`).\npub func distance_pointray(point: f32[4], origin: f32[4], direction: f32[4]) -> f32:\n    let d = [direction[0], direction[1], direction[2], 0.0]\n    let to = [point[0] - origin[0], point[1] - origin[1], point[2] - origin[2], 0.0]\n    let size2 = dot(d, d)\n    if size2 == 0.0:\n        return length(to)\n    let t = math32.max(dot(to, d) / size2, 0.0)\n    return length(to - d * t)\n\n## The distance from `point` to the plane through `origin` with normal `normal` (any\n## length but zero; VEX `planepointdistance`), never negative.\npub func planepointdistance(origin: f32[4], normal: f32[4], point: f32[4]) -> f32:\n    let n = normalized([normal[0], normal[1], normal[2], 0.0])\n    return math32.abs(dot([point[0] - origin[0], point[1] - origin[1], point[2] - origin[2], 0.0], n))\n",
    "# Fitting without clamps, and splines through key values (VEX `efit`, `fit10`, `fit11`,\n# `invlerp`, `lspline`, `cspline`, `kspline`, `spline`). Base has no variadic calls, so a\n# spline takes its keys as an `f32[16]` and how many of them count (1 to 16); its basis is\n# a number, the same codes luce-std's ramps use.\n\n## Each key holds until the next.\npub let spline_constant: i32 = 0\n## Straight lines between keys.\npub let spline_linear: i32 = 1\n## Catmull-Rom through the inner keys; the first and last only shape the ends (VEX\n## \"catrom\").\npub let spline_catrom: i32 = 2\n## A cubic through every key that never overshoots between them (VEX \"monotonecubic\").\npub let spline_monotone: i32 = 3\n## A uniform cubic B-spline over the keys, near but not through them (VEX \"bspline\").\npub let spline_bspline: i32 = 4\n\n## `value` mapped from `old_low..old_high` onto `new_low..new_high` without clamping (VEX\n## `efit`); an empty old range gives `new_low`.\npub func efit(value: f32, old_low: f32, old_high: f32, new_low: f32, new_high: f32) -> f32:\n    if old_low == old_high:\n        return new_low\n    return new_low + (value - old_low) / (old_high - old_low) * (new_high - new_low)\n\n## `value` clamped to `0..1` and mapped from 1 onto `new_low`, 0 onto `new_high` (VEX\n## `fit10`).\npub func fit10(value: f32, new_low: f32, new_high: f32) -> f32:\n    return fit(value, 1.0, 0.0, new_low, new_high)\n\n## `value` clamped to `-1..1` and mapped onto `new_low..new_high` (VEX `fit11`).\npub func fit11(value: f32, new_low: f32, new_high: f32) -> f32:\n    return fit(value, -1.0, 1.0, new_low, new_high)\n\n## Where `value` lies from `low` (0) to `high` (1), unclamped (VEX `invlerp`); an empty\n## range gives 0.\npub func invlerp(value: f32, low: f32, high: f32) -> f32:\n    if low == high:\n        return 0.0\n    return (value - low) / (high - low)\n\n## Straight lines through the first `count` keys, spread evenly over `t` in `0..1` (VEX\n## `lspline`).\npub func lspline(t: f32, keys: f32[16], count: i32) -> f32:\n    return spline(spline_linear, t, keys, count)\n\n## Catmull-Rom through the inner keys of the first `count`, spread evenly over `t` in\n## `0..1` (VEX `cspline`).\npub func cspline(t: f32, keys: f32[16], count: i32) -> f32:\n    return spline(spline_catrom, t, keys, count)\n\n## The spline of `basis` over the first `count` keys at `t` (clamped to `0..1`) (VEX\n## `spline`). The interpolating bases put key `i` at `t = i / (count - 1)`; Catmull-Rom\n## and the B-spline span the inner keys. Too few keys for a cubic fall back to lines.\npub func spline(basis: i32, t: f32, keys: f32[16], count: i32) -> f32:\n    let n = count if count < 16 else 16\n    if n < 1:\n        return 0.0\n    if n == 1:\n        return keys[0]\n    let u = math32.clamp(t, 0.0, 1.0)\n    if (basis == spline_catrom or basis == spline_bspline) and n >= 4:\n        let position = u * (f32)(n - 3)\n        let i = min_index((i32)math32.floor(position), n - 4)\n        let f = position - (f32)i\n        let p0 = keys[(usize)i]\n        let p1 = keys[(usize)(i + 1)]\n        let p2 = keys[(usize)(i + 2)]\n        let p3 = keys[(usize)(i + 3)]\n        if basis == spline_catrom:\n            return 0.5 * (2.0 * p1 + (p2 - p0) * f + (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * f * f + (3.0 * p1 - p0 - 3.0 * p2 + p3) * f * f * f)\n        let g = 1.0 - f\n        return (g * g * g * p0 + (3.0 * f * f * f - 6.0 * f * f + 4.0) * p1 + (-3.0 * f * f * f + 3.0 * f * f + 3.0 * f + 1.0) * p2 + f * f * f * p3) * (1.0 / 6.0)\n    let position = u * (f32)(n - 1)\n    let i = min_index((i32)math32.floor(position), n - 2)\n    let f = position - (f32)i\n    let a = keys[(usize)i]\n    let b = keys[(usize)(i + 1)]\n    if basis == spline_constant:\n        return b if f >= 1.0 else a\n    if basis == spline_monotone:\n        return monotone(keys, n, i, f)\n    return a + (b - a) * f\n\nfunc min_index(i: i32, most: i32) -> i32:\n    if i < 0: return 0\n    return i if i < most else most\n\n## Fritsch-Carlson monotone cubic between keys `i` and `i + 1` of `n` evenly spaced keys.\nfunc monotone(keys: f32[16], n: i32, i: i32, f: f32) -> f32:\n    let a = keys[(usize)i]\n    let b = keys[(usize)(i + 1)]\n    let slope = b - a\n    # Neighbor indices stay in range: both sides of a choice may be read.\n    let before = keys[(usize)(i - 1 if i > 0 else i)]\n    let after = keys[(usize)(i + 2 if i + 2 < n else i + 1)]\n    var m0 = slope if i == 0 else tangent(a - before, slope)\n    var m1 = slope if i + 2 >= n else tangent(slope, after - b)\n    if slope == 0.0:\n        m0 = 0.0\n        m1 = 0.0\n    let f2 = f * f\n    let f3 = f2 * f\n    return (2.0 * f3 - 3.0 * f2 + 1.0) * a + (f3 - 2.0 * f2 + f) * m0 + (-2.0 * f3 + 3.0 * f2) * b + (f3 - f2) * m1\n\n## A key's tangent from the slopes on its two sides: zero at a turn, else their harmonic\n## mean (which keeps the curve between the keys).\nfunc tangent(left: f32, right: f32) -> f32:\n    if left * right <= 0.0:\n        return 0.0\n    return 2.0 * left * right / (left + right)\n\n## The spline of `basis` through keys placed at `knots` (ascending; the first `count` of\n## each) at `t` (VEX `kspline`): `t` finds its place between the knots of the keys the\n## curve spans (the inner ones for Catmull-Rom and the B-spline), which then reads the\n## evenly spaced spline there.\npub func kspline(basis: i32, t: f32, keys: f32[16], knots: f32[16], count: i32) -> f32:\n    let n = count if count < 16 else 16\n    if n < 2:\n        return keys[0] if n == 1 else 0.0\n    let inner = (basis == spline_catrom or basis == spline_bspline) and n >= 4\n    let first: i32 = 1 if inner else 0\n    let last = n - 2 if inner else n - 1\n    var place: f32 = 0.0\n    if t >= knots[(usize)last]:\n        place = 1.0\n    elif t > knots[(usize)first]:\n        for i: i32 in first..<last:\n            let low = knots[(usize)i]\n            let high = knots[(usize)(i + 1)]\n            if t >= low and t < high:\n                place = ((f32)(i - first) + (t - low) / (high - low)) / (f32)(last - first)\n    return spline(basis, place, keys, n)\n",
    "# 3×3 and 4×4 matrices, VEX's `matrix3` and `matrix`. Like VEX, a vector multiplies a\n# matrix from the left (`v * M`): the rows are the images of the axes (a 4×4's last row is\n# the translation), and `a.multiply(b)` applies `a` first, then `b`. Rows are `f32[4]`;\n# a `Mat3` row keeps lane 3 at zero.\n\n## A 3×3 matrix: rows `x`, `y` and `z`, the images of the three axes.\npub struct Mat3:\n    pub var x: f32[4] = [1.0, 0.0, 0.0, 0.0]\n    pub var y: f32[4] = [0.0, 1.0, 0.0, 0.0]\n    pub var z: f32[4] = [0.0, 0.0, 1.0, 0.0]\n\n    ## The identity (VEX `ident()`).\n    pub static func identity() -> Mat3:\n        return Mat3(x = [1.0, 0.0, 0.0, 0.0], y = [0.0, 1.0, 0.0, 0.0], z = [0.0, 0.0, 1.0, 0.0])\n\n    ## The rotation by `angle` radians about `axis` (any length but zero), counterclockwise\n    ## looking down the axis, as VEX's `rotate` makes it.\n    pub static func rotation(angle: f32, axis: f32[4]) -> Mat3:\n        let k = normalized([axis[0], axis[1], axis[2], 0.0])\n        let c = math32.cos(angle)\n        let s = math32.sin(angle)\n        let t = 1.0 - c\n        return Mat3(x = [c + k[0] * k[0] * t, k[0] * k[1] * t + k[2] * s, k[0] * k[2] * t - k[1] * s, 0.0], y = [k[0] * k[1] * t - k[2] * s, c + k[1] * k[1] * t, k[1] * k[2] * t + k[0] * s, 0.0], z = [k[0] * k[2] * t + k[1] * s, k[1] * k[2] * t - k[0] * s, c + k[2] * k[2] * t, 0.0])\n\n    ## The scale by `s[0]`, `s[1]` and `s[2]` along the axes.\n    pub static func scaling(s: f32[4]) -> Mat3:\n        return Mat3(x = [s[0], 0.0, 0.0, 0.0], y = [0.0, s[1], 0.0, 0.0], z = [0.0, 0.0, s[2], 0.0])\n\n    ## Row `row`, column `column` (both 0 to 2; anything else reads 0).\n    pub func at(row: i32, column: i32) -> f32:\n        if column < 0 or column > 2: return 0.0\n        if row == 0: return self.x[column]\n        if row == 1: return self.y[column]\n        if row == 2: return self.z[column]\n        return 0.0\n\n    ## Rows and columns swapped (VEX `transpose`).\n    pub func transposed() -> Mat3:\n        return Mat3(x = [self.x[0], self.y[0], self.z[0], 0.0], y = [self.x[1], self.y[1], self.z[1], 0.0], z = [self.x[2], self.y[2], self.z[2], 0.0])\n\n    ## The determinant (VEX `determinant`).\n    pub func determinant() -> f32:\n        return dot(self.x, cross(self.y, self.z))\n\n    ## The inverse (VEX `invert`); a singular matrix (determinant 0) gives the zero matrix.\n    pub func inverted() -> Mat3:\n        let det = self.determinant()\n        if det == 0.0:\n            return Mat3(x = f32[4](0.0), y = f32[4](0.0), z = f32[4](0.0))\n        let r = 1.0 / det\n        let a = cross(self.y, self.z) * r\n        let b = cross(self.z, self.x) * r\n        let c = cross(self.x, self.y) * r\n        return Mat3(x = [a[0], b[0], c[0], 0.0], y = [a[1], b[1], c[1], 0.0], z = [a[2], b[2], c[2], 0.0])\n\n    ## This matrix, then `other`: VEX's `self * other`.\n    pub func multiply(other: Mat3) -> Mat3:\n        return Mat3(x = other.transform(self.x), y = other.transform(self.y), z = other.transform(self.z))\n\n    ## `v * self`: `v` (lanes 0 to 2) transformed; lane 3 of the result is zero.\n    pub func transform(v: f32[4]) -> f32[4]:\n        return self.x * v[0] + self.y * v[1] + self.z * v[2]\n\n    ## This matrix, then the rotation by `angle` radians about `axis` (VEX `rotate`).\n    pub func rotate(angle: f32, axis: f32[4]) -> Mat3:\n        return self.multiply(Mat3.rotation(angle, axis))\n\n    ## The rotation, then this matrix (VEX `prerotate`).\n    pub func prerotate(angle: f32, axis: f32[4]) -> Mat3:\n        return Mat3.rotation(angle, axis).multiply(self)\n\n    ## This matrix, then the scale `s` (VEX `scale`).\n    pub func scale(s: f32[4]) -> Mat3:\n        return self.multiply(Mat3.scaling(s))\n\n    ## The scale `s`, then this matrix (VEX `prescale`).\n    pub func prescale(s: f32[4]) -> Mat3:\n        return Mat3.scaling(s).multiply(self)\n\n## A 4×4 matrix: rows `x`, `y` and `z` (the images of the axes, lane 3 their projective\n## part) and `w`, whose lanes 0 to 2 are the translation.\npub struct Mat4:\n    pub var x: f32[4] = [1.0, 0.0, 0.0, 0.0]\n    pub var y: f32[4] = [0.0, 1.0, 0.0, 0.0]\n    pub var z: f32[4] = [0.0, 0.0, 1.0, 0.0]\n    pub var w: f32[4] = [0.0, 0.0, 0.0, 1.0]\n\n    ## The identity (VEX `ident()`).\n    pub static func identity() -> Mat4:\n        return Mat4(x = [1.0, 0.0, 0.0, 0.0], y = [0.0, 1.0, 0.0, 0.0], z = [0.0, 0.0, 1.0, 0.0], w = [0.0, 0.0, 0.0, 1.0])\n\n    ## `m` as the linear part, translating by `t` (lanes 0 to 2).\n    pub static func of(m: Mat3, t: f32[4]) -> Mat4:\n        return Mat4(x = m.x, y = m.y, z = m.z, w = [t[0], t[1], t[2], 1.0])\n\n    ## The translation by `t`.\n    pub static func translation(t: f32[4]) -> Mat4:\n        return Mat4.of(Mat3.identity(), t)\n\n    ## The rotation by `angle` radians about `axis` (see Mat3.rotation).\n    pub static func rotation(angle: f32, axis: f32[4]) -> Mat4:\n        return Mat4.of(Mat3.rotation(angle, axis), f32[4](0.0))\n\n    ## The scale by `s[0]`, `s[1]` and `s[2]`.\n    pub static func scaling(s: f32[4]) -> Mat4:\n        return Mat4.of(Mat3.scaling(s), f32[4](0.0))\n\n    ## Row `row`, column `column` (both 0 to 3; anything else reads 0).\n    pub func at(row: i32, column: i32) -> f32:\n        if column < 0 or column > 3: return 0.0\n        if row == 0: return self.x[column]\n        if row == 1: return self.y[column]\n        if row == 2: return self.z[column]\n        if row == 3: return self.w[column]\n        return 0.0\n\n    ## The upper-left 3×3: the linear part.\n    pub func mat3() -> Mat3:\n        return Mat3(x = [self.x[0], self.x[1], self.x[2], 0.0], y = [self.y[0], self.y[1], self.y[2], 0.0], z = [self.z[0], self.z[1], self.z[2], 0.0])\n\n    ## The translation (lanes 0 to 2 of row `w`).\n    pub func translation_part() -> f32[4]:\n        return [self.w[0], self.w[1], self.w[2], 0.0]\n\n    ## Rows and columns swapped (VEX `transpose`).\n    pub func transposed() -> Mat4:\n        return Mat4(x = [self.x[0], self.y[0], self.z[0], self.w[0]], y = [self.x[1], self.y[1], self.z[1], self.w[1]], z = [self.x[2], self.y[2], self.z[2], self.w[2]], w = [self.x[3], self.y[3], self.z[3], self.w[3]])\n\n    ## The determinant (VEX `determinant`).\n    pub func determinant() -> f32:\n        let m = minors(self)\n        return m.s[0] * m.c[3] - m.s[1] * m.c[2] + m.s[2] * m.c[1] + m.s[3] * m.c[0]\n\n    ## The inverse (VEX `invert`); a singular matrix (determinant 0) gives the zero matrix.\n    pub func inverted() -> Mat4:\n        let m = minors(self)\n        let det = m.s[0] * m.c[3] - m.s[1] * m.c[2] + m.s[2] * m.c[1] + m.s[3] * m.c[0]\n        if det == 0.0:\n            return Mat4(x = f32[4](0.0), y = f32[4](0.0), z = f32[4](0.0), w = f32[4](0.0))\n        let r = 1.0 / det\n        let a = self\n        # The adjugate from the 2×2 minors of the top rows (s) and the bottom rows (c).\n        let s0 = m.s[0]\n        let s1 = m.s[1]\n        let s2 = m.s[2]\n        let s3 = m.s[3]\n        let s4 = m.t[0]\n        let s5 = m.t[1]\n        let c5 = m.c[3]\n        let c4 = m.c[2]\n        let c3 = m.c[1]\n        let c2 = m.c[0]\n        let c1 = m.t[2]\n        let c0 = m.t[3]\n        return Mat4(x = [(a.y[1] * c5 - a.y[2] * c4 + a.y[3] * c3) * r, (-a.x[1] * c5 + a.x[2] * c4 - a.x[3] * c3) * r, (a.w[1] * s5 - a.w[2] * s4 + a.w[3] * s3) * r, (-a.z[1] * s5 + a.z[2] * s4 - a.z[3] * s3) * r], y = [(-a.y[0] * c5 + a.y[2] * c2 - a.y[3] * c1) * r, (a.x[0] * c5 - a.x[2] * c2 + a.x[3] * c1) * r, (-a.w[0] * s5 + a.w[2] * s2 - a.w[3] * s1) * r, (a.z[0] * s5 - a.z[2] * s2 + a.z[3] * s1) * r], z = [(a.y[0] * c4 - a.y[1] * c2 + a.y[3] * c0) * r, (-a.x[0] * c4 + a.x[1] * c2 - a.x[3] * c0) * r, (a.w[0] * s4 - a.w[1] * s2 + a.w[3] * s0) * r, (-a.z[0] * s4 + a.z[1] * s2 - a.z[3] * s0) * r], w = [(-a.y[0] * c3 + a.y[1] * c1 - a.y[2] * c0) * r, (a.x[0] * c3 - a.x[1] * c1 + a.x[2] * c0) * r, (-a.w[0] * s3 + a.w[1] * s1 - a.w[2] * s0) * r, (a.z[0] * s3 - a.z[1] * s1 + a.z[2] * s0) * r])\n\n    ## This matrix, then `other`: VEX's `self * other`.\n    pub func multiply(other: Mat4) -> Mat4:\n        return Mat4(x = other.transform4(self.x), y = other.transform4(self.y), z = other.transform4(self.z), w = other.transform4(self.w))\n\n    ## `v * self` for all four lanes of `v`.\n    pub func transform4(v: f32[4]) -> f32[4]:\n        return self.x * v[0] + self.y * v[1] + self.z * v[2] + self.w * v[3]\n\n    ## A position transformed (VEX `P * M`, lane 3 taken as 1); the result's lane 3 is zero\n    ## and there is no divide by the projective part (an affine matrix has none).\n    pub func transform_point(p: f32[4]) -> f32[4]:\n        let moved = self.x * p[0] + self.y * p[1] + self.z * p[2] + self.w\n        return [moved[0], moved[1], moved[2], 0.0]\n\n    ## A direction transformed: the linear part only, no translation.\n    pub func transform_vector(v: f32[4]) -> f32[4]:\n        let moved = self.x * v[0] + self.y * v[1] + self.z * v[2]\n        return [moved[0], moved[1], moved[2], 0.0]\n\n    ## This matrix, then the rotation by `angle` radians about `axis` (VEX `rotate`).\n    pub func rotate(angle: f32, axis: f32[4]) -> Mat4:\n        return self.multiply(Mat4.rotation(angle, axis))\n\n    ## The rotation, then this matrix (VEX `prerotate`).\n    pub func prerotate(angle: f32, axis: f32[4]) -> Mat4:\n        return Mat4.rotation(angle, axis).multiply(self)\n\n    ## This matrix, then the scale `s` (VEX `scale`).\n    pub func scale(s: f32[4]) -> Mat4:\n        return self.multiply(Mat4.scaling(s))\n\n    ## The scale `s`, then this matrix (VEX `prescale`).\n    pub func prescale(s: f32[4]) -> Mat4:\n        return Mat4.scaling(s).multiply(self)\n\n    ## This matrix, then the translation by `t` (VEX `translate`).\n    pub func translate(t: f32[4]) -> Mat4:\n        return self.multiply(Mat4.translation(t))\n\n    ## The translation by `t`, then this matrix (VEX `pretranslate`).\n    pub func pretranslate(t: f32[4]) -> Mat4:\n        return Mat4.translation(t).multiply(self)\n\n## The 2×2 minors a 4×4 determinant and inverse share: `s` and `t[0..1]` of the top two\n## rows, `c` and `t[2..3]` of the bottom two.\nstruct Minors:\n    var s: f32[4]\n    var c: f32[4]\n    var t: f32[4]\n\nfunc minors(a: Mat4) -> Minors:\n    let s = [a.x[0] * a.y[1] - a.y[0] * a.x[1], a.x[0] * a.y[2] - a.y[0] * a.x[2], a.x[0] * a.y[3] - a.y[0] * a.x[3], a.x[1] * a.y[2] - a.y[1] * a.x[2]]\n    let c = [a.z[0] * a.w[3] - a.w[0] * a.z[3], a.z[1] * a.w[2] - a.w[1] * a.z[2], a.z[1] * a.w[3] - a.w[1] * a.z[3], a.z[2] * a.w[3] - a.w[2] * a.z[3]]\n    let t = [a.x[1] * a.y[3] - a.y[1] * a.x[3], a.x[2] * a.y[3] - a.y[2] * a.x[3], a.z[0] * a.w[2] - a.w[0] * a.z[2], a.z[0] * a.w[1] - a.w[0] * a.z[1]]\n    return Minors(s = s, c = c, t = t)\n\n## The 3×3 identity (VEX `ident()` as a `matrix3`).\npub func ident3() -> Mat3:\n    return Mat3.identity()\n\n## The 4×4 identity (VEX `ident()` as a `matrix`).\npub func ident4() -> Mat4:\n    return Mat4.identity()\n",
    "# Building and taking apart transforms, VEX style: transform and rotation orders, Euler\n# angles, `maketransform` / `cracktransform`, `lookat`, `dihedral` and `polardecomp`.\n\n## Transform orders (VEX's `XFORM_SRT` ...): which of scale, rotate and translate apply\n## first. `xform_srt` scales, then rotates, then translates (VEX's default).\npub let xform_srt: i32 = 0\n## Scale, translate, rotate.\npub let xform_str: i32 = 1\n## Rotate, scale, translate.\npub let xform_rst: i32 = 2\n## Rotate, translate, scale.\npub let xform_rts: i32 = 3\n## Translate, scale, rotate.\npub let xform_tsr: i32 = 4\n## Translate, rotate, scale.\npub let xform_trs: i32 = 5\n\n## Rotation orders (VEX's `XFORM_XYZ` ...): the axis rotated about first, second and\n## third. `xform_xyz` rotates about X, then Y, then Z (VEX's default).\npub let xform_xyz: i32 = 0\n## X, then Z, then Y.\npub let xform_xzy: i32 = 1\n## Y, then X, then Z.\npub let xform_yxz: i32 = 2\n## Y, then Z, then X.\npub let xform_yzx: i32 = 3\n## Z, then X, then Y.\npub let xform_zxy: i32 = 4\n## Z, then Y, then X.\npub let xform_zyx: i32 = 5\n\n## The axis rotation order `order` turns about at step `step` (0 first, 2 last).\nfunc order_axis(order: i32, step: i32) -> i32:\n    let firsts: i32[6] = [0, 0, 1, 1, 2, 2]\n    let seconds: i32[6] = [1, 2, 0, 2, 0, 1]\n    let o = (usize)(order if order >= 0 and order <= 5 else 0)\n    if step == 0: return firsts[o]\n    if step == 1: return seconds[o]\n    return 3 - firsts[o] - seconds[o]\n\n## Unit axis `axis` (0, 1 or 2).\nfunc unit_axis(axis: i32) -> f32[4]:\n    if axis == 0: return [1.0, 0.0, 0.0, 0.0]\n    if axis == 1: return [0.0, 1.0, 0.0, 0.0]\n    return [0.0, 0.0, 1.0, 0.0]\n\n## The rotation turning by `angles[0]`, `angles[1]` and `angles[2]` radians about X, Y\n## and Z, in rotation order `order`.\npub func euler_matrix(angles: f32[4], order: i32) -> Mat3:\n    var m = Mat3.identity()\n    for step: i32 in 0..<3:\n        let axis = order_axis(order, step)\n        m = m.rotate(angles[(usize)axis], unit_axis(axis))\n    return m\n\n## Angles about X, Y and Z (radians) that `euler_matrix` turns into rotation `m` for\n## rotation order `order`; the middle axis's angle is within ±π/2.\npub func matrix_euler(m: Mat3, order: i32) -> f32[4]:\n    # In column form the rotation is R(third) R(second) R(first): Euler angles about\n    # (third, second, first), the classic Tait-Bryan extraction.\n    let a0 = order_axis(order, 2)\n    let a1 = order_axis(order, 1)\n    let odd = (a0 + 1) % 3 != a1\n    let i = a0\n    let j = (a0 + 2) % 3 if odd else (a0 + 1) % 3\n    let k = (a0 + 1) % 3 if odd else (a0 + 2) % 3\n    # Column-form entry (r, c) is the row-form entry (c, r).\n    var r0 = math32.atan2(m.at(k, j), m.at(k, k))\n    let c2 = math32.hypot(m.at(i, i), m.at(j, i))\n    var r1 = math32.atan2(-m.at(k, i), c2)\n    let s1 = math32.sin(r0)\n    let c1 = math32.cos(r0)\n    var r2 = math32.atan2(s1 * m.at(i, k) - c1 * m.at(i, j), c1 * m.at(j, j) - s1 * m.at(j, k))\n    if not odd:\n        r0 = -r0\n        r1 = -r1\n        r2 = -r2\n    var angles = f32[4](0.0)\n    angles[(usize)a0] = r0\n    angles[(usize)a1] = r1\n    angles[(usize)order_axis(order, 0)] = r2\n    return angles\n\n## The transform scaling by `s`, rotating by `r` (degrees about X, Y and Z, in rotation\n## order `xyz`) and translating by `t`, in transform order `trs`, about pivot `pivot`\n## (VEX `maketransform(trs, xyz, t, r, s, p)`).\npub func maketransform(trs: i32, xyz: i32, t: f32[4], r: f32[4], s: f32[4], pivot: f32[4] = [0.0, 0.0, 0.0, 0.0]) -> Mat4:\n    let rotate = Mat4.of(euler_matrix(r * (math32.pi / 180.0), xyz), f32[4](0.0))\n    let scale = Mat4.scaling(s)\n    let move = Mat4.translation(t)\n    var m = Mat4.identity()\n    if trs == xform_str: m = scale.multiply(move).multiply(rotate)\n    elif trs == xform_rst: m = rotate.multiply(scale).multiply(move)\n    elif trs == xform_rts: m = rotate.multiply(move).multiply(scale)\n    elif trs == xform_tsr: m = move.multiply(scale).multiply(rotate)\n    elif trs == xform_trs: m = move.multiply(rotate).multiply(scale)\n    else: m = scale.multiply(rotate).multiply(move)\n    if pivot[0] == 0.0 and pivot[1] == 0.0 and pivot[2] == 0.0:\n        return m\n    return Mat4.translation(pivot * -1.0).multiply(m).multiply(Mat4.translation(pivot))\n\n## One part of `m` as `maketransform(trs, xyz, ...)` with pivot `pivot` would build it:\n## `c` 0 the translation, 1 the rotation (degrees about X, Y and Z), 2 the scale (VEX\n## `cracktransform(trs, xyz, c, p, m)`). Shear is not taken apart: a sheared matrix\n## gives the nearest rotation and the axes' lengths.\npub func cracktransform(trs: i32, xyz: i32, c: i32, pivot: f32[4], m: Mat4) -> f32[4]:\n    let whole = Mat4.translation(pivot).multiply(m).multiply(Mat4.translation(pivot * -1.0))\n    let linear = whole.mat3()\n    # Scale before rotation scales the rows; rotation before scale, the columns.\n    let rows_scaled = trs == xform_srt or trs == xform_str or trs == xform_tsr\n    var scale = f32[4](0.0)\n    var turn = Mat3.identity()\n    if rows_scaled:\n        scale = [length(linear.x), length(linear.y), length(linear.z), 0.0]\n        turn = Mat3(x = linear.x * (1.0 / nonzero(scale[0])), y = linear.y * (1.0 / nonzero(scale[1])), z = linear.z * (1.0 / nonzero(scale[2])))\n    else:\n        let columns = linear.transposed()\n        scale = [length(columns.x), length(columns.y), length(columns.z), 0.0]\n        turn = Mat3(x = columns.x * (1.0 / nonzero(scale[0])), y = columns.y * (1.0 / nonzero(scale[1])), z = columns.z * (1.0 / nonzero(scale[2]))).transposed()\n    if turn.determinant() < 0.0:\n        # A mirror: one negative scale keeps the rest a rotation.\n        scale[0] = -scale[0]\n        turn = Mat3.scaling([-1.0, 1.0, 1.0, 0.0]).multiply(turn) if rows_scaled else turn.multiply(Mat3.scaling([-1.0, 1.0, 1.0, 0.0]))\n    if c == 1:\n        return matrix_euler(turn, xyz) * (180.0 / math32.pi)\n    if c == 2:\n        return scale\n    # The translation row went through whatever applies after the translation.\n    let moved = whole.translation_part()\n    if trs == xform_srt or trs == xform_rst:\n        return moved\n    if trs == xform_str:\n        return turn.transposed().transform(moved)\n    if trs == xform_rts:\n        return [moved[0] / nonzero(scale[0]), moved[1] / nonzero(scale[1]), moved[2] / nonzero(scale[2]), 0.0]\n    return linear.inverted().transform(moved)\n\nfunc nonzero(x: f32) -> f32:\n    return x if x != 0.0 else 1.0\n\n## The rotation that turns the negative Z axis toward `to - from`, keeping the Y axis as\n## close to `up` as it can (VEX `lookat`). `up` along the direction falls back to Z or Y.\npub func lookat(from_point: f32[4], to: f32[4], up: f32[4] = [0.0, 1.0, 0.0, 0.0]) -> Mat3:\n    let back = normalized([from_point[0] - to[0], from_point[1] - to[1], from_point[2] - to[2], 0.0])\n    if length2(back) == 0.0:\n        return Mat3.identity()\n    var side = cross(up, back)\n    if length2(side) < 1.0e-12:\n        side = cross([0.0, 0.0, 1.0, 0.0] if math32.abs(back[1]) > 0.9 else [0.0, 1.0, 0.0, 0.0], back)\n    let x = normalized(side)\n    return Mat3(x = x, y = cross(back, x), z = back)\n\n## The shortest rotation taking direction `a` onto direction `b` (VEX `dihedral`).\npub func dihedral(a: f32[4], b: f32[4]) -> Mat3:\n    let u = normalized([a[0], a[1], a[2], 0.0])\n    let v = normalized([b[0], b[1], b[2], 0.0])\n    let axis = cross(u, v)\n    let sine = length(axis)\n    let cosine = dot(u, v)\n    if sine < 1.0e-7:\n        if cosine > 0.0:\n            return Mat3.identity()\n        # Opposite directions: half a turn about any axis across them.\n        let across = cross(u, [0.0, 0.0, 1.0, 0.0] if math32.abs(u[0]) > 0.9 else [1.0, 0.0, 0.0, 0.0])\n        return Mat3.rotation(math32.pi, across)\n    return Mat3.rotation(math32.atan2(sine, cosine), axis)\n\n## The rotation part of `m`'s polar decomposition `m = R S`, `R` orthonormal (VEX\n## `polardecomp`): averaging with the inverse transpose until it settles.\npub func polardecomp(m: Mat3) -> Mat3:\n    var r = m\n    for _ in 0..<12:\n        let other = r.inverted().transposed()\n        r = Mat3(x = (r.x + other.x) * 0.5, y = (r.y + other.y) * 0.5, z = (r.z + other.z) * 0.5)\n    return r\n",
    "# Quaternions, VEX's `vector4` rotations: imaginary part `x`, `y`, `z`, real part `w`.\n# A rotation quaternion has length 1; `qrotate(q, v)` turns `v` by it.\n\n## A quaternion; the default is the identity rotation.\npub struct Quat:\n    pub var x: f32 = 0.0\n    pub var y: f32 = 0.0\n    pub var z: f32 = 0.0\n    pub var w: f32 = 1.0\n\n    ## The identity rotation.\n    pub static func identity() -> Quat:\n        return Quat(x = 0.0, y = 0.0, z = 0.0, w = 1.0)\n\n    ## The four parts as `[x, y, z, w]` (VEX's `vector4` layout).\n    pub func vector() -> f32[4]:\n        return [self.x, self.y, self.z, self.w]\n\n    ## Its length.\n    pub func length() -> f32:\n        return math32.sqrt(self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w)\n\n    ## Scaled to length 1; zero becomes the identity.\n    pub func normalized() -> Quat:\n        let size = self.length()\n        if size == 0.0:\n            return Quat.identity()\n        let r = 1.0 / size\n        return Quat(x = self.x * r, y = self.y * r, z = self.z * r, w = self.w * r)\n\n    ## The Hamilton product `self * other`: `other`'s rotation, then this one.\n    pub func multiply(other: Quat) -> Quat:\n        return Quat(x = self.w * other.x + self.x * other.w + self.y * other.z - self.z * other.y, y = self.w * other.y - self.x * other.z + self.y * other.w + self.z * other.x, z = self.w * other.z + self.x * other.y - self.y * other.x + self.z * other.w, w = self.w * other.w - self.x * other.x - self.y * other.y - self.z * other.z)\n\n    ## The inverse: the conjugate over the squared length; zero stays zero.\n    pub func inverted() -> Quat:\n        let size2 = self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w\n        if size2 == 0.0:\n            return Quat(x = 0.0, y = 0.0, z = 0.0, w = 0.0)\n        let r = 1.0 / size2\n        return Quat(x = -self.x * r, y = -self.y * r, z = -self.z * r, w = self.w * r)\n\n    ## `v` (lanes 0 to 2) turned by this rotation; lane 3 of the result is zero.\n    pub func rotate(v: f32[4]) -> f32[4]:\n        let u: f32[4] = [self.x, self.y, self.z, 0.0]\n        let t = cross(u, [v[0], v[1], v[2], 0.0]) * 2.0\n        let turned = [v[0], v[1], v[2], 0.0] + t * self.w + cross(u, t)\n        return [turned[0], turned[1], turned[2], 0.0]\n\n## The rotation by `angle` radians about `axis` (VEX `quaternion(angle, axis)`).\npub func quaternion(angle: f32, axis: f32[4]) -> Quat:\n    let k = normalized([axis[0], axis[1], axis[2], 0.0])\n    let s = math32.sin(angle * 0.5)\n    return Quat(x = k[0] * s, y = k[1] * s, z = k[2] * s, w = math32.cos(angle * 0.5))\n\n## The rotation matrix `m` as a quaternion (VEX `quaternion(matrix3)`).\npub func quaternion_of(m: Mat3) -> Quat:\n    # Column-form entries: r(i, j) is m's row j, column i.\n    let trace = m.x[0] + m.y[1] + m.z[2]\n    if trace > 0.0:\n        let s = math32.sqrt(trace + 1.0) * 2.0\n        return Quat(x = (m.y[2] - m.z[1]) / s, y = (m.z[0] - m.x[2]) / s, z = (m.x[1] - m.y[0]) / s, w = 0.25 * s)\n    if m.x[0] > m.y[1] and m.x[0] > m.z[2]:\n        let s = math32.sqrt(1.0 + m.x[0] - m.y[1] - m.z[2]) * 2.0\n        return Quat(x = 0.25 * s, y = (m.y[0] + m.x[1]) / s, z = (m.z[0] + m.x[2]) / s, w = (m.y[2] - m.z[1]) / s)\n    if m.y[1] > m.z[2]:\n        let s = math32.sqrt(1.0 + m.y[1] - m.x[0] - m.z[2]) * 2.0\n        return Quat(x = (m.y[0] + m.x[1]) / s, y = 0.25 * s, z = (m.z[1] + m.y[2]) / s, w = (m.z[0] - m.x[2]) / s)\n    let s = math32.sqrt(1.0 + m.z[2] - m.x[0] - m.y[1]) * 2.0\n    return Quat(x = (m.z[0] + m.x[2]) / s, y = (m.z[1] + m.y[2]) / s, z = 0.25 * s, w = (m.x[1] - m.y[0]) / s)\n\n## `a * b`: `b`'s rotation, then `a`'s (VEX `qmultiply`).\npub func qmultiply(a: Quat, b: Quat) -> Quat:\n    return a.multiply(b)\n\n## `v` turned by rotation `q` (VEX `qrotate`).\npub func qrotate(q: Quat, v: f32[4]) -> f32[4]:\n    return q.rotate(v)\n\n## The inverse of `q` (VEX `qinvert`).\npub func qinvert(q: Quat) -> Quat:\n    return q.inverted()\n\n## The rotation matrix of `q` (VEX `qconvert`): `m.transform(v)` is `qrotate(q, v)`.\npub func qconvert(q: Quat) -> Mat3:\n    return Mat3(x = q.rotate([1.0, 0.0, 0.0, 0.0]), y = q.rotate([0.0, 1.0, 0.0, 0.0]), z = q.rotate([0.0, 0.0, 1.0, 0.0]))\n\n## The rotation `t` of the way from `a` to `b` along the shorter arc, at constant speed\n## (VEX `slerp`).\npub func slerp(a: Quat, b: Quat, t: f32) -> Quat:\n    var d = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w\n    var to = b\n    if d < 0.0:\n        d = -d\n        to = Quat(x = -b.x, y = -b.y, z = -b.z, w = -b.w)\n    var wa = 1.0 - t\n    var wb = t\n    if d < 0.9995:\n        let angle = math32.acos(d)\n        let s = 1.0 / math32.sin(angle)\n        wa = math32.sin((1.0 - t) * angle) * s\n        wb = math32.sin(t * angle) * s\n    return Quat(x = a.x * wa + to.x * wb, y = a.y * wa + to.y * wb, z = a.z * wa + to.z * wb, w = a.w * wa + to.w * wb).normalized()\n\n## The rotation by `angles` (radians about X, Y and Z) in rotation order `order`\n## (`xform_xyz` ...; VEX `eulertoquaternion`).\npub func eulertoquaternion(angles: f32[4], order: i32) -> Quat:\n    var q = Quat.identity()\n    for step: i32 in 0..<3:\n        let axis = order_axis(order, step)\n        q = quaternion(angles[(usize)axis], unit_axis(axis)).multiply(q)\n    return q\n\n## Angles about X, Y and Z (radians) of rotation `q` in rotation order `order` (VEX\n## `quaterniontoeuler`).\npub func quaterniontoeuler(q: Quat, order: i32) -> f32[4]:\n    return matrix_euler(qconvert(q.normalized()), order)\n",
    "# Hashed-gradient Perlin noise with its derivative: the core of `pnoise` (periodic),\n# `flownoise` (rotating gradients), `noised`/`xnoised` (value and gradient) and `curlnoise`.\n# Unlike `noise`, whose permutation table the engine reads, each lattice corner's gradient\n# comes from `random_bits` of the corner, so the field lowers to plain column arithmetic\n# and wraps exactly at a period. Gradients are the twelve cube-edge directions, faded with\n# Perlin's quintic; the value is scaled to about -1..1, as `snoise`.\n\n## A noise value and its gradient (lanes 0 to 2; lane 3 zero).\npub struct NoiseD:\n    pub var value: f32 = 0.0\n    pub var derivative: f32[4] = [0.0, 0.0, 0.0, 0.0]\n\n## Scales the 3D sum into about -1..1 (Gustavson's factor for these gradients).\nlet gradient_scale: f32 = 0.936\n\n## The lattice coordinate `c` wrapped into `0..<period` (no wrap for a period below 1).\nfunc wrapped(c: i64, period: i32) -> i64:\n    if period < 1: return c\n    let p = (i64)period\n    return ((c % p) + p) % p\n\n## The hash of corner (x, y, z).\nfunc corner_bits(x: i64, y: i64, z: i64) -> u64:\n    let mixed = (x *% 0x9E3779B1) ^ (y *% 0x85EBCA77) ^ (z *% 0xC2B2AE3D)\n    return random_bits(mixed, 11)\n\n## One of the twelve edge directions (±1, ±1, 0) and the like, picked by `bits`.\nfunc edge_direction(bits: u64) -> f32[4]:\n    let group = (bits >> 8) % 3\n    let a: f32 = 1.0 if (bits & 1) == 0 else -1.0\n    let b: f32 = 1.0 if (bits & 2) == 0 else -1.0\n    let x: f32 = 0.0 if group == 2 else a\n    let y: f32 = b if group == 0 else (a if group == 2 else 0.0)\n    let z: f32 = 0.0 if group == 0 else b\n    return [x, y, z, 0.0]\n\n## Corner gradient for `bits`, turned by `flow` radians (times a per-corner rate in\n## 0.5..1.5) about a per-corner axis when `flow` is not zero.\nfunc corner_gradient(bits: u64, flow: f32) -> f32[4]:\n    let g = edge_direction(bits)\n    if flow == 0.0:\n        return g\n    let k = normalized(edge_direction(bits >> 16))\n    let angle = flow * (0.5 + (f32)((bits >> 40) & 1023) * (1.0 / 1023.0))\n    let c = math32.cos(angle)\n    let s = math32.sin(angle)\n    return g * c + cross(k, g) * s + k * (dot(k, g) * (1.0 - c))\n\nfunc fade(t: f32) -> f32:\n    return t * t * t * (t * (t * 6.0 - 15.0) + 10.0)\n\nfunc fade_slope(t: f32) -> f32:\n    return 30.0 * t * t * (t - 1.0) * (t - 1.0)\n\n## The eight corners of a lattice cell around `p` (wrapped by `periods`), their hashes\n## in corner order 000, 100, 010, 110, 001, 101, 011, 111, and `p`'s place in the cell.\nstruct Cell:\n    var bits: u64[8]\n    var offset: f32[4]\n\nfunc cell_of(p: f32[4], periods: i32[4]) -> Cell:\n    let fx = math32.floor(p[0])\n    let fy = math32.floor(p[1])\n    let fz = math32.floor(p[2])\n    let x0 = (i64)fx\n    let y0 = (i64)fy\n    let z0 = (i64)fz\n    let ax = wrapped(x0, periods[0])\n    let bx = wrapped(x0 + 1, periods[0])\n    let ay = wrapped(y0, periods[1])\n    let by = wrapped(y0 + 1, periods[1])\n    let az = wrapped(z0, periods[2])\n    let bz = wrapped(z0 + 1, periods[2])\n    return Cell(bits = [corner_bits(ax, ay, az), corner_bits(bx, ay, az), corner_bits(ax, by, az), corner_bits(bx, by, az), corner_bits(ax, ay, bz), corner_bits(bx, ay, bz), corner_bits(ax, by, bz), corner_bits(bx, by, bz)], offset = [p[0] - fx, p[1] - fy, p[2] - fz, 0.0])\n\n## Perlin's blend of the corners' gradients `g` (corner `k` in lanes `4k..4k+2`) at\n## `d` within the cell: the value and its gradient, scaled to about -1..1.\nfunc blend(g: f32[32], d: f32[4]) -> NoiseD:\n    let g000: f32[4] = [g[0], g[1], g[2], 0.0]\n    let g100: f32[4] = [g[4], g[5], g[6], 0.0]\n    let g010: f32[4] = [g[8], g[9], g[10], 0.0]\n    let g110: f32[4] = [g[12], g[13], g[14], 0.0]\n    let g001: f32[4] = [g[16], g[17], g[18], 0.0]\n    let g101: f32[4] = [g[20], g[21], g[22], 0.0]\n    let g011: f32[4] = [g[24], g[25], g[26], 0.0]\n    let g111: f32[4] = [g[28], g[29], g[30], 0.0]\n    let dx = d[0]\n    let dy = d[1]\n    let dz = d[2]\n    let n000 = dot(g000, [dx, dy, dz, 0.0])\n    let n100 = dot(g100, [dx - 1.0, dy, dz, 0.0])\n    let n010 = dot(g010, [dx, dy - 1.0, dz, 0.0])\n    let n110 = dot(g110, [dx - 1.0, dy - 1.0, dz, 0.0])\n    let n001 = dot(g001, [dx, dy, dz - 1.0, 0.0])\n    let n101 = dot(g101, [dx - 1.0, dy, dz - 1.0, 0.0])\n    let n011 = dot(g011, [dx, dy - 1.0, dz - 1.0, 0.0])\n    let n111 = dot(g111, [dx - 1.0, dy - 1.0, dz - 1.0, 0.0])\n    let u = fade(dx)\n    let v = fade(dy)\n    let w = fade(dz)\n    # The trilinear blend as a polynomial in the fades, and its derivative (Quilez).\n    let k1 = n100 - n000\n    let k2 = n010 - n000\n    let k3 = n001 - n000\n    let k4 = n000 - n100 - n010 + n110\n    let k5 = n000 - n010 - n001 + n011\n    let k6 = n000 - n100 - n001 + n101\n    let k7 = -n000 + n100 + n010 - n110 + n001 - n101 - n011 + n111\n    let value = n000 + k1 * u + k2 * v + k3 * w + k4 * u * v + k5 * v * w + k6 * w * u + k7 * u * v * w\n    let blended = g000 + (g100 - g000) * u + (g010 - g000) * v + (g001 - g000) * w + (g000 - g100 - g010 + g110) * (u * v) + (g000 - g010 - g001 + g011) * (v * w) + (g000 - g100 - g001 + g101) * (w * u) + (g100 + g010 - g000 - g110 + g001 - g101 - g011 + g111) * (u * v * w)\n    let slope: f32[4] = [fade_slope(dx) * (k1 + k4 * v + k6 * w + k7 * v * w), fade_slope(dy) * (k2 + k5 * w + k4 * u + k7 * w * u), fade_slope(dz) * (k3 + k6 * u + k5 * v + k7 * u * v), 0.0]\n    let gradient = (blended + slope) * gradient_scale\n    return NoiseD(value = value * gradient_scale, derivative = [gradient[0], gradient[1], gradient[2], 0.0])\n\n## The corners' gradients from bits `shift` up of each corner's hash, turned by `flow`\n## (written out: every lane index constant, so the lanes stay plain registers).\nfunc corner_gradients(cell: Cell, shift: u64, flow: f32) -> f32[32]:\n    let a = corner_gradient(cell.bits[0] >> shift, flow)\n    let b = corner_gradient(cell.bits[1] >> shift, flow)\n    let c = corner_gradient(cell.bits[2] >> shift, flow)\n    let d = corner_gradient(cell.bits[3] >> shift, flow)\n    let e = corner_gradient(cell.bits[4] >> shift, flow)\n    let f = corner_gradient(cell.bits[5] >> shift, flow)\n    let g = corner_gradient(cell.bits[6] >> shift, flow)\n    let h = corner_gradient(cell.bits[7] >> shift, flow)\n    return [a[0], a[1], a[2], 0.0, b[0], b[1], b[2], 0.0, c[0], c[1], c[2], 0.0, d[0], d[1], d[2], 0.0, e[0], e[1], e[2], 0.0, f[0], f[1], f[2], 0.0, g[0], g[1], g[2], 0.0, h[0], h[1], h[2], 0.0]\n\n## Signed gradient noise at `p` (lanes 0 to 2) with its gradient: periodic along an axis\n## whose period (`periods` lane) is 1 or more, its gradients turned by `flow` radians.\npub func gradient_noise(p: f32[4], periods: i32[4], flow: f32) -> NoiseD:\n    let cell = cell_of(p, periods)\n    return blend(corner_gradients(cell, 0, flow), cell.offset)\n\n## No period along any axis.\nlet unperiodic: i32[4] = [0, 0, 0, 0]\n\n## Periodic noise in about `0..1`, repeating every `px`, `py` and `pz` units along X, Y and\n## Z (VEX `pnoise(pos, px, py, pz)`; a period below 1 does not repeat). Its own gradient\n## lattice: not the same field as `noise`.\npub func pnoise(p: f32[4], px: i32, py: i32, pz: i32) -> f32:\n    return 0.5 + 0.5 * gradient_noise(p, [px, py, pz, 0], 0.0).value\n\n## Flow noise in about `-1..1`: gradient noise whose gradients turn by `flow` radians (each\n## lattice corner at its own rate), so advancing `flow` swirls the pattern in place (VEX\n## `flownoise(pos, flow)`; departure: signed, like `snoise`).\npub func flownoise(p: f32[4], flow: f32) -> f32:\n    return gradient_noise(p, unperiodic, flow).value\n\n## Noise in about `0..1` and its gradient (VEX `noised`): the gradient-noise lattice, not\n## `noise`'s.\npub func noised(p: f32[4]) -> NoiseD:\n    let n = gradient_noise(p, unperiodic, 0.0)\n    return NoiseD(value = 0.5 + 0.5 * n.value, derivative = n.derivative * 0.5)\n\n## Signed noise in about `-1..1` and its gradient (VEX `xnoised`; departure: Perlin, not\n## simplex, noise).\npub func xnoised(p: f32[4]) -> NoiseD:\n    return gradient_noise(p, unperiodic, 0.0)\n\n## A divergence-free field: the curl of three gradient-noise potentials (VEX `curlnoise`).\n## Its divergence is zero analytically; each lane is about `-2..2`.\npub func curlnoise(p: f32[4]) -> f32[4]:\n    # Three potentials on one lattice: each corner's hash gives three gradients.\n    let cell = cell_of(p, unperiodic)\n    let a = blend(corner_gradients(cell, 0, 0.0), cell.offset).derivative\n    let b = blend(corner_gradients(cell, 20, 0.0), cell.offset).derivative\n    let c = blend(corner_gradients(cell, 40, 0.0), cell.offset).derivative\n    return [c[1] - b[2], a[2] - c[0], b[0] - a[1], 0.0]\n\n## A divergence-free 2D field: the gradient of 2D noise turned a quarter (VEX\n## `curlnoise2d`).\npub func curlnoise2d(p: f32[2]) -> f32[2]:\n    let d = gradient_noise([p[0], p[1], 0.0, 0.0], unperiodic, 0.0).derivative\n    return [d[1], -d[0]]\n\n## Fractal noise of `turbulence` octaves (VEX `onoise(pos, turbulence, rough, atten)`):\n## octave `i` is gradient noise at `p * 2^i` weighted `rough^i`, the weighted mean taken\n## into `0..1` and raised to `atten` (1 keeps it, above 1 darkens).\npub func onoise(p: f32[4], turbulence: i32, rough: f32, atten: f32) -> f32:\n    var total: f32 = 0.0\n    var weight: f32 = 0.0\n    var amplitude: f32 = 1.0\n    var frequency: f32 = 1.0\n    let octaves = turbulence if turbulence > 0 else 1\n    for _ in 0..<octaves:\n        total += amplitude * gradient_noise(p * frequency, unperiodic, 0.0).value\n        weight += amplitude\n        amplitude *= rough\n        frequency *= 2.0\n    let base = math32.clamp(0.5 + 0.5 * total / weight, 0.0, 1.0)\n    return math32.pow(base, atten)\n",
    "# Cellular noise: Worley's feature points (VEX `wnoise`), MaterialX's cell noise and\n# alligator noise (VEX `anoise`). Each unit cell holds one feature point at a hashed\n# place; a lookup visits the 27 cells around the point.\n\n## Worley distances (VEX `wnoise`'s outputs): `f1` to the nearest feature point and `f2`\n## to the second nearest (Euclidean, not squared), and `seed`, a number in 0..2^24\n## naming the nearest point's cell (the same for every position nearest that point).\npub struct Worley:\n    pub var f1: f32 = 0.0\n    pub var f2: f32 = 0.0\n    pub var seed: i32 = 0\n\n## The hash of cell (x, y, z), stream `stream`.\nfunc cell_bits(x: i64, y: i64, z: i64, stream: u32) -> u64:\n    let mixed = (x *% 0x632BE59BD9B4E019) ^ (y *% 0x2CB92BA72F3D8DD7) ^ (z *% 0x2BC98388FB8FAC03)\n    return random_bits(mixed, stream)\n\n## A number in `[0, 1)` from the top 24 bits of `bits`.\nfunc unit_of(bits: u64) -> f32:\n    return (f32)(bits >> 40) * (1.0 / 16777216.0)\n\n## The feature point of cell (x, y, z): three 21-bit fractions of one hash.\nfunc feature(x: i64, y: i64, z: i64) -> f32[4]:\n    let bits = cell_bits(x, y, z, 21)\n    let scale: f32 = 1.0 / 2097152.0\n    return [(f32)x + (f32)(bits >> 43) * scale, (f32)y + (f32)((bits >> 22) & 0x1FFFFF) * scale, (f32)z + (f32)((bits >> 1) & 0x1FFFFF) * scale, 0.0]\n\n## The two nearest feature points' distances from `p` (VEX `wnoise(pos, seed, f1, f2)`;\n## departure: distances, not squared distances). Always `f1 <= f2`.\npub func wnoise(p: f32[4]) -> Worley:\n    let cx = (i64)math32.floor(p[0])\n    let cy = (i64)math32.floor(p[1])\n    let cz = (i64)math32.floor(p[2])\n    var f1: f32 = 1.0e9\n    var f2: f32 = 1.0e9\n    var nearest: i64 = 0\n    for i in 0..<27:\n        let x = cx + (i64)(i % 3) - 1\n        let y = cy + (i64)((i // 3) % 3) - 1\n        let z = cz + (i64)(i // 9) - 1\n        let d = distance([p[0], p[1], p[2], 0.0], feature(x, y, z))\n        if d < f1:\n            f2 = f1\n            f1 = d\n            nearest = (i64)i\n        elif d < f2:\n            f2 = d\n    let seed = (i32)(cell_bits(cx + nearest % 3 - 1, cy + (nearest // 3) % 3 - 1, cz + nearest // 9 - 1, 23) >> 40)\n    return Worley(f1 = f1, f2 = f2, seed = seed)\n\n## A random value in `[0, 1)` constant over each unit cell (MaterialX and VEX\n## `mx_cellnoise`).\npub func mx_cellnoise(p: f32[4]) -> f32:\n    return unit_of(cell_bits((i64)math32.floor(p[0]), (i64)math32.floor(p[1]), (i64)math32.floor(p[2]), 24))\n\n## Alligator noise in `0..1`: each cell's feature point carries a bump of random height\n## falling to zero one unit away; the value is the tallest bump less the second, which\n## leaves plates with cracks between them.\npub func alligator(p: f32[4]) -> f32:\n    let cx = (i64)math32.floor(p[0])\n    let cy = (i64)math32.floor(p[1])\n    let cz = (i64)math32.floor(p[2])\n    var first: f32 = 0.0\n    var second: f32 = 0.0\n    for i in 0..<27:\n        let x = cx + (i64)(i % 3) - 1\n        let y = cy + (i64)((i // 3) % 3) - 1\n        let z = cz + (i64)(i // 9) - 1\n        let d = distance([p[0], p[1], p[2], 0.0], feature(x, y, z))\n        let fall = math32.max(1.0 - d, 0.0)\n        let bump = unit_of(cell_bits(x, y, z, 25)) * fall * fall * fall\n        if bump > first:\n            second = first\n            first = bump\n        elif bump > second:\n            second = bump\n    return math32.min(first - second, 1.0)\n\n## Fractal alligator noise of `turbulence` octaves (VEX `anoise(pos, turbulence, rough,\n## atten)`): octave `i` at `p * 2^i` weighted `rough^i`, the weighted mean raised to\n## `atten`.\npub func anoise(p: f32[4], turbulence: i32, rough: f32, atten: f32) -> f32:\n    var total: f32 = 0.0\n    var weight: f32 = 0.0\n    var amplitude: f32 = 1.0\n    var frequency: f32 = 1.0\n    let octaves = turbulence if turbulence > 0 else 1\n    for _ in 0..<octaves:\n        total += amplitude * alligator(p * frequency)\n        weight += amplitude\n        amplitude *= rough\n        frequency *= 2.0\n    return math32.pow(math32.clamp(total / weight, 0.0, 1.0), atten)\n",
    "# Random draws and sampling: VEX's `nrandom`, `random_sobol` and `random_poisson`, and the\n# `sample_*` functions that warp uniform numbers in `[0, 1)` (from `rand`) onto shapes and\n# distributions.\n\n## A random number in `[0, 1)` for `seed` (VEX `nrandom`; departure: a pure function of\n## its seed, like `rand`, so cooks repeat; its own stream, apart from `rand`'s).\npub func nrandom(seed: i64) -> f32:\n    return rand_stream(seed, 7)\n\n## Point `index` of a 3D Sobol sequence, each lane in `[0, 1)` (VEX `random_sobol`):\n## low-discrepancy points that fill space more evenly than `rand`. `seed` scrambles the\n## sequence by an XOR of its bits, so different seeds give different, equally even sets.\npub func random_sobol(seed: i64, index: i64) -> f32[4]:\n    let n = (u64)index\n    var x: u32 = 0\n    var y: u32 = 0\n    var z: u32 = 0\n    # Direction numbers: the first dimension is van der Corput's; the second and third\n    # use the primitive polynomials x + 1 and x^2 + x + 1 (Joe and Kuo's m = 1; m = 1, 3).\n    var v1: u32 = 0x80000000\n    var v2: u32 = 0x80000000\n    var v2_before: u32 = 0\n    for bit: u32 in 0..<32:\n        if bit > 0:\n            v1 = v1 ^ (v1 >> 1)\n        if bit == 1:\n            v2_before = v2\n            v2 = 0xC0000000\n        elif bit > 1:\n            let next = v2 ^ v2_before ^ (v2_before >> 2)\n            v2_before = v2\n            v2 = next\n        if ((n >> (u64)bit) & 1) != 0:\n            x = x ^ ((u32)0x80000000 >> bit)\n            y = y ^ v1\n            z = z ^ v2\n    let scramble = random_bits(seed, 31)\n    x = x ^ (u32)(scramble >> 32)\n    y = y ^ (u32)scramble\n    z = z ^ (u32)(scramble >> 16)\n    return [(f32)(x >> 8) * (1.0 / 16777216.0), (f32)(y >> 8) * (1.0 / 16777216.0), (f32)(z >> 8) * (1.0 / 16777216.0), 0.0]\n\n## A Poisson-distributed count with mean `mean` for `seed` (VEX `random_poisson`):\n## Knuth's product of uniforms below a mean of 30, the rounded normal approximation above.\npub func random_poisson(seed: i64, mean: f32) -> i32:\n    if not (mean > 0.0):\n        return 0\n    if mean >= 30.0:\n        let drawn = mean + math32.sqrt(mean) * sample_normal(rand_stream(seed, 41))\n        return (i32)math32.max(math32.floor(drawn + 0.5), 0.0)\n    let threshold = math32.exp(-mean)\n    var chance: f32 = 1.0\n    var count: i32 = -1\n    var stream: u32 = 0\n    while chance > threshold and stream < 200:\n        chance *= (f32)(random_bits(seed, 42 + stream) >> 40) * (1.0 / 16777216.0)\n        count += 1\n        stream += 1\n    return count\n\n## A point uniformly inside the unit disk from two uniform numbers (VEX\n## `sample_circle_uniform`).\npub func sample_circle_uniform(u: f32[2]) -> f32[2]:\n    let r = math32.sqrt(u[0])\n    let angle = 2.0 * math32.pi * u[1]\n    return [r * math32.cos(angle), r * math32.sin(angle)]\n\n## A direction uniformly over the unit sphere from two uniform numbers (VEX\n## `sample_direction_uniform`).\npub func sample_direction_uniform(u: f32[2]) -> f32[4]:\n    let z = 1.0 - 2.0 * u[0]\n    let r = math32.sqrt(math32.max(1.0 - z * z, 0.0))\n    let angle = 2.0 * math32.pi * u[1]\n    return [r * math32.cos(angle), r * math32.sin(angle), z, 0.0]\n\n## A point uniformly inside the unit ball from three uniform numbers (VEX\n## `sample_sphere_uniform`).\npub func sample_sphere_uniform(u: f32[4]) -> f32[4]:\n    return sample_direction_uniform([u[1], u[2]]) * math32.cbrt(u[0])\n\n## A direction uniformly over the hemisphere around +Z from two uniform numbers (VEX\n## `sample_hemisphere(u)`).\npub func sample_hemisphere(u: f32[2]) -> f32[4]:\n    let z = u[0]\n    let r = math32.sqrt(math32.max(1.0 - z * z, 0.0))\n    let angle = 2.0 * math32.pi * u[1]\n    return [r * math32.cos(angle), r * math32.sin(angle), z, 0.0]\n\n## A direction uniformly within `max_angle` radians of `center` (any length but zero)\n## from two uniform numbers (VEX `sample_direction_cone(center, maxangle, u)`).\npub func sample_direction_cone(center: f32[4], max_angle: f32, u: f32[2]) -> f32[4]:\n    let lowest = math32.cos(math32.clamp(max_angle, 0.0, math32.pi))\n    let z = 1.0 - u[0] * (1.0 - lowest)\n    let r = math32.sqrt(math32.max(1.0 - z * z, 0.0))\n    let angle = 2.0 * math32.pi * u[1]\n    let local: f32[4] = [r * math32.cos(angle), r * math32.sin(angle), z, 0.0]\n    # Turn +Z onto the center direction.\n    return dihedral([0.0, 0.0, 1.0, 0.0], center).transform(local)\n\n## The standard normal quantile of a uniform number `u` in `(0, 1)` (VEX `sample_normal`):\n## Acklam's rational approximation, relative error below 1.2e-9 before rounding to f32.\npub func sample_normal(u: f32) -> f32:\n    let p = math.clamp((f64)u, 1.0e-12, 1.0 - 1.0e-12)\n    let low = 0.02425\n    if p < low or p > 1.0 - low:\n        let q = math.sqrt(-2.0 * math.log(p if p < low else 1.0 - p))\n        let tail = (((((-0.007784894002430293 * q - 0.3223964580411365) * q - 2.400758277161838) * q - 2.549732539343734) * q + 4.374664141464968) * q + 2.938163982698783) / ((((0.007784695709041462 * q + 0.3224671290700398) * q + 2.445134137142996) * q + 3.754408661907416) * q + 1.0)\n        return (f32)(tail if p < low else -tail)\n    let q = p - 0.5\n    let r = q * q\n    return (f32)((((((-39.69683028665376 * r + 220.9460984245205) * r - 275.9285104469687) * r + 138.3577518672690) * r - 30.66479806614716) * r + 2.506628274631) * q / (((((-54.47609879822406 * r + 161.5858368580409) * r - 155.6989798598866) * r + 66.80131188771972) * r - 13.28068155288572) * r + 1.0))\n\n## `sample_normal` with mean `mean` and standard deviation `deviation`.\npub func sample_normal_scaled(mean: f32, deviation: f32, u: f32) -> f32:\n    return mean + deviation * sample_normal(u)\n",
    "#==============================================================================================\n#\n#   math - The mathematical functions of the standard library\n#\n#   DESCRIPTION:\n#       `import math` for `math.sqrt(x)`, `from math import sqrt` for `sqrt(x)` (§16.6). The float\n#       functions are the C library's, declared here with the types Base gives them; the integer\n#       functions are Base.\n#\n#==============================================================================================\nextern func c_floor as \"floor\"(x: f64) -> f64\nextern func c_ceil as \"ceil\"(x: f64) -> f64\nextern func c_round as \"round\"(x: f64) -> f64\nextern func c_trunc as \"trunc\"(x: f64) -> f64\nextern func c_sqrt as \"sqrt\"(x: f64) -> f64\nextern func c_cbrt as \"cbrt\"(x: f64) -> f64\nextern func c_hypot as \"hypot\"(x: f64, y: f64) -> f64\nextern func c_fmod as \"fmod\"(x: f64, y: f64) -> f64\nextern func c_pow as \"pow\"(x: f64, y: f64) -> f64\nextern func c_exp as \"exp\"(x: f64) -> f64\nextern func c_exp2 as \"exp2\"(x: f64) -> f64\nextern func c_log as \"log\"(x: f64) -> f64\nextern func c_log2 as \"log2\"(x: f64) -> f64\nextern func c_log10 as \"log10\"(x: f64) -> f64\nextern func c_sin as \"sin\"(x: f64) -> f64\nextern func c_cos as \"cos\"(x: f64) -> f64\nextern func c_tan as \"tan\"(x: f64) -> f64\nextern func c_asin as \"asin\"(x: f64) -> f64\nextern func c_acos as \"acos\"(x: f64) -> f64\nextern func c_atan as \"atan\"(x: f64) -> f64\nextern func c_atan2 as \"atan2\"(y: f64, x: f64) -> f64\nextern func c_sinh as \"sinh\"(x: f64) -> f64\nextern func c_cosh as \"cosh\"(x: f64) -> f64\nextern func c_tanh as \"tanh\"(x: f64) -> f64\nextern func c_log1p as \"log1p\"(x: f64) -> f64\nextern func c_expm1 as \"expm1\"(x: f64) -> f64\nextern func c_fma as \"fma\"(x: f64, y: f64, z: f64) -> f64\nextern func c_fabs as \"fabs\"(x: f64) -> f64\nextern func c_nextafter as \"nextafter\"(x: f64, toward: f64) -> f64\nextern func c_frexp as \"frexp\"(x: f64, out exponent: i32) -> f64\nextern func c_scalbn as \"scalbn\"(x: f64, exponent: i32) -> f64\nextern func c_modf as \"modf\"(x: f64, out integral: f64) -> f64\nextern func c_remainder as \"remainder\"(x: f64, y: f64) -> f64\n## The ratio of a circle's circumference to its diameter.\npub let pi: f64 = 3.141592653589793\n## A full turn in radians, two pi.\npub let tau: f64 = 6.283185307179586\n## Euler's number.\npub let e: f64 = 2.718281828459045\n## Built from bits so that no constant expression needs to overflow.\npub let infinity: f64 = f64.bits(9218868437227405312)\n## A quiet not-a-number; unequal to everything, itself included.\npub let nan: f64 = f64.bits(9221120237041090560)\n\nlet sign_mask: u64 = 0x8000000000000000\nlet magnitude_mask: u64 = 0x7fffffffffffffff\nlet exponent_mask: u64 = 0x7ff0000000000000\n\n# mark: Real functions ========================================================================\n\n## Rounding: `floor` toward negative infinity, `ceil` toward positive, `round` to the\n## nearest with halves away from zero, `trunc` toward zero.\npub func floor(x: f64) -> f64:\n    return c_floor(x)\n\n## The smallest integer value not less than `x`.\npub func ceil(x: f64) -> f64:\n    return c_ceil(x)\n\n## `x` rounded to the nearest integer, halves away from zero.\npub func round(x: f64) -> f64:\n    return c_round(x)\n\n## `x` with its fractional part removed, toward zero.\npub func trunc(x: f64) -> f64:\n    return c_trunc(x)\n\n## The square root of `x`.\npub func sqrt(x: f64) -> f64:\n    return c_sqrt(x)\n\n## The cube root of `x`.\npub func cbrt(x: f64) -> f64:\n    return c_cbrt(x)\n\n## `sqrt(x * x + y * y)` without overflow in the squares.\npub func hypot(x: f64, y: f64) -> f64:\n    return c_hypot(x, y)\n\n## The remainder of `x / y` with the sign of `x`, as C's `fmod`; a zero has the sign of `x`.\npub func mod(x: f64, y: f64) -> f64:\n    let result = c_fmod(x, y)\n    # MinGW's fmod loses the dividend's zero sign under directed rounding, as some\n    # hosts' remainder does: restore it by bits, leaving a nonzero or NaN result.\n    return copysign(result, x) if (result.bits() & magnitude_mask) == 0 else result\n\n## `x` raised to the power `y`.\npub func pow(x: f64, y: f64) -> f64:\n    return c_pow(x, y)\n\n## `e` raised to the power `x`.\npub func exp(x: f64) -> f64:\n    return c_exp(x)\n\n## `2` raised to the power `x`.\npub func exp2(x: f64) -> f64:\n    return c_exp2(x)\n\n## The natural logarithm of `x`.\npub func log(x: f64) -> f64:\n    return c_log(x)\n\n## The base-2 logarithm of `x`.\npub func log2(x: f64) -> f64:\n    return c_log2(x)\n\n## The base-10 logarithm of `x`.\npub func log10(x: f64) -> f64:\n    return c_log10(x)\n\n## Natural logarithm of `1 + x`, retaining accuracy when adding `x` to one would\n## round it away. At -1 the result is negative infinity; below -1 it is NaN.\npub func log1p(x: f64) -> f64:\n    return c_log1p(x)\n\n## `exp(x) - 1` without cancellation near zero. Signed zero is preserved.\npub func expm1(x: f64) -> f64:\n    # The exact limit is representable in every rounding mode. Some host libm\n    # implementations instead round a finite-input approximation toward -1.\n    if x.bits() == (sign_mask | exponent_mask):\n        return -1.0\n    return c_expm1(x)\n\n## Multiply `x * y` and add `z` with one final rounding, rather than rounding the\n## product first. Uses the host's floating-point rounding mode.\npub func fma(x: f64, y: f64, z: f64) -> f64:\n    return c_fma(x, y, z)\n\n## The adjacent representable value toward `toward`; equal arguments return\n## `toward`, including its zero sign. A NaN argument produces NaN.\npub func nextafter(x: f64, toward: f64) -> f64:\n    return c_nextafter(x, toward)\n\n## The adjacent representable value toward positive infinity. Positive infinity\n## stays infinite; either zero becomes the smallest positive subnormal.\npub func next_up(x: f64) -> f64:\n    return c_nextafter(x, infinity)\n\n## The adjacent representable value toward negative infinity. Negative infinity\n## stays infinite; either zero becomes the smallest negative subnormal.\npub func next_down(x: f64) -> f64:\n    return c_nextafter(x, copysign(infinity, -1.0))\n\n## Split `x` into `(fraction, exponent)` such that `x = fraction * 2**exponent`.\n## A finite nonzero fraction has magnitude in [0.5, 1). Zero, infinity and NaN\n## are returned unchanged with exponent zero, avoiding an unspecified C exponent.\npub func frexp(x: f64) -> (f64, i32):\n    if x == 0.0 or not is_finite(x):\n        return (x, 0)\n    return c_frexp(x)\n\n## Scale by an integer power of two, without forming that power as an intermediate\n## float. Overflow/underflow follow the host rounding mode; signed zero survives.\npub func scalbn(x: f64, exponent: i32) -> f64:\n    return c_scalbn(x, exponent)\n\n## `(fractional, integral)` parts, both with the sign of `x`; the integral part\n## truncates toward zero. Infinity has a signed-zero fraction; NaN gives two NaNs.\npub func modf(x: f64) -> (f64, f64):\n    return c_modf(x)\n\n## `x - n*y` for the nearest integer `n` to `x/y`, with ties choosing even `n`.\n## This differs from `mod`, which truncates the quotient. Zero has the sign of `x`;\n## a zero divisor or infinite dividend produces NaN.\npub func remainder(x: f64, y: f64) -> f64:\n    let result = c_remainder(x, y)\n    # Some host implementations lose the dividend's zero sign under directed\n    # rounding. Restore it by bits without changing a nonzero or NaN result.\n    return copysign(result, x) if (result.bits() & magnitude_mask) == 0 else result\n\n## The sine of `x` radians.\npub func sin(x: f64) -> f64:\n    return c_sin(x)\n\n## The cosine of `x` radians.\npub func cos(x: f64) -> f64:\n    return c_cos(x)\n\n## The tangent of `x` radians.\npub func tan(x: f64) -> f64:\n    return c_tan(x)\n\n## The arcsine of `x`, in radians.\npub func asin(x: f64) -> f64:\n    return c_asin(x)\n\n## The arccosine of `x`, in radians.\npub func acos(x: f64) -> f64:\n    return c_acos(x)\n\n## The arctangent of `x`, in radians.\npub func atan(x: f64) -> f64:\n    return c_atan(x)\n\n## The angle of the point `(x, y)`, in all four quadrants.\npub func atan2(y: f64, x: f64) -> f64:\n    return c_atan2(y, x)\n\n## The hyperbolic sine of `x`.\npub func sinh(x: f64) -> f64:\n    return c_sinh(x)\n\n## The hyperbolic cosine of `x`.\npub func cosh(x: f64) -> f64:\n    return c_cosh(x)\n\n## The hyperbolic tangent of `x`.\npub func tanh(x: f64) -> f64:\n    return c_tanh(x)\n\n# erf and erfc follow the method of Sun's fdlibm (s_erf.c, as in FreeBSD's msun), written\n# anew in Base: a rational approximation in x² below 0.84375, one in |x| - 1 up to 1.25,\n# and above that erfc(x) = exp(-x² - 0.5625 + R(1/x²)/S(1/x²)) / x with two rational fits\n# split at 1/0.35. The coefficients are fdlibm's. Results are within two units in the last\n# place (tests/math_accuracy holds the evidence); the only host function used is exp.\n\n## erf(1) in 24 bits, which the middle interval adds to; being short, `erx + P/Q` and\n## `1 - erx - P/Q` round once.\nlet erx: f64 = 8.45062911510467529297e-01\n## 2/sqrt(pi) - 1, the slope of erf at zero beyond x.\nlet efx: f64 = 1.28379167095512586316e-01\n\n## erf(x) = x + x·P(x²)/Q(x²) on [0, 0.84375).\nfunc erf_small(x: f64) -> f64:\n    let z = x * x\n    let p = 1.28379167095512558561e-01 + z * (-3.25042107247001499370e-01 + z * (-2.84817495755985104766e-02 + z * (-5.77027029648944159157e-03 + z * -2.37630166566501626084e-05)))\n    let q = 1.0 + z * (3.97917223959155352819e-01 + z * (6.50222499887672944485e-02 + z * (5.08130628187576562776e-03 + z * (1.32494738004321644526e-04 + z * -3.96022827877536812320e-06))))\n    return p / q\n\n## erf(1 + s) - erx = P(s)/Q(s) on [0.84375, 1.25).\nfunc erf_middle(s: f64) -> f64:\n    let p = -2.36211856075265944077e-03 + s * (4.14856118683748331666e-01 + s * (-3.72207876035701323847e-01 + s * (3.18346619901161753674e-01 + s * (-1.10894694282396677476e-01 + s * (3.54783043256182359371e-02 + s * -2.16637559486879084300e-03)))))\n    let q = 1.0 + s * (1.06420880400844228286e-01 + s * (5.40397917702171048937e-01 + s * (7.18286544141962662868e-02 + s * (1.26171219808761642112e-01 + s * (1.36370839120290507362e-02 + s * 1.19844998467991074170e-02)))))\n    return p / q\n\n## erfc(a) for 1.25 <= a < 28: exp(-a² - 0.5625 + R/S) / a. The square is split at a with\n## its low 32 bits cleared, whose square is exact, so the exponent loses nothing.\nfunc erfc_tail(a: f64) -> f64:\n    let s = 1.0 / (a * a)\n    var r: f64\n    var q: f64\n    if a < 2.857142857142857:\n        r = -9.86494403484714822705e-03 + s * (-6.93858572707181764372e-01 + s * (-1.05586262253232909814e+01 + s * (-6.23753324503260060396e+01 + s * (-1.62396669462573470355e+02 + s * (-1.84605092906711035994e+02 + s * (-8.12874355063065934246e+01 + s * -9.81432934416914548592e+00))))))\n        q = 1.0 + s * (1.96512716674392571292e+01 + s * (1.37657754143519042600e+02 + s * (4.34565877475229228821e+02 + s * (6.45387271733267880336e+02 + s * (4.29008140027567833386e+02 + s * (1.08635005541779435134e+02 + s * (6.57024977031928170135e+00 + s * -6.04244152148580987438e-02)))))))\n    else:\n        r = -9.86494292470009928597e-03 + s * (-7.99283237680523006574e-01 + s * (-1.77579549177547519889e+01 + s * (-1.60636384855821916062e+02 + s * (-6.37566443368389627722e+02 + s * (-1.02509513161107724954e+03 + s * -4.83519191608651397019e+02)))))\n        q = 1.0 + s * (3.03380607434824582924e+01 + s * (3.25792512996573918826e+02 + s * (1.53672958608443695994e+03 + s * (3.19985821950859553908e+03 + s * (2.55305040643316442583e+03 + s * (4.74528541206955367215e+02 + s * -2.24409524465858183362e+01))))))\n    let z = f64.bits(a.bits() & 0xffffffff00000000)\n    return c_exp(-z * z - 0.5625) * c_exp((z - a) * (z + a) + r / q) / a\n\n## The error function, (2/√π)∫₀ˣ e^(-t²) dt, as Python's `math.erf`: odd, from -1 to 1.\n## Signed zero is preserved; infinities give ±1 and NaN gives NaN.\npub func erf(x: f64) -> f64:\n    let a = abs(x)\n    if not (a < 6.0):\n        if is_nan(x):\n            return x\n        return copysign(1.0, x)\n    if a < 0.84375:\n        if a < 3.725290298461914e-09:\n            # below 2^-28 the series is x + efx·x; scaling by 8 keeps subnormals exact\n            return 0.125 * (8.0 * x + (8.0 * efx) * x)\n        return x + x * erf_small(x)\n    let magnitude = erx + erf_middle(a - 1.0) if a < 1.25 else 1.0 - erfc_tail(a)\n    return copysign(magnitude, x)\n\n## The complementary error function, `1 - erf(x)`, as Python's `math.erfc`: accurate where\n## erf(x) is near 1 and the subtraction would lose every digit. It falls from 2 to 0 and\n## underflows to zero above about 27.2; infinities give 0 and 2, NaN gives NaN.\npub func erfc(x: f64) -> f64:\n    let a = abs(x)\n    if a < 0.84375:\n        if a < 1.3877787807814457e-17:\n            return 1.0 - x\n        let y = x * erf_small(x)\n        if x < 0.25:\n            return 1.0 - (x + y)\n        return 0.5 - (y + (x - 0.5))\n    if a < 1.25:\n        let p = erf_middle(a - 1.0)\n        return (1.0 - erx) - p if x > 0.0 else 1.0 + (erx + p)\n    if a < 28.0:\n        if x < -6.0:\n            return 2.0\n        let r = erfc_tail(a)\n        return r if x > 0.0 else 2.0 - r\n    if is_nan(x):\n        return x\n    return 0.0 if x > 0.0 else 2.0\n\n## True for quiet and signaling NaNs, independent of sign and payload.\npub func is_nan(x: f64) -> bool:\n    return (x.bits() & magnitude_mask) > exponent_mask\n\n## True for zeros, subnormals and normal finite values. Classification inspects the\n## representation and performs no floating-point arithmetic on a signaling NaN.\npub func is_finite(x: f64) -> bool:\n    return (x.bits() & exponent_mask) != exponent_mask\n\n## True only for positive or negative infinity.\npub func is_infinite(x: f64) -> bool:\n    return (x.bits() & magnitude_mask) == exponent_mask\n\n## Whether the sign bit is set, including negative zero and signed NaNs.\npub func signbit(x: f64) -> bool:\n    return (x.bits() & sign_mask) != 0\n\n## Clear the sign bit: negative zero becomes positive zero. NaN payload bits survive.\npub func abs(x: f64) -> f64:\n    return c_fabs(x)\n\n## The magnitude and payload of `magnitude` with the sign bit of `sign_source`.\npub func copysign(magnitude: f64, sign_source: f64) -> f64:\n    return f64.bits((magnitude.bits() & magnitude_mask) | (sign_source.bits() & sign_mask))\n\n## `-1.0` or `1.0` for nonzero numbers; zero and NaN are returned unchanged.\npub func sign(x: f64) -> f64:\n    if is_nan(x):\n        return x\n    if x < 0.0:\n        return -1.0\n    if x > 0.0:\n        return 1.0\n    return x\n\n## Minimum with NaN propagation (left NaN wins if both are NaNs). Negative zero\n## wins over positive zero; a selected NaN retains its original bits.\npub func min(a: f64, b: f64) -> f64:\n    if is_nan(a):\n        return a\n    if is_nan(b):\n        return b\n    if a == 0.0 and b == 0.0:\n        return f64.bits(a.bits() | b.bits())\n    return a if a < b else b\n\n## Maximum with NaN propagation (left NaN wins if both are NaNs). Positive zero\n## wins over negative zero; a selected NaN retains its original bits.\npub func max(a: f64, b: f64) -> f64:\n    if is_nan(a):\n        return a\n    if is_nan(b):\n        return b\n    if a == 0.0 and b == 0.0:\n        return f64.bits(a.bits() & b.bits())\n    return a if a > b else b\n\n## Restrict `x` to inclusive ordered bounds. Reversed or NaN bounds trap. An input\n## NaN or an input equal to a bound is returned unchanged, including its zero sign.\npub func clamp(x: f64, low: f64, high: f64) -> f64:\n    assert(low <= high)\n    if is_nan(x):\n        return x\n    return low if x < low else (high if x > high else x)\n\n## The integer functions. `//` and `%` truncate toward zero as C does (§7.2); these two\n## floor, so `div_floor(-7, 2)` is `-4` and `mod_floor(-7, 2)` is `1`.\npub func div_floor(a: i64, b: i64) -> i64:\n    let q = a // b\n    if (a % b != 0) and ((a < 0) != (b < 0)):\n        return q - 1\n    return q\n\n## The remainder of `a / b` with the divisor's sign (floored division).\npub func mod_floor(a: i64, b: i64) -> i64:\n    if b == -1:\n        return 0\n    let r = a % b\n    if r != 0 and ((r < 0) != (b < 0)):\n        return r + b\n    return r\n\n## The absolute value of `a`; the minimum i64 wraps to itself.\npub func iabs(a: i64) -> i64:\n    return 0 - a if a < 0 else a\n\n## Absolute value, or none for the minimum i64 whose positive magnitude cannot fit.\npub func checked_iabs(a: i64) -> i64?:\n    if a < 0:\n        return 0 -? a\n    return a\n\n## Floor division, or none for a zero divisor or a quotient outside i64.\npub func checked_div_floor(a: i64, b: i64) -> i64?:\n    if b == 0 or (a == -9223372036854775808 and b == -1):\n        return none\n    return div_floor(a, b)\n\n## Remainder with the divisor's sign, or none for a zero divisor. The minimum i64\n## modulo -1 is zero even though its quotient cannot be represented.\npub func checked_mod_floor(a: i64, b: i64) -> i64?:\n    if b == 0:\n        return none\n    if b == -1:\n        return 0\n    return mod_floor(a, b)\n\n## The smaller of `a` and `b`.\npub func imin(a: i64, b: i64) -> i64:\n    return a if a < b else b\n\n## The larger of `a` and `b`.\npub func imax(a: i64, b: i64) -> i64:\n    return a if a > b else b\n\n## Restrict `x` to inclusive ordered bounds. Reversed bounds trap.\npub func iclamp(x: i64, low: i64, high: i64) -> i64:\n    assert(low <= high)\n    return low if x < low else (high if x > high else x)\n",
    "#==============================================================================================\n#\n#   math32 - Single-precision mathematical functions\n#\n#   DESCRIPTION:\n#       Single-precision mathematics. `math32` has the floating-point operations of `math`, with\n#       f32 arguments and results and the same special-value contracts. Elementary functions\n#       call the host's single-precision libm entry points directly; they do not compute in f64\n#       and then introduce an extra rounding step. `erf` and `erfc` are the exception: they are\n#       `math`'s, rounded once to f32.\n#\n#==============================================================================================\nimport math\nextern func c_floor as \"floorf\"(x: f32) -> f32\nextern func c_ceil as \"ceilf\"(x: f32) -> f32\nextern func c_round as \"roundf\"(x: f32) -> f32\nextern func c_trunc as \"truncf\"(x: f32) -> f32\nextern func c_sqrt as \"sqrtf\"(x: f32) -> f32\nextern func c_cbrt as \"cbrtf\"(x: f32) -> f32\nextern func c_hypot as \"hypotf\"(x: f32, y: f32) -> f32\nextern func c_fmod as \"fmodf\"(x: f32, y: f32) -> f32\nextern func c_pow as \"powf\"(x: f32, y: f32) -> f32\nextern func c_exp as \"expf\"(x: f32) -> f32\nextern func c_exp2 as \"exp2f\"(x: f32) -> f32\nextern func c_log as \"logf\"(x: f32) -> f32\nextern func c_log2 as \"log2f\"(x: f32) -> f32\nextern func c_log10 as \"log10f\"(x: f32) -> f32\nextern func c_sin as \"sinf\"(x: f32) -> f32\nextern func c_cos as \"cosf\"(x: f32) -> f32\nextern func c_tan as \"tanf\"(x: f32) -> f32\nextern func c_asin as \"asinf\"(x: f32) -> f32\nextern func c_acos as \"acosf\"(x: f32) -> f32\nextern func c_atan as \"atanf\"(x: f32) -> f32\nextern func c_atan2 as \"atan2f\"(y: f32, x: f32) -> f32\nextern func c_sinh as \"sinhf\"(x: f32) -> f32\nextern func c_cosh as \"coshf\"(x: f32) -> f32\nextern func c_tanh as \"tanhf\"(x: f32) -> f32\nextern func c_log1p as \"log1pf\"(x: f32) -> f32\nextern func c_expm1 as \"expm1f\"(x: f32) -> f32\nextern func c_fma as \"fmaf\"(x: f32, y: f32, z: f32) -> f32\nextern func c_fabs as \"fabsf\"(x: f32) -> f32\nextern func c_nextafter as \"nextafterf\"(x: f32, toward: f32) -> f32\nextern func c_frexp as \"frexpf\"(x: f32, out exponent: i32) -> f32\nextern func c_scalbn as \"scalbnf\"(x: f32, exponent: i32) -> f32\nextern func c_modf as \"modff\"(x: f32, out integral: f32) -> f32\nextern func c_remainder as \"remainderf\"(x: f32, y: f32) -> f32\n## The ratio of a circle's circumference to its diameter.\npub let pi: f32 = 3.141592653589793\n## A full turn in radians, two pi.\npub let tau: f32 = 6.283185307179586\n## Euler's number.\npub let e: f32 = 2.718281828459045\n## Built from bits so that no constant expression needs to overflow.\npub let infinity: f32 = f32.bits(2139095040)\n## A quiet not-a-number; unequal to everything, itself included.\npub let nan: f32 = f32.bits(2143289344)\n\nlet sign_mask: u32 = 0x80000000\nlet magnitude_mask: u32 = 0x7fffffff\nlet exponent_mask: u32 = 0x7f800000\n\n# mark: Real functions ========================================================================\n\n## Rounding: `floor` toward negative infinity, `ceil` toward positive, `round` to the\n## nearest with halves away from zero, `trunc` toward zero.\npub func floor(x: f32) -> f32:\n    return c_floor(x)\n\n## The smallest integer value not less than `x`.\npub func ceil(x: f32) -> f32:\n    return c_ceil(x)\n\n## `x` rounded to the nearest integer, halves away from zero.\npub func round(x: f32) -> f32:\n    return c_round(x)\n\n## `x` with its fractional part removed, toward zero.\npub func trunc(x: f32) -> f32:\n    return c_trunc(x)\n\n## The square root of `x`.\npub func sqrt(x: f32) -> f32:\n    return c_sqrt(x)\n\n## The cube root of `x`.\npub func cbrt(x: f32) -> f32:\n    return c_cbrt(x)\n\n## `sqrt(x * x + y * y)` without overflow in the squares.\npub func hypot(x: f32, y: f32) -> f32:\n    return c_hypot(x, y)\n\n## The remainder of `x / y` with the sign of `x`, as C's `fmod`; a zero has the sign of `x`.\npub func mod(x: f32, y: f32) -> f32:\n    let result = c_fmod(x, y)\n    # MinGW's fmod loses the dividend's zero sign under directed rounding, as some\n    # hosts' remainder does: restore it by bits, leaving a nonzero or NaN result.\n    return copysign(result, x) if (result.bits() & magnitude_mask) == 0 else result\n\n## `x` raised to the power `y`.\npub func pow(x: f32, y: f32) -> f32:\n    return c_pow(x, y)\n\n## `e` raised to the power `x`.\npub func exp(x: f32) -> f32:\n    return c_exp(x)\n\n## `2` raised to the power `x`.\npub func exp2(x: f32) -> f32:\n    return c_exp2(x)\n\n## The natural logarithm of `x`.\npub func log(x: f32) -> f32:\n    return c_log(x)\n\n## The base-2 logarithm of `x`.\npub func log2(x: f32) -> f32:\n    return c_log2(x)\n\n## The base-10 logarithm of `x`.\npub func log10(x: f32) -> f32:\n    return c_log10(x)\n\n## Natural logarithm of `1 + x`, retaining accuracy when adding `x` to one would\n## round it away. At -1 the result is negative infinity; below -1 it is NaN.\npub func log1p(x: f32) -> f32:\n    return c_log1p(x)\n\n## `exp(x) - 1` without cancellation near zero. Signed zero is preserved.\npub func expm1(x: f32) -> f32:\n    # Preserve the exact negative-infinity limit under directed rounding too.\n    if x.bits() == (sign_mask | exponent_mask):\n        return -1.0\n    return c_expm1(x)\n\n## Multiply `x * y` and add `z` with one final rounding, rather than rounding the\n## product first. Uses the host's floating-point rounding mode.\npub func fma(x: f32, y: f32, z: f32) -> f32:\n    return c_fma(x, y, z)\n\n## The adjacent representable value toward `toward`; equal arguments return\n## `toward`, including its zero sign. A NaN argument produces NaN.\npub func nextafter(x: f32, toward: f32) -> f32:\n    return c_nextafter(x, toward)\n\n## The adjacent representable value toward positive infinity. Positive infinity\n## stays infinite; either zero becomes the smallest positive subnormal.\npub func next_up(x: f32) -> f32:\n    return c_nextafter(x, infinity)\n\n## The adjacent representable value toward negative infinity. Negative infinity\n## stays infinite; either zero becomes the smallest negative subnormal.\npub func next_down(x: f32) -> f32:\n    return c_nextafter(x, copysign(infinity, -1.0))\n\n## Split `x` into `(fraction, exponent)` such that `x = fraction * 2**exponent`.\n## A finite nonzero fraction has magnitude in [0.5, 1). Zero, infinity and NaN\n## are returned unchanged with exponent zero, avoiding an unspecified C exponent.\npub func frexp(x: f32) -> (f32, i32):\n    if x == 0.0 or not is_finite(x):\n        return (x, 0)\n    return c_frexp(x)\n\n## Scale by an integer power of two, without forming that power as an intermediate\n## float. Overflow/underflow follow the host rounding mode; signed zero survives.\npub func scalbn(x: f32, exponent: i32) -> f32:\n    return c_scalbn(x, exponent)\n\n## `(fractional, integral)` parts, both with the sign of `x`; the integral part\n## truncates toward zero. Infinity has a signed-zero fraction; NaN gives two NaNs.\npub func modf(x: f32) -> (f32, f32):\n    return c_modf(x)\n\n## `x - n*y` for the nearest integer `n` to `x/y`, with ties choosing even `n`.\n## This differs from `mod`, which truncates the quotient. Zero has the sign of `x`;\n## a zero divisor or infinite dividend produces NaN.\npub func remainder(x: f32, y: f32) -> f32:\n    let result = c_remainder(x, y)\n    # Match the f64 contract even if a host loses the zero sign while rounding.\n    return copysign(result, x) if (result.bits() & magnitude_mask) == 0 else result\n\n## The sine of `x` radians.\npub func sin(x: f32) -> f32:\n    return c_sin(x)\n\n## The cosine of `x` radians.\npub func cos(x: f32) -> f32:\n    return c_cos(x)\n\n## The tangent of `x` radians.\npub func tan(x: f32) -> f32:\n    return c_tan(x)\n\n## The arcsine of `x`, in radians.\npub func asin(x: f32) -> f32:\n    return c_asin(x)\n\n## The arccosine of `x`, in radians.\npub func acos(x: f32) -> f32:\n    return c_acos(x)\n\n## The arctangent of `x`, in radians.\npub func atan(x: f32) -> f32:\n    return c_atan(x)\n\n## The angle of the point `(x, y)`, in all four quadrants.\npub func atan2(y: f32, x: f32) -> f32:\n    return c_atan2(y, x)\n\n## The hyperbolic sine of `x`.\npub func sinh(x: f32) -> f32:\n    return c_sinh(x)\n\n## The hyperbolic cosine of `x`.\npub func cosh(x: f32) -> f32:\n    return c_cosh(x)\n\n## The hyperbolic tangent of `x`.\npub func tanh(x: f32) -> f32:\n    return c_tanh(x)\n\n## The error function, as `math.erf`, evaluated in f64 and rounded once to f32: within a\n## unit in the last place.\npub func erf(x: f32) -> f32:\n    return (f32)math.erf((f64)x)\n\n## The complementary error function, `1 - erf(x)` without cancellation, as `math.erfc`,\n## evaluated in f64 and rounded once to f32.\npub func erfc(x: f32) -> f32:\n    return (f32)math.erfc((f64)x)\n\n## True for quiet and signaling NaNs, independent of sign and payload.\npub func is_nan(x: f32) -> bool:\n    return (x.bits() & magnitude_mask) > exponent_mask\n\n## True for zeros, subnormals and normal finite values. Classification inspects the\n## representation and performs no floating-point arithmetic on a signaling NaN.\npub func is_finite(x: f32) -> bool:\n    return (x.bits() & exponent_mask) != exponent_mask\n\n## True only for positive or negative infinity.\npub func is_infinite(x: f32) -> bool:\n    return (x.bits() & magnitude_mask) == exponent_mask\n\n## Whether the sign bit is set, including negative zero and signed NaNs.\npub func signbit(x: f32) -> bool:\n    return (x.bits() & sign_mask) != 0\n\n## Clear the sign bit: negative zero becomes positive zero. NaN payload bits survive.\npub func abs(x: f32) -> f32:\n    return c_fabs(x)\n\n## The magnitude and payload of `magnitude` with the sign bit of `sign_source`.\npub func copysign(magnitude: f32, sign_source: f32) -> f32:\n    return f32.bits((magnitude.bits() & magnitude_mask) | (sign_source.bits() & sign_mask))\n\n## `-1.0` or `1.0` for nonzero numbers; zero and NaN are returned unchanged.\npub func sign(x: f32) -> f32:\n    if is_nan(x):\n        return x\n    if x < 0.0:\n        return -1.0\n    if x > 0.0:\n        return 1.0\n    return x\n\n## Minimum with NaN propagation (left NaN wins if both are NaNs). Negative zero\n## wins over positive zero; a selected NaN retains its original bits.\npub func min(a: f32, b: f32) -> f32:\n    if is_nan(a):\n        return a\n    if is_nan(b):\n        return b\n    if a == 0.0 and b == 0.0:\n        return f32.bits(a.bits() | b.bits())\n    return a if a < b else b\n\n## Maximum with NaN propagation (left NaN wins if both are NaNs). Positive zero\n## wins over negative zero; a selected NaN retains its original bits.\npub func max(a: f32, b: f32) -> f32:\n    if is_nan(a):\n        return a\n    if is_nan(b):\n        return b\n    if a == 0.0 and b == 0.0:\n        return f32.bits(a.bits() & b.bits())\n    return a if a > b else b\n\n## Restrict `x` to inclusive ordered bounds. Reversed or NaN bounds trap. An input\n## NaN or an input equal to a bound is returned unchanged, including its zero sign.\npub func clamp(x: f32, low: f32, high: f32) -> f32:\n    assert(low <= high)\n    if is_nan(x):\n        return x\n    return low if x < low else (high if x > high else x)\n",
]