Skip to content

Conversation

@AlexGuteniev
Copy link
Contributor

@AlexGuteniev AlexGuteniev commented Dec 22, 2025

The goal of the change is mostly clarity. Bit cast is a vocabulary function.

_Bit_cast was invented too late, otherwise _Atomic_reinterpret_as should have been _Bit_cast all along.

In addition to size checks, _Atomic_reinterpret_as also check for integrality, but that's an arbitrary requirement we don't really need to enforce.

Bit cast has some triviality check, which spotted unwrapped _Storage_for on lines 927 and 1062 (944 and 1080 before the change), fixed as a separate commit.

} else if constexpr (is_pointer_v<_Ty> && sizeof(_Integral) == sizeof(_Ty)) {
return reinterpret_cast<_Integral>(_Source);
} else {
_Integral _Result{}; // zero padding bits
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was misleading.

If _Ty has padding bits, this does zero the corresponding bits in _Integral, along with other bits.

But memcpy just below copies the values of these padding bits anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, after thinking about it a bit I can see it.

The intent was to make some non-power-of-two sized struct handled here. Like struct s { char a; char b; char c; };.

"Padding bits" is a misnomer; these are actually bytes to complete to integral type.

For now and until vNext we don't really have lock-free atomics for like struct s { char a; char b; char c; }. We may reconsider the current change to preserve this part, or defer this to vNext.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit to not having the best understanding on getting the compiler to zero out the correct bits but I believe I wrote tests that tried to introduce the infinite loop problem on bogus padding

@StephanTLavavej StephanTLavavej added the enhancement Something can be improved label Dec 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Something can be improved

Projects

Status: Initial Review

Development

Successfully merging this pull request may close these issues.

3 participants