0

I see something like int kind : 19;. I am not familar with this syntax. Could anybody let me know what it means? Thanks.

struct _jit_type
{
    unsigned int ref_count;
    int          kind         : 19;
    int          abi          : 8;
    int          is_fixed     : 1;
    int          layout_flags : 4;
    jit_nuint    size;
    jit_nuint    alignment;
    jit_type_t   sub_type;
    unsigned int num_components;
    struct jit_component components[1];
};

http://git.savannah.gnu.org/cgit/libjit.git/tree/jit/jit-internal.h#n784

abelenky
  • 63,815
  • 23
  • 109
  • 159
user1424739
  • 11,937
  • 17
  • 63
  • 152

1 Answers1

0
int kind : 19;

means 19 bits are used to memorize kind, it is a bit field

this may allows to reduce the memory (heap or stack) consumption, for instance if an other field just before or after is on 13 bits both of them can use 32 bits (depending on the other fields, alignment etc)

Example

struct S1 {
   int kind : 19;
   int value : 13;
};
struct S2 {
   int kind ;
   int value;
};

if int uses 32 bits sizeof(struct S2) is 8 while sizeof(struct S1) is 4


There are limitations :

For instance you cannot get the address of a bit field :

struct S1 s1;
int * p = &s1.kind;

produces the compiler error cannot take address of bit-field ‘kind’, this is because the type int:19 does not exist and using the address of the field kind as an int * is of course a non sense.

And as EugeneSh. says in a remark you cannot get the sizeof of a bitfield because the unity of sizeof is the size of a char ( sizeof(char) is 1 by definition ), so

sizeof(s1.kind)

produces the compiler error ‘sizeof’ applied to a bit-field

bruno
  • 32,421
  • 7
  • 25
  • 37
  • So the rest 32-19=13 bits of `int` are not used? What is the advantage of writing it in this way? What is difference between `int abi : 8;` and `char abi;`? Can't I just use `char` as an 8-bit integer? – user1424739 Feb 20 '19 at 18:17
  • @user1424739 yes, you cut the grass under my feet, I am editing my answer to speak about that ^^ – bruno Feb 20 '19 at 18:18
  • @user1424739 in a lot of cases `int abi : 8;` and `char abi;` does the same, but this is not guaranty by the language – bruno Feb 20 '19 at 18:20
  • Note, that attempting to `sizeof` a bitfield member is a constraint violation. – Eugene Sh. Feb 20 '19 at 18:27
  • @EugeneSh. also that yes – bruno Feb 20 '19 at 18:28
  • So code using bitfields is slower, as bit operations are needed before the data can be accessed? – user1424739 Feb 20 '19 at 18:29
  • @user1424739 yes it can be a little because the generated code uses masks to get / set the value of a bit field – bruno Feb 20 '19 at 18:33