Oriel — AI & technology worth noticing, every day.

Beating the compiler at signed 32-bit division

Web page · https://lemire.me/blog/2026/10/31/beating-the-compiler-at-signed-32-bit-division

First seen by Oriel on .

AI summary

The article exploits extra range when dividing signed 32-bit integers on 64-bit processors, shortening constant division and making fastmod 0.2.0 branchless for runtime divisors.

  • Among divisors from 3 through 1000 excluding powers of two, 376 of 990 require the compiler’s multiply-and-add correction because the ideal multiplier needs 33 bits.
  • The div32 template uses a 33-bit multiplier in signed 64-bit arithmetic, followed by a shift and sign-based rounding; it assumes a positive, non-power-of-two divisor.
  • For divisors 7, 60, and 365, Intel latency fell from roughly 10 cycles to 8, while Apple M4 latency improved by 12%.
  • For runtime divisor 7 on Intel, fastmod 0.2.0 achieved about 0.78 ns per division, versus 1.72 ns for division and over 3 ns previously.
  • The optimization should only replace compiler output requiring correction; for divisor 1000 on Apple M4, div32 was slower because it needed a separate sxtw instruction.

#integer division #compiler optimization #fastmod #cpu performance

Generated by gpt-5.6-sol. Check the original for details.

Metric trends

Last 30 days · Up to 120 recorded samples per source and metric, with reposts kept separate · First and latest retained

Points

Score reported by the source.

  • Lobsters: 13, latest observation ; +12 over observed period since

Comments

Comment count reported by the source.

  • Lobsters: 5, latest observation ; +5 over observed period since

Aliases

SourceExternal IDURL
Lobstersep5vsqhttps://lemire.me/blog/2026/10/31/beating-the-compiler-at-signed-32-bit-division/ Read discussion