Help with cross-endian bitfields?
Hi, all. I'm working on getting the Advansys driver working and what appears to be the final stumbling block is a difference in the layout of bitfields between little-endian and big-endian systems. In particular, there are structs like the following:
struct blah {
uchar a : 3;
uchar b : 3;
uchar c : 2;
}
On a little endian machine, this works like I expect. (blah & 0x07) == blah.a is true. On linuxppc (and apparently MacOS as well), this is NOT true. blah.a refers to the 3 most significant bits. The linuxppc behavior seems counterintuitive to me because I expected each successive member in a struct to be the next physical piece of data I encounter.
Can someone tell me what's going on here? Am I going to have to have ifdef's on these structs, or resort to mask macros, or is there a reasonable solution here.
Thanks,
Jerry Quinn
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
Quoted 3 lines · [email protected] wrote:
Hi, all. I'm working on getting the Advansys driver working and what appears to be the final stumbling block is a difference in the layout of bitfields between little-endian and big-endian systems.
IMHO, bit fields are not normally a good thing to be using if this is describing a real device register. The compiler is able to read/write in ways that may not be proper for hardware access. For device registers and when this is a data structure in memory, the cross-platform method of using macros for reading writing 16- and 32-bit values and then 'C' operators for testing/setting bits has worked pretty well in the past.....
-- Dan
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
Quoted 6 lines · Dan Malek
IMHO, bit fields are not normally a good thing to be using if this is describing a real device register. The compiler is able to read/write in ways that may not be proper for hardware access. For device registers and when this is a data structure in memory, the cross-platform method of using macros for reading writing 16- and 32-bit values and then 'C' operators for testing/setting bits has worked pretty well in the past.....
Yeah, I just looked in the PowerPC ABI for bitfield storage, and the relevant text said:
* Bit-fields are allocated from right to left (least to most significant) on Little-Endian implementations and from left to right (most to least significant) on Big-Endian implementations.
Josh
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
Quoted 18 lines · On Fri, May 05, 2000 at 03:27:21PM -0400, [email protected] wrote:
Hi, all. I'm working on getting the Advansys driver working and what appears to be the final stumbling block is a difference in the layout of bitfields between little-endian and big-endian systems. In particular, there are structs like the following:
struct blah {
uchar a : 3;
uchar b : 3;
uchar c : 2;
}On a little endian machine, this works like I expect. (blah &
0x07) == blah.a is true. On linuxppc (and apparently MacOS as well), this is NOT true. blah.a refers to the 3 most significant bits. The linuxppc behavior seems counterintuitive to me because I expected each successive member in a struct to be the next physical piece of data I encounter.
Quoted 3 lines
Can someone tell me what's going on here? Am I going to have to have ifdef's on these structs, or resort to mask macros, or is there a reasonable solution here.
As Dan Malek said, there is no defined ordering. The compiler is free to do whatever it wishes. You can't assume that. (I think.)
Also, by your intuition, the obvious thing IS happening. Remember that we are big-endian - the first thing in memory is big-endian. Thus, it follows logically that a should be in the first three (most significant) bits.
Dan
/--------------------------------\ /--------------------------------\
| Daniel Jacobowitz |__| SCS Class of 2002 | | Debian GNU/Linux Developer __ Carnegie Mellon University | | [email protected] | | [email protected] | \--------------------------------/ \--------------------------------/
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/