Not a patch, and unfinished... I'd be interested in results of non-gcc
compilers and/or non-i386 platforms.
Will rework this once I have some more spare time.

Regards,
Carl-Daniel

-- 
http://www.hailfinger.org/

#include <stdint.h>
#include <stdio.h>
#include <string.h>

struct bitfield_test_32bit {
	uint32_t var1;
	union {
		uint32_t var2;
		struct {
			uint32_t bf1	:31,
				 bf2	:1;
		};
	};
	union {
		uint32_t var3;
		struct {
			uint32_t bf3	:1,
				 bf4	:31;
		};
	};
	union {
		uint32_t var4;
		struct {
			uint32_t bf5	:24,
				 bf6	:4,
				 bf7	:4;
		};
	};
	union {
		uint32_t var6;
		struct {
			uint32_t bf9	:1,
					:30,
				 bf10	:1;
		};
	};
	union {
		uint32_t var7;
		struct {
			uint32_t	:30,
				 bf11	:1,
				 bf12	:1;
		};
	};
};

static const unsigned char bitfield_test_32bit_testvector[24] = {
	0x67, 0x45, 0x23, 0x01,
	0x00, 0x00, 0x00, 0x80,
	0x01, 0x00, 0x00, 0x00,
	0x01, 0x23, 0x45, 0x67,
	0x80, 0x00, 0x00, 0x00,
	0xff, 0xff, 0xff, 0xfc
};

/* Yes, I know, this one tests for little endianness...
 * We assume that the last bit specified in a bitfield is the highest bit.
 * We assume that a group of bits has the same internal order as a single
 * byte, i.e. shift+mask operations of a native endianness int-type variable
 * will yield the same values as a bitfield access without additional bit
 * swapping.
 */
int bitfield_check(void)
{
	struct bitfield_test_32bit bftest;

	if (sizeof(bftest) != 24) {
		printf("bitfield size broken\n");
		return 1;
	}
	memcpy(&bftest, bitfield_test_32bit_testvector, sizeof(bftest));
	/* Check if the arch is little endian. */
	if (bftest.var1 != 0x01234567) {
		printf("endianness unexpected\n");
		/* FIXME: Check if bigendian, convert endianness before
		 * continuing with checks.
		 */
		return 1;
	}
	if ((bftest.bf1 != 0) || (bftest.bf2 != 1)) {
		printf("bitfield bf1 broken\n");
		return 1;
	}
	if ((bftest.bf3 != 1) || (bftest.bf4 != 0)) {
		printf("bitfield bf3 broken\n");
		return 1;
	}
	if ((bftest.bf5 != 0x452301) || (bftest.bf6 != 7) || (bftest.bf7 != 6)) {
		printf("bitfield bf5 broken\n");
		return 1;
	}
#if 0
	if (bftest. != ) {
		printf(" broken\n");
		return 1;
	}
	if ( != ) {
		printf(" broken\n");
		return 1;
	}
	if ( != ) {
		printf(" broken\n");
		return 1;
	}
	if ( != ) {
		printf(" broken\n");
		return 1;
	}
	if ( != ) {
		printf(" broken\n");
		return 1;
	}
#endif
	return 0;
}

int main()
{
	printf("%s\n", bitfield_check() ? "FAIL" : "OK");
	return 0;
}
_______________________________________________
flashrom mailing list
[email protected]
http://www.flashrom.org/mailman/listinfo/flashrom

Reply via email to