Skip to content

FastFourierTransform::Log2 is recursive #260

Description

@gabrielfrasantos

Severity: low
Domain: analysis
Status: VERIFIED — read against a210d34 on 2026-08-10
Suggested labels: policy-violation, analysis

Summary

Log2() calls itself, violating the no-recursion rule in AGENTS.md. It is also a
non-static member function, so it cannot be used in a constant expression without an object.

Location

numerical/analysis/FastFourierTransform.hpp

Evidence

constexpr std::size_t Log2(std::size_t n)
{
    return (n <= 1) ? 0 : 1 + Log2(n >> 1);
}

Why it matters

Deterministic stack usage is a hard requirement for the target platforms. Although the depth here is
bounded by log₂(SIZE_MAX) and the compiler will usually fold it at -O3, the rule exists so that
stack bounds are provable without relying on optimiser behaviour.

Suggested fix

Iterative form, and make it static:

static constexpr std::size_t Log2(std::size_t n) noexcept
{
    std::size_t result{ 0 };
    while (n > 1)
    {
        n >>= 1;
        ++result;
    }
    return result;
}

Or use std::bit_width(n) - 1 from <bit> (C++20) for power-of-two inputs.

Notes

Consider a repository-wide grep for self-recursive functions as part of the same change.

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