is there a gcc option to fix this bit-field related C issue?

0

My code likes below, but after gcc compiles, the output of this routine is always 'unmatch'.

It seems RXC_SLOT is always 8-bit width in this code, not 6. Why?

#include <stdint.h>
#include <stdio.h>

typedef struct _RX_CONROL_t {
    union {
        struct {
            //32'b{RESERVED12, RX_ABORT, RXC_RECEIVE, RXC_SOURCE_PORT, RXC_SLOT}
            //32'b{     20bit,     1bit,        1bit,            4bit,     6bit}
             uint8_t RXC_SLOT         : 6;
             uint8_t RXC_SOURCE_PORT  : 4;
             uint8_t RXC_RECEIVE      : 1;
             uint8_t RX_ABORT         : 1;
             uint32_t RESERVED12       : 20;
        };
        uint32_t VALUE32;
    };
} RX_CONROL_t;

int main(void) {

    RX_CONROL_t rx_control = {.VALUE32 = 0};

    rx_control.RXC_SLOT = 3;
    rx_control.RXC_SOURCE_PORT = 2;
    rx_control.RXC_RECEIVE = 1;
    rx_control.RX_ABORT = 0;
    //32'b{RESERVED12, RX_ABORT, RXC_RECEIVE, RXC_SOURCE_PORT, RXC_SLOT}
    //32'b{     20'd0,     1'd0,        1'b1,            4'd2,     6'd3} == 32'h00000483
    if(rx_control.VALUE32 == 0x00000483) {
        printf("match\n");
        return 0;
    }
    else {
        printf("unmatch rx_control.VALUE32 is %x\n", rx_control.VALUE32); //FIXME: rx_control.VALUE32 is 1203 (why?)
        printf("%x %x %x %x %x\n", rx_control.RXC_SLOT, rx_control.RXC_SOURCE_PORT, rx_control.RXC_RECEIVE, rx_control.RX_ABORT, rx_control.RESERVED12);
        return 1;
    }
}
c
gcc
bit-fields
asked on Stack Overflow Sep 23, 2020 by odingo

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0