Has atomic type-punning through unions defined behaviour?

130 Views Asked by At

Type-punning through unions has defined behaviour in C:

typedef union
{
    uint32_t i;
    float f;
} MyUnion;

MyUnion u;
u.f = 4.2;
print("%"PRIu32, u.i); // defined behaviour

Given via a footnote in the C Standard:

If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called “type punning”). This might be a trap representation.


However, is it still defined behaviour when atomic operations are used? And what is that behaviour? Do we have some of the sequencing guarantees?

typedef union
{
    _Atomic uint32_t i;
    _Atomic float f; // assume sizeof(float) == sizeof(uint32_t)
} MyUnion;

int load(MyUnion* p)
{
    atomic_load(&p->i);
}

void store(MyUnion* p)
{
    return atomic_store(&p->i, 42);
}

You can also construct strange examples like:

typedef union
{
    _Atomic uint32_t i;
    struct
    {
        uint16_t h0; // or maybe _Atomic uint16_t ah0;
        _Atomic uint16_t ah1;
    };
} MyUnion;

6.5.2.3p5 at least says:

Accessing a member of an atomic structure or union object results in undefined behavior.

typedef _Atomic union
{
    uint32_t i;
    float f;
} MyUnion;

int load(MyUnion* p)
{
    return atomic_load(&p->i); // UB, also atomic_load on non-atomic type
    return p->i; // UB
}
1

There are 1 best solutions below

2
On

is it still defined behaviour when atomic operations are used?

The same conditions apply. It may be a trap representation, and _Atomic objects may differ in size, representation, alignment compared to the unqualified types. If it is not a trap representation, then the result will be (implementation-) defined.