Table of Contents
Fetching ...

Classic Round-Up Variant of Fast Unsigned Division by Constants: Algorithm and Full Proof

Yifei Li

TL;DR

This work addresses fast unsigned integer division by a constant divisor $d>0$ by elaborating the classic round-up variant that replaces division with a sequence of multiplications and shifts. It defines an $N$-bit precomputation using $p = \lceil \log_2 d \rceil$, $m = \lceil 2^{N+p}/d \rceil$, and $m_{lo} = m \& (2^{N}-1)$, yielding $q = \left\lfloor (m_{lo} n)/2^{N} \right\rfloor$ and the quotient $t = \left\lfloor (n+q)/2^{p} \right\rfloor$; an equivalent runtime form uses $t = \left\lfloor ((n-q)/2^{h}) \right\rfloor + q$ followed by a final shift, with $h = \min(p,1)$ to handle the $d=1$ edge case. The paper provides full correctness and feasibility proofs, including the relationship to a full magic number $m = \lceil 2^{N+p}/d \rceil$ and the lower $N$-bit part $m_{lo}$, as well as a no-overflow reformulation and a faster variant for bounded dividends (e.g., $n < 2^{N-1}$). These results formalize and close gaps in prior treatments, offering a division-by-constants technique that is both provably correct and practical for compilers and heterogeneous computing where magic-number computation may occur at runtime. Overall, the approach enables efficient, pipeline-friendly unsigned division using multiplication and shifts with explicit overflow considerations.

Abstract

Integer division instruction is generally expensive in most architectures. If the divisor is constant, the division can be transformed into combinations of several inexpensive integer instructions. This article discusses the classic round-up variant of the fast unsigned division by constants algorithm, and provides full proof of its correctness and feasibility. Additionally, a simpler variant for bounded dividends is presented.

Classic Round-Up Variant of Fast Unsigned Division by Constants: Algorithm and Full Proof

TL;DR

This work addresses fast unsigned integer division by a constant divisor by elaborating the classic round-up variant that replaces division with a sequence of multiplications and shifts. It defines an -bit precomputation using , , and , yielding and the quotient ; an equivalent runtime form uses followed by a final shift, with to handle the edge case. The paper provides full correctness and feasibility proofs, including the relationship to a full magic number and the lower -bit part , as well as a no-overflow reformulation and a faster variant for bounded dividends (e.g., ). These results formalize and close gaps in prior treatments, offering a division-by-constants technique that is both provably correct and practical for compilers and heterogeneous computing where magic-number computation may occur at runtime. Overall, the approach enables efficient, pipeline-friendly unsigned division using multiplication and shifts with explicit overflow considerations.

Abstract

Integer division instruction is generally expensive in most architectures. If the divisor is constant, the division can be transformed into combinations of several inexpensive integer instructions. This article discusses the classic round-up variant of the fast unsigned division by constants algorithm, and provides full proof of its correctness and feasibility. Additionally, a simpler variant for bounded dividends is presented.

Paper Structure

This paper contains 11 sections, 9 theorems, 39 equations, 3 algorithms.

Key Result

Theorem 4.1

Given $d \in\mathbb{U}_{N}^+$ as divisor, and $n\in\mathbb{U}_{N}$ as dividend, let where $m_{lo}$ is the magic number. It satisfies that where $p$ is the shift offset.

Theorems & Definitions (27)

  • Definition 4.1
  • Definition 4.2: shift offset
  • Definition 4.3: magic number
  • Theorem 4.1: \ref{['alg:faster-runtime']} correctness
  • Proposition 4.1
  • Proposition 4.2: partial feasibility
  • Corollary 4.1.1: \ref{['alg:runtime']} correctness
  • Remark 4.1
  • Proposition 4.3: full feasibility
  • Definition 5.1: full magic number
  • ...and 17 more