8

According to the documentation, an atomic supports T that is of an integral type, enumeration type, or a pointer type. Does Intel TBB support floats/doubles officially? I have seen some patches here and by Raf Schietekat here, which might/might not have been incorporated into the latest 4.0 release. From the patches I have read through, the only major difference I noticed was the addition of reinterpret_cast from integer type to float/double. If anyone could clarify this, I would appreciate it. Thanks!

Community
  • 1
  • 1
drselee
  • 81
  • 2

3 Answers3

1

You can easily add support for floating-point numbers building on top of 64 and 32 bit atomic integers. Atomic load/store/exchange can be implemented as direct wrappers using reinterpret_cast, the atomic arithmetic operations can be implemented using a loop with atomic compare exchange.

1

C++11 supports atomic floats and doubles. Arithmetic functions like std::atomic_fetch_add are only supported for integral types, though.

If you have a C++11 compiler, I would recommend to switch to std::atomic so you don't have to rely on undocumented behavior.

Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
  • 1
    Also worth noting that the arithmetic functions can be easily and efficiently emulated with `std::atomic`. – Cory Nelson Dec 28 '12 at 17:28
  • Interesting. What is the best way to do it? Is it a compare-and-swap loop? (Fetch the current value and try to swap it with the new result. If it fails because the value was changed, retry.) – Philipp Claßen Dec 28 '12 at 17:51
0

The test for non-integral types in test_atomic.cpp was added back in 2008 (soon after the time of the discussion with Raf). Thus since even early than the time of the question, TBB supports float&double atomics (though restricted to fetch_and_store and compare_and_exchange read-modify-write ops only).

Anton
  • 6,349
  • 1
  • 25
  • 53