News for hackers. Stuff that matters.
Re:Own Constant Folder in C/C++ by hifromwork on Saturday June 22, 2024 @08:21AM (#40758574)
Everything in this post applies to C too, so it's not C++ specific. And the same gotchas apply for every case when you use inline assembly. I wouldn't call it a trick... Just an interesting workaround for a performance bug in Clang.

The post can be boiled down to "Clang doesn't compile this intrinsic nicely, so just use inline asm directly. But remember that you need to have a non-asm special case to optimize constants too, and you can achieve this with __builtin_constant_p".

Re:Own Constant Folder in C/C++ by GrantMoyer on Saturday June 22, 2024 @09:32AM (#40759024)
I wouldn't call this a performance bug in clang. It's an optimization working as intended.
Re:Own Constant Folder in C/C++ by dooglius on Saturday June 22, 2024 @09:52AM (#40759160)
I would challenge you to find a processor on which the rsqrt plus two newton-raphson iterations is not slower than plain sqrt. (We don't know what mtune the author used)
Re:Own Constant Folder in C/C++ by GrantMoyer on Saturday June 22, 2024 @12:04PM (#40760007)
According to Intel, any processor before Skylake (section 15.12 from [1]).

[1]: https://cdrdv2.intel.com/v1/dl/getContent/814198?fileName=24...

Re:Own Constant Folder in C/C++ by pclmulqdq on Saturday June 22, 2024 @10:32AM (#40759398)
The author probably didn't use any mtune setting, which is likely the problem. If you look at older cores on Agner's instruction tables, SQRT has been getting steadily faster over time. This implementation is slightly faster on old Intel machines, for example.