← Back to Pull requests

Divide without normalization when the quotient is a single word #377

Closed
unknown closed this pull request without merging on Oct 06, 2026 11:05 UTC
View on GitHub
unknown unknown commented on Oct 03, 2026 11:58 UTC
Author

When the numerator and the divisor have the same size and the top word of the divisor is not zero, the quotient fits in a single word. Estimate it by dividing the top 2 words of the normalized numerator by the top word of the normalized divisor (2/1 division, at most 2 too large) and compute the remainder with submul() on the original operands, correcting the quotient 0–2 times. This skips normalizing the whole operands and the Knuth division. This is the "small quotient" case of GMP's mpn_tdiv_qr().

  • For equal sizes the top divisor word is checked first and the other divisor words are counted from the next word down.

  • For equal sizes the Knuth division then gets at most num_words - 1 divisor words, so it is not instantiated for udivrem<192, 192> (extends #381).

  • The tests are in #378.
  • Earlier revisions had a GCC fshr() workaround (superseded by #379) and an INTX_ASSUME bound on the Knuth divisor word count (dropped: no effect with clang, mixed results with GCC).

    Instructions per operation

    Average dynamic instruction counts per call, 64K random inputs, clang 23 and GCC 16 at -O2, -O3 and -O2 -march=x86-64-v3, compared with main after #382:

    | case | clang | GCC |
    |---|---|---|
    | uint256 / full-width divisor | −40 … −160 | −55 … −176 |
    | uint512 / full-width divisor | −49 … −227 | −76 … −324 |
    | uint192 / full-width divisor | −27 … −155 | −48 … −135 |
    | uint256 / 3-word divisor | +2 | −13 … −17 |
    | to_string() uint256 | −20 … −145 | +11 … +270 |

    Other sizes (mulmod, addmod, 512/256, 320/256) are not affected.

    Regressions:

  • clang, uint256 with a 3-word divisor: +2. This is the extra top-word check.

  • clang -O3, uint192 with a 2-word divisor: +1 … +2.

  • GCC, uint256 with a 1- or 2-word divisor: +1 … +6.2, and so to_string(): +11 … +270 (most at -O2 and -march=x86-64-v3). GCC inlines udivrem_top1() into udivrem() and the larger function costs a few instructions on the other paths: with udivrem_top1() not inlined most of this goes away.

  • GCC, uint192 with a 2-word divisor: +5 … +38. Without the Knuth division GCC does not inline udivrem_by2() into udivrem<192, 192> anymore.

  • GCC -O3, uint512 with 1- to 6-word divisors: +1 … +3.5.
  • Together with #382, compared with main, to_string() is −50 … −348 instructions with clang and −29 … −726 with GCC.

    Code size

    clang: udivrem() instantiations are the same size or smaller (udivrem<192, 192> 204 → 137 instructions); clang calls udivrem_top1() out of line. GCC: +100 … +165 instructions per udivrem() instantiation because it inlines udivrem_top1() with its submul() and add() loops. GCC no longer emits the out-of-line udivrem_knuth() (1081 bytes at -O2) for code dividing only uint<192> by uint<192>.

    Coverage

    The changed lines have 100% coverage with clang -fcoverage-mcdc and GCC -fcondition-coverage.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01LYMd9JA9ad8WxPeZGwwzcS

    Diff: 375 lines (+281 / -94) across 4 files Reviewer: chfast Review duration: 4266m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 375 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.355
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #377"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.9% (MEDIUM) · Higher = larger blast radius
    42.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.9% · Safe to merge
    16.9%
    Repository