Date: Wed, 30 Apr 2025 20:01:49 +0200
The crucial point, and this doesn't seem to be getting through to you, is
that branch prediction doesn't fail anyway. Division by zero is something
that's never supposed to happen, and branches where the same path is always
taken are essentially free on modern CPUs.
Even for cold code where the predictor has no data yet, you can use
[[unlikely]] to make it predict that the divisor is nonzero on the first
try.
Therefore, these claims about checking for zero are nothing more than
conjecture, and they are almost certainly wrong. Even if they weren't
wrong, the compiler could transform if (divisor == 0) into something
better, at least in theory.
that branch prediction doesn't fail anyway. Division by zero is something
that's never supposed to happen, and branches where the same path is always
taken are essentially free on modern CPUs.
Even for cold code where the predictor has no data yet, you can use
[[unlikely]] to make it predict that the divisor is nonzero on the first
try.
Therefore, these claims about checking for zero are nothing more than
conjecture, and they are almost certainly wrong. Even if they weren't
wrong, the compiler could transform if (divisor == 0) into something
better, at least in theory.
Received on 2025-04-30 18:02:03