This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[cxx-mem-model] bitfield tests


[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.

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.

Attachment: curr
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]