r/programming • • Sep 14 '26

How GCC Eliminates Unnecessary Integer Division

https://leetarxiv.substack.com/p/how-gcc-eliminates-unnecessary-integer
253 Upvotes

52 comments sorted by

View all comments

21

u/LonelyAndroid11942 Sep 14 '26 edited Sep 14 '26

So is the purpose of this to reduce the difficulties that come with float precision?

EDIT: The comments are informative, if they’re not made by jerks. I clearly have much to learn. I appreciate the patience of those who have it, and wish a very strong “go step on some legos” to those who do not.

85

u/ExtraGoated Sep 14 '26

my understanding is that it replaces an expensive integer division with an equivalent cheap imul and cmp instructions

40

u/Jhavul Sep 14 '26 edited Sep 15 '26

yeah, when division is 80 cycles and multiplications are 1, you get insane performance gains even if it takes 20 multiplications to make the magic numbers

e: sorry apparently I should specify, 80 cycles idiv is for hardware >4 years old, it's closer to 20 cycles on modern hardware, and multiplication can take up to 3 cycles. so in the very worst case, it's a 3x speedup if you can swap one idiv for two imul, if you never do sequential operations, and have the newest CPU possible. it gets way better in every real life scenario.

11

u/QuestionableEthics42 Sep 14 '26

It's not 80 not even close. And some architectures have apparently got it down to near imul speeds. imul is also 1-3, not always 1.

32

u/ascii Sep 14 '26

Integer division is still not pipelined and 64 bit division takes ~80 cycles on Skylake, which is still a very common architecture out there in the real world. Skylake derived CPUs only stopped being manufactured at the end of 2024.

That said, Arrow lake and Zen 5 idiv use partial pipelining (new instruction every 4-6 cycles) and have cut latencies down to 10-15 cycles.

imul obviously does not take only one cycle, but because it's fully pipelined, it effectively takes one cycle in the right context.

Overall, parent isn't too far off.

-7

u/QuestionableEthics42 Sep 14 '26

80 is on the bad end. Apparently it's 20-80 cycles, from some quick googling on my phone. And they put the cycles for imul on the good end, so not a fair comparison. (Though it's still a huge difference, just not quite as exaggerated as they made it sound)

→ More replies (7)

1

u/pheonixblade9 Sep 15 '26

addressing mode can also make a big difference, as well as cache locality and pipelining.

37

u/DuploJamaal Sep 14 '26

The purpose is that multiplication is a lot faster than division.

Bit shifts are even faster. Shifting to the right once is like dividing by two, twice like division by four,...

So instead of doing an expensive division it does a multiplication followed by a bit shift to get the same result.

So instead of 20 CPU cycles it will then just be 5

12

u/LonelyAndroid11942 Sep 14 '26

I appreciate the explanation! I work with software for a living, but getting a peek under the hood and some deep info is fantastic. Cheers!

3

u/watchpigsfly Sep 15 '26

Check out godbolt.org

Learning to use a debugger properly will change the way you think about your code and it’s easier than you think

9

u/Sopel97 Sep 14 '26

? there is no floating point math here

-22

u/LonelyAndroid11942 Sep 14 '26 edited Sep 14 '26

Division produces floats. At least at the high level I work at.

EDIT: wow, I didn’t know this sub was fully populated by the same bullies that obsessively stalked StackOverflow and made fun of people for asking a question. Heaven forbid someone doesn’t know something. Y’all could, I dunno, take a second to help someone learn? Instead of being dicks?

Fuck ever commenting in this sub again, I guess.

19

u/QuaternionsRoll Sep 14 '26

Integer division refers to truncated division. Some languages elect to define division operator(s) in other ways, but that is no longer truncated division.

5

u/LonelyAndroid11942 Sep 14 '26

Today I learned. That makes it a good day.

23

u/cthulu0 Sep 14 '26

Integer division, which is what is this article is about, doens't produce floats:

regular division: 1/3.0 = 0.333……

integer division: 1/3 = 0

5

u/UloPe Sep 15 '26

You didn’t ask though.

You made a claim of fact that’s not true.

→ More replies (1)

5

u/Successful-Money4995 Sep 14 '26

Actually...!

Floating point math is perfectly "precise". If you preform the same floating point math twice in a row, you will get exactly the same answer.

Where it fails is in accuracy. The result of floating point math may not be correct. It might be very very close.

Also, the claim of precision assumes understanding that floating point operations are not associativity.

4

u/max123246 Sep 15 '26

The issue with floating point is that (a + b) + c != a + (b + c) in all cases due to that accuracy issue you mentioned. So the order of your operations matters a lot, which can be difficult to guarantee given CPU pipelining, multittheading, GPUs, etc. By default compilers will use slower but deterministic floating point operation orders to make it at least always give you the same answer

5

u/meneldal2 Sep 15 '26

Afaik the CPU is not allowed to reorder floating point ops, but the compiler can (depending on the options you give it).

4

u/ygra Sep 15 '26

What was it? -funsafe-math-optimizations are neither fun, nor safe :)

-1

u/LonelyAndroid11942 Sep 14 '26

Deterministic operations that are mathematically imprecise don’t quite fit the bill of precision in my mind. But then, I’ve been doing math in JavaScript for so long that I’m very used to the 0.000000000068% chance of a float precision error actually causing a problem.

5

u/Potterrrrrrrr Sep 15 '26

There’s nothing more precise than JavaScript maths, I use it to prove that 0.2 + 0.1 != 0.3 all the time

-2

u/LonelyAndroid11942 Sep 15 '26

This person gets my joke. I like this person.

4

u/metahivemind Sep 15 '26 edited Sep 16 '26

[removed] — view removed comment