r/programming • • 27d ago

Beating the compiler at signed 32-bit division

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

23 comments sorted by

74

u/Takeoded 27d ago

has anyone reported this on gcc bugtracker under missed optimization ?

1

u/adriano-1370 18d ago

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

38

u/happyscrappy 27d 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 26d 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 27d ago

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

-28

u/14u2c 27d ago

When people stop using LLVM.

17

u/AlyoshaV 26d ago

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

2

u/Takeoded 26d ago

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

1

u/14u2c 26d ago

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

4

u/mort96 26d ago

Why couldn't LLVM do this optimization?

0

u/14u2c 26d ago

It already does, that's the point.

4

u/mort96 26d 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 26d 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 26d 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 27d ago

Elaborate?

1

u/septum-funk 25d ago

clang is the llvm c compiler ?

13

u/Axman6 27d ago

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

28

u/listre 27d ago

Very demure, very mindful

-20

u/FakeGrape823 27d ago

๐Ÿ˜‚ why they down voted you

-8

u/shadow336k 27d ago

๐Ÿ˜‚ why they down voted you

2

u/jcGyo 26d ago

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

0

u/[deleted] 27d ago

[deleted] โ€” view deleted comment

19

u/R1chterScale 27d 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.

9

u/Sharlinator 27d ago

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