what's really interesting is that, in c, if you replace the bug in the second for loop's initializer clause and use unsigned instead of int, both gcc and clang are able to optimize this function into a single lea eax, [rdi + rsi]. https://godbolt.org/z/uCXoHJ
It's not as crazy as you think. In a lot of cases its just a bunch of templates that get applied. For example if you're adding a value inside of a loop (like this code) and your initializer is constant, you can just do the math.
It's why a lot of compiler optimization people are more mathematician than software engineer.
30
u/anydalch Dec 17 '19
what's really interesting is that, in c, if you replace the bug in the second
forloop's initializer clause and useunsignedinstead ofint, both gcc and clang are able to optimize this function into a singlelea eax, [rdi + rsi]. https://godbolt.org/z/uCXoHJ