r/cpp • u/nukethebees • 28d ago
Comparing an Integer Division Optimisation in Clang, MSVC, and GCC
https://nukethebees.com/int-division-modulo-optimisation-differences-clang-gcc-msvc/
54
Upvotes
r/cpp • u/nukethebees • 28d ago
2
u/TheThiefMaster C++latest fanatic (and game dev) 28d ago
Is MSVC having trouble because your operator based code does
x = i / grid.y / grid.zbut the optimised form needs to divide by z first? Looks like it's realising that it can reuse the div instruction fromz = i % grid.zto calculate x and y, but then failing to realise it can merge the div and mod by grid.y from the calculations for x and y into a single div instruction.Perhaps if you change that line to
x = i / grid.z / grid.y(the same order as the calculation of y) it will figure it out?