3 ms·
Rather than std::swap_if which is rather niche, I would prefer to see std::select(cond, if_true, if_false), with the guarantee that unlike the ternary operator
by orlp 2y ago
Rather than std::swap_if which is rather niche, I would prefer to see std::select(cond, if_true, if_false), with the guarantee that unlike the ternary operator it eagerly evaluates both arguments and selects between them branchlessly if possible.
Something similar is coming to Rust as well: https://doc.rust-lang.org/nightly/core/intrinsics/fn.select_unpredictable.html https://doc.rust-lang.org/nightly/core/intrinsics/fn.select_...
- account42 2y agoWouldn't the sensible lower level primitive be Clang's __builtin_unpredictable? If you standardize that then a librry can easily build your select function on top of it. I guess GCC's __builtin_expect_with_probability with probabilily 0.5 should have the same meaning.
- gpderetta 2y ago> I guess GCC's __builtin_expect_with_probability with probabilily 0.5 should have the same meaning. But it doesn't! the sequence 1010101010... has probability 50% that it assumes one or the other value, yet is completely predictable.
- tialaramex 2y agoAlso the compiler vendors have seen this movie before and know how it ends. Too often a "performance hint" is garbage and ignoring it will improve performance. Which is unfortunate because these are genuinely sometimes useful, but it's a "We can't have nice things" situation - for every expert who is carefully applying a hint to a hot path after careful reading of the manual for the specific CPU model they use, there are dozens of idiots who will copy-paste that hint believing that it's magic speed-up juice, never having measured and with no idea what it's really doing or why. The intrinsics have the advantage that at least you're expressing what you meant, and it is obvious whose fault it is when that's slower because you screwed up - hints make it too easy for programmers to justify a tantrum when their "hint" isn't magic speed-up juice after all.
- orlp 2y agoFor what it's worth, I recommended against mentioning `unpredictable` anywhere in the name when we stabilize the intrinsic. IMO the constant-timeness is much more important, e.g. in audio DSP code or for cryptography you really care that it's a `cmov`, regardless of the predictability of the predicate.
- account42 2y agoA constant time guarantee would make this unimplementable since CPU vendors do not make that guarantee.
- gpderetta 2y agoUnless they do: https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/resources/data-operand-independent-timing-instructions.html https://www.intel.com/content/www/us/en/developer/articles/t...
- tialaramex 2y agoMakes sense.
- account42 2y agoCompiler vendors are the ones who add both the intrinsics and attribute hints. I also don't see a reason why "programmers" who engage in wishful thinking would limit that to hints and not apply the same to intrinsics.
- tialaramex 2y agoCommunication is the reason. "Hints" are not good communication. There's a real difference between the wishful thinking which leads to programmers writing incorrect hints and them writing code that outright says what it's doing and then being surprised it did what they asked.