There are already a lot of questions and answers about the dangers of expecting two floats produced by separate computations to be exactly equal, because floating point numbers are not real numbers. This question is not about correctness contingent on equality checking, it's about caching based on it.
Imagine you have this code:
if(myfloat != _last_float) {
refresh_expensive_computation(myfloat);
_last_float = myfloat;
}
In this instance the equality comparison purely exists to prevent doing redundant work. We are avoiding doing the expensive computation again if its input is unchanged (we assume the expensive function is deterministic and no other inputs to it have changed).
In the event the two are really equal (meaning they would be if we could compute with reals instead of floating point) but are mistakenly detected not to be, in the worst case we do the expensive computation redundantly but our program's answer is still correct. AFAIK they can only mistakenly compare equal if the computation was done in a register that is wider than the memory representation of float (e.g. on 32 bit x86 when the 80 bit fp registers are enabled), and after getting converted to the memory representation they happen to both be bitwise equal. In that case the difference must be beyond the memory representation's precision, which must be below the epsilon for comparisons that matter to me, because otherwise I'd be using a wider type like double.
So I'm going to assert this use of floating point equality is safe. So the first question is, am I wrong?
Secondly, if we assume it's safe, I'd like to avoid mistakenly returning true because it causes a costly computation. One way to avoid that on machines with wider registers than memory representations would be to use memcmp to force it to compare memory representations (the semantics won't be exactly the same for NaN, which will now compare true against the exactly identical bitwise instance of itself, but for caching that's an improvement, or for +0 and -0 but that could be special cased). However that memcmp will be slower than a floating point comparison in registers. Is there a way to detect when a platform has wider registers, so I can #ifdef or similar to get the optimized implementation on platforms where that is safe?