Skip to content

Floating-point math functions are width-generic builtins: cos(x) and x.cos() for f32/f64, no width suffix (D42) - #1154

Merged
ehartford merged 2 commits into
mainfrom
math-builtins
Sep 16, 2026
Merged

ehartford merged 2 commits into
mainfrom
math-builtins

Conversation

@ehartford

@ehartford ehartford commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Implements D42 (ruled 2026-09-16): cos(x) and x.cos() 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.

Why

With has no ad-hoc overloading, so std.math shipped cos_f64(x) over an extern fn cos(f64). That makes the user spell a type the compiler already reads off the argument — the Go tax (math.Cos is float64-only). abs/min/max/mul_add were already width-generic builtins; only the transcendentals had been left out. Reference check (in .reference/): Zig math.cos(anytype) over @cos; Rust x.cos() per float type over intrinsics; Swift free cos(_:) per type, with tgmath splitting LLVM-intrinsic functions from libm-only ones (the split adopted here); Go is the float64-only outlier; Vale has one float so 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).
  • Sema types both spellings and records free calls in math_builtin_calls, so MirLower never re-derives the decision. 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 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 ordinary RK_CAST.
  • MirLower tags both spellings MirIntrinsic.MATH_FN + row id at the NK_CALL dispatch, before variant/src()/drop/generic/signature routing 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). 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). An extern fn declaration does not: D29 makes every extern globally visible, and the migrator's pub extern fn cos(f64) in the prelude-closure std.re corpus was ambient in every program — with analyze showed a bare cos(0.0f32) resolving to lib/std/re/defs.w and returning f64. For f64 the two are the same symbol; only the builtin is correct for f32. 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-c emits cos((1.0)), tanf((1.0)), pow(...), sqrtf(...).
  • Battery (seed-driven, pinned seed): build, :fixpoint, :test, :test-green — see checks.
  • Tree sweep: no caller of any deleted _f64 wrapper; two one-liner docs updated. test/spec/spec_ss16_ffi_and_c_import.w fails on a bootstrap stage1 with stdio.h opaque-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 local extern fn cos now yields to the builtin with the same symbol).

Eric Hartford 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.
@ehartford
ehartford merged commit 0a0ff13 into main Sep 16, 2026
5 checks passed
@ehartford
ehartford deleted the math-builtins branch September 19, 2026 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant