Table of Contents

Galaxy collision

Two spiral galaxies meet, and every body pulls on every other: 2,048 stars and the two cores make 4.2 million pairs of gravity a frame. Each WebAssembly instruction takes one body against four.

Starting the module...
Drag turn the view Scroll zoom in and out Right-click drop a black hole

The view turns slowly until you drag it. A black hole lands where you click, on the plane through the middle facing you; there can be six, and a seventh moves the oldest.

The kernel

Each galaxy is a heavy core with a disk of stars on circular orbits, the second disk tilted against the first. Gravity is summed over all pairs, softened so that close passes stay finite:

a_i = Σ_j m_j (p_j − p_i) / (|p_j − p_i|² + 0.0025)^(3/2)

The loop splats one body's x, y and z into f32x4s and takes four other bodies per iteration. Three differences, one f32x4.sqrt and one f32x4.div give four pairs' factors at once. Four lanes of sums are added at the end. A body's pull on itself is zero, because its differences are, so no pair is skipped. Then every body moves by symplectic Euler (v += a dt, then p += v dt), which keeps the orbits' energy over thousands of frames.

The stars are drawn as light. Each adds its galaxy's colour to three planes of f32 (red, green, blue) that fade by 0.72 a frame, so a moving star leaves a short trail. A table maps each plane's light to the pixel's channel, saturating softly, over the theme's background. One test skips a group of four dark pixels.

These are the four pairs of one iteration:

            local.get 1
            v128.load 0
            local.get 3
            f32x4.sub
            local.tee 9
            local.get 9
            f32x4.mul
            local.get 1
            v128.load 16416
            local.get 4
            f32x4.sub
            local.tee 10
            local.get 10
            f32x4.mul
            f32x4.add
            local.get 1
            v128.load 32832
            local.get 5
            f32x4.sub
            local.tee 11
            local.get 11
            f32x4.mul
            f32x4.add
            local.get 14
            f32x4.add
            local.set 12        // r^2 + eps^2: ((dx^2 + dy^2) + dz^2) + eps^2
            local.get 1
            v128.load 98496     // m_j / (r2 sqrt r2)
            local.get 12
            local.get 12
            f32x4.sqrt
            f32x4.mul
            f32x4.div
            local.set 13

The whole kernel, with the setup of the encounters, the drawing and the controls, is galaxy.wasm.s. Under Node on an i9-13900K, a frame takes about 2.2 ms at 1,024 stars, 5 ms at 2,048 and 14 ms at 4,096. The gravity runs at about 1.3 billion pairs a second, and drawing takes 1 to 2 ms, depending on how much of the sky is lit. The figures under the demo are your browser's.

Build one yourself

dotnet new asmwasm -n Kernel      # a WebAssembly library in LLVM's WebAssembly assembly
dotnet build Kernel               # Kernel/bin/Debug/freestanding/browser-wasm/Kernel.wasm
node Kernel/run.mjs               # calls it from Node; a page loads it the same way

The WebAssembly guide covers the language, the FUNCTION macros and the traps. One trap this kernel met: local.tee stores a value and also leaves it on the stack. When nothing takes it, llvm-mc reports the leftover only at the end of the function (type mismatch, expected [] but got [i32]), far from the line. local.set leaves nothing.