Re:How much your computer can do in a second (2015)
by martincmartin on Saturday July 11, 2020 @03:26PM
(#23805035)
I'm surprised gcc can't figure out that this is NUMBER*(NUMBER-1) and eliminate the loop entirely.
[ Parent
| on HN ]
Re:How much your computer can do in a second (2015)
by bigdict on Saturday July 11, 2020 @04:19PM
(#23805552)
> I'm surprised gcc can't figure out that this is NUMBER * (NUMBER-1)
Huh? Do you mean NUMBER instead? If it were s += i then that would be NUMBER * (NUMBER-1) / 2.
Re:How much your computer can do in a second (2015)
by martincmartin on Saturday July 11, 2020 @07:06PM
(#23806977)
You're right, I forgot the / 2.
Re:How much your computer can do in a second (2015)
by zozbot234 on Saturday July 11, 2020 @03:27PM
(#23805042)
It probably can, at high-enough optimization level.
Re:How much your computer can do in a second (2015)
by martincmartin on Saturday July 11, 2020 @03:40PM
(#23805196)
They used -O2, which produces the straight forward loop. [0]
But with -O3, for NUMBER > 51, is uses some other odd loop. [1]
Seems to be O(n) no matter what though.
[0]: https://godbolt.org/z/czcaYf [1]: https://godbolt.org/z/sP194Y
Re:How much your computer can do in a second (2015)
by saagarjha on Sunday July 12, 2020 @03:27AM
(#23809557)
Clang knows what’s up: https://godbolt.org/z/7zKffz. GCC is adding the numbers four at a time by stuffing 0, 1, 2, 3 in an XMM register, taking a packed addition within the register, and then doing a pairwise packed addition of the register with 4, 4, 4, 4 to get the next four numbers.
Re:How much your computer can do in a second (2015)
by jrockway on Saturday July 11, 2020 @03:36PM
(#23805152)
gcc and clang both perform this optimization for me at -O3.
Re:How much your computer can do in a second (2015)
by martincmartin on Saturday July 11, 2020 @03:42PM
(#23805222)
Thanks. On godbolt, I see clang do it at -O3. But gcc still has a loop, where the body has add but no mul, although I don't fully understand the body. Where do you see it in gcc?
Re:How much your computer can do in a second (2015)
by entha_saava on Sunday July 12, 2020 @04:59AM
(#23809973)
Did you mean
s += i;
?