# A new algorithm makes signed 64-bit floor division up to 20% faster

Original title: A new faster algorithm to compute 64-bit signed floor division

> Hacker News story 50256409 · language: en · submitted 2026-10-27
> Web version: https://zeli.app/story/50256409
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 157 · by [benjoffe](<https://news.ycombinator.com/user?id=benjoffe>) · 💬 13
[original](<https://www.benjoffe.com/fast-floordiv>) · [HN discussion](<https://news.ycombinator.com/item?id=50256409>)

## Summary

Floor division rounds toward negative infinity and is often what you want for timestamps and repeating patterns, yet mainstream compilers don't provide it and common implementations are slow. Ben Joffe introduces a new multiply-shift method for compile-time constant divisors that benchmarks 10–20% faster on ARM and 5–10% faster on x86 for most 64-bit divisors. He also notes that Hacker's Delight barely covers the topic and that a prominent library shipped a buggy version for years.

## Key quote

> Floor division can be much faster than you probably realise.

## HN discussion (13 comments)

### [derf_](<https://news.ycombinator.com/item?id=50285708>)

> I love to see this kind of optimization, of course, but there are other cases where Robison division is great, just because it has basically the same form for every divisor:
>
> 1. The divisor is not actually a compile-time constant, but comes from a small set. You can compute a table of corresponding constants and use the same code for every divisor.
>
> 2. You want to do many divisions by many different divisors (whether known at compile-time or drawn from a small set, like above, or pre-computable and applied many times... think of something like a quantization matrix for image coding, for example). Here Robison easily SIMDifies, whereas an algorithm that needs different code paths for every divisor is going to be much more difficult.
>
> Regarding the lack of native signed-unsigned multiplies, as someone who does a lot of multimedia processing, I find the set of available multiplication instructions on x86 and ARM to be rather shamefully limited (especially x86). I recall a colleague who used to optimize for VideoCore (the GPU in a Raspberry Pi) saying that when he first started he was astounded that it had 80 different multiply instructions, but within a year or two he had used basically all of them.

### [rf15](<https://news.ycombinator.com/item?id=50285381>)

> \> As you can see, the technique is so simple that I'd expect that somebody has done this before, but I've looked around and not found it documented anywhere.
>
> I'm one of those people I suppose. Yes, if you want to play with microoptimisations and part of a calculation are known, you can flip a division into a mult (and shift).
>
> This might seem somewhat useless at first, but you can use this to e.g. speed up calculations in nested loops, where you can precompute this for relevant loop-layers.

### [yje07039](<https://news.ycombinator.com/item?id=50286032>)

> I have great respect for those who conduct research on algorithm optimization.

---

## Cite

- Page: https://zeli.app/story/50256409
- Date: 2026-10-27
- Source: benjoffe.com (https://www.benjoffe.com/fast-floordiv)
- Hacker News: https://news.ycombinator.com/item?id=50256409

## Other languages

- zh: https://zeli.app/zh/story/50256409 · markdown: https://zeli.app/zh/story/50256409.md
- ja: https://zeli.app/ja/story/50256409 · markdown: https://zeli.app/ja/story/50256409.md
- ko: https://zeli.app/ko/story/50256409 · markdown: https://zeli.app/ko/story/50256409.md
- de: https://zeli.app/de/story/50256409 · markdown: https://zeli.app/de/story/50256409.md
- es: https://zeli.app/es/story/50256409 · markdown: https://zeli.app/es/story/50256409.md
- ru: https://zeli.app/ru/story/50256409 · markdown: https://zeli.app/ru/story/50256409.md
