Atomic floats, is this implementation safe?

54 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Quoting - Raf Schietekat

Would the following set of features be acceptable with float and double: {compare_and_,fetch_and_,}store(), operator=, load(), operator value_type(), operator{+=,-=,*=,/=}?

compare_and_swap() needs to be omitted because it would be fraught with problems, as already discussed.

Integer types have their use even for operations like compare_and_or(), but with floating points it seems better to wipe the slate clean than to add, for consistency, numerous operations referring to multiplication and division.

Still, it does not seem fully satisfactory: maybe operators ++ and -- should still be available. But then why not have ++ for bool? And *= and /= for integers? And what about >>= and <<=? But adding is always nicer than taking away, so...

for float and double operators +=, -=, ++, --, *=, /= are understandable

But you may use for &=, |=, ^= operatorsthe binary operators on the binary values of the floating point number. Examples of use are flipping the sign bit, forcing the sign bit to 1/0, truncating some number of least significant bits, saving some number of the least significant bits, rounding to a specific least significant bit, etc...

For <<= and >>= this would be subjective. It could be a power of two shifting, a power of 10 shifting, power and rooting.

Jim Dempsey

"There is nothing wrong with a compare and swap on floating point data as long as the comparand is obtained using register moves (as opposed to floating point load and store)." The idea is to hide these details.

It does not seem appropriate to provide operators that are not available on the underlying fundamental types, but whoever wishes to do something like that (and takes the responsibility for dealing with the binary representation of the floating-point types) will have compare_and_store() as a building block.


Leave a Comment

Please sign in to add a comment. Not a member? Join today