This is the mail archive of the gcc-bugs@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]

Re: overlooked implementation


"Mark M. Young" <youngmm@hera.wku.edu> writes:
> };
> not everything works as expected.  With the former contents, the 'int'
> representation of 'v1' and 'v2' (as one byte) is '126' (0111
> 1110); however, with the latter contents, the 'int' representation of 'v1'
> and 'v2' (as one byte) is '252' (1111 1100 or 111 1110 0)!  This tells me
> that the bits are left-aligned in memory--they should be right-aligned.

The ordering is not defined in the standard and depends on the platform 
(usually gcc mirrors the behaviour of the host compilers). The Linux 
kernel uses the following method to cope with that for TCP headers, which 
seems to work reasonable with all architecture Linux runs on:

struct tcphdr {
	__u16	source;
	__u16	dest;
	__u32	seq;
	__u32	ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
	__u16	res1:4,
		doff:4,
		fin:1,
		syn:1,
		rst:1,
		psh:1,
		ack:1,
		urg:1,
		res2:2;
#elif defined(__BIG_ENDIAN_BITFIELD)
	__u16	doff:4,
		res1:4,
		res2:2,
		urg:1,
		ack:1,
		psh:1,
		rst:1,
		syn:1,
		fin:1;
#else
#error	"Adjust your <asm/byteorder.h> defines"
#endif	
	__u16	window;
	__u16	check;
	__u16	urg_ptr;
};

If you need more control than that you need to switch to GNAT.
I guess it would be useful to have __attribute__((lebitfield))
and __attribute__((bebitfield)) too, but so far nobody implemented it.

-Andi

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