by catlifeonmars · 2y ago · 0 comments
"storing two uint32 as a uint64"
I think C++ can now do "these bytes are now that type", called something like start_lifetime_as. C probably can't, though using a union might be legitimate. The language rules in this area are a mess.
Rob Pike has blogged about this kind of thing. [0]
Perhaps also of interest: both C and C++ provide a (portable and standard) means of determining whether atomic operations on uint64_t are assured to be lock-free. [1][2] (Assuming of course that the uint64_t type exists - it's in the standard but it's optional.)
[0] https://commandcenter.blogspot.com/2012/04/byte-order-fallac... ( discussion: https://news.ycombinator.com/item?id=3796378 )
[1] https://en.cppreference.com/w/c/atomic/atomic_is_lock_free
[2] https://en.cppreference.com/w/cpp/atomic/atomic_is_lock_free
Using a single uint64 as the memory type works, but you no longer have two different names fields and have to pack/unpack them by hand.
There's no ub if you use the compiler extension, just totally clear code that does the right thing
AFAIK this trick does work on SPARC though.
Intel's latest uarch does partial store forwarding.
There was a paper from a few years ago trying to define semantics for mixed-size accesses (not for x86 though) https://www.cl.cam.ac.uk/~pes20/popl17/mixed-size.pdf
I don't think the parent was talking about this, though; they were just talking about using a single large physical location, which logically contains multiple smaller values. Accesses to a single location happen order, so there is indeed no need for fencing between accesses to it. Usually you get a full 128 bits (at least amd64/aarch64/ppc64; not riscv yet but I expect they will get there).
That said—mixed-size can be useful despite the lack of semantics (I think linux uses them in a few places?). sooo