Skip to content

[fft] Simplify series operand access: direct indexing, span conversion, zero-pad-tolerant extend_to - #79

Merged
ecnerwala merged 9 commits into
masterfrom
devin/1785327766-series-span-cleanup
Jul 31, 2026
Merged

[fft] Simplify series operand access: direct indexing, span conversion, zero-pad-tolerant extend_to#79
ecnerwala merged 9 commits into
masterfrom
devin/1785327766-series-span-cleanup

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Removes the recurring rough edges in the series:: layer (now includes the operator normalization originally split out as #80):

  1. underlying() is gone. The like contract is now: direct indexing plus two span borrows — into the engine primitives and into the series layer's own exactness-tagged span:

    concept like = ... requires(const S& s, int i) {
        { s[i] } -> convertible_to<const value_type&>;
        { std::span<const value_type>(s) };                          // borrows into engine ops
        requires convertible_to<const S&, span<E, S::exact_v>>;      // borrows into the series layer
    };

    Each wrapper provides operator span<E, exact_v> (vec already had it; cached/cached_span/prefix_cached gain it), and vec gains explicit vec(span<E, exact_>) so materializing an owned copy of any series-like is just exact<E> r(c). std::span<const T> borrows go through std::span's range constructor (deliberately no conversion operator on series::span — offering both paths makes every implicit conversion ambiguous under -Wconversion). cached/prefix_cached keep a non-contract uncached() unwrapper for the by-reference case (subproduct_tree::rev_prod). At the sites: q.underlying()[0]q[0], span<E, false> a = a_.underlying();span<E, false> a = a_;.

  2. The sz(coeffs) zero-padding footgun. extend_to's doubling loop clamps each step's read to the coefficients that fit:

    while (t.size() < m) {
        int s = t.size();
        t.v.resize(2 * s);
        core::extend(t.v, coeffs.first(min(sz(coeffs), 2 * s)));
    }

    By the prefix contract, a size-s transform can only exist if all nonzero coefficients fit in 2s — so anything past the clamp is necessarily zero and dropping it is exact (no value inspection, no float-equality trimming). The top-level sz(coeffs) <= 2 * m assert is the one conservative check kept. The old "must extend_to before padding" ordering constraint in kth_term_of_rational_function is gone, and a cache seeded from short coeffs can later be grown with a longer zero-padded buffer of the same sequence.

  3. Series operators normalized onto cached_span (folded from [fft] Normalize series operators onto cached_span operands #80). detail::whole_operand is the whole-span counterpart to product_operand: any like operand becomes a cached_span (borrowed coefficients + the cache serving them):

    template <like S>
    cached_span<E, S::exact_v> whole_operand(const S& s, fft::transformed<E>& tmp) {
        return {s, whole_cache_or(s, tmp)};
    }

    square/multiply_add2/middle_product and kth_term_of_linear_recurrence run on that form (auto av = detail::whole_operand(a, ta_); then av, av.cache() straight into the fft:: entry points); operator*'s call sites, operator+/operator-, ps_inv, ps_log's assert, and cached::operator== drop their coefficient plumbing. Internals uniformly use series::span, not std::span, for coefficient views.

No algorithmic or semantic changes: cache selection, precisions, and transform sizes are identical throughout.

Testing

  • Full audit of all fft code (all engines' extend_to/transform/finish/downsample/negate_arg against the clamped-prefix contract; series operator cache pairings; poly.hpp/online.hpp call sites) — one issue found and fixed: the dual span-conversion ambiguity above (it produced -Wconversion warnings at every implicit borrow).
  • Full unit suite green (2822854 assertions, 106 cases, all engines) after the underlying() removal.
  • Full Library Checker verification: every fft-related problem AC under both g++ and g++-sanitizer environments (convolutions incl. crt/split, all FPS ops, composition, multipoint/interpolation, characteristic polynomial, kth_term_of_linearly_recurrent_sequence 20/20); kth_term/multipoint/interpolation re-verified after the contract change.
  • Padded-extend harness: seed a transform from unpadded coeffs, extend with a longer zero-padded buffer, compare finish(sq(...)) against a fresh transform — exact agreement (err = 0) across ntt/split/real for lengths {1,2,3,5} × seeds {2,4} × targets {8,16} × paddings.
  • Real-engine length-1 extend_to verified bit-identical to fresh transforms across seed/target sizes.

Stacked follow-up: #81 (storage-separation prototype).

Link to Devin session: https://app.devin.ai/sessions/66a2d877f2354e30b3fdfa0c15061db2
Requested by: @ecnerwala


Open in Devin Review (Beta)

…n, zero-pad-tolerant extend_to

Add operator[] and implicit std::span<const T> conversion to the series-like
contract so operands borrow straight into the engine primitives; drop the
scattered underlying()/.coeffs() at the kth_term call sites.

Make extend_to ignore trailing zero coefficients (via fft::trim_zeros), so
zero-padded buffers are accepted and the "extend_to before padding" ordering
constraint in kth_term_of_rational_function goes away.

Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
@ecnerwala ecnerwala self-assigned this Jul 29, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown

GCC Code Coverage Report

📂 Overall coverage

Metric Coverage
Lines 🟡 7909/9647 (82.0%)
Functions 🟢 1063/1167 (91.1%)
Branches 🟡 6287/8016 (78.4%)

@beta-devin-ai-integration beta-devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Open in Devin Review (Beta)

Comment thread src/fft/series.hpp Outdated
Comment on lines +343 to +344
// Seed the loop transforms from any whole caches; the buffers below hold the
// current p, q (zero-padded, which extend_to ignores).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 New comment packs two clauses onto one line, against the repository's comment formatting rule

The newly added comment above the transform seeding puts two clauses on the same line and wraps mid-clause (// Seed the loop transforms from any whole caches; the buffers below hold the at src/fft/series.hpp:343-344), so it does not follow the repository's documented comment layout.
Impact: Comment formatting diverges from the project's stated style, making the comment harder to edit line-by-line.

Rule in AGENTS.md: one sentence/clause per comment line

AGENTS.md ("Comment style") requires: "Start each sentence/clause on a new line — this makes comments easier to read and edit in a line-based editor. Don't rewrap them into paragraphs." The added comment instead joins two independent clauses with ; on the first line and continues the second clause onto the next line.

Suggested change
// Seed the loop transforms from any whole caches; the buffers below hold the
// current p, q (zero-padded, which extend_to ignores).
// Seed the loop transforms from any whole caches;
// the buffers below hold the current p, q (zero-padded, which extend_to ignores).
Open in Devin Review (Beta)

Was this helpful? React with 👍 or 👎 to provide feedback.

devin-ai-integration Bot and others added 2 commits July 29, 2026 12:44
Each doubling step reads only coeffs.first(2 * t.size()): by the prefix
contract, coefficients past twice the existing transform's size must be zero,
so zero-padded buffers work without inspecting values. Removes fft::trim_zeros
and the floating-point equality workaround it needed.

Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
Add detail::whole_operand, the whole-span counterpart to product_operand:
any series-like operand becomes a cached_span (borrowed coefficients + the
cache serving them). square/multiply_add2/middle_product and
kth_term_of_linear_recurrence run on that form instead of hand-pairing
underlying() spans with whole_cache_or, and call sites lean on the implicit
std::span conversion instead of .coeffs().

Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
devin-ai-integration Bot and others added 2 commits July 29, 2026 19:58
Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
devin-ai-integration Bot and others added 4 commits July 29, 2026 22:07
Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
…s via a span constructor

Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
…poses prod() nodes

Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
Co-Authored-By: Andrew He <he.andrew.mail@gmail.com>
@ecnerwala
ecnerwala merged commit 09c867a into master Jul 31, 2026
17 checks passed
@ecnerwala
ecnerwala deleted the devin/1785327766-series-span-cleanup branch July 31, 2026 04:24
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