Skip to content

length-comparison

Status: stub. The full-length analysis is queued for a v1.0.x patch release per ADR 0018, section 5, criterion #6. The companion rule page at docs/rules/length-comparison.md contains the canonical detection logic + GPU reasoning.

TL;DR

length(v) computes sqrt(dot(v, v)). On AMD RDNA/RDNA 2/RDNA 3, NVIDIA Turing/Ada Lovelace, and Intel Xe-HPG, sqrt is a quarter-rate transcendental instruction (v_sqrt_f32 on RDNA). Comparing length(v) < r introduces a sqrt whose only purpose is to produce a value that is immediately compared against r. Since sqrt is monotonically increasing for non-negative inputs, the comparison length(v) < r is equivalent to dot(v, v) < r * r for any r >= 0. The squared form eliminates the sqrt entirely, replacing a quarter-rate transcendental with two full-rate FP32 multiply-adds.

What the rule fires on

A relational comparison of the form length(v) < r, length(v) > r, length(v) <= r, or length(v) >= r where r is any scalar expression. The rule also matches distance(a, b) < r and related forms (which lower to length(a - b) < r). It does not fire when length appears in an arithmetic expression rather than a direct comparison, or when the comparison involves expressions that are not a single scalar radius (e.g., length(v) < f(x) is still matched, but requires the radius expression to be trivially non-negative for the squared form to be safe; the tool emits a note when it cannot verify this).

See the What it detects section of the rule page for the full pattern definition.

Why it matters

The full GPU-mechanism analysis lives in the Why it matters on a GPU section of the companion rule page.

Examples

The bad / good code snippets are kept canonical on the rule page; see length-comparison.md -> Examples.

See also


This is a v1.0-ship stub. Full analysis pending; track issue link TBD.

© 2026 NelCit — Apache-2.0 (code), CC-BY-4.0 (docs).