C++ Logo

std-discussion

Advanced search

Re: Float exceptions and std::atomic

From: Nate Eldredge <nate_at_[hidden]>
Date: Wed, 23 Aug 2023 20:45:16 -0600 (MDT)
On Wed, 23 Aug 2023, Thiago Macieira via Std-Discussion wrote:

> From the point of view of implementations, the standard is the one that needs
> to be fixed. Until we have per-instruction "suppress exception" mode (on x86,
> that's only available with AVX512 and AVX10), atomic FP operations would need
> to repeatedly save, update, and restore the FP environment, which aren't
> trivial instructions. However, given that CPUs usually don't have atomic FP
> operations anyway, implementations will likely need to lock mutexes around the
> non-atomic ops, so the cost of modifying the FP environment may be acceptable.

std::atomic<float> and std::atomic<double> are lock-free on major
implementations, so there's no mutex. Typically fetch_add and friends are
implemented with a compare-exchange loop.

LL/SC platforms like ARM could perhaps do better by just doing a
floating-point add inside the LL/SC, but I don't think current
implementations do this optimization.

-- 
Nate Eldredge
nate_at_[hidden]

Received on 2023-08-24 02:45:20