Atomic Operations on non-integral types?

Atomic Operations on non-integral types?

Would it be possible to support atomic operations for float and double through atomic.h? Is there some architecture restriction that make this impossible? I'm not very familiar with this type of thing. So, if this is a silly question, forgive me.


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

We limited atomic to operations that were commonly available in hardware. An alternative would be to provide many more operations, e.g. atomic <<=or floating-point+=,but so would be presenting "primitives" that really were not primitives. There are a few non-primitives even then. On Itanium, the fetch_and_add is implemented in hardware only for certain values +/-(1, 4, 8, 16) if I remember correctly..

If we were to add support for more operations, I would be inclined to add some kind of template that allowed any STL unary function object to be used to specify an atomic update. E.g., x.apply( std::negate ) to atomically negate a location. Binders (20.3.6 in ISO C++ standard) could be used for creating useful unary functions from binary functions. It would be implemented using a compare-and-swap loop. We' have to restrict the argument type to a POD type that fits in an integer.

Perhaps such a template should not even be a member of class atomic, but a separate template algorithm.

I think the first paragraph answered my question. I was interested in doing a fetch_and_add operation with doubles. Am I correct in understanding that this is not commonly available in hardware?

It's not available on x86 or Itanium. IBM's Power does atomic operations via a special load instruction followed by a later special conditional store, and so can emulate fetch-and-add for floating-point (and just about anything else). But in general, fetch-and-add for floating-point is not widely supported.

Leave a Comment

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