Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Microcontroller software stacks Date: Sat, 15 Aug 2026 17:31:36 -0700 Organization: A noiseless patient Spider Lines: 173 Message-ID: <864igu52jb.fsf@linuxsc.com> References: <10v7b32$2u85v$1@dont-email.me> <10vjsg2$259m3$3@dont-email.me> <10vkk65$l8v$1@reader1.panix.com> <10vlvie$2ne3j$2@dont-email.me> <10vmh2e$b44$1@reader1.panix.com> <86h5mv8umk.fsf@linuxsc.com> <865x1c7a63.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Sun, 16 Aug 2026 00:31:37 +0000 (UTC) Injection-Info: dont-email.me; logging-data="4010383"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19EjI6js88qKsqLuwRdI+TJa3Z7xqM6nO0="; posting-host="d60e9cb3bc0ca954ed7c56d99ae2d4fd" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:RMMOWVGVDGHnhLQLNooIDm1HAy0= sha1:kOrfagnU/ukQE1MzkL6LerPrkCc= sha256:a2dB445JOxHmxB8+yrUnh23B27lA29q3/X9MdBAcqss= sha1:iNBhOVegyucsfFKJi1hQQt3sM5o= sha256:4fdrWHvVdeYKx0IceXlkqaa6UzuAWXfHYeuye+Vbgyc= Xref: csiph.com comp.lang.c:401227 scott@slp53.sl.home (Scott Lurndal) writes: > Tim Rentsch writes: > >> scott@slp53.sl.home (Scott Lurndal) writes: >> >>> Tim Rentsch writes: >>> >>>> scott@slp53.sl.home (Scott Lurndal) writes: >>>> >>>>> One might also define data structures for control and status >>>>> registers using bitfield structs. >>>> >>>> Yeah. This kind of application (among others) I consider one of >>>> the motivating forces behind bitfields. >>>> >>>> [Some whitespace trimming done in the excerpt below.] >>>> >>>>> e.g. for the SATA UAHC_GLB_OOBR register: >>>>> >>>>> union UAHC_GBL_OOBR { >>>>> uint32_t u; >>>>> struct UAHC_GBL_OOBR_s { >>>>> #if __BYTE_ORDER == __BIG_ENDIAN >>>>> uint32_t we : 1; /**< R/W/H - Write enable. */ >>>>> uint32_t cwmin : 7; /**< R/W/H - COMWAKE minimum value [...] */ >>>>> uint32_t cwmax : 8; /**< R/W/H - COMWAKE maximum value [...] */ >>>>> uint32_t cimin : 8; /**< R/W/H - COMINIT minimum value [...] */ >>>>> uint32_t cimax : 8; /**< R/W/H - COMINIT maximum value [...] */ >>>>> #else >>>>> uint32_t cimax : 8; >>>>> uint32_t cimin : 8; >>>>> uint32_t cwmax : 8; >>>>> uint32_t cwmin : 7; >>>>> uint32_t we : 1; >>>>> #endif >>>>> } s; >>>>> }; >>>> >>>> To me it seems kind of goofy to use uint32_t for the bitfields type. >>>> I would just use unsigned, which is just as sure to work as intended, >>>> isn't it? >>> >>> The SATA hardware register is defined as a 32-bit register in the >>> SATA specification. Therefore we explicitly declare it as such. >> >> I understand the motivation for using uint32_t for the union member >> u. My question is only about the type used for the bitfields. Do >> you know of any platform, or even suspect that there might be a >> platform, where using 'unsigned' rather than 'uint32_t' for the type >> of the bitfields makes any difference at all? > > I haven't canvassed all the available platforms, and don't have > any desire so to do. In my opinion, using the same type for the > bitfield members as was used for the corresponding union > element is simply logical. If we declare the integer part > of the union as uint64_t, we use the same time for the > bitfields (and yes, we could have used unsigned long > (or unsigned long long on the micky OS) - uint64_t > is less typing. Out of curiosity I put together a short program to explore how bit-field types are understood by the compiler. Here is the program: #include #include typedef struct { _Bool ub : 1; unsigned char uc : 8; unsigned short us :16; unsigned int ui :32; unsigned long ul :64; unsigned long long ull :64; uint8_t u1 : 1; uint8_t u8 : 8; uint16_t u16 :16; uint32_t u32 :32; uint64_t u64 :64; uint64_t x1 : 1; uint64_t x8 : 8; uint64_t x16 :16; uint64_t x32 :32; uint64_t x64 :64; } Bitfields; #define whatkind(e) ( \ _Generic( e, \ _Bool : "_Bool", \ unsigned char : "unsigned char", \ unsigned short : "unsigned short", \ unsigned int : "unsigned int", \ unsigned long : "unsigned long", \ unsigned long long : "unsigned long long", \ default : "" \ ) \ ) int main(){ Bitfields bf; printf( " whatkind( bf.ub ) is %s\n", whatkind( bf.ub ) ); printf( " whatkind( bf.uc ) is %s\n", whatkind( bf.uc ) ); printf( " whatkind( bf.us ) is %s\n", whatkind( bf.us ) ); printf( " whatkind( bf.ui ) is %s\n", whatkind( bf.ui ) ); printf( " whatkind( bf.ul ) is %s\n", whatkind( bf.ul ) ); printf( " whatkind( bf.ull ) is %s\n", whatkind( bf.ull ) ); printf( "\n" ); printf( " whatkind( bf.u1 ) is %s\n", whatkind( bf.u1 ) ); printf( " whatkind( bf.u8 ) is %s\n", whatkind( bf.u8 ) ); printf( " whatkind( bf.u16 ) is %s\n", whatkind( bf.u16 ) ); printf( " whatkind( bf.u32 ) is %s\n", whatkind( bf.u32 ) ); printf( " whatkind( bf.u64 ) is %s\n", whatkind( bf.u64 ) ); printf( "\n" ); printf( " whatkind( bf.x1 ) is %s\n", whatkind( bf.x1 ) ); printf( " whatkind( bf.x8 ) is %s\n", whatkind( bf.x8 ) ); printf( " whatkind( bf.x16 ) is %s\n", whatkind( bf.x16 ) ); printf( " whatkind( bf.x32 ) is %s\n", whatkind( bf.x32 ) ); printf( " whatkind( bf.x64 ) is %s\n", whatkind( bf.x64 ) ); } and the output (using gcc) whatkind( bf.ub ) is _Bool whatkind( bf.uc ) is unsigned char whatkind( bf.us ) is unsigned short whatkind( bf.ui ) is unsigned int whatkind( bf.ul ) is unsigned long whatkind( bf.ull ) is unsigned long long whatkind( bf.u1 ) is whatkind( bf.u8 ) is unsigned char whatkind( bf.u16 ) is unsigned short whatkind( bf.u32 ) is unsigned int whatkind( bf.u64 ) is unsigned long whatkind( bf.x1 ) is whatkind( bf.x8 ) is unsigned char whatkind( bf.x16 ) is unsigned short whatkind( bf.x32 ) is unsigned int whatkind( bf.x64 ) is unsigned long and the output (using clang) whatkind( bf.ub ) is _Bool whatkind( bf.uc ) is unsigned char whatkind( bf.us ) is unsigned short whatkind( bf.ui ) is unsigned int whatkind( bf.ul ) is unsigned long whatkind( bf.ull ) is unsigned long long whatkind( bf.u1 ) is unsigned char whatkind( bf.u8 ) is unsigned char whatkind( bf.u16 ) is unsigned short whatkind( bf.u32 ) is unsigned int whatkind( bf.u64 ) is unsigned long whatkind( bf.x1 ) is unsigned long whatkind( bf.x8 ) is unsigned long whatkind( bf.x16 ) is unsigned long whatkind( bf.x32 ) is unsigned long whatkind( bf.x64 ) is unsigned long which serves to reinforce my view that bit-field types should be chosen from the basic types, and should be the narrowest type that covers the width of the corresponding bit-field member (with possible adjustment for widths that match two types, as for example unsigned long and unsigned long long).