On Tue, Mar 29, 2011 at 7:00 PM, Aldy Hernandez <al...@redhat.com> wrote: > [Language lawyers, please correct me if I have mis-interpreted the upcoming > standard in any way.] > > In the C++ memory model, contiguous bitfields comprise a single memory > location, so it's fair game to bit twiddle them when setting them. For > example: > > struct { > unsigned int a : 4; > unsigned int b : 4; > unsigned int c : 4; > }; > > In the above example, you can touch <b> and <c> while setting <a>. No race > there. > > However, non contiguous bitfields are a different story: > > struct { > unsigned int a : 4; > char b; > unsigned int c : 6; > }; > > Here we have 3 distinct memory locations, so you can't touch <b> or <c> > while setting <a>. No bit twiddling allowed. > > Similarly for bitfields separated by a zero-length bitfield: > > struct { > unsigned int a : 4; > int : 0; > unsigned int c : 6; > }; > > In the above example, <a> and <c> are distinct memory locations. > > Also, a structure/union boundary will also separate previously contiguous > bit sequences: > > struct { > unsigned int a : 4; > struct { unsigned int b : 4 } BBB; > unsigned int c : 4; > }; > > Here we have 3 distinct memory locations, so again, we can't clobber <b> or > <c> while setting <a>. > > The patch below adds a non-contiguous bit test (bitfields-2.C) which passes > on x86, but upon assembly inspection, fails on PPC64, s390, and Alpha. > These 3 architectures bit-twiddle their way out of the problem.
The memory model is not implementable on strict-alignment targets that do not have a byte store operation. But we previously said that ;) Also consider global vars char a; char b; accessing them on strict-align targets may access adjacent globals (that's a problem anyway, also with alias analysis). Richard. > There is also a similar test already in the testsuite (bitfields.C) which is > similar except one field is a volatile. This test fails on x86-64 as well > and is the subject of PR48128 which Jakub is currently tackling. > > As soon as Jakub finishes with PR48128, I will be working on getting these > bitfield tests working in a C++ memory model fashion. > > Committing to branch. >