A new faster algorithm to compute 64-bit signed floor division
Web page · https://benjoffe.com/fast-floordiv
First seen by Oriel on .
AI summary
This article presents a new compile-time algorithm for signed 64-bit floor division by constants, reporting 10–20% faster ARM throughput and 5–10% faster x86 throughput for many divisors.
- Floor division rounds toward −∞, avoiding incorrect timestamp, angle, and tile calculations produced by standard truncating division for negative inputs.
- The proposed function uses a sign-adjusted magic multiplier and wide multiply-shift, with generated implementations available for 64-bit, 32-bit, 16-bit, and 8-bit widths.
- Benchmarks report faster throughput than existing algorithms on most ARM divisors and around 80% of positive x86 divisors, while remaining x86 cases are slower.
- The article compares common division, Granlund-Montgomery, positive-divisor XOR, and Robison multiply-add techniques, including their instruction-level trade-offs.
- A C++ generator, boundary tests covering 65,535 divisors, and reusable templates support developers implementing constant-divisor floor, positive-modulus, or Euclidean division.
#integer division #c++ #compiler optimization #low-level arithmetic
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.
- Hacker News: 157, latest observation ; +152 over observed period since
- Lobsters: 47, latest observation ; +45 over observed period since
Comments
Comment count reported by the source.
- Hacker News: 13, latest observation ; +13 over observed period since
- Lobsters: 7, latest observation ; +7 over observed period since
Aliases
| Source | External ID | URL |
|---|---|---|
| Hacker News | 50256409 | https://www.benjoffe.com/fast-floordiv Read discussion |
| Lobsters | o5to4p | https://www.benjoffe.com/fast-floordiv Read discussion |