There is a certain structure with bit fields. Let's say

struct _example{ unsigned f10:1; unsigned f11:1; unsigned f2:2; unsigned f4:4; unsigned f8:8; unsigned f16:16; }; 

When accessing f10 in the form a10 | = 1, the code orb $1,(%rcx) orl $1,(%rcx) , and orl $1,(%rcx) needed. Register% rcx for example, contains the address of the beginning of the structure.

You can declare the corresponding variables volatile, but this solution is suitable for the very first field.

If you think about it, the compiler cannot determine the boundaries of 32-bit words in the structure, therefore it refers to bytes. https://gcc.godbolt.org/z/17JQWM

  • Why so? Do you think it will be faster? - HolyBlackCat
  • Tell the name and version of the compiler. Ideally, do an example on gcc.godbolt.org that would generate orb $1,(%rcx) . - HolyBlackCat 4:26 pm
  • one
    @HolyBlackCat MMIO, 32-bit addressing is required. gcc.godbolt.org/z/5S7lEc - Adokenai
  • Write something like int set_enable() { volatile unsigned int *p = (__typeof__(p))&ex; return *p |= 1; } int set_enable() { volatile unsigned int *p = (__typeof__(p))&ex; return *p |= 1; } int set_enable() { volatile unsigned int *p = (__typeof__(p))&ex; return *p |= 1; } and the compiler will hear you - avp
  • @avp is then simpler to use unsigned int instead of the whole structure and set values ​​to bit masks. Only need to describe a bunch of masks. - Adokenai

1 answer 1

It's impossible. The compiler cannot calculate 32-bit bounds.

  • As a fallback there is an inline assembly. - HolyBlackCat
  • @HolyBlackCat is not an option. Because, we get the same bitwise operations in the code as in the case of representing the structure as a set of unsigned int fields, and not bit fields. Even more code. I have inline functions with bts, but I hoped that there are better options. - Adokenai