19 programs measured with axle bench: astar_grid, bicubic_sample, display_compose, fbm_noise, gap_dda, ggx_brdf, hit_record, mat4_chain, nbody_f64, particles_grid, pcg_montecarlo, probe_trilinear, quat_slerp, sh9_irradiance, shaft_march, sparse_fill, tonemap_srgb, trig_poly, voxel_dda. Run time, peak RAM at compile and run, and binary size, with the exact source for each language.
Benchmarks for v0.23.0
400 A* queries and 130 BFS flood fills on a 512^2 obstacle grid - a hand-rolled binary heap on an i64 array with packed keys and lazy deletion, stamped scratch arrays, a ring queue.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O3 | 845 ms | 46.2 MB | 361 ms | 125.9 MB | 324 KB |
| C++ -O2 | 859 ms | 46.2 MB | 148 ms | 98.8 MB | 16 KB |
| C++ -O3 | 883 ms | 46.2 MB | 158 ms | 99.2 MB | 16 KB |
| Axle -O2 | 916 ms | 46.2 MB | 346 ms | 126.2 MB | 323 KB |
| Rust -O2 | 926 ms | 46.2 MB | 182 ms | 153.7 MB | 3867 KB |
| Rust -O3 | 950 ms | 46.2 MB | 183 ms | 153.7 MB | 3867 KB |
| Axle -O1 | 1.02 s | 46.2 MB | 307 ms | 128.0 MB | 323 KB |
| Rust -O1 | 1.15 s | 46.2 MB | 170 ms | 153.0 MB | 3868 KB |
| C++ -O1 | 1.26 s | 46.2 MB | 130 ms | 97.4 MB | 16 KB |
| C++ -O0 | 2.03 s | 46.2 MB | 96 ms | 90.6 MB | 16 KB |
| Axle -O0 | 2.98 s | 46.2 MB | 92 ms | 127.3 MB | 330 KB |
| Rust -O0 | 5.49 s | 46.2 MB | 117 ms | 150.0 MB | 3892 KB |
// A* path queries and BFS flood fills on a 512 x 512 obstacle grid (27%
// blocked, from an integer hash), integer algorithmics with array churn.
//
// A*: 4-connected, unit step cost, Manhattan heuristic (admissible, so every
// path is optimal), a binary min-heap hand-rolled on an i64 array with lazy
// deletion (a stale entry is skipped when popped), keys packed as
// f << 40 | h << 20 | node so a plain integer compare orders the queue and
// breaks ties towards the node nearer the goal. Per-query scratch (g score,
// open stamp, closed stamp) is reset by stamping with the query number, not
// by clearing. The flood fills reuse a ring queue over the whole grid.
//
// Stresses: heap sift loops, scattered loads and stores over 1 MB arrays,
// 64-bit key packing, a ring-buffer queue, neighbour bounds tests.
//
// One line is printed: queries that reached the goal, total path length,
// total nodes expanded, then flood-fill cells reached and the sum of their
// distances.
//
// Equivalent to astar_grid.{c,cpp,rs} - same grid, same queries, same heap,
// same tie-breaking, same printed line.
const W : i32 = 512;
const H : i32 = 512;
const CELLS : i32 = 262144;
const QUERIES : i32 = 400;
const FLOODS : i32 = 130;
class Heap {
data : i64[];
size : i32;
constructor(capacity : i32) {
self.data = new i64[capacity];
self.size = 0;
}
pub fn clear(mut self) : void {
self.size = 0;
}
pub fn isEmpty(self) : bool {
return self.size == 0;
}
pub fn push(mut self, key : i64) : void {
let mut i : i32 = self.size;
self.size = i + 1;
self.data[i] = key;
while (i > 0) {
let parent : i32 = (i - 1) / 2;
if (self.data[parent] <= self.data[i]) {
break;
}
let t : i64 = self.data[parent];
self.data[parent] = self.data[i];
self.data[i] = t;
i = parent;
}
}
pub fn pop(mut self) : i64 {
let top : i64 = self.data[0];
let n : i32 = self.size - 1;
self.size = n;
if (n > 0) {
self.data[0] = self.data[n];
let mut i : i32 = 0;
while (true) {
let l : i32 = 2 * i + 1;
if (l >= n) {
break;
}
let mut smallest : i32 = l;
if (l + 1 < n && self.data[l + 1] < self.data[l]) {
smallest = l + 1;
}
if (self.data[i] <= self.data[smallest]) {
break;
}
let t : i64 = self.data[i];
self.data[i] = self.data[smallest];
self.data[smallest] = t;
i = smallest;
}
}
return top;
}
}
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn iabs(v : i32) : i32 {
return v < 0 ? -v : v;
}
fn manhattan(a : i32, b : i32) : i32 {
return iabs((a & (W - 1)) - (b & (W - 1))) + iabs((a >> 9) - (b >> 9));
}
fn pickOpen(blocked : u8[], seed : u32) : i32 {
let mut c : i32 = (hash32(seed) % (CELLS as u32)) as i32;
while (blocked[c] != 0) {
c = (c + 1) % CELLS;
}
return c;
}
fn main() : void {
let mut blocked = new u8[262144];
for (i of 0..CELLS) {
if (hash32((i as u32) + 0x7a000000) % 100 < 27) {
blocked[i] = 1;
}
}
let mut g = new i32[262144];
let mut openStamp = new i32[262144];
let mut closedStamp = new i32[262144];
let heap : Heap = new Heap(1048576);
let mut dxs = new i32[4];
let mut dys = new i32[4];
dxs[0] = 1;
dxs[1] = -1;
dxs[2] = 0;
dxs[3] = 0;
dys[0] = 0;
dys[1] = 0;
dys[2] = 1;
dys[3] = -1;
let mut reached : i64 = 0;
let mut pathTotal : i64 = 0;
let mut expanded : i64 = 0;
for (q of 1..=QUERIES) {
let start : i32 = pickOpen(blocked, (q as u32) * 2 + 0xa0000000);
let goal : i32 = pickOpen(blocked, (q as u32) * 2 + 0xa0000001);
heap.clear();
g[start] = 0;
openStamp[start] = q;
let h0 : i32 = manhattan(start, goal);
heap.push(((h0 as i64) << 40) | ((h0 as i64) << 20) | (start as i64));
let mut found : i32 = -1;
while (!heap.isEmpty()) {
let key : i64 = heap.pop();
let node : i32 = (key & 0xFFFFF) as i32;
if (closedStamp[node] == q) {
continue;
}
closedStamp[node] = q;
expanded = expanded + 1;
if (node == goal) {
found = g[node];
break;
}
let nx : i32 = node & (W - 1);
let ny : i32 = node >> 9;
for (d of 0..4) {
let mx : i32 = nx + dxs[d];
let my : i32 = ny + dys[d];
if (mx < 0 || mx >= W || my < 0 || my >= H) {
continue;
}
let nb : i32 = (my << 9) | mx;
if (blocked[nb] != 0) {
continue;
}
let ng : i32 = g[node] + 1;
if (openStamp[nb] != q || ng < g[nb]) {
g[nb] = ng;
openStamp[nb] = q;
let hh : i32 = manhattan(nb, goal);
heap.push((((ng + hh) as i64) << 40) | ((hh as i64) << 20) | (nb as i64));
}
}
}
if (found >= 0) {
reached = reached + 1;
pathTotal = pathTotal + (found as i64);
}
}
// BFS flood fills over a ring queue, distances in g, visited by stamp.
let mut ring = new i32[262144];
let mut floodCells : i64 = 0;
let mut floodDist : i64 = 0;
for (f of 1..=FLOODS) {
let stamp : i32 = QUERIES + f;
let src : i32 = pickOpen(blocked, (f as u32) + 0xb0000000);
let mut head : i32 = 0;
let mut tail : i32 = 0;
let mut count : i32 = 0;
ring[tail] = src;
tail = (tail + 1) & (CELLS - 1);
count = count + 1;
g[src] = 0;
openStamp[src] = stamp;
while (count > 0) {
let node : i32 = ring[head];
head = (head + 1) & (CELLS - 1);
count = count - 1;
floodCells = floodCells + 1;
floodDist = floodDist + (g[node] as i64);
let nx : i32 = node & (W - 1);
let ny : i32 = node >> 9;
for (d of 0..4) {
let mx : i32 = nx + dxs[d];
let my : i32 = ny + dys[d];
if (mx < 0 || mx >= W || my < 0 || my >= H) {
continue;
}
let nb : i32 = (my << 9) | mx;
if (blocked[nb] != 0 || openStamp[nb] == stamp) {
continue;
}
openStamp[nb] = stamp;
g[nb] = g[node] + 1;
ring[tail] = nb;
tail = (tail + 1) & (CELLS - 1);
count = count + 1;
}
}
}
println("${reached} ${pathTotal} ${expanded} ${floodCells} ${floodDist}");
}60M bilinear plus Catmull-Rom bicubic samples of a 512^2 f32 image with clamp-to-edge addressing - sixteen clamped, indexed loads per sample, the bounds-check case.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O1 | 1.43 s | 46.5 MB | 112 ms | 97.0 MB | 16 KB |
| Axle -O3 | 1.84 s | 46.5 MB | 132 ms | 126.1 MB | 321 KB |
| Axle -O2 | 1.84 s | 46.5 MB | 129 ms | 126.2 MB | 321 KB |
| Rust -O2 | 1.99 s | 46.5 MB | 135 ms | 148.5 MB | 3864 KB |
| Rust -O3 | 2.05 s | 46.5 MB | 137 ms | 148.3 MB | 3864 KB |
| Rust -O1 | 2.11 s | 46.5 MB | 123 ms | 149.0 MB | 3864 KB |
| C++ -O3 | 2.20 s | 46.5 MB | 123 ms | 98.0 MB | 16 KB |
| C++ -O2 | 2.21 s | 46.5 MB | 123 ms | 98.0 MB | 16 KB |
| Axle -O1 | 2.99 s | 46.5 MB | 107 ms | 127.2 MB | 321 KB |
| Axle -O0 | 10.18 s | 46.5 MB | 87 ms | 126.1 MB | 323 KB |
| C++ -O0 | 11.76 s | 46.5 MB | 97 ms | 92.3 MB | 16 KB |
| Rust -O0 | 12.59 s | 46.5 MB | 100 ms | 146.7 MB | 3875 KB |
// Bilinear and Catmull-Rom bicubic sampling of a 512 x 512 f32 image at
// hashed coordinates that run four texels past every edge, so the
// clamp-to-edge addressing is exercised on one sample in sixteen. The
// bicubic filter reads a 4 x 4 neighbourhood: sixteen clamped, indexed loads
// per sample.
//
// Stresses: array indexing with integer clamps (the bounds-check case: every
// index is clamped, so a compiler that can prove it removes the checks),
// f32 -> int floor, dependent multiply-add chains for the weights.
//
// One line is printed: the sum of the quantised bicubic results (x 4096,
// truncated), of the bilinear results, and how many bicubic results
// overshot 1.0.
//
// Equivalent to bicubic_sample.{c,cpp,rs} - same image, same taps, same
// weights, same printed line.
const W : i32 = 512;
const H : i32 = 512;
const N_SAMPLES : u32 = 60000000;
const INV24 : f32 = 5.9604644775390625e-8;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn ifloor(x : f32) : i32 {
let t : i32 = x as i32;
if ((t as f32) > x) {
return t - 1;
}
return t;
}
fn clampi(v : i32, lo : i32, hi : i32) : i32 {
if (v < lo) {
return lo;
}
if (v > hi) {
return hi;
}
return v;
}
fn texel(img : f32[], x : i32, y : i32) : f32 {
return img[clampi(y, 0, H - 1) * W + clampi(x, 0, W - 1)];
}
fn lerp(a : f32, b : f32, t : f32) : f32 {
return a + (b - a) * t;
}
fn bilinear(img : f32[], u : f32, v : f32) : f32 {
let x0 : i32 = ifloor(u);
let y0 : i32 = ifloor(v);
let tx : f32 = u - (x0 as f32);
let ty : f32 = v - (y0 as f32);
let a : f32 = lerp(texel(img, x0, y0), texel(img, x0 + 1, y0), tx);
let b : f32 = lerp(texel(img, x0, y0 + 1), texel(img, x0 + 1, y0 + 1), tx);
return lerp(a, b, ty);
}
fn row4(img : f32[], y : i32, x0 : i32, w0 : f32, w1 : f32, w2 : f32, w3 : f32) : f32 {
return w0 * texel(img, x0 - 1, y) + w1 * texel(img, x0, y) + w2 * texel(img, x0 + 1, y)
+ w3 * texel(img, x0 + 2, y);
}
fn bicubic(img : f32[], u : f32, v : f32) : f32 {
let x0 : i32 = ifloor(u);
let y0 : i32 = ifloor(v);
let tx : f32 = u - (x0 as f32);
let ty : f32 = v - (y0 as f32);
let xw0 : f32 = (((-0.5 as f32) * tx + (1.0 as f32)) * tx - (0.5 as f32)) * tx;
let xw1 : f32 = ((1.5 as f32) * tx - (2.5 as f32)) * tx * tx + (1.0 as f32);
let xw2 : f32 = (((-1.5 as f32) * tx + (2.0 as f32)) * tx + (0.5 as f32)) * tx;
let xw3 : f32 = ((0.5 as f32) * tx - (0.5 as f32)) * tx * tx;
let yw0 : f32 = (((-0.5 as f32) * ty + (1.0 as f32)) * ty - (0.5 as f32)) * ty;
let yw1 : f32 = ((1.5 as f32) * ty - (2.5 as f32)) * ty * ty + (1.0 as f32);
let yw2 : f32 = (((-1.5 as f32) * ty + (2.0 as f32)) * ty + (0.5 as f32)) * ty;
let yw3 : f32 = ((0.5 as f32) * ty - (0.5 as f32)) * ty * ty;
let r0 : f32 = row4(img, y0 - 1, x0, xw0, xw1, xw2, xw3);
let r1 : f32 = row4(img, y0, x0, xw0, xw1, xw2, xw3);
let r2 : f32 = row4(img, y0 + 1, x0, xw0, xw1, xw2, xw3);
let r3 : f32 = row4(img, y0 + 2, x0, xw0, xw1, xw2, xw3);
return yw0 * r0 + yw1 * r1 + yw2 * r2 + yw3 * r3;
}
fn main() : void {
let mut img = new f32[262144];
for (i of 0..262144) {
img[i] = unit(hash32(i as u32));
}
let mut cubicSum : i64 = 0;
let mut linearSum : i64 = 0;
let mut overshoot : i64 = 0;
let mut i : u32 = 0;
while (i < N_SAMPLES) {
let u : f32 = unit(hash32(i * 2 + 0x51000000)) * (520.0 as f32) - (4.0 as f32);
let v : f32 = unit(hash32(i * 2 + 0x51000001)) * (520.0 as f32) - (4.0 as f32);
let c : f32 = bicubic(img, u, v);
let l : f32 = bilinear(img, u, v);
cubicSum = cubicSum + ((c * (4096.0 as f32)) as i64);
linearSum = linearSum + ((l * (4096.0 as f32)) as i64);
if (c > (1.0 as f32)) {
overshoot = overshoot + 1;
}
i = i + 1;
}
println("${cubicSum} ${linearSum} ${overshoot}");
}Display composition of a 1024x512 RGB f32 picture for 40 exposures - 5-point clamped sharpen (nine stencil loads), a bloom read carried along the row at exactly 2x, vignette, knee-gated desaturation divide, interpolated tone table, sRGB code table, dither and word packing.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O3 | 695 ms | 46.0 MB | 321 ms | 111.0 MB | 20 KB |
| C++ -O2 | 697 ms | 46.0 MB | 318 ms | 111.0 MB | 20 KB |
| C++ -O1 | 859 ms | 46.0 MB | 302 ms | 109.9 MB | 16 KB |
| Axle -O3 | 930 ms | 46.0 MB | 213 ms | 126.1 MB | 326 KB |
| Axle -O2 | 933 ms | 46.0 MB | 210 ms | 126.0 MB | 326 KB |
| Axle -O1 | 945 ms | 46.0 MB | 144 ms | 127.9 MB | 324 KB |
| Rust -O2 | 1.10 s | 46.0 MB | 191 ms | 159.0 MB | 3869 KB |
| Rust -O1 | 1.19 s | 46.0 MB | 192 ms | 155.8 MB | 3870 KB |
| Rust -O3 | 1.26 s | 46.0 MB | 196 ms | 157.7 MB | 3869 KB |
| C++ -O0 | 5.04 s | 46.0 MB | 263 ms | 105.8 MB | 20 KB |
| Axle -O0 | 5.13 s | 46.0 MB | 93 ms | 126.2 MB | 331 KB |
| Rust -O0 | 5.35 s | 46.0 MB | 120 ms | 159.1 MB | 3893 KB |
// Display composition of a software renderer: sharpen, bloom, vignette, grade,
// tone table, sRGB table, dither, pack. A 1024 x 512 RGB f32 picture of hashed
// radiance (a squared uniform plus a horizontal ramp) and a 512 x 256 RGB f32
// "bloom" picture are composed into one i32 word per pixel, several times with
// different exposures.
//
// Per pixel: a 5-point sharpen of each channel clamped to the range of its
// neighbours (interior pixels only), the exposure multiply, the bloom read
// bilinearly at exactly 2x with the fixed weights 3/4 and 1/4 by carrying three
// bloom columns along the row (one new column for every two pixels), a
// vignette 1 - 0.9 r^2, luminance, a highlight desaturation whose single
// division is taken only above a knee, a tone gain read from a 4097-entry f32
// table by linear interpolation, the code of each channel from an 8193-entry
// i32 table indexed by a two-segment linear index (split at 1/16), an integer
// dither from a 64 x 64 tile, and shifts and ors into one word.
//
// Stresses: stencil loads with bounds checks (nine per pixel), float -> int
// conversions, table lookups, a predictable branch per pixel, and a row loop
// that carries nine floats.
//
// One line is printed: the sum of all output words over all passes, then how
// many words have a low byte of 255.
//
// Equivalent to display_compose.{c,cpp,rs} - same pictures, same tables, same
// operation order, same printed line.
const W : i32 = 1024;
const H : i32 = 512;
const HW : i32 = 512;
const HH : i32 = 256;
const PASSES : i32 = 40;
const INV24 : f32 = 5.9604644775390625e-8;
const TABLE : i32 = 4096;
const TONE_MAX : f32 = 8.0;
const WORD_SPLIT : f32 = 0.0625;
const WORD_LOW : f32 = 65536.0;
const WORD_HIGH : f32 = 4369.0;
const DITHER_SIDE : i32 = 64;
const SHARPEN : f32 = 0.5;
const VIGNETTE : f32 = 0.9;
const LUMA_R : f32 = 0.2126;
const LUMA_G : f32 = 0.7152;
const LUMA_B : f32 = 0.0722;
const CLIP_KNEE : f32 = 1.0;
const CLIP_RANGE : f32 = 1.0;
const CLIP_DESAT : f32 = 0.6;
const GRADE : f32 = 1.1;
const BLOOM_STRENGTH : f32 = 0.5;
const BLOOM_GAIN : f32 = 0.3;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn minf(a : f32, b : f32) : f32 {
return a < b ? a : b;
}
fn maxf(a : f32, b : f32) : f32 {
return a > b ? a : b;
}
fn clampIdx(i : i32, n : i32) : i32 {
if (i < 0) {
return 0;
}
if (i >= n) {
return n - 1;
}
return i;
}
fn srgbEncode(c : f32) : f32 {
if (c <= (0.0031308 as f32)) {
return c * (12.92 as f32);
}
let s1 : f32 = c.sqrt();
let s2 : f32 = s1.sqrt();
let s3 : f32 = s2.sqrt();
return (0.662002687 as f32) * s1 + (0.684122060 as f32) * s2 - (0.323583601 as f32) * s3
- (0.0225411470 as f32) * c;
}
// One channel sharpened: the value plus a share of its difference from the mean
// of its four neighbours, kept inside the range of those neighbours and itself.
fn sharp(c : f32, l : f32, r : f32, u : f32, d : f32) : f32 {
let lo : f32 = minf(minf(l, r), minf(u, d));
let hi : f32 = maxf(maxf(l, r), maxf(u, d));
let s : f32 = c + (c - (l + r + u + d) * (0.25 as f32)) * SHARPEN;
return minf(maxf(s, minf(lo, c)), maxf(hi, c));
}
// The entry of the word table for the linear value x: the nearest of 4096 over
// 0..1/16, or of 4096 over the rest. Below 0 reads the first, above 1 the last.
fn wordIndex(x : f32) : i32 {
let c : f32 = minf(maxf(x, (0.0 as f32)), (1.0 as f32));
if (c < WORD_SPLIT) {
return (c * WORD_LOW + (0.5 as f32)) as i32;
}
return ((c - WORD_SPLIT) * WORD_HIGH + (TABLE as f32) + (0.5 as f32)) as i32;
}
fn compose(hdr : f32[], lv1 : f32[], tone : f32[], word : i32[], grain : i32[], mut out : i32[], exposure : f32) : void {
let kb : f32 = BLOOM_STRENGTH * BLOOM_GAIN;
let bloom : bool = kb > (0.0 as f32);
let kNear : f32 = kb * (0.75 as f32);
let kFar : f32 = kb * (0.25 as f32);
let invW : f32 = (1.0 as f32) / (W as f32);
let invH : f32 = (1.0 as f32) / (H as f32);
let mask : i32 = DITHER_SIDE - 1;
let row : i64 = (W * 3) as i64;
let toneScale : f32 = (TABLE as f32) / TONE_MAX;
for (y of 0..H) {
let dy : f32 = ((y as f32) + (0.5 as f32)) * invH - (0.5 as f32);
let dy2 : f32 = dy * dy;
let inner : bool = y > 0 && y < H - 1;
let ditherRow : i32 = (y & mask) * DITHER_SIDE;
let by : i32 = y >> 1;
let rowA : i64 = ((clampIdx(by, HH) * HW) * 3) as i64;
let rowB : i64 = ((clampIdx(by + ((y & 1) * 2 - 1), HH) * HW) * 3) as i64;
let mut pr : f32 = 0.0;
let mut pg : f32 = 0.0;
let mut pb : f32 = 0.0;
let mut cr : f32 = 0.0;
let mut cg : f32 = 0.0;
let mut cb : f32 = 0.0;
let mut nr : f32 = 0.0;
let mut ng : f32 = 0.0;
let mut nb : f32 = 0.0;
if (bloom) {
nr = lv1[rowA] * kNear + lv1[rowB] * kFar;
ng = lv1[rowA + 1] * kNear + lv1[rowB + 1] * kFar;
nb = lv1[rowA + 2] * kNear + lv1[rowB + 2] * kFar;
cr = nr;
cg = ng;
cb = nb;
}
let mut at : i64 = ((y * W) * 3) as i64;
let mut o : i64 = (y * W) as i64;
for (x of 0..W) {
let mut r : f32 = hdr[at];
let mut g : f32 = hdr[at + 1];
let mut b : f32 = hdr[at + 2];
if (inner && x > 0 && x < W - 1) {
r = sharp(r, hdr[at - 3], hdr[at + 3], hdr[at - row], hdr[at + row]);
g = sharp(g, hdr[at - 2], hdr[at + 4], hdr[at - row + 1], hdr[at + row + 1]);
b = sharp(b, hdr[at - 1], hdr[at + 5], hdr[at - row + 2], hdr[at + row + 2]);
}
r = r * exposure;
g = g * exposure;
b = b * exposure;
if (bloom) {
if ((x & 1) == 0) {
pr = cr;
pg = cg;
pb = cb;
cr = nr;
cg = ng;
cb = nb;
let k : i64 = (clampIdx((x >> 1) + 1, HW) * 3) as i64;
nr = lv1[rowA + k] * kNear + lv1[rowB + k] * kFar;
ng = lv1[rowA + k + 1] * kNear + lv1[rowB + k + 1] * kFar;
nb = lv1[rowA + k + 2] * kNear + lv1[rowB + k + 2] * kFar;
r = r + cr * (0.75 as f32) + pr * (0.25 as f32);
g = g + cg * (0.75 as f32) + pg * (0.25 as f32);
b = b + cb * (0.75 as f32) + pb * (0.25 as f32);
} else {
r = r + cr * (0.75 as f32) + nr * (0.25 as f32);
g = g + cg * (0.75 as f32) + ng * (0.25 as f32);
b = b + cb * (0.75 as f32) + nb * (0.25 as f32);
}
}
let dx : f32 = ((x as f32) + (0.5 as f32)) * invW - (0.5 as f32);
let dim : f32 = (1.0 as f32) - VIGNETTE * (dx * dx + dy2);
r = r * dim;
g = g * dim;
b = b * dim;
let grey : f32 = LUMA_R * r + LUMA_G * g + LUMA_B * b;
let peak : f32 = maxf(maxf(r, maxf(g, b)), (0.0 as f32));
if (peak > CLIP_KNEE) {
let fade : f32 = minf((peak - CLIP_KNEE) / (peak + CLIP_RANGE), (1.0 as f32)) * CLIP_DESAT;
r = r + (grey - r) * fade;
g = g + (grey - g) * fade;
b = b + (grey - b) * fade;
}
r = grey + (r - grey) * GRADE;
g = grey + (g - grey) * GRADE;
b = grey + (b - grey) * GRADE;
let m : f32 = maxf(maxf(r, maxf(g, b)), (0.0 as f32));
let mut gain : f32 = 0.0;
if (m >= TONE_MAX) {
gain = (1.0 as f32) / ((1.0 as f32) + (0.25 as f32) * m);
} else {
let f : f32 = m * toneScale;
let k : i32 = f as i32;
let lo : f32 = tone[k as i64];
gain = lo + (tone[(k + 1) as i64] - lo) * (f - (k as f32));
}
let n : i32 = grain[(ditherRow + (x & mask)) as i64];
out[o] = (((word[wordIndex(r * gain) as i64] + n) >> 8) << 16)
| (((word[wordIndex(g * gain) as i64] + n) >> 8) << 8)
| ((word[wordIndex(b * gain) as i64] + n) >> 8);
at = at + 3;
o = o + 1;
}
}
}
fn main() : void {
let mut hdr = new f32[1572864];
for (y of 0..H) {
for (x of 0..W) {
let i0 : i32 = (y * W + x) * 3;
let ramp : f32 = (x as f32) * (0.0029296875 as f32);
let u0 : f32 = unit(hash32((i0 as u32) + 0x3c000000));
let u1 : f32 = unit(hash32((i0 as u32) + 1 + 0x3c000000));
let u2 : f32 = unit(hash32((i0 as u32) + 2 + 0x3c000000));
hdr[i0] = u0 * u0 * (1.5 as f32) + ramp;
hdr[i0 + 1] = u1 * u1 * (1.5 as f32) + ramp * (0.8 as f32);
hdr[i0 + 2] = u2 * u2 * (1.5 as f32) + ramp * (0.6 as f32);
}
}
let mut lv1 = new f32[393216];
for (i of 0..393216) {
let u : f32 = unit(hash32((i as u32) + 0x2b000000));
lv1[i] = u * u * (2.0 as f32);
}
let mut tone = new f32[4097];
for (i of 0..4097) {
let m : f32 = (i as f32) * (0.001953125 as f32);
tone[i] = (1.0 as f32) / ((1.0 as f32) + (0.25 as f32) * m);
}
let mut word = new i32[8193];
for (i of 0..8193) {
let mut v : f32 = 0.0;
if (i < TABLE) {
v = (i as f32) * ((1.0 as f32) / WORD_LOW);
} else {
v = WORD_SPLIT + ((i - TABLE) as f32) / WORD_HIGH;
}
let e : f32 = minf(srgbEncode(minf(v, (1.0 as f32))), (1.0 as f32));
word[i] = (e * (65280.0 as f32) + (0.5 as f32)) as i32;
}
let mut grain = new i32[4096];
for (i of 0..4096) {
grain[i] = (hash32((i as u32) + 0x5bd1e995) >> 24) as i32;
}
let mut out = new i32[524288];
let mut total : i64 = 0;
let mut hot : i64 = 0;
for (pass of 0..PASSES) {
let exposure : f32 = (0.5 as f32) + (pass as f32) * (0.0625 as f32);
compose(hdr, lv1, tone, word, grain, out, exposure);
for (i of 0..524288) {
total = total + (out[i] as i64);
if ((out[i] & 255) == 255) {
hot = hot + 1;
}
}
}
println("${total} ${hot}");
}Five-octave 2D value-noise fBm over a 1024^2 grid for 16 passes - integer hash feeding f32 smoothstep and lerp chains through a tree of small calls.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O3 | 689 ms | 44.4 MB | 106 ms | 97.5 MB | 16 KB |
| Axle -O1 | 897 ms | 44.4 MB | 89 ms | 126.0 MB | 315 KB |
| C++ -O2 | 913 ms | 44.4 MB | 103 ms | 97.2 MB | 16 KB |
| C++ -O1 | 931 ms | 44.4 MB | 100 ms | 96.4 MB | 16 KB |
| Axle -O2 | 1.01 s | 44.4 MB | 101 ms | 127.9 MB | 315 KB |
| Axle -O3 | 1.02 s | 44.4 MB | 101 ms | 126.1 MB | 315 KB |
| Rust -O3 | 1.10 s | 44.4 MB | 106 ms | 137.3 MB | 3863 KB |
| Rust -O2 | 1.11 s | 44.4 MB | 103 ms | 138.9 MB | 3863 KB |
| Rust -O1 | 1.13 s | 44.4 MB | 103 ms | 140.7 MB | 3863 KB |
| Axle -O0 | 3.01 s | 44.4 MB | 84 ms | 125.2 MB | 315 KB |
| C++ -O0 | 3.85 s | 44.4 MB | 86 ms | 92.0 MB | 16 KB |
| Rust -O0 | 3.91 s | 44.4 MB | 88 ms | 139.9 MB | 3869 KB |
// 2D value noise and fractal Brownian motion: an integer-hash lattice,
// smoothstep interpolation, five octaves (lacunarity 2, gain 0.5), sampled on
// a 1024 x 1024 grid for several passes, each pass shifted by a whole number
// of lattice cells.
//
// Stresses: integer hashing feeding float lerps, a hand-written floor, f32
// multiply-add chains, and a call tree of small functions (noise -> lattice
// -> hash) the optimiser has to flatten.
//
// One line is printed: the sum of the quantised fbm values (x 65536,
// truncated), then the largest quantised value seen.
//
// Equivalent to fbm_noise.{c,cpp,rs} — same hash, same lattice, same octave
// order, same printed line.
const SIZE : i32 = 1024;
const PASSES : i32 = 16;
const OCTAVES : u32 = 5;
const INV24 : f32 = 5.9604644775390625e-8;
const STEP : f32 = 0.015625;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn ifloor(x : f32) : i32 {
let t : i32 = x as i32;
if ((t as f32) > x) {
return t - 1;
}
return t;
}
fn lattice(ix : i32, iy : i32, oct : u32) : f32 {
let h : u32 = hash32(((ix as u32) * 0x27d4eb2d) ^ ((iy as u32) * 0x165667b1)
^ (oct * 0x9e3779b9));
return ((h >> 8) as f32) * INV24;
}
fn smooth(t : f32) : f32 {
return t * t * ((3.0 as f32) - (2.0 as f32) * t);
}
fn lerp(a : f32, b : f32, t : f32) : f32 {
return a + (b - a) * t;
}
fn noise(x : f32, y : f32, oct : u32) : f32 {
let ix : i32 = ifloor(x);
let iy : i32 = ifloor(y);
let sx : f32 = smooth(x - (ix as f32));
let sy : f32 = smooth(y - (iy as f32));
let v00 : f32 = lattice(ix, iy, oct);
let v10 : f32 = lattice(ix + 1, iy, oct);
let v01 : f32 = lattice(ix, iy + 1, oct);
let v11 : f32 = lattice(ix + 1, iy + 1, oct);
return lerp(lerp(v00, v10, sx), lerp(v01, v11, sx), sy);
}
fn fbm(x : f32, y : f32) : f32 {
let mut sum : f32 = 0.0;
let mut amp : f32 = 0.5;
let mut freq : f32 = 1.0;
for (o of 0..OCTAVES) {
sum = sum + amp * noise(x * freq, y * freq, o);
freq = freq * (2.0 as f32);
amp = amp * (0.5 as f32);
}
return sum;
}
fn main() : void {
let mut total : i64 = 0;
let mut best : i64 = 0;
for (pass of 0..PASSES) {
let off : f32 = (pass * 7) as f32;
for (py of 0..SIZE) {
let y : f32 = (py as f32) * STEP + off;
for (px of 0..SIZE) {
let x : f32 = (px as f32) * STEP + off;
let q : i64 = (fbm(x, y) * (65536.0 as f32)) as i64;
total = total + q;
if (q > best) {
best = q;
}
}
}
}
println("${total} ${best}");
}4M rays through a 128^3 world of 16^3 sections that skip empty space three ways - whole empty sections, a per-cell gap-field cube, or one Amanatides-Woo step: data-dependent branches, u8/u16 loads, float floors, class state reloaded across calls.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O2 | 1.11 s | 46.2 MB | 356 ms | 111.1 MB | 20 KB |
| C++ -O3 | 1.14 s | 46.2 MB | 349 ms | 111.1 MB | 20 KB |
| Axle -O3 | 1.15 s | 46.2 MB | 322 ms | 129.7 MB | 328 KB |
| Axle -O2 | 1.16 s | 46.2 MB | 314 ms | 128.1 MB | 328 KB |
| Rust -O3 | 1.16 s | 46.2 MB | 218 ms | 158.2 MB | 3869 KB |
| Rust -O2 | 1.17 s | 46.2 MB | 215 ms | 160.4 MB | 3869 KB |
| C++ -O1 | 1.18 s | 46.2 MB | 329 ms | 110.1 MB | 20 KB |
| Axle -O1 | 1.23 s | 46.2 MB | 237 ms | 128.2 MB | 327 KB |
| Rust -O1 | 1.24 s | 46.2 MB | 206 ms | 154.5 MB | 3870 KB |
| C++ -O0 | 1.89 s | 46.2 MB | 262 ms | 106.0 MB | 20 KB |
| Axle -O0 | 2.29 s | 46.2 MB | 96 ms | 124.3 MB | 334 KB |
| Rust -O0 | 2.71 s | 46.2 MB | 124 ms | 154.4 MB | 3902 KB |
// Voxel ray walk that skips empty space three ways. The world is 128^3 cells in
// 8^3 sections of 16^3 (a heightfield of blocky hashed terrain plus a few
// hashed floating cells, so most of the air above the ground is whole empty
// sections). A u8 per section says empty or mixed. A u8 gap field, computed
// once before the timed loop (not timed), holds for every cell the radius,
// capped at 4, of the largest cube of empty cells centred on it.
//
// The timed loop walks rays from hashed points in the air. At each cell: in an
// empty section, leave the 16-box in one move; in a mixed section whose cell
// has gap >= 2, leave the (2g+1)-cube in one move; otherwise take one
// Amanatides-Woo step. Leaving a box recomputes the three exit times from the
// box faces, takes the smallest, and re-derives the cell on the two other axes
// by the floor of the point; the walk stops on a solid cell, on leaving the
// world, or after GUARD iterations.
//
// Stresses: data-dependent branches (three walking modes), u8 and u16 loads
// with bounds checks, float -> int floors, a class whose section fields are
// written and reloaded across calls.
//
// One line is printed: rays that hit, the sum of the hit cell indices, the
// total number of walk iterations.
//
// Equivalent to gap_dda.{c,cpp,rs} - same world, same gap field, same rays,
// same walk, same printed line.
const N_RAYS : u32 = 4000000;
const GUARD : i32 = 512;
const INV24 : f32 = 5.9604644775390625e-8;
const NEVER : f32 = 1.0e30;
const PARALLEL : f32 = 1.0e-6;
const T_MAX : f32 = 400.0;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn ifloor(v : f32) : i32 {
let i : i32 = v as i32;
if ((i as f32) > v) {
return i - 1;
}
return i;
}
// The index of cell (x, y, z) in the section-major arrays: the section in the
// high bits, the cell inside it in the low twelve.
fn cellIndex(x : i32, y : i32, z : i32) : i32 {
let sec : i32 = (((y >> 4) * 8 + (z >> 4)) * 8) + (x >> 4);
return (sec << 12) | ((y & 15) << 8) | ((z & 15) << 4) | (x & 15);
}
// The solid cells inside the box [x0, x1) x [y0, y1) x [z0, z1) from the prefix sums.
fn boxSum(pre : i32[], x0 : i32, y0 : i32, z0 : i32, x1 : i32, y1 : i32, z1 : i32) : i32 {
return pre[(y1 * 129 + z1) * 129 + x1] - pre[(y0 * 129 + z1) * 129 + x1] - pre[(y1 * 129 + z0) * 129 + x1]
- pre[(y1 * 129 + z1) * 129 + x0] + pre[(y0 * 129 + z0) * 129 + x1] + pre[(y0 * 129 + z1) * 129 + x0]
+ pre[(y1 * 129 + z0) * 129 + x0] - pre[(y0 * 129 + z0) * 129 + x0];
}
class Walker {
pub classes : u8[];
pub cache : u16[];
pub gaps : u8[];
pub steps : i64;
pub cSec : i32;
pub cBase : i64;
pub cCls : i32;
constructor(classes : u8[], cache : u16[], gaps : u8[]) {
self.classes = classes;
self.cache = cache;
self.gaps = gaps;
self.steps = 0;
self.cSec = 0;
self.cBase = 0;
self.cCls = 0;
}
// Looks up the section of cell (cx, cy, cz): its number, the base of its cells and its class.
pub fn resolve(mut self, cx : i32, cy : i32, cz : i32) : void {
let sec : i32 = (((cy >> 4) * 8 + (cz >> 4)) * 8) + (cx >> 4);
self.cSec = sec;
self.cBase = ((sec as i64) << 12);
self.cCls = self.classes[sec] as i32;
}
// Walks the ray from (ox, oy, oz) along (dx, dy, dz); the index of the solid
// cell it enters, or -1.
pub fn walk(mut self, ox : f32, oy : f32, oz : f32, dx : f32, dy : f32, dz : f32) : i32 {
let mut cx : i32 = ifloor(ox);
let mut cy : i32 = ifloor(oy);
let mut cz : i32 = ifloor(oz);
let mut stepX : i32 = 1;
let mut stepY : i32 = 1;
let mut stepZ : i32 = 1;
let mut tdx : f32 = NEVER;
let mut tdy : f32 = NEVER;
let mut tdz : f32 = NEVER;
let mut tmx : f32 = NEVER;
let mut tmy : f32 = NEVER;
let mut tmz : f32 = NEVER;
if (dx > PARALLEL) {
tdx = (1.0 as f32) / dx;
tmx = ((cx + 1) as f32 - ox) * tdx;
} else if (dx < -PARALLEL) {
stepX = -1;
tdx = (-1.0 as f32) / dx;
tmx = (ox - (cx as f32)) * tdx;
}
if (dy > PARALLEL) {
tdy = (1.0 as f32) / dy;
tmy = ((cy + 1) as f32 - oy) * tdy;
} else if (dy < -PARALLEL) {
stepY = -1;
tdy = (-1.0 as f32) / dy;
tmy = (oy - (cy as f32)) * tdy;
}
if (dz > PARALLEL) {
tdz = (1.0 as f32) / dz;
tmz = ((cz + 1) as f32 - oz) * tdz;
} else if (dz < -PARALLEL) {
stepZ = -1;
tdz = (-1.0 as f32) / dz;
tmz = (oz - (cz as f32)) * tdz;
}
let cache : u16[] = self.cache;
let gaps : u8[] = self.gaps;
let mut t : f32 = 0.0;
let mut axis : i32 = 1;
self.resolve(cx, cy, cz);
let mut cls : i32 = self.cCls;
let mut base : i64 = self.cBase;
let mut csx : i32 = cx >> 4;
let mut csy : i32 = cy >> 4;
let mut csz : i32 = cz >> 4;
let mut guard : i32 = 0;
let mut found : i32 = -1;
while (guard < GUARD) {
guard = guard + 1;
let mut box : i32 = 0;
let mut lx : i32 = 0;
let mut ly : i32 = 0;
let mut lz : i32 = 0;
if (cls == 0) {
box = 16;
lx = cx & -16;
ly = cy & -16;
lz = cz & -16;
} else {
let g : i32 = gaps[base + (((cy & 15) << 8) | ((cz & 15) << 4) | (cx & 15)) as i64] as i32;
if (g >= 2) {
box = 2 * g + 1;
lx = cx - g;
ly = cy - g;
lz = cz - g;
}
}
if (box > 0) {
let mut ex : f32 = NEVER;
let mut ey : f32 = NEVER;
let mut ez : f32 = NEVER;
if (dx > PARALLEL) {
ex = (((lx + box) as f32) - ox) * tdx;
} else if (dx < -PARALLEL) {
ex = (ox - (lx as f32)) * tdx;
}
if (dy > PARALLEL) {
ey = (((ly + box) as f32) - oy) * tdy;
} else if (dy < -PARALLEL) {
ey = (oy - (ly as f32)) * tdy;
}
if (dz > PARALLEL) {
ez = (((lz + box) as f32) - oz) * tdz;
} else if (dz < -PARALLEL) {
ez = (oz - (lz as f32)) * tdz;
}
if (ex <= ey && ex <= ez) {
t = ex;
axis = 0;
} else if (ey <= ez) {
t = ey;
axis = 1;
} else {
t = ez;
axis = 2;
}
if (t > T_MAX) {
break;
}
if (axis == 0) {
if (stepX > 0) {
cx = lx + box;
} else {
cx = lx - 1;
}
} else {
cx = ifloor(ox + dx * t);
}
if (axis == 1) {
if (stepY > 0) {
cy = ly + box;
} else {
cy = ly - 1;
}
} else {
cy = ifloor(oy + dy * t);
}
if (axis == 2) {
if (stepZ > 0) {
cz = lz + box;
} else {
cz = lz - 1;
}
} else {
cz = ifloor(oz + dz * t);
}
if (dx > PARALLEL) {
tmx = (((cx + 1) as f32) - ox) * tdx;
} else if (dx < -PARALLEL) {
tmx = (ox - (cx as f32)) * tdx;
}
if (dy > PARALLEL) {
tmy = (((cy + 1) as f32) - oy) * tdy;
} else if (dy < -PARALLEL) {
tmy = (oy - (cy as f32)) * tdy;
}
if (dz > PARALLEL) {
tmz = (((cz + 1) as f32) - oz) * tdz;
} else if (dz < -PARALLEL) {
tmz = (oz - (cz as f32)) * tdz;
}
} else {
if (tmx < tmy && tmx < tmz) {
cx = cx + stepX;
t = tmx;
tmx = tmx + tdx;
axis = 0;
} else if (tmy < tmz) {
cy = cy + stepY;
t = tmy;
tmy = tmy + tdy;
axis = 1;
} else {
cz = cz + stepZ;
t = tmz;
tmz = tmz + tdz;
axis = 2;
}
if (t > T_MAX) {
break;
}
}
if (cx < 0 || cx >= 128 || cy < 0 || cy >= 128 || cz < 0 || cz >= 128) {
break;
}
if ((cx >> 4) != csx || (cy >> 4) != csy || (cz >> 4) != csz) {
csx = cx >> 4;
csy = cy >> 4;
csz = cz >> 4;
self.resolve(cx, cy, cz);
cls = self.cCls;
base = self.cBase;
}
let s : i32 = cache[base + (((cy & 15) << 8) | ((cz & 15) << 4) | (cx & 15)) as i64] as i32;
if (s != 0) {
found = (cy * 128 + cz) * 128 + cx;
break;
}
}
self.steps = self.steps + (guard as i64);
return found;
}
}
fn main() : void {
// Terrain: blocky heights plus fine grain, 20 to 59 cells high.
let mut hm = new i32[16384];
for (z of 0..128) {
for (x of 0..128) {
let coarse : u32 = hash32(((x >> 3) as u32) * 1013 + ((z >> 3) as u32) * 7919 + 0x1234567) % 36;
let fine : u32 = hash32((x as u32) * 65537 + (z as u32) + 0xabc) % 5;
hm[z * 128 + x] = 20 + (coarse as i32) + (fine as i32);
}
}
let mut cache = new u16[2097152];
let mut classes = new u8[512];
for (y of 0..128) {
for (z of 0..128) {
for (x of 0..128) {
let ci : i32 = cellIndex(x, y, z);
if (y < hm[z * 128 + x]) {
cache[ci] = (1 + ((x + y + z) & 3)) as u16;
} else if (hash32((ci as u32) + 0x51ed270b) % 65536 < 4) {
cache[ci] = 9;
}
if (cache[ci] != 0) {
classes[ci >> 12] = 1;
}
}
}
}
// Prefix sums of the solid cells, then the gap field from them.
let mut pre = new i32[2146689];
for (y of 1..129) {
for (z of 1..129) {
for (x of 1..129) {
let mut solid : i32 = 0;
if (cache[cellIndex(x - 1, y - 1, z - 1)] != 0) {
solid = 1;
}
pre[(y * 129 + z) * 129 + x] = solid + pre[((y - 1) * 129 + z) * 129 + x]
+ pre[(y * 129 + (z - 1)) * 129 + x] + pre[(y * 129 + z) * 129 + (x - 1)]
- pre[((y - 1) * 129 + (z - 1)) * 129 + x] - pre[((y - 1) * 129 + z) * 129 + (x - 1)]
- pre[(y * 129 + (z - 1)) * 129 + (x - 1)] + pre[((y - 1) * 129 + (z - 1)) * 129 + (x - 1)];
}
}
}
let mut gaps = new u8[2097152];
for (y of 0..128) {
for (z of 0..128) {
for (x of 0..128) {
let ci : i32 = cellIndex(x, y, z);
if (cache[ci] == 0) {
let mut g : i32 = 0;
let mut more : bool = true;
while (more && g < 4) {
let n : i32 = g + 1;
if (x - n >= 0 && x + n <= 127 && y - n >= 0 && y + n <= 127 && z - n >= 0 && z + n <= 127
&& boxSum(pre, x - n, y - n, z - n, x + n + 1, y + n + 1, z + n + 1) == 0) {
g = n;
} else {
more = false;
}
}
gaps[ci] = g as u8;
}
}
}
}
let w : Walker = new Walker(classes, cache, gaps);
let mut hits : i64 = 0;
let mut idxSum : i64 = 0;
let mut r : u32 = 0;
while (r < N_RAYS) {
let b : u32 = r * 16;
let x0 : i32 = (hash32(b + 1) % 128) as i32;
let z0 : i32 = (hash32(b + 2) % 128) as i32;
let mut y0 : i32 = 60 + ((hash32(b + 3) % 68) as i32);
while (y0 < 127 && w.cache[cellIndex(x0, y0, z0)] != 0) {
y0 = y0 + 1;
}
let ox : f32 = (x0 as f32) + unit(hash32(b + 4));
let oy : f32 = (y0 as f32) + unit(hash32(b + 5));
let oz : f32 = (z0 as f32) + unit(hash32(b + 6));
let dx : f32 = unit(hash32(b + 7)) * (2.0 as f32) - (1.0 as f32);
let dy : f32 = (unit(hash32(b + 8)) * (2.0 as f32) - (1.0 as f32)) * (0.6 as f32) - (0.5 as f32);
let dz : f32 = unit(hash32(b + 9)) * (2.0 as f32) - (1.0 as f32);
let hit : i32 = w.walk(ox, oy, oz, dx, dy, dz);
if (hit >= 0) {
hits = hits + 1;
idxSum = idxSum + (hit as i64);
}
r = r + 1;
}
println("${hits} ${idxSum} ${w.steps}");
}250M Cook-Torrance GGX BRDF evaluations (GGX D, Smith-Schlick G, Schlick F, Lambert) over 4096 hashed N/V/L triples and 16 roughness values - dense f32 maths with several guarded divides.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O3 | 1.26 s | 44.5 MB | 140 ms | 126.1 MB | 323 KB |
| Axle -O1 | 1.27 s | 44.5 MB | 102 ms | 126.0 MB | 320 KB |
| Axle -O2 | 1.30 s | 44.5 MB | 123 ms | 129.2 MB | 321 KB |
| Rust -O3 | 1.39 s | 44.5 MB | 143 ms | 154.3 MB | 3867 KB |
| C++ -O1 | 1.48 s | 44.5 MB | 276 ms | 110.1 MB | 16 KB |
| C++ -O3 | 1.49 s | 44.5 MB | 283 ms | 110.5 MB | 16 KB |
| C++ -O2 | 1.53 s | 44.5 MB | 280 ms | 110.5 MB | 16 KB |
| Rust -O2 | 1.76 s | 44.5 MB | 130 ms | 151.1 MB | 3864 KB |
| Rust -O1 | 2.24 s | 44.5 MB | 126 ms | 150.8 MB | 3865 KB |
| C++ -O0 | 5.06 s | 44.5 MB | 257 ms | 106.5 MB | 16 KB |
| Axle -O0 | 6.01 s | 44.5 MB | 86 ms | 127.1 MB | 324 KB |
| Rust -O0 | 13.75 s | 44.5 MB | 102 ms | 152.4 MB | 3878 KB |
// Cook-Torrance GGX BRDF evaluated over many (N, V, L, roughness) samples:
// GGX normal distribution, Smith-Schlick geometry, Schlick Fresnel, plus a
// Lambert diffuse term, all f32.
//
// 4096 hashed unit-vector triples are built once (rejection sampling the
// ball, then one sqrt and one division each); the timed loop walks them
// again and again, pairing every triple with one of sixteen roughness
// values. The half vector costs a sqrt and a division per sample; the BRDF
// itself two more divisions, each guarded.
//
// Stresses: dense f32 arithmetic with several divides, small-array loads
// (bounds checks), early-out branches on facing tests.
//
// One line is printed: the samples that faced the light, then the sum of the
// quantised BRDF x cosine (x 256, clamped at 64 before quantising,
// truncated).
//
// Equivalent to ggx_brdf.{c,cpp,rs} — same vectors, same formulas, same
// printed line.
const SETS : i32 = 4096;
const N_EVAL : u32 = 250000000;
const INV24 : f32 = 5.9604644775390625e-8;
const PI : f32 = 3.141592653589793;
const F0 : f32 = 0.04;
const EPS : f32 = 1.0e-6;
struct V3 {
x : f32;
y : f32;
z : f32;
}
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
// A hashed unit vector: the first of 16 rejection attempts that lands in the
// ball, normalised; (0, 0, 1) if none does.
fn unitVector(base : u32) : V3 {
for (a of 0..16) {
let k : u32 = base + (a as u32) * 4;
let x : f32 = unit(hash32(k + 1)) * (2.0 as f32) - (1.0 as f32);
let y : f32 = unit(hash32(k + 2)) * (2.0 as f32) - (1.0 as f32);
let z : f32 = unit(hash32(k + 3)) * (2.0 as f32) - (1.0 as f32);
let r2 : f32 = x * x + y * y + z * z;
if (r2 > (1.0 as f32) || r2 < (1.0e-4 as f32)) {
continue;
}
let inv : f32 = (1.0 as f32) / r2.sqrt();
return V3 { x: x * inv, y: y * inv, z: z * inv };
}
return V3 { x: 0.0, y: 0.0, z: 1.0 };
}
fn main() : void {
let mut vecs = new f32[36864];
let mut rough = new f32[16];
for (i of 0..SETS) {
for (m of 0..3) {
let v : V3 = unitVector(((i * 3 + m) as u32) * 64);
vecs[i * 9 + m * 3 + 0] = v.x;
vecs[i * 9 + m * 3 + 1] = v.y;
vecs[i * 9 + m * 3 + 2] = v.z;
}
}
for (j of 0..16) {
rough[j] = (0.04 as f32) + (j as f32) * (0.06 as f32);
}
let mut facing : i64 = 0;
let mut sum : i64 = 0;
let mut i : u32 = 0;
while (i < N_EVAL) {
let s : i32 = (i & ((SETS as u32) - 1)) as i32;
let r : f32 = rough[((i >> 12) & 15) as i32];
let nx : f32 = vecs[s * 9 + 0];
let ny : f32 = vecs[s * 9 + 1];
let nz : f32 = vecs[s * 9 + 2];
let vx : f32 = vecs[s * 9 + 3];
let vy : f32 = vecs[s * 9 + 4];
let vz : f32 = vecs[s * 9 + 5];
let lx : f32 = vecs[s * 9 + 6];
let ly : f32 = vecs[s * 9 + 7];
let lz : f32 = vecs[s * 9 + 8];
i = i + 1;
let ndl : f32 = nx * lx + ny * ly + nz * lz;
let ndv : f32 = nx * vx + ny * vy + nz * vz;
if (ndl <= (0.0 as f32) || ndv <= (0.0 as f32)) {
continue;
}
let mut hx : f32 = vx + lx;
let mut hy : f32 = vy + ly;
let mut hz : f32 = vz + lz;
let h2 : f32 = hx * hx + hy * hy + hz * hz;
if (h2 < (1.0e-12 as f32)) {
continue;
}
let hinv : f32 = (1.0 as f32) / h2.sqrt();
hx = hx * hinv;
hy = hy * hinv;
hz = hz * hinv;
let ndh : f32 = nx * hx + ny * hy + nz * hz;
let vdh : f32 = vx * hx + vy * hy + vz * hz;
facing = facing + 1;
let a : f32 = r * r;
let a2 : f32 = a * a;
let dd : f32 = ndh * ndh * (a2 - (1.0 as f32)) + (1.0 as f32);
let d : f32 = a2 / (PI * dd * dd);
let kk : f32 = (r + (1.0 as f32)) * (r + (1.0 as f32)) / (8.0 as f32);
let g1l : f32 = ndl / (ndl * ((1.0 as f32) - kk) + kk);
let g1v : f32 = ndv / (ndv * ((1.0 as f32) - kk) + kk);
let g : f32 = g1l * g1v;
let m : f32 = (1.0 as f32) - vdh;
let m2 : f32 = m * m;
let fr : f32 = F0 + ((1.0 as f32) - F0) * (m2 * m2 * m);
let denom : f32 = (4.0 as f32) * ndl * ndv;
let spec : f32 = denom > EPS ? d * g * fr / denom : (0.0 as f32);
let diffuse : f32 = ((1.0 as f32) - fr) * ((1.0 as f32) / PI);
let lo : f32 = (diffuse + spec) * ndl;
let clamped : f32 = lo < (64.0 as f32) ? lo : (64.0 as f32);
sum = sum + ((clamped * (256.0 as f32)) as i64);
}
println("${facing} ${sum}");
}24M ray probes of a unit grid, each returning an 18-field 72-byte record by value that is then stored whole into a field of long-lived state and read back - aggregate return ABI, record copy, store-to-load forwarding, a mutating method on a class.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O3 | 174 ms | 45.7 MB | 135 ms | 126.2 MB | 318 KB |
| C++ -O3 | 576 ms | 45.7 MB | 277 ms | 110.1 MB | 16 KB |
| C++ -O1 | 598 ms | 45.7 MB | 273 ms | 109.8 MB | 16 KB |
| C++ -O2 | 598 ms | 45.7 MB | 277 ms | 110.1 MB | 16 KB |
| Axle -O1 | 625 ms | 45.7 MB | 109 ms | 126.1 MB | 316 KB |
| Axle -O2 | 630 ms | 45.7 MB | 121 ms | 126.1 MB | 316 KB |
| Rust -O3 | 631 ms | 45.7 MB | 117 ms | 147.3 MB | 3864 KB |
| Rust -O2 | 634 ms | 45.7 MB | 118 ms | 150.2 MB | 3864 KB |
| Rust -O1 | 640 ms | 45.7 MB | 114 ms | 149.2 MB | 3864 KB |
| C++ -O0 | 1.45 s | 45.7 MB | 258 ms | 105.9 MB | 16 KB |
| Rust -O0 | 1.57 s | 45.7 MB | 99 ms | 149.9 MB | 3871 KB |
| Axle -O0 | 2.07 s | 45.7 MB | 89 ms | 126.2 MB | 321 KB |
// A hot function that returns a large record by value. `Hit` has 18 fields (a
// flag, the distance, three cell coordinates, a face, a 3-float normal, four
// ints, a float, an int and a 3-float vector: 72 bytes). `probe` shoots a ray
// at an axis-aligned unit grid: three guarded reciprocals, the nearest of the
// three plane crossings, and every field of the record filled from it. The
// caller `plain` stores the whole record into the `last` slot of the tracer
// (a field of long-lived state, as a worker does with the hit it just made)
// and the loop then accumulates from four of its fields. Neither function is
// marked inline or noinline. Each hashed ray is probed twice (the ray and its
// coordinate-rotated twin), so `plain` has two call sites and a compiler cannot just
// fold the one call into the loop.
//
// Stresses: the aggregate return convention, the copy of a 72-byte record into
// a field, store-to-load forwarding of the fields read back, a mutating method
// on a class (three cell stores and a counter per call).
//
// One line is printed: hits, sum of hit distances (x 256, truncated), sum of
// the x coordinates of the hits, sum of their faces, sum of their colours.
//
// Equivalent to hit_record.{c,cpp,rs} - same rays, same record, same printed
// line.
const N_RAYS : u32 = 12000000;
const INV24 : f32 = 5.9604644775390625e-8;
const NEVER : f32 = 1.0e30;
const PARALLEL : f32 = 1.0e-6;
const T_MAX : f32 = 0.75;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn ifloor(v : f32) : i32 {
let i : i32 = v as i32;
if ((i as f32) > v) {
return i - 1;
}
return i;
}
struct Vec3 {
x : f32;
y : f32;
z : f32;
}
struct Hit {
hit : bool;
t : f32;
x : i32;
y : i32;
z : i32;
face : i32;
nx : f32;
ny : f32;
nz : f32;
state : i32;
kind : i32;
color : i32;
fluid : i32;
top : f32;
emit : i32;
tr : Vec3;
}
class Tracer {
pub ax : i32;
pub ay : i32;
pub az : i32;
pub cx : i32;
pub cy : i32;
pub cz : i32;
pub calls : i64;
pub last : Hit;
constructor() {
self.ax = 3;
self.ay = -5;
self.az = 11;
self.cx = 0;
self.cy = 0;
self.cz = 0;
self.calls = 0;
self.last = Hit {
hit: false,
t: 0.0,
x: 0,
y: 0,
z: 0,
face: 1,
nx: 0.0,
ny: 1.0,
nz: 0.0,
state: 0,
kind: 0,
color: 0,
fluid: 0,
top: 1.0,
emit: 0,
tr: Vec3 { x: 1.0, y: 1.0, z: 1.0 },
};
}
// The first plane crossing of the ray from (ox, oy, oz) along (dx, dy, dz)
// through the unit grid anchored at (ax, ay, az), as a record.
pub fn probe(mut self, ox : f32, oy : f32, oz : f32, dx : f32, dy : f32, dz : f32) : Hit {
let ax : i32 = self.ax;
let ay : i32 = self.ay;
let az : i32 = self.az;
let cx : i32 = ax + ifloor(ox);
let cy : i32 = ay + ifloor(oy);
let cz : i32 = az + ifloor(oz);
let mut stepX : i32 = 1;
let mut stepY : i32 = 1;
let mut stepZ : i32 = 1;
let mut tdx : f32 = NEVER;
let mut tdy : f32 = NEVER;
let mut tdz : f32 = NEVER;
let mut tmx : f32 = NEVER;
let mut tmy : f32 = NEVER;
let mut tmz : f32 = NEVER;
if (dx > PARALLEL) {
tdx = (1.0 as f32) / dx;
tmx = (((cx + 1 - ax) as f32) - ox) * tdx;
} else if (dx < -PARALLEL) {
stepX = -1;
tdx = (-1.0 as f32) / dx;
tmx = (ox - ((cx - ax) as f32)) * tdx;
}
if (dy > PARALLEL) {
tdy = (1.0 as f32) / dy;
tmy = (((cy + 1 - ay) as f32) - oy) * tdy;
} else if (dy < -PARALLEL) {
stepY = -1;
tdy = (-1.0 as f32) / dy;
tmy = (oy - ((cy - ay) as f32)) * tdy;
}
if (dz > PARALLEL) {
tdz = (1.0 as f32) / dz;
tmz = (((cz + 1 - az) as f32) - oz) * tdz;
} else if (dz < -PARALLEL) {
stepZ = -1;
tdz = (-1.0 as f32) / dz;
tmz = (oz - ((cz - az) as f32)) * tdz;
}
let mut t : f32 = tmx;
let mut x : i32 = cx;
let mut y : i32 = cy;
let mut z : i32 = cz;
let mut face : i32 = 0;
let mut nx : f32 = 0.0;
let mut ny : f32 = 0.0;
let mut nz : f32 = 0.0;
if (tmx <= tmy && tmx <= tmz) {
x = cx + stepX;
face = stepX > 0 ? 5 : 4;
nx = stepX > 0 ? (-1.0 as f32) : (1.0 as f32);
} else if (tmy <= tmz) {
t = tmy;
y = cy + stepY;
face = stepY > 0 ? 0 : 1;
ny = stepY > 0 ? (-1.0 as f32) : (1.0 as f32);
} else {
t = tmz;
z = cz + stepZ;
face = stepZ > 0 ? 3 : 2;
nz = stepZ > 0 ? (-1.0 as f32) : (1.0 as f32);
}
let state : i32 = (x * 31 + y * 17 + z * 7) & 1023;
let kind : i32 = state & 7;
self.cx = cx;
self.cy = cy;
self.cz = cz;
self.calls = self.calls + 1;
return Hit {
hit: t <= T_MAX,
t: t,
x: x,
y: y,
z: z,
face: face,
nx: nx,
ny: ny,
nz: nz,
state: state,
kind: kind,
color: state * 66051 + 255,
fluid: (state >> 3) & 1,
top: (1.0 as f32) - t * (0.125 as f32),
emit: (state >> 6) & 15,
tr: Vec3 {
x: (1.0 as f32) - t * (0.05 as f32),
y: (1.0 as f32) - t * (0.1 as f32),
z: (1.0 as f32) - t * (0.2 as f32),
},
};
}
// Probes the ray, keeps the whole record in `last` and answers whether it hit.
pub fn plain(mut self, ox : f32, oy : f32, oz : f32, dx : f32, dy : f32, dz : f32) : bool {
let res : Hit = self.probe(ox, oy, oz, dx, dy, dz);
self.last = res;
return res.hit;
}
}
fn main() : void {
let tr : Tracer = new Tracer();
let mut hits : i64 = 0;
let mut sumT : i64 = 0;
let mut sumX : i64 = 0;
let mut sumFace : i64 = 0;
let mut sumColor : i64 = 0;
let mut r : u32 = 0;
while (r < N_RAYS) {
let b : u32 = r * 8;
let ox : f32 = unit(hash32(b + 1)) * (64.0 as f32);
let oy : f32 = unit(hash32(b + 2)) * (64.0 as f32);
let oz : f32 = unit(hash32(b + 3)) * (64.0 as f32);
let dx : f32 = unit(hash32(b + 4)) * (2.0 as f32) - (1.0 as f32);
let dy : f32 = unit(hash32(b + 5)) * (2.0 as f32) - (1.0 as f32);
let dz : f32 = unit(hash32(b + 6)) * (2.0 as f32) - (1.0 as f32);
if (tr.plain(ox, oy, oz, dx, dy, dz)) {
hits = hits + 1;
sumT = sumT + ((tr.last.t * (256.0 as f32)) as i64);
sumX = sumX + (tr.last.x as i64);
sumFace = sumFace + (tr.last.face as i64);
sumColor = sumColor + (tr.last.color as i64);
}
if (tr.plain(oz, ox, oy, dz, dx, dy)) {
hits = hits + 1;
sumT = sumT + ((tr.last.t * (256.0 as f32)) as i64);
sumX = sumX + (tr.last.x as i64);
sumFace = sumFace + (tr.last.face as i64);
sumColor = sumColor + (tr.last.color as i64);
}
r = r + 1;
}
println("${hits} ${sumT} ${sumX} ${sumFace} ${sumColor}");
}24M iterations of 4x4 f32 matrix multiply chains and point transforms with 64-byte structs passed and returned by value - M = M * Rx * Ry * T, rotations from a rational parameterisation.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O3 | 518 ms | 44.4 MB | 109 ms | 97.9 MB | 16 KB |
| C++ -O2 | 523 ms | 44.4 MB | 110 ms | 97.9 MB | 16 KB |
| Axle -O3 | 732 ms | 44.4 MB | 117 ms | 126.1 MB | 316 KB |
| C++ -O1 | 905 ms | 44.4 MB | 112 ms | 97.2 MB | 16 KB |
| Rust -O3 | 1.22 s | 44.4 MB | 117 ms | 141.6 MB | 3864 KB |
| Axle -O2 | 1.63 s | 44.4 MB | 119 ms | 126.1 MB | 316 KB |
| Axle -O1 | 1.69 s | 44.4 MB | 103 ms | 125.8 MB | 317 KB |
| Rust -O1 | 1.74 s | 44.4 MB | 110 ms | 143.0 MB | 3865 KB |
| Rust -O2 | 1.76 s | 44.4 MB | 114 ms | 144.1 MB | 3865 KB |
| Rust -O0 | 3.38 s | 44.4 MB | 95 ms | 142.5 MB | 3868 KB |
| C++ -O0 | 3.58 s | 44.4 MB | 94 ms | 93.4 MB | 16 KB |
| Axle -O0 | 26.76 s | 44.4 MB | 103 ms | 126.1 MB | 344 KB |
// 4x4 matrix multiply chains and point transforms, f32, structs passed and
// returned by value.
//
// The matrix is sixteen scalar fields (row-major, m<row><col>), the product a
// plain sum of four products per cell in a fixed order. Each iteration builds
// two rotations (cos / sin from the rational parameterisation
// c = (1-t^2)/(1+t^2), s = 2t/(1+t^2); the divisor is never below one) and a
// small translation, folds them into the running matrix M = M * Rx * Ry * T,
// then transforms a hashed point by M.
//
// Stresses: 64-byte struct passing and returning, long dependent f32
// multiply-add chains, a float division per rotation.
//
// One line is printed: the sum of the transformed points' components
// (x 1024, truncated) for x, y and z, then the final matrix trace
// (x 1024, truncated).
//
// Equivalent to mat4_chain.{c,cpp,rs} - same cell order, same sums, same
// printed line.
const N_ITER : u32 = 24000000;
const INV24 : f32 = 5.9604644775390625e-8;
struct V3 {
x : f32;
y : f32;
z : f32;
}
struct M4 {
m00 : f32;
m01 : f32;
m02 : f32;
m03 : f32;
m10 : f32;
m11 : f32;
m12 : f32;
m13 : f32;
m20 : f32;
m21 : f32;
m22 : f32;
m23 : f32;
m30 : f32;
m31 : f32;
m32 : f32;
m33 : f32;
static fn identity() : M4 {
return M4 {
m00: 1.0,
m01: 0.0,
m02: 0.0,
m03: 0.0,
m10: 0.0,
m11: 1.0,
m12: 0.0,
m13: 0.0,
m20: 0.0,
m21: 0.0,
m22: 1.0,
m23: 0.0,
m30: 0.0,
m31: 0.0,
m32: 0.0,
m33: 1.0,
};
}
static fn rotX(c : f32, s : f32) : M4 {
return M4 {
m00: 1.0,
m01: 0.0,
m02: 0.0,
m03: 0.0,
m10: 0.0,
m11: c,
m12: -s,
m13: 0.0,
m20: 0.0,
m21: s,
m22: c,
m23: 0.0,
m30: 0.0,
m31: 0.0,
m32: 0.0,
m33: 1.0,
};
}
static fn rotY(c : f32, s : f32) : M4 {
return M4 {
m00: c,
m01: 0.0,
m02: s,
m03: 0.0,
m10: 0.0,
m11: 1.0,
m12: 0.0,
m13: 0.0,
m20: -s,
m21: 0.0,
m22: c,
m23: 0.0,
m30: 0.0,
m31: 0.0,
m32: 0.0,
m33: 1.0,
};
}
static fn translate(x : f32, y : f32, z : f32) : M4 {
return M4 {
m00: 1.0,
m01: 0.0,
m02: 0.0,
m03: x,
m10: 0.0,
m11: 1.0,
m12: 0.0,
m13: y,
m20: 0.0,
m21: 0.0,
m22: 1.0,
m23: z,
m30: 0.0,
m31: 0.0,
m32: 0.0,
m33: 1.0,
};
}
pub fn mul(self, b : M4) : M4 {
return M4 {
m00: self.m00 * b.m00 + self.m01 * b.m10 + self.m02 * b.m20 + self.m03 * b.m30,
m01: self.m00 * b.m01 + self.m01 * b.m11 + self.m02 * b.m21 + self.m03 * b.m31,
m02: self.m00 * b.m02 + self.m01 * b.m12 + self.m02 * b.m22 + self.m03 * b.m32,
m03: self.m00 * b.m03 + self.m01 * b.m13 + self.m02 * b.m23 + self.m03 * b.m33,
m10: self.m10 * b.m00 + self.m11 * b.m10 + self.m12 * b.m20 + self.m13 * b.m30,
m11: self.m10 * b.m01 + self.m11 * b.m11 + self.m12 * b.m21 + self.m13 * b.m31,
m12: self.m10 * b.m02 + self.m11 * b.m12 + self.m12 * b.m22 + self.m13 * b.m32,
m13: self.m10 * b.m03 + self.m11 * b.m13 + self.m12 * b.m23 + self.m13 * b.m33,
m20: self.m20 * b.m00 + self.m21 * b.m10 + self.m22 * b.m20 + self.m23 * b.m30,
m21: self.m20 * b.m01 + self.m21 * b.m11 + self.m22 * b.m21 + self.m23 * b.m31,
m22: self.m20 * b.m02 + self.m21 * b.m12 + self.m22 * b.m22 + self.m23 * b.m32,
m23: self.m20 * b.m03 + self.m21 * b.m13 + self.m22 * b.m23 + self.m23 * b.m33,
m30: self.m30 * b.m00 + self.m31 * b.m10 + self.m32 * b.m20 + self.m33 * b.m30,
m31: self.m30 * b.m01 + self.m31 * b.m11 + self.m32 * b.m21 + self.m33 * b.m31,
m32: self.m30 * b.m02 + self.m31 * b.m12 + self.m32 * b.m22 + self.m33 * b.m32,
m33: self.m30 * b.m03 + self.m31 * b.m13 + self.m32 * b.m23 + self.m33 * b.m33,
};
}
pub fn point(self, v : V3) : V3 {
return V3 {
x: self.m00 * v.x + self.m01 * v.y + self.m02 * v.z + self.m03,
y: self.m10 * v.x + self.m11 * v.y + self.m12 * v.z + self.m13,
z: self.m20 * v.x + self.m21 * v.y + self.m22 * v.z + self.m23,
};
}
}
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn main() : void {
let mut m : M4 = M4::identity();
let mut sx : i64 = 0;
let mut sy : i64 = 0;
let mut sz : i64 = 0;
let mut i : u32 = 0;
while (i < N_ITER) {
let b : u32 = i * 8;
let t1 : f32 = unit(hash32(b + 1)) * (2.0 as f32) - (1.0 as f32);
let t2 : f32 = unit(hash32(b + 2)) * (2.0 as f32) - (1.0 as f32);
let i1 : f32 = (1.0 as f32) / ((1.0 as f32) + t1 * t1);
let i2 : f32 = (1.0 as f32) / ((1.0 as f32) + t2 * t2);
let rx : M4 = M4::rotX(((1.0 as f32) - t1 * t1) * i1, (2.0 as f32) * t1 * i1);
let ry : M4 = M4::rotY(((1.0 as f32) - t2 * t2) * i2, (2.0 as f32) * t2 * i2);
let tr : M4 = M4::translate(
(unit(hash32(b + 3)) - (0.5 as f32)) * (0.02 as f32),
(unit(hash32(b + 4)) - (0.5 as f32)) * (0.02 as f32),
(unit(hash32(b + 5)) - (0.5 as f32)) * (0.02 as f32)
);
m = m.mul(rx).mul(ry).mul(tr);
let p : V3 = V3 {
x: unit(hash32(b + 6)) * (2.0 as f32) - (1.0 as f32),
y: unit(hash32(b + 7)) * (2.0 as f32) - (1.0 as f32),
z: unit(hash32(b + 8)) * (2.0 as f32) - (1.0 as f32),
};
let q : V3 = m.point(p);
sx = sx + ((q.x * (1024.0 as f32)) as i64);
sy = sy + ((q.y * (1024.0 as f32)) as i64);
sz = sz + ((q.z * (1024.0 as f32)) as i64);
i = i + 1;
}
let trace : f32 = m.m00 + m.m11 + m.m22 + m.m33;
let tq : i64 = (trace * (1024.0 as f32)) as i64;
println("${sx} ${sy} ${sz} ${tq}");
}O(n^2) softened gravity over 1024 f64 bodies for 300 steps - an f64 sqrt and divide per pair, struct-of-arrays loads, a skip branch in the inner loop.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O2 | 803 ms | 45.1 MB | 110 ms | 125.8 MB | 316 KB |
| Axle -O1 | 804 ms | 45.1 MB | 95 ms | 127.4 MB | 316 KB |
| Axle -O3 | 805 ms | 45.1 MB | 110 ms | 125.9 MB | 316 KB |
| C++ -O1 | 829 ms | 45.1 MB | 276 ms | 109.2 MB | 16 KB |
| C++ -O3 | 837 ms | 45.1 MB | 286 ms | 110.4 MB | 16 KB |
| C++ -O2 | 839 ms | 45.1 MB | 284 ms | 110.4 MB | 16 KB |
| Rust -O3 | 1.04 s | 45.1 MB | 135 ms | 149.9 MB | 3865 KB |
| Rust -O2 | 1.04 s | 45.1 MB | 136 ms | 149.9 MB | 3865 KB |
| Rust -O1 | 1.07 s | 45.1 MB | 152 ms | 148.6 MB | 3868 KB |
| C++ -O0 | 3.15 s | 45.1 MB | 257 ms | 105.4 MB | 16 KB |
| Axle -O0 | 3.37 s | 45.1 MB | 84 ms | 127.2 MB | 320 KB |
| Rust -O0 | 9.08 s | 45.1 MB | 104 ms | 147.9 MB | 3880 KB |
// O(n^2) gravitational N-body, f64, Plummer-softened. 1024 bodies with hashed
// positions, velocities and masses; each step computes every acceleration
// from all other bodies (a sqrt and a division per pair), then kicks the
// velocities and drifts the positions (semi-implicit Euler). Struct-of-arrays
// layout in heap f64 arrays.
//
// Stresses: f64 sqrt / div throughput, the inner loop's array loads (bounds
// checks), a `j != i` skip inside a hot loop.
//
// One line is printed: the sums of the quantised x, y, z positions (x 1000,
// truncated) and the quantised kinetic energy (x 1000, truncated).
//
// Equivalent to nbody_f64.{c,cpp,rs} - same initial state, same pair order,
// same printed line.
const N : i32 = 1024;
const STEPS : i32 = 300;
const INV24 : f64 = 5.9604644775390625e-8;
const DT : f64 = 0.01;
const SOFT2 : f64 = 0.0025;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f64 {
return ((h >> 8) as f64) * INV24;
}
fn main() : void {
let mut x = new f64[1024];
let mut y = new f64[1024];
let mut z = new f64[1024];
let mut vx = new f64[1024];
let mut vy = new f64[1024];
let mut vz = new f64[1024];
let mut m = new f64[1024];
let mut ax = new f64[1024];
let mut ay = new f64[1024];
let mut az = new f64[1024];
for (i of 0..N) {
let b : u32 = (i as u32) * 8;
x[i] = (unit(hash32(b + 1)) * 2.0 - 1.0) * 10.0;
y[i] = (unit(hash32(b + 2)) * 2.0 - 1.0) * 10.0;
z[i] = (unit(hash32(b + 3)) * 2.0 - 1.0) * 10.0;
vx[i] = (unit(hash32(b + 4)) * 2.0 - 1.0) * 0.5;
vy[i] = (unit(hash32(b + 5)) * 2.0 - 1.0) * 0.5;
vz[i] = (unit(hash32(b + 6)) * 2.0 - 1.0) * 0.5;
m[i] = 0.5 + unit(hash32(b + 7));
}
for (step of 0..STEPS) {
for (i of 0..N) {
let mut sx : f64 = 0.0;
let mut sy : f64 = 0.0;
let mut sz : f64 = 0.0;
let xi : f64 = x[i];
let yi : f64 = y[i];
let zi : f64 = z[i];
for (j of 0..N) {
if (j == i) {
continue;
}
let dx : f64 = x[j] - xi;
let dy : f64 = y[j] - yi;
let dz : f64 = z[j] - zi;
let r2 : f64 = dx * dx + dy * dy + dz * dz + SOFT2;
let s : f64 = m[j] / (r2 * r2.sqrt());
sx = sx + dx * s;
sy = sy + dy * s;
sz = sz + dz * s;
}
ax[i] = sx;
ay[i] = sy;
az[i] = sz;
}
for (i of 0..N) {
vx[i] = vx[i] + ax[i] * DT;
vy[i] = vy[i] + ay[i] * DT;
vz[i] = vz[i] + az[i] * DT;
x[i] = x[i] + vx[i] * DT;
y[i] = y[i] + vy[i] * DT;
z[i] = z[i] + vz[i] * DT;
}
}
let mut px : i64 = 0;
let mut py : i64 = 0;
let mut pz : i64 = 0;
let mut ke : f64 = 0.0;
for (i of 0..N) {
px = px + ((x[i] * 1000.0) as i64);
py = py + ((y[i] * 1000.0) as i64);
pz = pz + ((z[i] * 1000.0) as i64);
ke = ke + 0.5 * m[i] * (vx[i] * vx[i] + vy[i] * vy[i] + vz[i] * vz[i]);
}
let kq : i64 = (ke * 1000.0) as i64;
println("${px} ${py} ${pz} ${kq}");
}20000 spheres in a 32^3 box for 150 steps - semi-implicit Euler, wall bounces, a counting-sort uniform grid rebuilt every step and pairwise impulse collisions over 27 neighbour cells.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O2 | 770 ms | 44.3 MB | 306 ms | 110.7 MB | 16 KB |
| C++ -O3 | 771 ms | 44.3 MB | 307 ms | 110.7 MB | 16 KB |
| C++ -O1 | 822 ms | 44.3 MB | 291 ms | 109.7 MB | 16 KB |
| Rust -O2 | 863 ms | 44.3 MB | 173 ms | 152.2 MB | 3866 KB |
| Rust -O3 | 863 ms | 44.3 MB | 175 ms | 153.9 MB | 3866 KB |
| Axle -O3 | 906 ms | 44.3 MB | 349 ms | 126.1 MB | 326 KB |
| Axle -O2 | 907 ms | 44.3 MB | 345 ms | 128.0 MB | 326 KB |
| Axle -O1 | 970 ms | 44.3 MB | 275 ms | 125.2 MB | 323 KB |
| Rust -O1 | 1.10 s | 44.3 MB | 193 ms | 151.4 MB | 3871 KB |
| C++ -O0 | 1.50 s | 44.3 MB | 262 ms | 105.7 MB | 20 KB |
| Axle -O0 | 1.91 s | 44.3 MB | 91 ms | 124.3 MB | 331 KB |
| Rust -O0 | 4.88 s | 44.3 MB | 117 ms | 151.2 MB | 3895 KB |
// A particle integrator with sphere-sphere collisions found through a uniform
// grid. 20000 equal spheres of radius 0.5 fall inside a 32^3 box with
// bouncing walls.
//
// Each step: semi-implicit Euler (velocity first, then position), wall
// clamps with restitution, a counting-sort rebuild of the 32^3 cell grid
// (clear, count, prefix sum, scatter), then a pass over every particle's
// 3 x 3 x 3 cell neighbourhood that resolves each overlapping pair once
// (j > i) with a positional push and an impulse along the contact normal.
// Positions and velocities are flat f32 arrays (x, y, z interleaved).
//
// Stresses: integer array churn (counting sort), gather loads through the
// sorted index list, a sqrt and a division per contact, store-to-load chains
// on particles that touch several neighbours, branchy neighbour loops.
//
// One line is printed: the contact count, then the sums of the quantised
// final positions (x 256, truncated) for x, y, z, then the quantised kinetic
// energy (x 64).
//
// Equivalent to particles_grid.{c,cpp,rs} - same initial state, same pass
// order, same pair order, same printed line.
const N : i32 = 20000;
const STEPS : i32 = 150;
const GRID : i32 = 32;
const CELLS : i32 = 32768;
const INV24 : f32 = 5.9604644775390625e-8;
const RADIUS : f32 = 0.5;
const DIAM : f32 = 1.0;
const DIAM2 : f32 = 1.0;
const BOX : f32 = 32.0;
const DT : f32 = 0.008333333333333333;
const GRAV : f32 = 9.8;
const WALL_E : f32 = 0.6;
const PAIR_E : f32 = 0.5;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn clampi(v : i32, lo : i32, hi : i32) : i32 {
if (v < lo) {
return lo;
}
if (v > hi) {
return hi;
}
return v;
}
fn main() : void {
let mut pos = new f32[60000];
let mut vel = new f32[60000];
let mut cellOf = new i32[20000];
let mut start = new i32[32769];
let mut cursor = new i32[32768];
let mut items = new i32[20000];
for (i of 0..N) {
for (k of 0..3) {
let h : u32 = (i * 6 + k) as u32;
pos[i * 3 + k] = RADIUS + unit(hash32(h)) * (BOX - DIAM);
vel[i * 3 + k] = (unit(hash32(h + 3)) * (2.0 as f32) - (1.0 as f32)) * (3.0 as f32);
}
}
let mut contacts : i64 = 0;
for (step of 0..STEPS) {
// Semi-implicit Euler and wall bounces.
for (i of 0..N) {
vel[i * 3 + 1] = vel[i * 3 + 1] - GRAV * DT;
for (k of 0..3) {
let a : i32 = i * 3 + k;
pos[a] = pos[a] + vel[a] * DT;
if (pos[a] < RADIUS) {
pos[a] = RADIUS;
vel[a] = -vel[a] * WALL_E;
} else if (pos[a] > BOX - RADIUS) {
pos[a] = BOX - RADIUS;
vel[a] = -vel[a] * WALL_E;
}
}
}
// Counting sort of the particles into the cell grid.
for (c of 0..=CELLS) {
start[c] = 0;
}
for (i of 0..N) {
let cx : i32 = clampi(pos[i * 3 + 0] as i32, 0, GRID - 1);
let cy : i32 = clampi(pos[i * 3 + 1] as i32, 0, GRID - 1);
let cz : i32 = clampi(pos[i * 3 + 2] as i32, 0, GRID - 1);
let c : i32 = (cz << 10) | (cy << 5) | cx;
cellOf[i] = c;
start[c + 1] = start[c + 1] + 1;
}
for (c of 0..CELLS) {
start[c + 1] = start[c + 1] + start[c];
cursor[c] = start[c];
}
for (i of 0..N) {
let c : i32 = cellOf[i];
items[cursor[c]] = i;
cursor[c] = cursor[c] + 1;
}
// Pair resolution over the 3 x 3 x 3 neighbourhood.
for (i of 0..N) {
let c : i32 = cellOf[i];
let cz : i32 = c >> 10;
let cy : i32 = (c >> 5) & 31;
let cx : i32 = c & 31;
for (oz of -1..=1) {
let nz : i32 = cz + oz;
if (nz < 0 || nz >= GRID) {
continue;
}
for (oy of -1..=1) {
let ny : i32 = cy + oy;
if (ny < 0 || ny >= GRID) {
continue;
}
for (ox of -1..=1) {
let nx : i32 = cx + ox;
if (nx < 0 || nx >= GRID) {
continue;
}
let nc : i32 = (nz << 10) | (ny << 5) | nx;
for (s of start[nc]..start[nc + 1]) {
let j : i32 = items[s];
if (j <= i) {
continue;
}
let dx : f32 = pos[j * 3 + 0] - pos[i * 3 + 0];
let dy : f32 = pos[j * 3 + 1] - pos[i * 3 + 1];
let dz : f32 = pos[j * 3 + 2] - pos[i * 3 + 2];
let d2 : f32 = dx * dx + dy * dy + dz * dz;
if (d2 >= DIAM2 || d2 < (1.0e-12 as f32)) {
continue;
}
let dist : f32 = d2.sqrt();
let inv : f32 = (1.0 as f32) / dist;
let ux : f32 = dx * inv;
let uy : f32 = dy * inv;
let uz : f32 = dz * inv;
let push : f32 = (DIAM - dist) * (0.5 as f32);
pos[i * 3 + 0] = pos[i * 3 + 0] - ux * push;
pos[i * 3 + 1] = pos[i * 3 + 1] - uy * push;
pos[i * 3 + 2] = pos[i * 3 + 2] - uz * push;
pos[j * 3 + 0] = pos[j * 3 + 0] + ux * push;
pos[j * 3 + 1] = pos[j * 3 + 1] + uy * push;
pos[j * 3 + 2] = pos[j * 3 + 2] + uz * push;
let vn : f32 = (vel[j * 3 + 0] - vel[i * 3 + 0]) * ux
+ (vel[j * 3 + 1] - vel[i * 3 + 1]) * uy
+ (vel[j * 3 + 2] - vel[i * 3 + 2]) * uz;
if (vn < (0.0 as f32)) {
let imp : f32 = -((1.0 as f32) + PAIR_E) * vn * (0.5 as f32);
vel[i * 3 + 0] = vel[i * 3 + 0] - imp * ux;
vel[i * 3 + 1] = vel[i * 3 + 1] - imp * uy;
vel[i * 3 + 2] = vel[i * 3 + 2] - imp * uz;
vel[j * 3 + 0] = vel[j * 3 + 0] + imp * ux;
vel[j * 3 + 1] = vel[j * 3 + 1] + imp * uy;
vel[j * 3 + 2] = vel[j * 3 + 2] + imp * uz;
}
contacts = contacts + 1;
}
}
}
}
}
}
let mut sx : i64 = 0;
let mut sy : i64 = 0;
let mut sz : i64 = 0;
let mut energy : f32 = 0.0;
for (i of 0..N) {
sx = sx + ((pos[i * 3 + 0] * (256.0 as f32)) as i64);
sy = sy + ((pos[i * 3 + 1] * (256.0 as f32)) as i64);
sz = sz + ((pos[i * 3 + 2] * (256.0 as f32)) as i64);
energy = energy
+ (vel[i * 3 + 0] * vel[i * 3 + 0] + vel[i * 3 + 1] * vel[i * 3 + 1]
+ vel[i * 3 + 2] * vel[i * 3 + 2]);
}
let eq : i64 = (energy * (64.0 as f32)) as i64;
println("${contacts} ${sx} ${sy} ${sz} ${eq}");
}300M iterations of a PCG32-driven Monte Carlo - pi by hit count, an integral with a division, a cosine-hemisphere moment with a sqrt, and a multiply-shift histogram: integer state update mixed with f64 maths.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O1 | 1.27 s | 45.2 MB | 88 ms | 127.3 MB | 315 KB |
| Axle -O3 | 1.28 s | 45.2 MB | 93 ms | 124.1 MB | 315 KB |
| Axle -O2 | 1.29 s | 45.2 MB | 94 ms | 128.0 MB | 315 KB |
| Rust -O2 | 1.30 s | 45.2 MB | 112 ms | 151.5 MB | 3863 KB |
| Rust -O3 | 1.30 s | 45.2 MB | 112 ms | 153.8 MB | 3863 KB |
| C++ -O3 | 1.38 s | 45.2 MB | 262 ms | 109.8 MB | 16 KB |
| C++ -O1 | 1.38 s | 45.2 MB | 262 ms | 109.4 MB | 16 KB |
| C++ -O2 | 1.38 s | 45.2 MB | 261 ms | 109.8 MB | 16 KB |
| Rust -O1 | 1.99 s | 45.2 MB | 107 ms | 147.7 MB | 3863 KB |
| Axle -O0 | 6.14 s | 45.2 MB | 81 ms | 126.1 MB | 316 KB |
| C++ -O0 | 6.46 s | 45.2 MB | 255 ms | 105.2 MB | 16 KB |
| Rust -O0 | 6.97 s | 45.2 MB | 95 ms | 148.7 MB | 3871 KB |
// PCG32 (XSH-RR) driving four Monte-Carlo estimators at once, an
// integer + f64 mix: pi by the quarter-circle hit count, the integral of
// 1/(1+x^2) over [0,1] (a division per sample), the mean of z^3 for a cosine
// hemisphere sample z = sqrt(1-v) (a sqrt per sample), and a 6-bucket
// histogram of a bounded-integer draw (multiply-shift, no modulo). Three
// generator calls per iteration.
//
// Stresses: 64-bit multiply-add state update, 32-bit rotate by a variable
// count, u32 -> f64 conversion, f64 sqrt and division, a data-dependent
// counter, a tiny array update (a bounds check).
//
// One line is printed: circle hits, the quantised 1/(1+x^2) sum and z^3 sum
// (x 1000, truncated), then the six histogram counts.
//
// Equivalent to pcg_montecarlo.{c,cpp,rs} - same generator, same estimators,
// same printed line.
const N_ITER : i64 = 300000000;
const PCG_MULT : u64 = 6364136223846793005;
const PCG_INC : u64 = 1442695040888963407;
const INV32 : f64 = 2.3283064365386963e-10;
class Pcg {
state : u64;
constructor(seed : u64) {
self.state = seed;
}
pub fn next(mut self) : u32 {
let old : u64 = self.state;
self.state = old * PCG_MULT + PCG_INC;
let xs : u32 = ((((old >> 18) ^ old) >> 27) & 0xFFFFFFFF) as u32;
let rot : u32 = (old >> 59) as u32;
return (xs >> rot) | (xs << ((0 - rot) & 31));
}
pub fn unit(mut self) : f64 {
return (self.next() as f64) * INV32;
}
}
fn main() : void {
let g : Pcg = new Pcg(0x853c49e6748fea9b);
let mut hist = new i64[6];
let mut inside : i64 = 0;
let mut sumF : f64 = 0.0;
let mut sumZ3 : f64 = 0.0;
let mut i : i64 = 0;
while (i < N_ITER) {
let u : f64 = g.unit();
let v : f64 = g.unit();
if (u * u + v * v < 1.0) {
inside = inside + 1;
}
sumF = sumF + 1.0 / (1.0 + u * u);
let z : f64 = (1.0 - v).sqrt();
sumZ3 = sumZ3 + z * z * z;
let d : u32 = g.next();
let bucket : i32 = (((d as u64) * 6) >> 32) as i32;
hist[bucket] = hist[bucket] + 1;
i = i + 1;
}
let qf : i64 = (sumF * 1000.0) as i64;
let qz : i64 = (sumZ3 * 1000.0) as i64;
println("${inside} ${qf} ${qz} ${hist[0]} ${hist[1]} ${hist[2]} ${hist[3]} ${hist[4]} ${hist[5]}");
}40M trilinear lookups in a 48^3 RGB f32 light-probe grid - 24 indexed gathers and 21 lerps per sample.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O1 | 1.13 s | 45.4 MB | 110 ms | 96.8 MB | 16 KB |
| Axle -O2 | 1.53 s | 45.4 MB | 112 ms | 125.7 MB | 321 KB |
| Axle -O3 | 1.55 s | 45.4 MB | 112 ms | 127.7 MB | 321 KB |
| Rust -O3 | 1.72 s | 45.4 MB | 122 ms | 147.6 MB | 3864 KB |
| Rust -O2 | 1.72 s | 45.4 MB | 120 ms | 150.9 MB | 3864 KB |
| Axle -O1 | 1.76 s | 45.4 MB | 98 ms | 128.2 MB | 321 KB |
| Rust -O1 | 1.79 s | 45.4 MB | 114 ms | 145.5 MB | 3864 KB |
| C++ -O3 | 2.10 s | 45.4 MB | 117 ms | 98.0 MB | 16 KB |
| C++ -O2 | 2.11 s | 45.4 MB | 117 ms | 98.0 MB | 16 KB |
| C++ -O0 | 4.33 s | 45.4 MB | 94 ms | 92.2 MB | 16 KB |
| Axle -O0 | 4.97 s | 45.4 MB | 84 ms | 125.9 MB | 323 KB |
| Rust -O0 | 6.62 s | 45.4 MB | 98 ms | 146.6 MB | 3874 KB |
// Trilinear light-probe lookups in a 48 x 48 x 48 grid of RGB f32 probes
// (1.3 MB of interleaved floats). Each sample is a hashed position inside
// the grid; the lookup floors it to a cell (clamped to the last cell), reads
// the eight corner probes for all three channels, and blends them with seven
// lerps per channel.
//
// Stresses: indexed gathers from a 3D array (24 loads per sample, bounds
// checks), index arithmetic in i32, f32 lerp chains.
//
// One line is printed: the sums of the quantised R, G and B results
// (x 1024, truncated).
//
// Equivalent to probe_trilinear.{c,cpp,rs} - same probes, same samples, same
// blend order, same printed line.
const D : i32 = 48;
const N_SAMPLES : u32 = 40000000;
const INV24 : f32 = 5.9604644775390625e-8;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn lerp(a : f32, b : f32, t : f32) : f32 {
return a + (b - a) * t;
}
fn cell(p : f32) : i32 {
let i : i32 = p as i32;
return i > D - 2 ? D - 2 : i;
}
fn trilinear(probes : f32[], i000 : i32, tx : f32, ty : f32, tz : f32, c : i32) : f32 {
let sx : i32 = 3;
let sy : i32 = D * 3;
let sz : i32 = D * D * 3;
let c00 : f32 = lerp(probes[i000 + c], probes[i000 + sx + c], tx);
let c10 : f32 = lerp(probes[i000 + sy + c], probes[i000 + sy + sx + c], tx);
let c01 : f32 = lerp(probes[i000 + sz + c], probes[i000 + sz + sx + c], tx);
let c11 : f32 = lerp(probes[i000 + sz + sy + c], probes[i000 + sz + sy + sx + c], tx);
let c0 : f32 = lerp(c00, c10, ty);
let c1 : f32 = lerp(c01, c11, ty);
return lerp(c0, c1, tz);
}
fn main() : void {
let mut probes = new f32[331776];
for (i of 0..331776) {
probes[i] = unit(hash32((i as u32) + 0x5b000000)) * (4.0 as f32);
}
let mut sr : i64 = 0;
let mut sg : i64 = 0;
let mut sb : i64 = 0;
let mut i : u32 = 0;
while (i < N_SAMPLES) {
let k : u32 = i * 4;
let x : f32 = unit(hash32(k + 1)) * (47.0 as f32);
let y : f32 = unit(hash32(k + 2)) * (47.0 as f32);
let z : f32 = unit(hash32(k + 3)) * (47.0 as f32);
let ix : i32 = cell(x);
let iy : i32 = cell(y);
let iz : i32 = cell(z);
let tx : f32 = x - (ix as f32);
let ty : f32 = y - (iy as f32);
let tz : f32 = z - (iz as f32);
let i000 : i32 = ((iz * D + iy) * D + ix) * 3;
sr = sr + ((trilinear(probes, i000, tx, ty, tz, 0) * (1024.0 as f32)) as i64);
sg = sg + ((trilinear(probes, i000, tx, ty, tz, 1) * (1024.0 as f32)) as i64);
sb = sb + ((trilinear(probes, i000, tx, ty, tz, 2) * (1024.0 as f32)) as i64);
i = i + 1;
}
println("${sr} ${sg} ${sb}");
}12M quaternion slerp and nlerp chain steps in f32 - struct by value, sqrt and guarded division per renormalisation, polynomial atan2 and Taylor sin for the slerp weights, a data-dependent slerp/nlerp branch.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O3 | 797 ms | 44.5 MB | 286 ms | 110.6 MB | 16 KB |
| C++ -O2 | 799 ms | 44.5 MB | 289 ms | 110.7 MB | 16 KB |
| C++ -O1 | 823 ms | 44.5 MB | 285 ms | 110.4 MB | 16 KB |
| Axle -O2 | 833 ms | 44.5 MB | 117 ms | 127.8 MB | 316 KB |
| Axle -O3 | 833 ms | 44.5 MB | 117 ms | 125.8 MB | 316 KB |
| Axle -O1 | 855 ms | 44.5 MB | 102 ms | 126.0 MB | 316 KB |
| Rust -O3 | 988 ms | 44.5 MB | 118 ms | 141.9 MB | 3864 KB |
| Rust -O1 | 1.02 s | 44.5 MB | 114 ms | 142.7 MB | 3864 KB |
| Rust -O2 | 1.02 s | 44.5 MB | 116 ms | 143.7 MB | 3864 KB |
| Rust -O0 | 2.12 s | 44.5 MB | 96 ms | 142.4 MB | 3869 KB |
| Axle -O0 | 2.19 s | 44.5 MB | 90 ms | 125.8 MB | 319 KB |
| C++ -O0 | 3.42 s | 44.5 MB | 266 ms | 106.6 MB | 16 KB |
// Quaternion slerp and nlerp chains with renormalisation, f32.
//
// One chain folds a hashed unit quaternion into the running orientation with
// slerp (shortest arc; the half-angle comes from a polynomial atan2 of
// sqrt(1 - d^2) and d, sin() from a degree-13 Taylor polynomial, both
// written out here; near-parallel pairs fall back to nlerp). A second chain
// does the same with nlerp alone. Every renormalisation is a sqrt and a
// guarded divide.
//
// Stresses: a 16-byte struct passed and returned by value, sqrt and division
// latency, a data-dependent branch between slerp and nlerp, polynomial
// evaluation.
//
// One line is printed: sums of the quantised x, y, z components (x 1024,
// truncated) of the slerp chain, then of the x component of the nlerp chain.
//
// Equivalent to quat_slerp.{c,cpp,rs} — same polynomials, same fallbacks,
// same printed line.
const N_ITER : u32 = 12000000;
const INV24 : f32 = 5.9604644775390625e-8;
const S1 : f32 = -0.16666666666666666;
const S2 : f32 = 0.008333333333333333;
const S3 : f32 = -1.984126984126984e-4;
const S4 : f32 = 2.7557319223985893e-6;
const S5 : f32 = -2.505210838544172e-8;
const S6 : f32 = 1.6059043836821613e-10;
const ATAN0 : f32 = 8.05374449538e-2;
const ATAN1 : f32 = -1.38776856032e-1;
const ATAN2 : f32 = 1.99777106478e-1;
const ATAN3 : f32 = -3.33329491539e-1;
const TAN_3PI_8 : f32 = 2.414213562373095;
const TAN_PI_8 : f32 = 0.4142135623730950;
const PIO2 : f32 = 1.5707963267948966;
const PIO4 : f32 = 0.7853981633974483;
const PI : f32 = 3.141592653589793;
struct Quat {
x : f32;
y : f32;
z : f32;
w : f32;
pub fn dot(self, b : Quat) : f32 {
return self.x * b.x + self.y * b.y + self.z * b.z + self.w * b.w;
}
pub fn scale(self, s : f32) : Quat {
return Quat { x: self.x * s, y: self.y * s, z: self.z * s, w: self.w * s };
}
pub fn plus(self, b : Quat) : Quat {
return Quat { x: self.x + b.x, y: self.y + b.y, z: self.z + b.z, w: self.w + b.w };
}
pub fn normalize(self) : Quat {
// Written out: `self.dot(self)` inside a struct method fails LLVM
// verification in Axle 0.20 (a by-value `self` argument is passed as a pointer).
let len2 : f32 = self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w;
if (len2 < (1.0e-12 as f32)) {
return Quat { x: 0.0, y: 0.0, z: 0.0, w: 1.0 };
}
let inv : f32 = (1.0 as f32) / len2.sqrt();
return self.scale(inv);
}
pub fn nlerp(self, b : Quat, t : f32) : Quat {
let d : f32 = self.dot(b);
let bb : Quat = d < (0.0 as f32) ? b.scale(-1.0) : b;
return self.scale((1.0 as f32) - t).plus(bb.scale(t)).normalize();
}
pub fn slerp(self, b : Quat, t : f32) : Quat {
let mut d : f32 = self.dot(b);
let mut bb : Quat = b;
if (d < (0.0 as f32)) {
bb = b.scale(-1.0);
d = -d;
}
if (d > (0.9995 as f32)) {
return self.nlerp(bb, t);
}
let s2 : f32 = (1.0 as f32) - d * d;
let sn : f32 = s2 > (0.0 as f32) ? s2.sqrt() : (0.0 as f32);
let theta : f32 = atan2Poly(sn, d);
let s : f32 = sinTaylor(theta);
if (s < (1.0e-6 as f32)) {
return self.nlerp(bb, t);
}
let inv : f32 = (1.0 as f32) / s;
let w1 : f32 = sinTaylor(((1.0 as f32) - t) * theta) * inv;
let w2 : f32 = sinTaylor(t * theta) * inv;
return self.scale(w1).plus(bb.scale(w2)).normalize();
}
}
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn sinTaylor(x : f32) : f32 {
let z : f32 = x * x;
return x * ((1.0 as f32) + z * (S1 + z * (S2 + z * (S3 + z * (S4 + z * (S5 + z * S6))))));
}
fn atanPoly(x : f32) : f32 {
let neg : bool = x < (0.0 as f32);
let mut a : f32 = neg ? -x : x;
let mut base : f32 = 0.0;
if (a > TAN_3PI_8) {
base = PIO2;
a = -((1.0 as f32) / a);
} else if (a > TAN_PI_8) {
base = PIO4;
a = (a - (1.0 as f32)) / (a + (1.0 as f32));
}
let z : f32 = a * a;
let p : f32 = ((((ATAN0 * z + ATAN1) * z + ATAN2) * z + ATAN3) * z) * a + a;
let r : f32 = base + p;
return neg ? -r : r;
}
fn atan2Poly(y : f32, x : f32) : f32 {
if (x == (0.0 as f32)) {
if (y > (0.0 as f32)) { return PIO2; }
if (y < (0.0 as f32)) { return -PIO2; }
return 0.0;
}
let a : f32 = atanPoly(y / x);
if (x < (0.0 as f32)) {
if (y >= (0.0 as f32)) { return a + PI; }
return a - PI;
}
return a;
}
fn main() : void {
let mut qs : Quat = Quat { x: 0.0, y: 0.0, z: 0.0, w: 1.0 };
let mut qn : Quat = Quat { x: 0.0, y: 0.0, z: 0.0, w: 1.0 };
let mut sx : i64 = 0;
let mut sy : i64 = 0;
let mut sz : i64 = 0;
let mut nx : i64 = 0;
let mut i : u32 = 0;
while (i < N_ITER) {
let b : u32 = i * 8;
let r : Quat = Quat {
x: unit(hash32(b + 1)) * (2.0 as f32) - (1.0 as f32),
y: unit(hash32(b + 2)) * (2.0 as f32) - (1.0 as f32),
z: unit(hash32(b + 3)) * (2.0 as f32) - (1.0 as f32),
w: unit(hash32(b + 4)) * (2.0 as f32) - (1.0 as f32),
}.normalize();
let t : f32 = unit(hash32(b + 5));
qs = qs.slerp(r, t);
qn = qn.nlerp(r, t);
sx = sx + ((qs.x * (1024.0 as f32)) as i64);
sy = sy + ((qs.y * (1024.0 as f32)) as i64);
sz = sz + ((qs.z * (1024.0 as f32)) as i64);
nx = nx + ((qn.x * (1024.0 as f32)) as i64);
i = i + 1;
}
println("${sx} ${sy} ${sz} ${nx}");
}Order-2 spherical-harmonic projection of an analytic sky (7M rejection-sampled directions, f64 accumulators) and diffuse irradiance for 16M normals - small-array loads and stores in the hot loop.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O1 | 875 ms | 44.8 MB | 277 ms | 109.8 MB | 16 KB |
| C++ -O2 | 940 ms | 44.8 MB | 294 ms | 110.7 MB | 16 KB |
| C++ -O3 | 942 ms | 44.8 MB | 291 ms | 110.7 MB | 16 KB |
| Axle -O1 | 956 ms | 44.8 MB | 115 ms | 127.8 MB | 316 KB |
| Axle -O2 | 1.00 s | 44.8 MB | 137 ms | 125.9 MB | 317 KB |
| Axle -O3 | 1.00 s | 44.8 MB | 138 ms | 126.1 MB | 317 KB |
| Rust -O2 | 1.05 s | 44.8 MB | 143 ms | 154.0 MB | 3865 KB |
| Rust -O3 | 1.06 s | 44.8 MB | 149 ms | 153.8 MB | 3865 KB |
| Rust -O1 | 1.19 s | 44.8 MB | 149 ms | 149.3 MB | 3867 KB |
| C++ -O0 | 2.57 s | 44.8 MB | 259 ms | 106.4 MB | 16 KB |
| Axle -O0 | 3.30 s | 44.8 MB | 87 ms | 125.6 MB | 320 KB |
| Rust -O0 | 8.18 s | 44.8 MB | 106 ms | 149.7 MB | 3882 KB |
// Order-2 spherical harmonics (9 coefficients x RGB): projection of an
// analytic sky onto SH, then diffuse irradiance evaluation for many normals
// (Ramamoorthi & Hanrahan).
//
// Directions come from rejection sampling the unit ball with hashed
// coordinates and one sqrt and one division to normalise. The 27 projection
// accumulators are f64 in a heap array; the basis lives in a small f32
// scratch array that is written and read back every sample.
//
// Stresses: small-array loads and stores in the hot loop (bounds checks), an
// f32 -> f64 widening, a rejection loop with a data-dependent branch, a sqrt
// and a division per direction.
//
// One line is printed: the accepted projection samples, then the sums of the
// quantised irradiance (x 1024, truncated, clamped at zero) for R, G and B.
//
// Equivalent to sh9_irradiance.{c,cpp,rs} — same sampling, same basis, same
// accumulation order, same printed line.
const N_PROJECT : i64 = 7000000;
const N_EVAL : i64 = 16000000;
const EVAL_SEED : u32 = 0x10000000;
const INV24 : f32 = 5.9604644775390625e-8;
const Y00 : f32 = 0.282095;
const Y1 : f32 = 0.488603;
const Y2A : f32 = 1.092548;
const Y2B : f32 = 0.315392;
const Y2C : f32 = 0.546274;
struct Rgb {
r : f32;
g : f32;
b : f32;
}
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn shBasis(mut b : f32[], x : f32, y : f32, z : f32) : void {
b[0] = Y00;
b[1] = Y1 * y;
b[2] = Y1 * z;
b[3] = Y1 * x;
b[4] = Y2A * (x * y);
b[5] = Y2A * (y * z);
b[6] = Y2B * ((3.0 as f32) * z * z - (1.0 as f32));
b[7] = Y2A * (x * z);
b[8] = Y2C * (x * x - y * y);
}
// Radiance of the analytic sky seen along the unit vector (x, y, z).
fn sky(x : f32, y : f32, z : f32) : Rgb {
let t : f32 = (0.5 as f32) * (y + (1.0 as f32));
let sd : f32 = x * (0.48 as f32) + y * (0.64 as f32) + z * (0.6 as f32);
let s : f32 = sd > (0.0 as f32) ? sd : (0.0 as f32);
let s2 : f32 = s * s;
let s4 : f32 = s2 * s2;
let s8 : f32 = s4 * s4;
return Rgb {
r: ((1.0 as f32) - t) * (0.9 as f32) + t * (0.25 as f32) + s8 * (6.0 as f32),
g: ((1.0 as f32) - t) * (0.8 as f32) + t * (0.45 as f32) + s8 * (5.0 as f32),
b: ((1.0 as f32) - t) * (0.7 as f32) + t * (0.85 as f32) + s8 * (3.5 as f32),
};
}
fn main() : void {
let mut acc = new f64[27];
let mut basis = new f32[9];
let mut accepted : i64 = 0;
let mut a : u32 = 0;
while (accepted < N_PROJECT) {
let k : u32 = a * 4;
a = a + 1;
let mut x : f32 = unit(hash32(k + 1)) * (2.0 as f32) - (1.0 as f32);
let mut y : f32 = unit(hash32(k + 2)) * (2.0 as f32) - (1.0 as f32);
let mut z : f32 = unit(hash32(k + 3)) * (2.0 as f32) - (1.0 as f32);
let r2 : f32 = x * x + y * y + z * z;
if (r2 > (1.0 as f32) || r2 < (1.0e-4 as f32)) {
continue;
}
let inv : f32 = (1.0 as f32) / r2.sqrt();
x = x * inv;
y = y * inv;
z = z * inv;
accepted = accepted + 1;
let l : Rgb = sky(x, y, z);
shBasis(basis, x, y, z);
for (j of 0..9) {
acc[j * 3 + 0] = acc[j * 3 + 0] + ((l.r * basis[j]) as f64);
acc[j * 3 + 1] = acc[j * 3 + 1] + ((l.g * basis[j]) as f64);
acc[j * 3 + 2] = acc[j * 3 + 2] + ((l.b * basis[j]) as f64);
}
}
// Fold in the 4 pi / N sphere weight and the cosine-lobe convolution.
let w : f64 = 12.566370614359172 / (N_PROJECT as f64);
let mut coef = new f32[27];
for (j of 0..9) {
let band : f64 = j == 0 ? 3.141592653589793 : (j
< 4 ? 2.0943951023931953 : 0.7853981633974483);
for (c of 0..3) {
coef[j * 3 + c] = (acc[j * 3 + c] * w * band) as f32;
}
}
let mut er : i64 = 0;
let mut eg : i64 = 0;
let mut eb : i64 = 0;
let mut e : u32 = 0;
let mut done : i64 = 0;
while (done < N_EVAL) {
let k : u32 = e * 4 + EVAL_SEED;
e = e + 1;
let mut x : f32 = unit(hash32(k + 1)) * (2.0 as f32) - (1.0 as f32);
let mut y : f32 = unit(hash32(k + 2)) * (2.0 as f32) - (1.0 as f32);
let mut z : f32 = unit(hash32(k + 3)) * (2.0 as f32) - (1.0 as f32);
let r2 : f32 = x * x + y * y + z * z;
if (r2 > (1.0 as f32) || r2 < (1.0e-4 as f32)) {
continue;
}
let inv : f32 = (1.0 as f32) / r2.sqrt();
x = x * inv;
y = y * inv;
z = z * inv;
done = done + 1;
shBasis(basis, x, y, z);
let mut sr : f32 = 0.0;
let mut sg : f32 = 0.0;
let mut sb : f32 = 0.0;
for (j of 0..9) {
sr = sr + coef[j * 3 + 0] * basis[j];
sg = sg + coef[j * 3 + 1] * basis[j];
sb = sb + coef[j * 3 + 2] * basis[j];
}
sr = sr > (0.0 as f32) ? sr : (0.0 as f32);
sg = sg > (0.0 as f32) ? sg : (0.0 as f32);
sb = sb > (0.0 as f32) ? sb : (0.0 as f32);
er = er + ((sr * (1024.0 as f32)) as i64);
eg = eg + ((sg * (1024.0 as f32)) as i64);
eb = eb + ((sb * (1024.0 as f32)) as i64);
}
println("${accepted} ${er} ${eg} ${eb}");
}400 sun positions x 14400 blocks, each a 24-step screen-space march over a prefiltered 160x90 sky-share picture - bilinear reads with clamps, a smoothstep border window, a carried decaying weight, a sqrt and two divides per march.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O1 | 1.11 s | 45.8 MB | 285 ms | 110.0 MB | 16 KB |
| C++ -O3 | 1.13 s | 45.8 MB | 299 ms | 110.9 MB | 16 KB |
| C++ -O2 | 1.14 s | 45.8 MB | 301 ms | 111.0 MB | 16 KB |
| Axle -O2 | 1.34 s | 45.8 MB | 162 ms | 125.9 MB | 323 KB |
| Axle -O3 | 1.35 s | 45.8 MB | 164 ms | 125.8 MB | 323 KB |
| Axle -O1 | 1.35 s | 45.8 MB | 117 ms | 125.9 MB | 322 KB |
| Rust -O3 | 1.54 s | 45.8 MB | 205 ms | 155.1 MB | 3871 KB |
| Rust -O1 | 1.58 s | 45.8 MB | 160 ms | 155.1 MB | 3865 KB |
| Rust -O2 | 1.64 s | 45.8 MB | 165 ms | 157.0 MB | 3867 KB |
| C++ -O0 | 5.51 s | 45.8 MB | 262 ms | 106.6 MB | 16 KB |
| Axle -O0 | 5.90 s | 45.8 MB | 90 ms | 126.3 MB | 327 KB |
| Rust -O0 | 7.07 s | 45.8 MB | 111 ms | 151.8 MB | 3885 KB |
// Screen-space light-shaft march over a prefiltered sky mask. A 1280 x 720 i32
// id picture is built from the hash (blocks of 16 x 16 pixels that are mostly
// "sky" or mostly something else, with a pixel of noise in sixteen), the share
// of sky in every 8 x 8 block is computed once (a 160 x 90 f32 picture), and
// then for every block a 24-step march toward a moving "sun" pixel reads that
// share picture bilinearly. Each step is weighted by a smooth window that falls
// to zero at the picture border and by a weight that decays geometrically
// along the march; the result is the weighted share of sky seen, divided by
// the larger of the weight seen and a floor, attenuated with the distance to
// the sun. The march is repeated for 400 sun positions, some outside the
// picture.
//
// Stresses: bilinear reads with clamps (four loads per step, index clamping),
// float -> int conversions, a branch on the window, a small inner loop with a
// carried product (the decaying weight) and carried positions, a sqrt and two
// divisions per march.
//
// One line is printed: the sum over the share picture (x 2^20, truncated), then
// the sum over all marches (x 2^16, truncated).
//
// Equivalent to shaft_march.{c,cpp,rs} - same picture, same marches, same
// printed line.
const W : i32 = 1280;
const H : i32 = 720;
const STEP : i32 = 8;
const GW : i32 = 160;
const GH : i32 = 90;
const SUNS : u32 = 400;
const INV24 : f32 = 5.9604644775390625e-8;
const SHAFT_STEPS : i32 = 24;
const SHAFT_SPAN : f32 = 0.92;
const SHAFT_REACH : f32 = 0.55;
const SHAFT_DECAY : f32 = 0.93;
const SHAFT_MARGIN : f32 = 0.12;
const SHAFT_FLOOR : f32 = 0.35;
const SHAFT_GLARE : f32 = 0.5;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn minf(a : f32, b : f32) : f32 {
return a < b ? a : b;
}
fn maxf(a : f32, b : f32) : f32 {
return a > b ? a : b;
}
// The smaller of two integers.
fn upTo(i : i32, limit : i32) : i32 {
if (i > limit) {
return limit;
}
return i;
}
fn smoothstep(edge0 : f32, edge1 : f32, v : f32) : f32 {
if (edge0 == edge1) {
if (v < edge0) {
return 0.0;
}
return 1.0;
}
let t : f32 = minf(maxf((v - edge0) / (edge1 - edge0), (0.0 as f32)), (1.0 as f32));
return t * t * ((3.0 as f32) - (2.0 as f32) * t);
}
// The pictures and the sun a march reads.
struct ShaftPlane {
w : i32;
h : i32;
sunX : f32;
sunY : f32;
}
// The weight of a step at (sx, sy): 1 inside the picture, falling smoothly to 0
// across `margin` pixels at its border.
fn window(p : ShaftPlane, sx : f32, sy : f32, margin : f32) : f32 {
if (sx < (0.0 as f32) || sy < (0.0 as f32) || sx >= (p.w as f32) || sy >= (p.h as f32)) {
return 0.0;
}
let nearest : f32 = minf(minf(sx, (p.w as f32) - sx), minf(sy, (p.h as f32) - sy));
return smoothstep((0.0 as f32), margin, nearest);
}
// Rows `begin` up to `end` of the sky's share of the picture, one value for each
// block of `step` by `step` pixels.
fn skyShare(sids : i32[], sky : i32, w : i32, h : i32, step : i32, mut out : f32[], begin : i32, end : i32) : void {
let gw : i32 = (w + step - 1) / step;
for (gy of begin..end) {
let y0 : i32 = gy * step;
let y1 : i32 = upTo(y0 + step, h);
for (gx of 0..gw) {
let x0 : i32 = gx * step;
let x1 : i32 = upTo(x0 + step, w);
let mut n : i32 = 0;
for (y of y0..y1) {
let row : i64 = (y as i64) * (w as i64);
for (x of x0..x1) {
if (sids[row + (x as i64)] == sky) {
n = n + 1;
}
}
}
out[(gy * gw + gx) as i64] = (n as f32) / (((x1 - x0) * (y1 - y0)) as f32);
}
}
}
// The march from the middle of block (gx, gy) toward the sun, reading the
// averaged sky bilinearly.
fn marchShare(p : ShaftPlane, share : f32[], step : i32, gx : i32, gy : i32) : f32 {
let gw : i32 = (p.w + step - 1) / step;
let gh : i32 = (p.h + step - 1) / step;
let px : f32 = ((gx * step) as f32) + (step as f32) * (0.5 as f32);
let py : f32 = ((gy * step) as f32) + (step as f32) * (0.5 as f32);
let dx : f32 = p.sunX - px;
let dy : f32 = p.sunY - py;
let len : f32 = (dx * dx + dy * dy).sqrt();
let longest : i32 = p.w > p.h ? p.w : p.h;
let shortest : i32 = p.w < p.h ? p.w : p.h;
let reach : f32 = SHAFT_REACH * (longest as f32);
let scale : f32 = minf(SHAFT_SPAN, reach / maxf(len, (0.001 as f32)));
let margin : f32 = SHAFT_MARGIN * (shortest as f32);
let inv : f32 = (1.0 as f32) / (step as f32);
let stepX : f32 = dx * scale / (SHAFT_STEPS as f32);
let stepY : f32 = dy * scale / (SHAFT_STEPS as f32);
let mut sky : f32 = 0.0;
let mut seen : f32 = 0.0;
let mut total : f32 = 0.0;
let mut weight : f32 = 1.0;
let mut sx : f32 = px + stepX * (0.5 as f32);
let mut sy : f32 = py + stepY * (0.5 as f32);
for (k of 0..SHAFT_STEPS) {
total = total + weight;
let win : f32 = window(p, sx, sy, margin);
if (win > (0.0 as f32)) {
let fx : f32 = maxf(sx * inv - (0.5 as f32), (0.0 as f32));
let fy : f32 = maxf(sy * inv - (0.5 as f32), (0.0 as f32));
let x0 : i32 = upTo(fx as i32, gw - 1);
let y0 : i32 = upTo(fy as i32, gh - 1);
let x1 : i32 = upTo(x0 + 1, gw - 1);
let y1 : i32 = upTo(y0 + 1, gh - 1);
let tx : f32 = fx - (x0 as f32);
let ty : f32 = fy - (y0 as f32);
let top : f32 = share[(y0 * gw + x0) as i64]
+ (share[(y0 * gw + x1) as i64] - share[(y0 * gw + x0) as i64]) * tx;
let low : f32 = share[(y1 * gw + x0) as i64]
+ (share[(y1 * gw + x1) as i64] - share[(y1 * gw + x0) as i64]) * tx;
seen = seen + weight * win;
sky = sky + weight * win * (top + (low - top) * ty);
}
weight = weight * SHAFT_DECAY;
sx = sx + stepX;
sy = sy + stepY;
}
let near : f32 = len / (SHAFT_GLARE * (longest as f32));
return (sky / maxf(seen, SHAFT_FLOOR * total)) / ((1.0 as f32) + near * near);
}
fn main() : void {
let mut sids = new i32[921600];
for (y of 0..H) {
for (x of 0..W) {
let cell : u32 = ((y >> 4) as u32) * 104729 + ((x >> 4) as u32) * 7919 + 0x1111;
let mut id : i32 = 0;
if (hash32(cell) % 100 >= 55) {
id = 1 + ((hash32(cell + 1) & 7) as i32);
}
let i : i32 = y * W + x;
if ((hash32((i as u32) + 0xa5a5) & 15) == 0) {
id = id == 0 ? 3 : 0;
}
sids[i] = id;
}
}
let mut share = new f32[14400];
skyShare(sids, 0, W, H, STEP, share, 0, GH);
let mut shareSum : i64 = 0;
for (i of 0..14400) {
shareSum = shareSum + ((share[i] * (1048576.0 as f32)) as i64);
}
let mut marchSum : i64 = 0;
let mut s : u32 = 0;
while (s < SUNS) {
let sunX : f32 = unit(hash32(s * 2 + 1)) * (1600.0 as f32) - (160.0 as f32);
let sunY : f32 = unit(hash32(s * 2 + 2)) * (900.0 as f32) - (90.0 as f32);
let p : ShaftPlane = ShaftPlane { w: W, h: H, sunX: sunX, sunY: sunY };
for (gy of 0..GH) {
for (gx of 0..GW) {
let v : f32 = marchShare(p, share, STEP, gx, gy);
marchSum = marchSum + ((v * (65536.0 as f32)) as i64);
}
}
s = s + 1;
}
println("${shareSum} ${marchSum}");
}Fill of a 1024x512 picture from a light sample every 4th pixel for 128 passes - plane-id-validated bilinear blend, a two-sqrt nearest-compatible fallback, a 12-field corner struct passed by value to a helper, unpredictable branches.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O3 | 645 ms | 45.5 MB | 331 ms | 111.2 MB | 20 KB |
| C++ -O2 | 652 ms | 45.5 MB | 331 ms | 111.2 MB | 20 KB |
| C++ -O1 | 749 ms | 45.5 MB | 309 ms | 110.7 MB | 20 KB |
| Axle -O3 | 897 ms | 45.5 MB | 188 ms | 127.1 MB | 322 KB |
| Rust -O3 | 1.07 s | 45.5 MB | 197 ms | 160.2 MB | 3868 KB |
| Rust -O2 | 1.16 s | 45.5 MB | 188 ms | 160.6 MB | 3869 KB |
| Axle -O1 | 1.31 s | 45.5 MB | 139 ms | 127.7 MB | 321 KB |
| Axle -O2 | 1.31 s | 45.5 MB | 186 ms | 127.9 MB | 322 KB |
| Rust -O1 | 1.43 s | 45.5 MB | 177 ms | 162.9 MB | 3869 KB |
| C++ -O0 | 3.24 s | 45.5 MB | 263 ms | 106.5 MB | 20 KB |
| Axle -O0 | 9.99 s | 45.5 MB | 102 ms | 126.2 MB | 338 KB |
| Rust -O0 | 10.67 s | 45.5 MB | 121 ms | 163.3 MB | 3894 KB |
// Interpolation of a sparse light signal with a validity test, as a renderer
// that traces one pixel in sixteen and fills the rest does. A 1024 x 512
// picture carries an i32 plane id and an f32 x 3 position per pixel (large flat
// regions of 128 x 128 pixels, with about three thousand small 3 x 3 islands of
// other ids), and a light sample (f32 x 3) at every 4th pixel each way. For
// every other pixel the four surrounding samples are blended bilinearly,
// restricted to those whose pixel has the same id and renormalised; if none has,
// the heaviest sample whose id has the same low 3 bits and whose position is at
// a similar distance from the eye (|da - db| <= 0.15 max(da, db) + 0.5, two
// square roots) is taken; else the pixel gets zero. The result goes to an
// f32 x 3 output. 128 passes, each with the eye moved a little.
//
// The four sample corners and their weights travel as a 12-field struct passed
// by value to the helper `sharedLight`; the helper is not inlined by hand.
//
// Stresses: gathered loads from three arrays, unpredictable branches (the
// islands), a 12-field struct by value, short sqrt chains, a class whose arrays
// are reloaded across the loop.
//
// One line is printed: the sum of all outputs over all passes (x 256,
// truncated), then the number of pixels taken by the blend, by the nearest
// compatible sample, and by neither.
//
// Equivalent to sparse_fill.{c,cpp,rs} - same picture, same samples, same
// printed line.
const W : i32 = 1024;
const H : i32 = 512;
const S : i32 = 4;
const GW : i32 = 256;
const GH : i32 = 128;
const PASSES : i32 = 128;
const INV24 : f32 = 5.9604644775390625e-8;
const EPS : f32 = 1.0e-4;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
struct V3 {
x : f32;
y : f32;
z : f32;
}
// The corners of the sample cell around a pixel: their pixels, their places in
// the light grid and their bilinear weights.
struct Corners {
a : i64;
b : i64;
c : i64;
d : i64;
ga : i64;
gb : i64;
gc : i64;
gd : i64;
wa : f32;
wb : f32;
wc : f32;
wd : f32;
}
// The light a pixel takes and which path gave it: 0 blend, 1 nearest compatible, 2 none.
struct Lit {
x : f32;
y : f32;
z : f32;
path : i32;
}
fn clampIdx(v : i32, lo : i32, hi : i32) : i32 {
if (v < lo) {
return lo;
}
if (v > hi) {
return hi;
}
return v;
}
// True when two hit points lie at about the same distance from the eye.
fn similar(a : V3, b : V3, eye : V3) : bool {
let ax : f32 = a.x - eye.x;
let ay : f32 = a.y - eye.y;
let az : f32 = a.z - eye.z;
let bx : f32 = b.x - eye.x;
let by : f32 = b.y - eye.y;
let bz : f32 = b.z - eye.z;
let da : f32 = (ax * ax + ay * ay + az * az).sqrt();
let db : f32 = (bx * bx + by * by + bz * bz).sqrt();
let d : f32 = da - db;
let ad : f32 = d < (0.0 as f32) ? -d : d;
let mx : f32 = da > db ? da : db;
return ad <= (0.15 as f32) * mx + (0.5 as f32);
}
class Frame {
pub sid : i32[];
pub pos : f32[];
pub light : f32[];
pub out : f32[];
pub n0 : i64;
pub n1 : i64;
pub n2 : i64;
constructor(sid : i32[], pos : f32[], light : f32[], out : f32[]) {
self.sid = sid;
self.pos = pos;
self.light = light;
self.out = out;
self.n0 = 0;
self.n1 = 0;
self.n2 = 0;
}
// Fills every pixel that is not a sample from the samples around it.
pub fn fill(mut self, eye : V3) : void {
let invS : f32 = (1.0 as f32) / (S as f32);
let mut c0 : i64 = 0;
let mut c1 : i64 = 0;
let mut c2 : i64 = 0;
for (y of 0..H) {
let gy0 : i32 = y / S;
let gy1 : i32 = clampIdx(gy0 + 1, 0, GH - 1);
let ty : f32 = ((y - gy0 * S) as f32) * invS;
for (x of 0..W) {
let gx0 : i32 = x / S;
if (gx0 * S == x && gy0 * S == y) {
continue;
}
let gx1 : i32 = clampIdx(gx0 + 1, 0, GW - 1);
let tx : f32 = ((x - gx0 * S) as f32) * invS;
let i : i64 = (y * W + x) as i64;
let k : Corners = Corners {
a: ((gy0 * S) * W + gx0 * S) as i64,
b: ((gy0 * S) * W + gx1 * S) as i64,
c: ((gy1 * S) * W + gx0 * S) as i64,
d: ((gy1 * S) * W + gx1 * S) as i64,
ga: (gy0 * GW + gx0) as i64,
gb: (gy0 * GW + gx1) as i64,
gc: (gy1 * GW + gx0) as i64,
gd: (gy1 * GW + gx1) as i64,
wa: ((1.0 as f32) - tx) * ((1.0 as f32) - ty),
wb: tx * ((1.0 as f32) - ty),
wc: ((1.0 as f32) - tx) * ty,
wd: tx * ty,
};
let p : V3 = V3 { x: self.pos[i * 3], y: self.pos[i * 3 + 1], z: self.pos[i * 3 + 2] };
let lit : Lit = self.sharedLight(k, self.sid[i], p, eye);
self.out[i * 3] = lit.x;
self.out[i * 3 + 1] = lit.y;
self.out[i * 3 + 2] = lit.z;
if (lit.path == 0) {
c0 = c0 + 1;
} else if (lit.path == 1) {
c1 = c1 + 1;
} else {
c2 = c2 + 1;
}
}
}
self.n0 = self.n0 + c0;
self.n1 = self.n1 + c1;
self.n2 = self.n2 + c2;
}
// The light the four samples at `k` give a pixel on the plane `sid` at `pos`:
// their bilinear blend over those on the same plane, else the heaviest of
// those facing the same way at about the same distance, else nothing.
fn sharedLight(self, k : Corners, sid : i32, pos : V3, eye : V3) : Lit {
let mut sx : f32 = 0.0;
let mut sy : f32 = 0.0;
let mut sz : f32 = 0.0;
let mut weight : f32 = 0.0;
if (self.sid[k.a] == sid) {
sx = sx + self.light[k.ga * 3] * k.wa;
sy = sy + self.light[k.ga * 3 + 1] * k.wa;
sz = sz + self.light[k.ga * 3 + 2] * k.wa;
weight = weight + k.wa;
}
if (self.sid[k.b] == sid) {
sx = sx + self.light[k.gb * 3] * k.wb;
sy = sy + self.light[k.gb * 3 + 1] * k.wb;
sz = sz + self.light[k.gb * 3 + 2] * k.wb;
weight = weight + k.wb;
}
if (self.sid[k.c] == sid) {
sx = sx + self.light[k.gc * 3] * k.wc;
sy = sy + self.light[k.gc * 3 + 1] * k.wc;
sz = sz + self.light[k.gc * 3 + 2] * k.wc;
weight = weight + k.wc;
}
if (self.sid[k.d] == sid) {
sx = sx + self.light[k.gd * 3] * k.wd;
sy = sy + self.light[k.gd * 3 + 1] * k.wd;
sz = sz + self.light[k.gd * 3 + 2] * k.wd;
weight = weight + k.wd;
}
if (weight > EPS) {
let inv : f32 = (1.0 as f32) / weight;
return Lit { x: sx * inv, y: sy * inv, z: sz * inv, path: 0 };
}
let face : i32 = sid & 7;
let mut best : f32 = 0.0;
let mut from : i64 = -1;
if (k.wa > best && self.kin(k.a, face, pos, eye)) {
best = k.wa;
from = k.ga;
}
if (k.wb > best && self.kin(k.b, face, pos, eye)) {
best = k.wb;
from = k.gb;
}
if (k.wc > best && self.kin(k.c, face, pos, eye)) {
best = k.wc;
from = k.gc;
}
if (k.wd > best && self.kin(k.d, face, pos, eye)) {
best = k.wd;
from = k.gd;
}
if (from < 0) {
return Lit { x: 0.0, y: 0.0, z: 0.0, path: 2 };
}
return Lit { x: self.light[from * 3], y: self.light[from * 3 + 1], z: self.light[from * 3 + 2], path: 1 };
}
// True when the sample at pixel `at` faces `face` at about the distance of `pos`.
fn kin(self, at : i64, face : i32, pos : V3, eye : V3) : bool {
let s : i32 = self.sid[at];
let q : V3 = V3 { x: self.pos[at * 3], y: self.pos[at * 3 + 1], z: self.pos[at * 3 + 2] };
return s >= 0 && (s & 7) == face && similar(q, pos, eye);
}
}
fn main() : void {
let mut sid = new i32[524288];
let mut pos = new f32[1572864];
for (y of 0..H) {
for (x of 0..W) {
let rh : u32 = hash32(((x >> 7) as u32) * 7919 + ((y >> 7) as u32) * 104729 + 0x77);
let mut id : i32 = ((((rh >> 4) & 63) * 8) + (rh & 7)) as i32;
let ch : u32 = hash32(((x >> 3) as u32) * 31337 + ((y >> 3) as u32) * 2713 + 0x99);
if ((ch & 7) < 3) {
let ox : i32 = ((ch >> 3) % 6) as i32;
let oy : i32 = ((ch >> 8) % 6) as i32;
let lx : i32 = x & 7;
let ly : i32 = y & 7;
if (lx >= ox && lx < ox + 3 && ly >= oy && ly < oy + 3) {
let ih : u32 = hash32(ch + 0x1234);
let mut low : i32 = ((ih >> 9) & 7) as i32;
if (((ih >> 20) & 1) == 0) {
low = id & 7;
}
id = ((((ih >> 12) & 63) * 8) as i32) + low;
}
}
let i : i32 = y * W + x;
sid[i] = id;
pos[i * 3] = (x as f32) * (0.0625 as f32) + (((id * 37) & 15) as f32) * (0.5 as f32);
pos[i * 3 + 1] = (y as f32) * (0.0625 as f32) + (((id * 53) & 15) as f32) * (0.5 as f32);
pos[i * 3 + 2] = (4.0 as f32) + ((((id >> 3) & 63)) as f32) * (0.5 as f32);
}
}
let mut light = new f32[98304];
for (i of 0..98304) {
light[i] = unit(hash32((i as u32) + 0x6d000000)) * (4.0 as f32);
}
let out = new f32[1572864];
let fr : Frame = new Frame(sid, pos, light, out);
let mut total : i64 = 0;
for (pass of 0..PASSES) {
let p : f32 = pass as f32;
let eye : V3 = V3 {
x: (20.0 as f32) + p * (1.5 as f32),
y: (10.0 as f32) - p * (0.75 as f32),
z: (-8.0 as f32) + p * (0.5 as f32),
};
fr.fill(eye);
for (i of 0..1572864) {
total = total + ((fr.out[i] * (256.0 as f32)) as i64);
}
}
println("${total} ${fr.n0} ${fr.n1} ${fr.n2}");
}Tone mapping of a 1024^2 HDR RGB f32 buffer for 60 exposures - ACES fit (one divide), sqrt-polynomial sRGB encode instead of pow, clamp and float-to-u8 store; bandwidth plus a vectorisable kernel.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| Axle -O3 | 605 ms | 45.1 MB | 115 ms | 125.8 MB | 321 KB |
| Axle -O2 | 607 ms | 45.1 MB | 114 ms | 127.8 MB | 321 KB |
| Rust -O2 | 689 ms | 45.1 MB | 123 ms | 151.0 MB | 3864 KB |
| Rust -O3 | 689 ms | 45.1 MB | 122 ms | 148.3 MB | 3864 KB |
| C++ -O3 | 1.16 s | 45.1 MB | 267 ms | 110.3 MB | 16 KB |
| C++ -O2 | 1.17 s | 45.1 MB | 267 ms | 110.3 MB | 16 KB |
| Axle -O1 | 1.18 s | 45.1 MB | 96 ms | 125.9 MB | 320 KB |
| C++ -O1 | 1.26 s | 45.1 MB | 262 ms | 109.5 MB | 16 KB |
| Rust -O1 | 3.21 s | 45.1 MB | 115 ms | 150.5 MB | 3864 KB |
| C++ -O0 | 5.33 s | 45.1 MB | 252 ms | 105.3 MB | 16 KB |
| Axle -O0 | 5.78 s | 45.1 MB | 83 ms | 127.4 MB | 322 KB |
| Rust -O0 | 10.70 s | 45.1 MB | 98 ms | 147.5 MB | 3877 KB |
// Tone mapping and sRGB encoding of an HDR f32 buffer. A 1024 x 1024 RGB
// image of hashed radiance (a squared uniform, scaled to [0, 16)) is tone
// mapped several times with different exposures.
//
// Per channel: exposure multiply, the Narkowicz ACES fit (a rational
// function, one division), a clamp, then the sRGB transfer curve. pow() is
// replaced by Tardif's sqrt-based polynomial fit (three IEEE-exact square
// roots), so every language computes the same value; the linear toe below
// 0.0031308 is kept. The result is rounded to u8 and stored in an output
// buffer.
//
// Stresses: streaming f32 loads and u8 stores over 12 MB + 3 MB (memory
// bandwidth plus compute), a division and three sqrts per channel, float
// clamps and a float -> u8 conversion, a branch the optimiser may turn into
// a select.
//
// One line is printed: the sum of all output bytes over all passes, then how
// many output bytes were saturated at 255.
//
// Equivalent to tonemap_srgb.{c,cpp,rs} - same image, same curve, same
// printed line.
const PIXELS : i32 = 1048576;
const PASSES : i32 = 60;
const INV24 : f32 = 5.9604644775390625e-8;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn aces(x : f32) : f32 {
return (x * ((2.51 as f32) * x + (0.03 as f32)))
/ (x * ((2.43 as f32) * x + (0.59 as f32)) + (0.14 as f32));
}
fn srgbEncode(c : f32) : f32 {
if (c <= (0.0031308 as f32)) {
return c * (12.92 as f32);
}
let s1 : f32 = c.sqrt();
let s2 : f32 = s1.sqrt();
let s3 : f32 = s2.sqrt();
return (0.662002687 as f32) * s1 + (0.684122060 as f32) * s2 - (0.323583601 as f32) * s3
- (0.0225411470 as f32) * c;
}
fn toByte(v : f32) : u8 {
let c : f32 = v < (0.0 as f32) ? (0.0 as f32) : (v > (1.0 as f32) ? (1.0 as f32) : v);
return (c * (255.0 as f32) + (0.5 as f32)) as u8;
}
fn main() : void {
let mut hdr = new f32[3145728];
let mut out = new u8[3145728];
for (i of 0..3145728) {
let u : f32 = unit(hash32((i as u32) + 0x3c000000));
hdr[i] = u * u * (16.0 as f32);
}
let mut total : i64 = 0;
let mut saturated : i64 = 0;
for (pass of 0..PASSES) {
let exposure : f32 = (0.25 as f32) + (pass as f32) * (0.125 as f32);
for (i of 0..3145728) {
let mapped : f32 = aces(hdr[i] * exposure);
out[i] = toByte(srgbEncode(mapped));
}
for (i of 0..3145728) {
total = total + (out[i] as i64);
if (out[i] == 255) {
saturated = saturated + 1;
}
}
}
println("${total} ${saturated}");
}20M iterations of sin, cos and atan2 from hand-written Cephes minimax polynomials (Cody-Waite reduction, Horner kernels, quadrant switch) in f32 - the same polynomial and operation order in every language, no libm call.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O2 | 601 ms | 47.2 MB | 103 ms | 97.1 MB | 16 KB |
| C++ -O3 | 602 ms | 47.2 MB | 103 ms | 97.1 MB | 16 KB |
| C++ -O1 | 630 ms | 47.2 MB | 102 ms | 96.8 MB | 16 KB |
| Axle -O2 | 774 ms | 47.2 MB | 102 ms | 126.1 MB | 316 KB |
| Axle -O3 | 775 ms | 47.2 MB | 102 ms | 125.9 MB | 316 KB |
| Axle -O1 | 817 ms | 47.2 MB | 96 ms | 125.9 MB | 316 KB |
| Rust -O3 | 842 ms | 47.2 MB | 103 ms | 134.1 MB | 3864 KB |
| Rust -O2 | 846 ms | 47.2 MB | 104 ms | 134.1 MB | 3864 KB |
| Rust -O1 | 877 ms | 47.2 MB | 104 ms | 134.2 MB | 3864 KB |
| Axle -O0 | 1.51 s | 47.2 MB | 82 ms | 127.9 MB | 316 KB |
| Rust -O0 | 1.66 s | 47.2 MB | 88 ms | 135.4 MB | 3867 KB |
| C++ -O0 | 1.79 s | 47.2 MB | 90 ms | 91.9 MB | 16 KB |
// sin / cos / atan2 from hand-written minimax polynomials, f32 throughout.
//
// The kernels are Cephes' single-precision ones (the lineage fdlibm's
// k_sinf / k_cosf / atanf share): a Cody-Waite argument reduction by pi/2
// in three pieces, then a degree-7 sine, a degree-8 cosine and a degree-9
// arctangent. No libm call is made anywhere, so every language runs the very
// same polynomial and the same f32 operation order; the only library
// function is the IEEE-exact integer truncation `as i32`.
//
// Stresses: f32 multiply-add chains (Horner), float -> int truncation, a
// quadrant switch on the low bits of the reduced integer, a data-dependent
// branch ladder in atan2, and a guarded f32 division.
//
// Each iteration derives three arguments from an integer hash: an angle in
// [-1000, 1000) for sin and cos, and a pair (y, x) for atan2. The three
// results are quantised (x 4096, truncated) and summed in i64, so the sums
// are exact whatever the float rounding of the last place. One line is
// printed: the sin sum, the cos sum and the atan2 sum.
//
// Equivalent to trig_poly.{c,cpp,rs} — same constants, same operation order,
// same hash, same printed line.
const INV24 : f32 = 5.9604644775390625e-8;
const TWO_OVER_PI : f32 = 0.63661977236758134;
const PIO2_1 : f32 = 1.5703125;
const PIO2_2 : f32 = 4.837512969970703125e-4;
const PIO2_3 : f32 = 7.54978995489188216e-8;
const SIN0 : f32 = -1.9515295891e-4;
const SIN1 : f32 = 8.3321608736e-3;
const SIN2 : f32 = -1.6666654611e-1;
const COS0 : f32 = 2.443315711809948e-5;
const COS1 : f32 = -1.388731625493765e-3;
const COS2 : f32 = 4.166664568298827e-2;
const ATAN0 : f32 = 8.05374449538e-2;
const ATAN1 : f32 = -1.38776856032e-1;
const ATAN2 : f32 = 1.99777106478e-1;
const ATAN3 : f32 = -3.33329491539e-1;
const TAN_3PI_8 : f32 = 2.414213562373095;
const TAN_PI_8 : f32 = 0.4142135623730950;
const PIO2 : f32 = 1.5707963267948966;
const PIO4 : f32 = 0.7853981633974483;
const PI : f32 = 3.141592653589793;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn ifloor(x : f32) : i32 {
let t : i32 = x as i32;
if ((t as f32) > x) {
return t - 1;
}
return t;
}
// sin(r) and cos(r) of a reduced argument |r| <= pi/4.
fn sinKernel(r : f32) : f32 {
let z : f32 = r * r;
return (((SIN0 * z + SIN1) * z + SIN2) * z) * r + r;
}
fn cosKernel(r : f32) : f32 {
let z : f32 = r * r;
return (((COS0 * z + COS1) * z + COS2) * z) * z - (0.5 as f32) * z + (1.0 as f32);
}
fn reduce(x : f32, n : i32) : f32 {
let fn_ : f32 = n as f32;
return ((x - fn_ * PIO2_1) - fn_ * PIO2_2) - fn_ * PIO2_3;
}
fn sinPoly(x : f32) : f32 {
let n : i32 = ifloor(x * TWO_OVER_PI + (0.5 as f32));
let r : f32 = reduce(x, n);
let q : i32 = n & 3;
if (q == 0) { return sinKernel(r); }
if (q == 1) { return cosKernel(r); }
if (q == 2) { return -sinKernel(r); }
return -cosKernel(r);
}
fn cosPoly(x : f32) : f32 {
let n : i32 = ifloor(x * TWO_OVER_PI + (0.5 as f32));
let r : f32 = reduce(x, n);
let q : i32 = n & 3;
if (q == 0) { return cosKernel(r); }
if (q == 1) { return -sinKernel(r); }
if (q == 2) { return -cosKernel(r); }
return sinKernel(r);
}
fn atanPoly(x : f32) : f32 {
let neg : bool = x < (0.0 as f32);
let mut a : f32 = neg ? -x : x;
let mut base : f32 = 0.0;
if (a > TAN_3PI_8) {
base = PIO2;
a = -((1.0 as f32) / a);
} else if (a > TAN_PI_8) {
base = PIO4;
a = (a - (1.0 as f32)) / (a + (1.0 as f32));
}
let z : f32 = a * a;
let p : f32 = ((((ATAN0 * z + ATAN1) * z + ATAN2) * z + ATAN3) * z) * a + a;
let r : f32 = base + p;
return neg ? -r : r;
}
fn atan2Poly(y : f32, x : f32) : f32 {
if (x == (0.0 as f32)) {
if (y > (0.0 as f32)) { return PIO2; }
if (y < (0.0 as f32)) { return -PIO2; }
return 0.0;
}
let a : f32 = atanPoly(y / x);
if (x < (0.0 as f32)) {
if (y >= (0.0 as f32)) { return a + PI; }
return a - PI;
}
return a;
}
fn main() : void {
let N : u32 = 20000000;
let mut sinSum : i64 = 0;
let mut cosSum : i64 = 0;
let mut atanSum : i64 = 0;
let mut i : u32 = 0;
while (i < N) {
let h : u32 = hash32(i * 3 + 1);
let angle : f32 = (unit(h) * (2.0 as f32) - (1.0 as f32)) * (1000.0 as f32);
let y : f32 = unit(hash32(i * 3 + 2)) * (2.0 as f32) - (1.0 as f32);
let x : f32 = unit(hash32(i * 3 + 3)) * (2.0 as f32) - (1.0 as f32);
sinSum = sinSum + ((sinPoly(angle) * (4096.0 as f32)) as i64);
cosSum = cosSum + ((cosPoly(angle) * (4096.0 as f32)) as i64);
atanSum = atanSum + ((atan2Poly(y, x) * (4096.0 as f32)) as i64);
i = i + 1;
}
println("${sinSum} ${cosSum} ${atanSum}");
}6M Amanatides-Woo DDA rays through a 64^3 u8 voxel grid - a bounds-checked byte load per step, a three-way float compare ladder, integer cell stepping, guarded reciprocals.
| Variant | Run | Compile | Output | ||
|---|---|---|---|---|---|
| Run time | RAM | Compile time | RAM | Binary | |
| C++ -O1 | 719 ms | 43.8 MB | 272 ms | 109.8 MB | 16 KB |
| Rust -O2 | 765 ms | 43.8 MB | 133 ms | 146.7 MB | 3865 KB |
| Rust -O1 | 802 ms | 43.8 MB | 116 ms | 145.7 MB | 3864 KB |
| Axle -O1 | 817 ms | 43.8 MB | 110 ms | 127.7 MB | 321 KB |
| Rust -O3 | 837 ms | 43.8 MB | 136 ms | 148.7 MB | 3865 KB |
| Axle -O3 | 856 ms | 43.8 MB | 161 ms | 126.0 MB | 324 KB |
| Axle -O2 | 857 ms | 43.8 MB | 160 ms | 125.7 MB | 324 KB |
| C++ -O3 | 904 ms | 43.8 MB | 285 ms | 110.7 MB | 16 KB |
| C++ -O2 | 905 ms | 43.8 MB | 287 ms | 110.7 MB | 16 KB |
| C++ -O0 | 1.37 s | 43.8 MB | 258 ms | 105.7 MB | 16 KB |
| Axle -O0 | 1.57 s | 43.8 MB | 86 ms | 128.0 MB | 324 KB |
| Rust -O0 | 2.01 s | 43.8 MB | 97 ms | 145.6 MB | 3874 KB |
// Voxel DDA ray march (Amanatides & Woo) through a 64^3 grid of u8 cells.
//
// The grid is filled from an integer hash (about 4% solid cells). Each ray
// has a hashed origin inside the grid and a hashed unit direction; the march
// steps cell by cell along the axis with the smallest tMax, stops on a solid
// cell, on leaving the grid, or after MAX_STEPS. A direction component of
// exactly zero uses a huge reciprocal instead of dividing by zero.
//
// Stresses: an indexed u8 load per step (a bounds check in Axle), a
// three-way float compare ladder, integer cell stepping, one sqrt and a few
// divisions per ray.
//
// One line is printed: hits, sum of hit distances (x 256, truncated), total
// steps.
//
// Equivalent to voxel_dda.{c,cpp,rs} — same grid, same rays, same stepping
// order, same printed line.
const G : i32 = 64;
const MAX_STEPS : i32 = 192;
const N_RAYS : u32 = 6000000;
const INV24 : f32 = 5.9604644775390625e-8;
const BIG : f32 = 1.0e30;
fn hash32(x : u32) : u32 {
let mut h : u32 = x;
h = h ^ (h >> 16);
h = h * 0x7feb352d;
h = h ^ (h >> 15);
h = h * 0x846ca68b;
h = h ^ (h >> 16);
return h;
}
fn unit(h : u32) : f32 {
return ((h >> 8) as f32) * INV24;
}
fn main() : void {
let mut grid = new u8[262144];
for (i of 0..262144) {
let h : u32 = hash32((i as u32) + 0x9e3779b9);
if (h % 100 < 4) {
grid[i] = 1;
}
}
let mut hits : i64 = 0;
let mut distSum : i64 = 0;
let mut stepsTotal : i64 = 0;
let mut r : u32 = 0;
while (r < N_RAYS) {
let b : u32 = r * 8;
let ox : f32 = unit(hash32(b + 1)) * (64.0 as f32);
let oy : f32 = unit(hash32(b + 2)) * (64.0 as f32);
let oz : f32 = unit(hash32(b + 3)) * (64.0 as f32);
let mut dx : f32 = unit(hash32(b + 4)) * (2.0 as f32) - (1.0 as f32);
let mut dy : f32 = unit(hash32(b + 5)) * (2.0 as f32) - (1.0 as f32);
let mut dz : f32 = unit(hash32(b + 6)) * (2.0 as f32) - (1.0 as f32);
let len2 : f32 = dx * dx + dy * dy + dz * dz;
if (len2 < (1.0e-6 as f32)) {
dx = 1.0;
dy = 0.0;
dz = 0.0;
} else {
let inv : f32 = (1.0 as f32) / len2.sqrt();
dx = dx * inv;
dy = dy * inv;
dz = dz * inv;
}
let mut ix : i32 = ox as i32;
let mut iy : i32 = oy as i32;
let mut iz : i32 = oz as i32;
let sx : i32 = dx > (0.0 as f32) ? 1 : -1;
let sy : i32 = dy > (0.0 as f32) ? 1 : -1;
let sz : i32 = dz > (0.0 as f32) ? 1 : -1;
let rx : f32 = dx == (0.0 as f32) ? BIG : (1.0 as f32) / dx;
let ry : f32 = dy == (0.0 as f32) ? BIG : (1.0 as f32) / dy;
let rz : f32 = dz == (0.0 as f32) ? BIG : (1.0 as f32) / dz;
let tdx : f32 = rx < (0.0 as f32) ? -rx : rx;
let tdy : f32 = ry < (0.0 as f32) ? -ry : ry;
let tdz : f32 = rz < (0.0 as f32) ? -rz : rz;
let nx : i32 = sx > 0 ? ix + 1 : ix;
let ny : i32 = sy > 0 ? iy + 1 : iy;
let nz : i32 = sz > 0 ? iz + 1 : iz;
let mut tmx : f32 = dx == (0.0 as f32) ? BIG : ((nx as f32) - ox) * rx;
let mut tmy : f32 = dy == (0.0 as f32) ? BIG : ((ny as f32) - oy) * ry;
let mut tmz : f32 = dz == (0.0 as f32) ? BIG : ((nz as f32) - oz) * rz;
let mut t : f32 = 0.0;
let mut hit : bool = false;
for (s of 0..MAX_STEPS) {
stepsTotal = stepsTotal + 1;
if (grid[(iz * G + iy) * G + ix] != 0) {
hit = true;
break;
}
if (tmx < tmy) {
if (tmx < tmz) {
ix = ix + sx;
t = tmx;
tmx = tmx + tdx;
} else {
iz = iz + sz;
t = tmz;
tmz = tmz + tdz;
}
} else {
if (tmy < tmz) {
iy = iy + sy;
t = tmy;
tmy = tmy + tdy;
} else {
iz = iz + sz;
t = tmz;
tmz = tmz + tdz;
}
}
if (ix < 0 || ix >= G || iy < 0 || iy >= G || iz < 0 || iz >= G) {
break;
}
}
if (hit) {
hits = hits + 1;
distSum = distSum + ((t * (256.0 as f32)) as i64);
}
r = r + 1;
}
println("${hits} ${distSum} ${stepsTotal}");
}