News for hackers. Stuff that matters.
Re:Compiler Optimizations are Awesome by Sean1708 on Thursday June 01, 2017 @09:36AM (#14460084)
As a very simple example see if you can spot the loop in the optimised version of a sum to n function[0] (hint: you can't). I don't know to what extent this scales up to more complex programs, but it is at least possible in principle.

[0]: https://godbolt.org/g/dRUYA3

Re:Compiler Optimizations are Awesome by wolfgke on Thursday June 01, 2017 @12:33PM (#14461783)
It does not work very well:

> https://godbolt.org/g/qmQhLK

Exercise: Write an assembly function that is faster than "-O3" and loop-free. Should be easy.

Re:Compiler Optimizations are Awesome by wolfgke on Thursday June 01, 2017 @06:27PM (#14465483)
UPDATE: For those who don't want to do the exercise: The Intel compiler (icc) optimizes it in about the way that I would if I were to write it in assembly:

> https://godbolt.org/g/LntTnY

Re:Compiler Optimizations are Awesome by AstralStorm on Friday June 02, 2017 @03:20AM (#14468104)
Which is slower on non-Intel and older CPUs. Add -march=native next time. Nets you a vector version. If you disable sse it generates essentially what icc does with less reliance on ucode.

See, rep is a workaround for older AMD CPU branch predictor. In addition gcc does not use the highly opcoded variant of lea with offsets because it is slow on older Intel.

GCC version makes for much better speculative execution too due to the use of the adder.

Re:Compiler Optimizations are Awesome by wolfgke on Friday June 02, 2017 @09:11AM (#14469574)
> Which is slower on non-Intel and older CPUs. Add -march=native next time. Nets you a vector version.

Accepted (though the central advantage of SHLX (with-march=native) and SHL (no -march=native) for this example lies in the greater flexibility of register parameters). To my defense I only have a computer whose processor supports BMI2 for a few months now - so I could not play around with BMI2 before. Otherwise I am sure I would have known such tricks.

> See, rep is a workaround for older AMD CPU branch predictor. In addition gcc does not use the highly opcoded variant of lea with offsets because it is slow on older Intel.

I know that. My personal code philosophy is to avoid such hacks for circumventing performance bugs in outdated processors.

Re:Compiler Optimizations are Awesome by AstralStorm on Friday June 02, 2017 @01:07PM (#14471523)
By outdated you mean 5 year old? This is not for ancient Athlons. Those were 64 bit chips already.

Likewise preferring microcoded lea shafts everything older than Haswell on Intel side. Not to mention modern Atom.

Re:Compiler Optimizations are Awesome by Sean1708 on Friday June 02, 2017 @05:39AM (#14468669)
Yes but the point was that compilers do changes algorithms.