Skip to content

Tests: numeric intrinsics coverage (§17) #511

Closed

Description

@ehartford

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 avoid abs(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 for f32 and f64 only." Maps to LLVM's llvm.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 — int min/max return receiver type; int abs returns unsigned_counterpart(recv) (src/SemaDecl.w:2259-2271, i32→u32 etc.); float min/max/abs/mul_add return receiver type.
  • Codegen: src/CodegenDispatch.w:7198-7297 — float min/max via llvm.minnum.*/llvm.maxnum.* (line 7205), float abs via llvm.fabs.f32/f64 (7241), FMA via llvm.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.24 are all ok|gap in out/req-campaign/g12.tsv).

Defects found while verifying (current stage2 binary):

  1. Unsigned min/max use signed compare. src/CodegenDispatch.w:7229-7235 hard-codes wl_int_slt()/wl_int_sgt(). Verified: let a: u32 = 4294967290; let b: u32 = 5; b.max(a) returns 5 (wrong — 4294967290 is treated as −6). Violates 17.6.2.11/13.
  2. Unsigned abs is not the identity. src/CodegenDispatch.w:7262-7268 does 0 - x + slt-select unconditionally; verified (4294967290u32).abs() != 4294967290u32. Violates 17.6.2.18.
  3. Mixed-type operands accepted. (x: i32).min(y: i64) passes with check — sema never checks the argument type against the receiver. Violates 17.6.2.12 (a rule requirement, so it needs a diagnostic + negative test).

Adjacent constraints for the test author:

Implementation plan

  1. Fix unsigned min/max (src/CodegenDispatch.w:7229-7235): select the predicate with the existing helper Codegen.mir_operand_is_unsigned (src/CodegenDispatch.w:1519-1523, backed by mir_sema_type_is_unsigned): ult/ugt for unsigned receivers, slt/sgt for signed.
  2. 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 for INT_MIN).
  3. Add the same-type rule in sema: where int/float method args are checked for min/max (alongside src/SemaCheck.w:9885-9894 typing), require the argument type to equal the receiver type. Error text: min/max operands must have the same type (mirroring spec wording). mul_add on integers already errors (unknown method 'mul_add' for type 'i32' — verified); keep that behavior and pin it with a test.
  4. 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)==7 via bindings, negatives ((-5).min(2)); unsigned regression: u32 near-max vs small value for both min and max (kills bug 1); a u8/u64 width sample; //! expect-stdout: ok with 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 asserts r != r. Covers 17.6.2.15 (llvm.minnum/maxnum behavior).
  • spec_ss17_6a_abs.w — signed: (-42).abs() == 42u32 and the type is u32 (e.g. assign to let r: u32); i32 INT_MIN case: (-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.0 style, picking constants where the doubly-rounded result differs); f32 variant. Covers 17.6.2.20/21/24.

test/compile_errors/:

  • err_minmax_mixed_types.w — i32 receiver, i64 arg ⇒ //! 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) on i32 ⇒ //! expect-error: unknown method 'mul_add' (pins 17.6.2.22 int rejection).

Float asserts use comparisons only (no float printing) so the suite does not depend on #438.

Acceptance criteria

  • Unsigned min/max return the unsigned-correct operand (regression test passes).
  • Unsigned abs is the identity; signed abs returns the unsigned counterpart, INT_MIN case exact.
  • Mixed-type min/max is a compile error with the documented text; mul_add remains int-rejected.
  • Float min/max NaN semantics and mul_add single-rounding verified by executing tests.
  • Every requirement ID in Traceability is exercised by a named test.
  • with build && with build :fixpoint && with build :test pass; with build :test-green recorded.

Dependencies

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

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    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) selects a (should be b=1) — signed comparison.
    • a.max(b) selects b=1 (should be a) — signed comparison.
    • a.abs() returns 294967296 (should be identity 4000000000).
    • a.min(b_i32) type-checks and returns a value with no same-type error.

    Root causes

    1. min/max use signed icmp — src/CodegenDispatch.w:9738-9741: integer min/max lower to wl_int_slt()/wl_int_sgt() unconditionally. Fix: use wl_int_ult()/wl_int_ugt() when the operand is unsigned. Signedness is already available via Codegen.mir_operand_is_unsigned (src/CodegenDispatch.w:1810) — same pattern the atomic path already uses (AtomicRmwOp.UMIN, :8881).
    2. abs not identity on unsigned — src/CodegenDispatch.w:9767-9772: integer abs always does negate + slt 0 select. Fix: for unsigned operands, return the value unchanged (identity), per spec §17.6a ("For unsigned integers: identity function").
    3. mixed-type min/max accepted — sema at src/SemaCheck.w:15138-15145 types 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 == against uN literals 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.

  2. ehartford commented on Jul 4, 2026

    @ehartford
    ContributorAuthor

    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/ugt for unsigned receivers (u32.min(1)==1), abs is identity for unsigned (§17.6a), and mixed-type u32.min(i32) is now rejected (min/max operands must be the same type) while literals still adapt and as casts are accepted. Coverage: test/spec/spec_ss17_6a_numeric_builtins.w + test/compile_errors/err_min_max_mixed_type.w.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions