Repository navigation
Description
Tests: numeric intrinsics coverage (§17)
Summary
The §17.6a numeric builtins (min, max, abs, mul_add) are implemented end-to-end (sema typing, MIR intrinsics, LLVM lowering) but have zero direct tests. Writing the tests immediately exposes two real codegen bugs on unsigned integers (verified by execution: min/max use signed comparison; abs is not the identity) and one missing sema rule (mixed-type min/max operands are accepted). This issue is therefore tests plus the three root-cause fixes the tests prove.
Spec
§17.6a Compiler Intrinsics, "Numeric builtins":
a.min(b)/a.max(b)— smaller/larger of two values; "Both operands must be the same type. Defined for all integer and floating-point types. Return type matches the input type." For floats, IEEE 754-2019 minimum/maximum semantics: "NaN is never selected unless both operands are NaN."x.abs()— "For signed integers, the return type is the corresponding unsigned type to avoidabs(INT_MIN)undefined behavior. … For unsigned integers: identity function. For floats: return type is the same float type (clears the sign bit per IEEE 754)."a.mul_add(b, c)— fused multiply-add, "a * b + c as a single floating-point operation with one rounding step. Available forf32andf64only." Maps to LLVM'sllvm.fma.
Current state
Implementation, verified:
- MIR mapping:
src/MirLower.w:5887-5894(TY_INT:min/max/abs;TY_FLOAT:min/max/abs/mul_add) →MirIntrinsic.MIN/MAX/ABS/FMA(src/Mir.w:167-170). - Sema return types:
src/SemaCheck.w:9885-9894— intmin/maxreturn receiver type; intabsreturnsunsigned_counterpart(recv)(src/SemaDecl.w:2259-2271, i32→u32 etc.); floatmin/max/abs/mul_addreturn receiver type. - Codegen:
src/CodegenDispatch.w:7198-7297— float min/max viallvm.minnum.*/llvm.maxnum.*(line 7205), float abs viallvm.fabs.f32/f64(7241), FMA viallvm.fma.f32/f64(7272); integer min/max via icmp+select; integer abs via negate+select. - Coverage: no test in
test/exercises any of these (17.6.2.11–17.6.2.24are allok|gapin out/req-campaign/g12.tsv).
Defects found while verifying (current stage2 binary):
- Unsigned
min/maxuse signed compare.src/CodegenDispatch.w:7229-7235hard-codeswl_int_slt()/wl_int_sgt(). Verified:let a: u32 = 4294967290; let b: u32 = 5; b.max(a)returns5(wrong — 4294967290 is treated as −6). Violates17.6.2.11/13. - Unsigned
absis not the identity.src/CodegenDispatch.w:7262-7268does0 - x+slt-select unconditionally; verified(4294967290u32).abs() != 4294967290u32. Violates17.6.2.18. - Mixed-type operands accepted.
(x: i32).min(y: i64)passeswith check— sema never checks the argument type against the receiver. Violates17.6.2.12(a rule requirement, so it needs a diagnostic + negative test).
Adjacent constraints for the test author:
- Open bug Bug: default float display prints 'inf' for most values (§15.4) #438: default float display prints
inffor most values (verified:print(f"{2.75 as f64}")printsinf). Float tests must assert via comparisons (assert(r == 17.0)), not stdout of floats, to avoid depending on Bug: default float display prints 'inf' for most values (§15.4) #438. - Literal-receiver lexing:
3.min(7)lexes3.as a float prefix and fails to parse; tests should call the methods on bindings or parenthesized literals. - Comptime evaluation of these methods is not implemented (no
"min"/"max"/"abs"/"mul_add"insrc/ComptimeEval.w); §17.6a does not require it, so it is out of scope here (the bit-method analog is Comptime evaluation of bit-manipulation methods (§4.2.4) #422).
Implementation plan
- Fix unsigned min/max (
src/CodegenDispatch.w:7229-7235): select the predicate with the existing helperCodegen.mir_operand_is_unsigned(src/CodegenDispatch.w:1519-1523, backed bymir_sema_type_is_unsigned):ult/ugtfor unsigned receivers,slt/sgtfor signed. - Fix unsigned abs (
src/CodegenDispatch.w:7262-7268): when the operand is unsigned, return it unchanged (identity); keep negate+select for signed (the resulting bit pattern reinterpreted as the unsigned counterpart is correct forINT_MIN). - Add the same-type rule in sema: where int/float method args are checked for
min/max(alongsidesrc/SemaCheck.w:9885-9894typing), require the argument type to equal the receiver type. Error text:min/max operands must have the same type(mirroring spec wording).mul_addon integers already errors (unknown method 'mul_add' for type 'i32'— verified); keep that behavior and pin it with a test. - Tests (below). No rt/Link changes; standard gates.
Test plan
test/spec/ (named per §17.6a):
spec_ss17_6a_minmax_int.w— signed:3.min(7)==3,3.max(7)==7via bindings, negatives ((-5).min(2)); unsigned regression:u32near-max vs small value for bothminandmax(kills bug 1); a u8/u64 width sample;//! expect-stdout: okwith asserts.spec_ss17_6a_minmax_float.w—1.5.min(2.5)==1.5,max==2.5(assert by comparison); NaN semantics:let nan = 0.0/0.0; assert(nan.min(3.0) == 3.0); assert(3.0.max(nan) == 3.0); both-NaN case assertsr != r. Covers17.6.2.15(llvm.minnum/maxnum behavior).spec_ss17_6a_abs.w— signed:(-42).abs() == 42u32and the type isu32(e.g. assign tolet r: u32);i32INT_MINcase:(-2147483648).abs() == 2147483648u32(17.6.2.17, the reason for the unsigned return); unsigned identity (kills bug 2); float:(-3.14).abs() == 3.14, return type same float (17.6.2.19).spec_ss17_6a_mul_add.w—3.0.mul_add(4.0, 5.0) == 17.0; a single-rounding witness where fma differs from separate mul+add (e.g.let a = 1.0 + 2.0e-16; assert(a.mul_add(a, -1.0) != a * a - 1.0style, picking constants where the doubly-rounded result differs); f32 variant. Covers17.6.2.20/21/24.
test/compile_errors/:
err_minmax_mixed_types.w—i32receiver,i64arg ⇒//! expect-error: min/max operands must have the same type(kills gap 3,17.6.2.12).err_mul_add_int.w—x.mul_add(4, 5)oni32⇒//! expect-error: unknown method 'mul_add'(pins17.6.2.22int rejection).
Float asserts use comparisons only (no float printing) so the suite does not depend on #438.
Acceptance criteria
- Unsigned
min/maxreturn the unsigned-correct operand (regression test passes). - Unsigned
absis the identity; signedabsreturns the unsigned counterpart,INT_MINcase exact. - Mixed-type
min/maxis a compile error with the documented text;mul_addremains int-rejected. - Float
min/maxNaN semantics andmul_addsingle-rounding verified by executing tests. - Every requirement ID in Traceability is exercised by a named test.
-
with build && with build :fixpoint && with build :testpass;with build :test-greenrecorded.
Dependencies
- Bug: default float display prints 'inf' for most values (§15.4) #438 (float display prints
inf) — avoided by comparison-style asserts, noted so test failures aren't misattributed; not a blocker. - Comptime evaluation of bit-manipulation methods (§4.2.4) #422 (comptime evaluation of bit-manipulation methods) — sibling scope for comptime evaluation of integer methods; numeric comptime folding intentionally excluded here.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 17.6.2.11, 17.6.2.12, 17.6.2.13, 17.6.2.14, 17.6.2.15, 17.6.2.16, 17.6.2.17, 17.6.2.18, 17.6.2.19, 17.6.2.20, 17.6.2.21, 17.6.2.22, 17.6.2.24.
Activity
ehartford commentedon Jul 3, 2026
Coverage-sweep triage — all three bugs reproduced on HEAD, root-caused to exact lines.
Verified by execution (
u32 a = 4000000000,b = 1):a.min(b)selectsa(should beb=1) — signed comparison.a.max(b)selectsb=1(should bea) — signed comparison.a.abs()returns294967296(should be identity4000000000).a.min(b_i32)type-checks and returns a value with no same-type error.Root causes
src/CodegenDispatch.w:9738-9741: integer min/max lower towl_int_slt()/wl_int_sgt()unconditionally. Fix: usewl_int_ult()/wl_int_ugt()when the operand is unsigned. Signedness is already available viaCodegen.mir_operand_is_unsigned(src/CodegenDispatch.w:1810) — same pattern the atomic path already uses (AtomicRmwOp.UMIN, :8881).src/CodegenDispatch.w:9767-9772: integer abs always does negate +slt 0select. Fix: for unsigned operands, return the value unchanged (identity), per spec §17.6a ("For unsigned integers: identity function").src/SemaCheck.w:15138-15145types min/max/abs/mul_add but never enforces "Both operands must be the same type" (§17.6a). Fix: reject when the two operands differ in type with a diagnostic.Note
Visible verification of the min/max/abs values is currently masked by #639 (unsigned f-string interpolation formats as signed). Until #639 lands, the fixtures for this issue must assert via
==againstuNliterals rather than printing. #639 and this issue are the same "unsigned-awareness in codegen" theme and are best fixed together in one cycle.This issue stays open as a fix (not coverage-only); the tests land with the fixes.
ehartford commentedon Jul 4, 2026
Fixed in 7cb60f7. Build + fixpoint (stage2==stage3) + full test suite green (GATES EXIT: 0). All three codegen/sema bugs fixed plus the tests this issue asked for: min/max use
ult/ugtfor unsigned receivers (u32.min(1)==1), abs is identity for unsigned (§17.6a), and mixed-typeu32.min(i32)is now rejected (min/max operands must be the same type) while literals still adapt andascasts are accepted. Coverage:test/spec/spec_ss17_6a_numeric_builtins.w+test/compile_errors/err_min_max_mixed_type.w.Fix unsigned-integer display + min/max/abs + same-type rule (#511, #639)