bit-fields in C

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
kan
Posts: 19
Joined: Sat Jan 22, 2005 12:00 am
Location: India

bit-fields in C

Post by kan »

hi,
i want to make use of bit-fields to store contents of ecx register to test individual bits,to verify which fetures are supported by processor after 'cpuid' has been executed.
i declare by bit fields in a structure as follows :

struct StandardFeatures1
{
unsigned SSE3Support:1; //SSE3 Support available if set.
unsigned :12; //Reserved bits 12.
unsigned CMPXCHG16B:1; //CMPXCHG16B Instruction support available if set.
unsigned :18; //Reserved bits 18.
};

and then i use this structure in another structure (nested structures) as follows :

/*Stores Processor Information*/
struct ProcessorInfo
{
unsigned int MaxFuncNumber;
unsigned int Stepping,Family,Model,ExtendedFamily,ExtendedModel;
unsigned int EffectiveFamily,EffectiveModel;
unsigned int BrandID,CLFLUSHSize,LogicalMPCount,APICID;
*******ERROR STATEMENT********************
struct StandardFeatures1 StdFeatures1;
***********************************************

char CPUIDSupport,AMDPresent,IntelPresent;
char ProcessorVendor[13];
};

i have declared these structures in a header file. when i compile my source i get following error message :

KLoader.h:12: error: field `StdFeatures1' has incomplete type

my compile options are :
gcc -o KLoader.o -c KLoader.c -Wall -Werror -W --no-warn -nostdlib -nostartfiles -nodefaultlibs

What prob does compiler has? Such structure nesting is allowed in Standard C! (c99)
Nothings Impossible :)
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: bit-fields in C

Post by carbonBased »

The C standard also states that the order and position of bits is implementation dependant, which means that structure may or may not accurately reflect the contents of the register.

In order words, there's nothing in C to garauntee that SSE3Support is the first bit, followed by 12 bytes, then CMPXCHG16B then another 18.

It's entirely possible for each bitfield is actually occupy a fully byte.

You're probably better off defining some macros:

#define SSE3SUPPORT_MASK 0x80000000
#define sse3Supported(n) n & SSE3SUPPORT_Mask

--Jeff
Post Reply