C.9-10Vector Examples II: strcmp & Fractional LMUL

Part III Linux boot: optional Vol. I (Unprivileged) pp. 658–662 · ~2 min read

Two examples that show the vector ISA’s subtler features earning their keep: fault-only-first for unknown-length data, and fractional LMUL for register-pressure relief.

strcmp — the data-dependent loop

strcmp:
    li   t1, 0
loop:
    vsetvli t0, x0, e8, m2, ta, ma   # max-length byte strip
    add  a0, a0, t1
    vle8ff.v v8,  (a0)               # FOF: safe load past unknown end
    add  a1, a1, t1
    vle8ff.v v16, (a1)
    vmseq.vi v0, v8, 0               # NUL in src1?
    vmsne.vv v1, v8, v16             # bytes differ?
    vmor.mm  v0, v0, v1              # either = exit
    vfirst.m a2, v0                  # first exit position (-1 = none)
    csrr t1, vl                      # bytes actually fetched
    bltz a2, loop                    # no exit → next strip
    ...                              # compare the differing byte, return

The whole point is vle8ff.v: strcmp can’t know the string length, so a fixed-width load could fault reading past the end into an unmapped page. Fault-only-first loads as many bytes as are safely accessible, truncating vl at the fault boundary — then the mask logic (vmseq/vmsne/vmor + vfirst) finds the first NUL-or-mismatch across the whole strip in parallel.

Fractional LMUL — register pressure

The C.10 loop mixes one signed char array with a dozen long arrays. Holding SEW/LMUL constant, the byte data rides at a fractional LMUL (occupying a fraction of one register) while each long operand gets a full register. Without fractional LMUL, the narrowest type would force LMUL=1 and the wide types would each need multiple registers — exhausting the 32-register file. Fractional LMUL parks narrow data compactly so all the wide operands stay register-resident.

Hardware Designer Notes

strcmp is the single best stress test for a vector LSU’s fault-only-first path — the fault-truncation logic is subtle and this example exercises it directly. If your core handles strcmp correctly across page boundaries, the hardest FOF corner is covered. That closes the vector examples; the remaining appendices cover the vector calling convention (placeholder), bitmanip examples, and extension rationale.

Minimal Linux-boot hart MUST

  • Nothing new — these exercise FOF loads and fractional-LMUL configurations already specified; they are correctness targets

MAY simplify / trap-and-emulate

  • Use strcmp as the FOF-load conformance test (the element-0-faults / element-k-truncates behavior) and C.10 as the fractional-LMUL register-allocation test

Check yourself — strcmp & fractional LMUL

1.The strcmp example uses vle8ff.v (fault-only-first). Why is that essential for a string function?

2.The fractional-LMUL example has one byte-array operation and many long-array operations in a loop. How does fractional LMUL help?

2 questions