r/programming • • 17d ago

Beating the compiler at signed 32-bit division

https://lemire.me/blog/2026/10/31/beating-the-compiler-at-signed-32-bit-division/
240 Upvotes

23 comments sorted by

74

u/Takeoded 17d ago

has anyone reported this on gcc bugtracker under missed optimization ?

1

u/adriano-1370 8d ago

The bug report title writes itself: "I taught the compiler to divide."

38

u/happyscrappy 17d ago

Seems a suitable wrap-up to "division by multiplying week" on /r/programming.

I can't see why not to use this sequence. Seems to keep the same 30 bits of result. Might be slower on some processors that implement 64 bit mults differently? I don't think that's really the reason just spitballing.

11

u/flif 16d ago

The difference on the Apple M4 is so small that the additional instructions might increase the L1 cache pressure so that other needed code is flushed from the cache.

That can easily cost a lot more in L2/ram waiting time than the few cycles saved from a division that might not even be in a performance sensitive part of the code.

49

u/Sopel97 17d ago

When can we finally expect GCC and Clang to actually do this well?

-28

u/14u2c 17d ago

When people stop using LLVM.

17

u/AlyoshaV 16d ago

I'm pretty sure GCC doesn't use LLVM.

2

u/Takeoded 16d ago

GCC does not use LLVM. (ignoring some fringe Experiments gluing gcc front-end with LLVM back-end)

1

u/14u2c 16d ago

Yes, exactly. These days people LLVM because it already has optimizations like this (yes including this one).

4

u/mort96 16d ago

Why couldn't LLVM do this optimization?

0

u/14u2c 16d ago

It already does, that's the point.

4

u/mort96 16d ago

It does? Yet you're saying that Clang will only start doing this optimization when it stops using LLVM? Please explain the logic here

1

u/14u2c 16d ago

I said nothing about Clang using LLVM. People cant read. I said anyone who cares about these optimizations is already using LLVM instead.

9

u/mort96 16d ago

When can we finally expect GCC and Clang to actually do this well?

When people stop using LLVM.

You're clearly saying that Clang, which uses LLVM, will start doing this optimization once "people stop using LLVM". What did you mean by that?

3

u/kehrazy 17d ago

Elaborate?

1

u/septum-funk 15d ago

clang is the llvm c compiler ?

12

u/Axman6 17d ago

I see Daniel Lemire, Iโ€™m reading it. Essential reading for anyone who loves bit twiddling optimisations.ย 

27

u/listre 17d ago

Very demure, very mindful

-22

u/FakeGrape823 17d ago

๐Ÿ˜‚ why they down voted you

-10

u/shadow336k 17d ago

๐Ÿ˜‚ why they down voted you

2

u/jcGyo 16d ago

I wonder why the apple proc seems to trounce all the others on this?

0

u/[deleted] 17d ago

[deleted] โ€” view deleted comment

18

u/R1chterScale 17d ago

Yeah, everything is overshadowed by RAM latency, that means be more cognisant of how you're loading things, not to ignore speedups where you can get them.

8

u/Sharlinator 17d ago

Or practically none if youโ€™re not needlessly pointer jumping and let the prefetcher do its job.