• Re: Microcontroller software stacks (was Re: this girl calls c ugly)

    From Tim Rentsch@3:633/10 to All on Fri Aug 14 13:23:11 2026
    cross@spitfire.i.gajendra.net (Dan Cross) writes:

    In article <86h5mv8umk.fsf@linuxsc.com>,
    Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:

    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?

    No. There are issues of alignment and padding one must consider
    when using bitfields to model hardware registers, particularly
    if (say) a device driver is meant to be shared across ISAs.

    Using the exact width types really does make a difference; it's
    IB what those properties are, though we're usually at the mercy
    of the target platform's ABI anyway at that point.

    The motivation for my query was not to ask an abstract theoretical
    question but a specific and pragmatic one.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Dan Cross@3:633/10 to All on Tue Aug 18 20:32:27 2026
    In article <861pc078pc.fsf@linuxsc.com>,
    Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
    cross@spitfire.i.gajendra.net (Dan Cross) writes:

    In article <86h5mv8umk.fsf@linuxsc.com>,
    Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:

    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?

    No. There are issues of alignment and padding one must consider
    when using bitfields to model hardware registers, particularly
    if (say) a device driver is meant to be shared across ISAs.

    Using the exact width types really does make a difference; it's
    IB what those properties are, though we're usually at the mercy
    of the target platform's ABI anyway at that point.

    The motivation for my query was not to ask an abstract theoretical
    question but a specific and pragmatic one.

    My response was practical and pragmatic, and arises in code that
    is written by real-world systems programmers.

    I understand that is not your domain of expertise.

    - Dan C.


    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Johann 'Myrkraverk' Oskarsson@3:633/10 to All on Thu Aug 20 04:51:34 2026
    On 22/06/2026 5:13 AM, Tim Rentsch wrote:


    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?
    As long as both /unsigned/ and /uint32_t/ are of the same /bit width/.

    In DOS, you can find /unsigned/ to be 16bits and not 32bits. And in
    the future, I'm sure someone here in comp.lang.c, or maybe comp.arch,
    will make a computer with /unsigned/ as 64bits.

    So, it really depends on whether you want to account for the past and
    the future, or if you're only coding in the present; plus of course
    visual aesthetics, because some people find uint32_t ugly.
    --
    Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
    I'm not from the Internet, I just work there. | via Easynews.com https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)