atomicCAS-in-a-loop method is mentioned in the programming guide as being useful for “any atomic operation” and in my experience that is a true statement for atomic operations on sizes up to 64 bit. The main drawback that I am aware of is related to performance, and this is especially noticeable when multiple threads are attempting to do atomics on the same location.
float atomicMax operation based on ordering similarities to
unsigned is roughly captured in the answer by Xiaojin An, however as already indicated in that answer, making use of the ordering similarities is not possible in all cases by a simple cast from e.g.
int. There are ranges that have to be mapped and handled. I think that answer is mostly correct, but according to my testing it does not give the proper result in all cases for atomicMax when the argument is
float negative zero, and I have made a comment to that effect. Nevertheless a careful implementation like that, which handled all cases of interest correctly, should be preferred for performance reasons over the
atomicCAS-in-a-loop method, I would expect, as it only needs to do one atomic op to complete the work.
I’m not aware that anyone has worked out a similar casting-and-ordering strategy for
double and none of my comments in this thread were intended to communicate that. I don’t know if it is possible or not, I have not tried it, and I have not seen indications of anyone trying it.
With respect to accuracy, I would only attempt to discuss approaches that I would consider to have “proper accuracy”. My comments above assume one is interested only in approaches where the results of the proposed atomicMax operation are considered correct from a mathematical interpretation of maximum. Therefore I believe it is possible with sufficient attention to ranges and special cases to achieve an accurate
float atomicMax using the previously discussed methods. The
atomicCAS-in-a-loop method, even though it is typically casting to another data type, is not advanced in the programming guide as something that may be somehow “inaccurate”. It is intended to be accurate.