You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The Metal lane forces precise:: trigonometry and MTLMathModeSafe: a third of the voxel frame, same checksums with fast::; an opt-in? #887
The Metal lane compiles the program with MTLMathModeSafe and #define sin precise::sin (cos, sqrt, exp, log, tan, pow, fmod likewise). On the voxel program of #885 and #886 the precise trigonometry is a third of the untouched frame: the emitted C rebuilt by hand with fast::sin, fast::cos and fast::sqrt renders the same ten checksums in 4 ms instead of 6 (15 instead of 17 with 300 edits). The math mode alone changes nothing in time and flips one frame's checksum. Is an opt-in for the fast functions something you would take, a flag on the binary or a pragma in the program, or is precise the line for CPU/GPU parity?
What I did
gfx/05_craft.bend in AdrielSantana/metal-bending, after #886's fix: the terrain is 12.0 + 5.0 * F32.sin(x * 0.26) + 4.0 * F32.cos(z * 0.19), computed once per column crossing of the DDA, and the camera basis is four trigonometric calls per pixel. Bench as in #886 (five frames, total(9n) checksum, "built" = 300 blocks), the timer around the ! only, three alternating runs. bend x.bend -o x.c, then the C edited and built as bend does (clang -x objective-c -fobjc-arc -fmodules -std=c11 -O3 -DBEND_METAL=1 x.c -lpthread -lm -o x && ./x --gpu-build). Apple M5, Bend 2.0.17.
512², Metal
untouched
300 blocks placed
checksums
as emitted (MTLMathModeSafe, precise::)
6 ms
17 ms
reference
fast::sin, fast::cos, fast::sqrt
4 ms
15 ms
all ten identical
MTLMathModeFast, precise:: kept
6 ms
17 ms
one frame differs
both
3–4 ms
14–16 ms
all ten identical
The inputs are small (the sine's argument is at most 32 × 0.26), which is where fast::sin is exact enough; a program with large arguments would see a difference, so this is not a case for changing the default.
For scale, the same pixel function written by hand in WGSL runs in 1.34 ms on the same GPU through Chrome, which compiles Metal shaders with fastMathEnabled unless strict math is requested, so that number had the fast functions. With them the compiled leaf is at 3x of the hand shader; the remaining part I could not attribute from the outside (the fork skeleton alone, every pixel the sky, is 1–2 ms against 0.2 in the WGSL runtime; the ray about 2.5 against 1.1).
Written by Claude (Anthropic) with Adriel Santana driving; the numbers are from his M5.
Done in 2.0.19: sin, cos and tan now compile to Metal's fast:: variants. The other shims (sqrt, exp, log, log2, log10, pow, fmod) stay precise::, and the math mode stays MTLMathModeSafe.
Measured here on the shader demo, the draw stage went from 3584 to 3494 us a frame, and all 46 of its state lines are identical. That is less than your 6 ms to 4 ms, because your gain came from fast:: on every name, not on trig alone. If you want to make the case for widening it, a per-name measurement on a scene you can pin would be the way; sqrt, exp and pow are the next candidates. Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.
Per name, on 2.0.19 as released and on its emitted C rebuilt by hand, same scene, same ten checksums on every row:
512², Metal
untouched
300 blocks placed
2.0.19 with precise::sin/cos/tan put back
5–6 ms
16–18 ms
2.0.19 as released (fast:: trig, precise::sqrt)
4 ms
14–16 ms
2.0.19 plus fast::sqrt
4 ms
13–16 ms
So on this scene the trig was all of it and sqrt is not a candidate; my earlier 6 → 4 was the trig too (the sqrt was in the patch but adds nothing measurable). Thanks for both.
Activity
README: the precise-math question is bendlang/bend#887
The Metal lane computes sin, cos and tan with fast:: trig
VictorTaelin commentedon Sep 20, 2026
Done in 2.0.19:
sin,cosandtannow compile to Metal'sfast::variants. The other shims (sqrt,exp,log,log2,log10,pow,fmod) stayprecise::, and the math mode staysMTLMathModeSafe.Measured here on the shader demo, the draw stage went from 3584 to 3494 us a frame, and all 46 of its state lines are identical. That is less than your 6 ms to 4 ms, because your gain came from
fast::on every name, not on trig alone. If you want to make the case for widening it, a per-name measurement on a scene you can pin would be the way;sqrt,expandpoware the next candidates.Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.
AdrielSantana commentedon Sep 20, 2026
Per name, on 2.0.19 as released and on its emitted C rebuilt by hand, same scene, same ten checksums on every row:
precise::sin/cos/tanput backfast::trig,precise::sqrt)fast::sqrtSo on this scene the trig was all of it and
sqrtis not a candidate; my earlier 6 → 4 was the trig too (the sqrt was in the patch but adds nothing measurable). Thanks for both.The Metal lane computes sin, cos and tan with fast:: trig