# Beating the compiler at signed 32-bit division

DevFeed: [Beating the compiler at signed 32-bit division](<https://devfeed.tech/articles/beating-the-compiler-at-signed-32-bit-division-110587.md>)

Original publisher: [Read original article](<https://lemire.me/blog/2026/10/31/beating-the-compiler-at-signed-32-bit-division/>)

Author: Daniel Lemire

Published: 2026-10-31T13:05:35Z

Content type: article

Language: en

Sources: [Daniel Lemire](<https://devfeed.tech/sources/daniel-lemire.md>)

Topics: [performance-engineering](<https://devfeed.tech/topics/performance-engineering.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [LLVM](<https://devfeed.tech/topics/llvm.md>), [clang](<https://devfeed.tech/topics/clang.md>), [vectorization techniques](<https://devfeed.tech/topics/vectorization-techniques.md>)

Tags: [32-bit](<https://devfeed.tech/tags/32-bit.md>), [64-bit](<https://devfeed.tech/tags/64-bit.md>), [algorithms](<https://devfeed.tech/tags/algorithms.md>), [apple](<https://devfeed.tech/tags/apple.md>), [arm](<https://devfeed.tech/tags/arm.md>), [arrow](<https://devfeed.tech/tags/arrow.md>), [autovectorization](<https://devfeed.tech/tags/autovectorization.md>), [benchmark](<https://devfeed.tech/tags/benchmark.md>), [branch](<https://devfeed.tech/tags/branch.md>), [c](<https://devfeed.tech/tags/c.md>), [clang](<https://devfeed.tech/tags/clang.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [compilers](<https://devfeed.tech/tags/compilers.md>), [cycles](<https://devfeed.tech/tags/cycles.md>), [gcc](<https://devfeed.tech/tags/gcc.md>), [latency](<https://devfeed.tech/tags/latency.md>), [performance](<https://devfeed.tech/tags/performance.md>), [processor](<https://devfeed.tech/tags/processor.md>), [zen-5](<https://devfeed.tech/tags/zen-5.md>)

## AI overview

A signed 32-bit division optimization can use an unsigned 33-bit reciprocal multiplier on 64-bit processors, avoiding the compiler's correction step when that multiplier does not fit a signed 32-bit value. The article gives a `div32` implementation for positive, non-power-of-two constant divisors and reports lower dependency-chain latency for divisors 7, 60, and 365; throughput gains vary by compiler and processor. For divisor 1000, where the compiler's multiplier fits signed 32 bits, the new method is slightly slower on Apple M4, so the author recommends using it only when correction is needed. For runtime divisors, a revised `fastmod` signed multiplication removes a sign-dependent branch: in the reported random-sign test it is over twice as fast as hardware division on Intel and enables partial loop vectorization on M4 when autovectorization is allowed.

## Source excerpt

Dividing integers is slow on our processors. A 32-bit division costs: processor latency throughput Emerald Rapids about 12 cycles one every 6 cycles Zen 4 9 to 12 cycles one every 6 cycles Zen 5 10 to 13 cycles one every 6 cycles Arrow Lake performance core 12 cycles one every 6 cycles Apple M4 ... Continue reading Beating the compiler at signed 32-bit division