Repository navigation
Floating-point math functions are width-generic builtins: cos(x) and x.cos() for f32/f64, no width suffix (D42) - #1154
Merged
Merged
Conversation
added 2 commits
September 16, 2026 02:06
`cos(x)` and `x.cos()` now work for f32 and f64 with no width in the name, for the full libm surface; the `*_f64` wrappers in std.math are deleted. The intrinsic-vs-libm split is hidden from the caller. With has no ad-hoc overloading, so std.math had shipped `cos_f64(x)` over an `extern fn cos(f64)` — making the user spell a type the compiler already reads off the argument (the Go tax). abs/min/max/mul_add were already width-generic builtins; the transcendentals had been left out. Mechanism, one source of truth (the D6 FnAbi rule applied to builtins): - src/MathBuiltins.w: the table (name, arity, LLVM intrinsic, libm name). - Sema types both spellings; free calls are recorded in math_builtin_calls so MirLower never re-derives the decision. - MirLower tags both spellings MirIntrinsic.MATH_FN with the row id at the NK_CALL dispatch, before any call shape (variant, src(), drop, generic, signature) can claim the node. - LLVM codegen: one arm — `llvm.cos.f32/f64` where LLVM has an intrinsic, else the width-correct libm symbol (`tan` / `tanf`); the same split Swift's tgmath keys on. C backend: `cos` / `cosf`. Precedence: a definition of the same name in scope shadows the builtin. An `extern fn` declaration does not: D29 makes every extern global, and the migrator's `pub extern fn cos(f64)` in the prelude-closure `std.re` corpus was ambient in every program and would have returned f64 for `cos(0.0f32)`. For f64 the two are the same symbol; only the builtin is correct for f32. Removing the redundant migrator decls is #1153. Spec: §17.6a gains the math builtins (wording proposed for blessing). Decision log: D42. Tests: test/behavior/math_builtins.w (both spellings, both widths, both arities, both tiers) and three compile_errors guards.
`sqrt(2)`, `sqrt(n)`, and `pow(2.0f32, 3)` now work. The builtin's type check had been stricter than the language: With converts an integer to a float parameter or binding everywhere (`f(2)`, `let x: f64 = n`, `x.min(1)`), and the old f64 extern accepted `sqrt(2)`, so rejecting it was a ceremony regression. Rule: the call's float type is the float operand's width, or f64 when no operand is a float; two floats must agree; an integer operand converts (lowered through the ordinary RK_CAST). The float operand fixes the width — as With's own `x.min(1)` and Rust's `powi` do — rather than C's tgmath promote-to-double, so `pow(2.0f32, 3)` stays f32. Tests: integer literal, i32, i64, and int-follows-f32 cases added to test/behavior/math_builtins.w; the int-argument error test becomes a non-numeric one. Spec §17.6a and D42 state the rule.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements D42 (ruled 2026-09-16):
cos(x)andx.cos()work forf32andf64with no width in the name, for the full libm surface; the*_f64wrappers instd.mathare deleted; the intrinsic-vs-libm split is hidden.Why
With has no ad-hoc overloading, so
std.mathshippedcos_f64(x)over anextern fn cos(f64). That makes the user spell a type the compiler already reads off the argument — the Go tax (math.Cosisfloat64-only).abs/min/max/mul_addwere already width-generic builtins; only the transcendentals had been left out. Reference check (in.reference/): Zigmath.cos(anytype)over@cos; Rustx.cos()per float type over intrinsics; Swift freecos(_:)per type, withtgmathsplitting LLVM-intrinsic functions from libm-only ones (the split adopted here); Go is thefloat64-only outlier; Vale has onefloatso never faces it.Mechanism — one source of truth
src/MathBuiltins.w: the table (name, arity, LLVM intrinsic base, libm base). Every phase reads it; nothing can drift (the D6 FnAbi rule applied to builtins).math_builtin_calls, so MirLower never re-derives the decision. The call's float type is the float operand's width, orf64when no operand is a float; two floats must agree; an integer operand converts to that type (sqrt(2),sqrt(n),pow(2.0f32, 3)), exactly as an integer converts to a float parameter or binding anywhere in With (f(2),let x: f64 = n— verified). MirLower lowers the conversion through the ordinaryRK_CAST.MirIntrinsic.MATH_FN+ row id at the NK_CALL dispatch, before variant/src()/drop/generic/signature routing can claim the node.llvm.cos.f32/f64where LLVM has an intrinsic, else the width-correct libm symbol (tan/tanf). C backend (--emit-c):cos/cosf.Precedence
A function of the same name defined in scope shadows the builtin (verified: a user
fn cos(i32)wins). Anextern fndeclaration does not: D29 makes every extern globally visible, and the migrator'spub extern fn cos(f64)in the prelude-closurestd.recorpus was ambient in every program —with analyzeshowed a barecos(0.0f32)resolving tolib/std/re/defs.wand returningf64. Forf64the two are the same symbol; only the builtin is correct forf32. Removing the redundant migrator declarations is #1153 (not blocking).Spec wording — for your blessing
§17.6a gains a Floating-point math entry (in this PR,
docs/with-specification.md). Per the D39 precedent the exact words are yours to bless; edit freely.Verification
test/behavior/math_builtins.w: both spellings × both widths × both arities × both tiers, integer operands (literal, i32, i64, and an int following an f32 operand's width), the spiral's nested-math shape, and result-type checks —0 failures.test/compile_errors/err_math_builtin_{mixed_width,non_numeric,arity}.w: the three diagnostics.--emit-cemitscos((1.0)),tanf((1.0)),pow(...),sqrtf(...).build,:fixpoint,:test,:test-green— see checks._f64wrapper; two one-liner docs updated.test/spec/spec_ss16_ffi_and_c_import.wfails on a bootstrap stage1 withstdio.hopaque-struct errors — verified identical on a clean base-commit worktree (pre-existing; the release binary CI tests passes it).Deferred, per ruling
The spiral fixture, the release UAT, and the withlang.org homepage flip to
cos(x)only after a release ships this compiler (the site promises its examples compile on the current release). They are unchanged here and behave identically (their localextern fn cosnow yields to the builtin with the same symbol).