On Thu, 19 Sep 2024, mmalcom...@nvidia.com wrote:

> 6) Anything special about floating point maths that I'm tripping up on?

Correct atomic operations with floating-point operands should ensure that 
exceptions raised exactly correspond to the operands for which the 
operation succeeded, and not to the operands for any previous attempts 
where the compare-exchange failed.  There is a lengthy note in the C 
standard (in C11 it's a footnote in 6.5.16.2, in C17 it's a Note in 
6.5.16.2 and in C23 that subclause has become 6.5.17.3) that discusses 
appropriate code sequences to achieve this.  In GCC the implementation of 
this is in c-typeck.cc:build_atomic_assign, which in turn calls 
targetm.atomic_assign_expand_fenv (note that we have the complication for 
C of not introducing libm dependencies in code that only uses standard 
language features and not <math.h>, <fenv.h> or <complex.h>, so direct use 
of <fenv.h> functions is inappropriate here).

I would expect such built-in functions to follow the same semantics for 
floating-point exceptions as _Atomic compound assignment does.  (Note that 
_Atomic compound assignment is more general in the allowed operands, 
because compound assignment is a heterogeneous operation - for example, 
the special floating-point logic in build_atomic_assign includes the case 
where the LHS of the compound assignment is of atomic integer type but the 
RHS is of floating type.  However, built-in functions allow memory orders 
other than seq_cst to be used, whereas _Atomic compound assignment is 
limited to the seq_cst case.)

So it would seem appropriate for the implementation of such built-in 
functions to make use of targetm.atomic_assign_expand_fenv for 
floating-point environment handling, and for testcases to include tests 
analogous to c11-atomic-exec-5.c that exceptions are being handled 
correctly.

Cf. N2329 which suggested such operations for C in <stdatomic.h> (but 
tried to do to many things in one paper to be accepted into C); it didn't 
go into the floating-point exceptions semantics but simple correctness 
would indicate avoiding spurious exceptions from discarded computations.

-- 
Joseph S. Myers
josmy...@redhat.com

Reply via email to