gcc 包含位域结构体的结构体的大小如何打包?

eqzww0vc  于 2023-02-23  发布在  其他
关注(0)|答案(1)|浏览(176)

我用C语言定义了下面的结构

struct __attribute__((__packed__)) Input {
    enum {
        I_PE_left     = 0, 
        I_PE_up       = 1, 
        I_PE_right    = 2, 
        I_PE_down     = 3,
        I_LSU_left    = 4,
        I_LSU_up      = 5,
        I_LSU_right   = 6,
        I_LSU_down    = 7,
        I_LR_0        = 8,
        I_LR_1        = 9,
        I_LR_2        = 10,
        I_IMMI_0      = 11,
        I_IMMI_1      = 12,
        I_IMMI_2      = 13,
        I_UNUSE       = 15
    } src:4;
};  
struct __attribute__((__packed__)) Instr {
    struct Input inputx;
    struct Input inputy;
    struct Input inputz;
    struct Input inputzz;
};

在上面的示例中,sizeof(struct Instr)为4,这意味着即使是打包的结构体也至少需要1个字节。
然而,我想把sizeof(struct Instr)推到2,但不知道怎么做。
完整的定义如下。我可能需要递归地定义结构,因为我想多次使用结构定义。

struct __attribute__((__packed__)) Output {
    enum {
        O_S_PE_left     = 0, 
        O_S_PE_up       = 1, 
        O_S_PE_right    = 2, 
        O_S_PE_down     = 3,
        O_S_LSU_left    = 4,
        O_S_LSU_up      = 5,
        O_S_LSU_right   = 6,
        O_S_LSU_down    = 7,
        O_S_CALC_res    = 8,
        O_S_LR_0        = 9,
        O_S_LR_1        = 10,
        O_S_LR_2        = 11,
        O_S_IMMI_0      = 12,
        O_S_IMMI_1      = 13,
        O_S_IMMI_2      = 14,
        O_S_UNUSE       = 15
    } src:4;
    enum {
        O_I_LR_0        = 0,
        O_I_LR_1        = 1,
        O_I_LR_2        = 2,
        O_I_GR          = 3,
        O_I_FIFO        = 4,
        O_I_PE_DIR      = 5,
        O_I_LSU         = 6
    } dst:3;
};
struct __attribute__((__packed__)) Input {
    enum {
        I_PE_left     = 0, 
        I_PE_up       = 1, 
        I_PE_right    = 2, 
        I_PE_down     = 3,
        I_LSU_left    = 4,
        I_LSU_up      = 5,
        I_LSU_right   = 6,
        I_LSU_down    = 7,
        I_LR_0        = 8,
        I_LR_1        = 9,
        I_LR_2        = 10,
        I_IMMI_0      = 11,
        I_IMMI_1      = 12,
        I_IMMI_2      = 13,
        I_UNUSE       = 15
    } src:4;
};  
struct __attribute__((__packed__)) Instr {
    unsigned char un_use:6;
    unsigned char ctl_id:2;
    unsigned char output_ctl_right:2;
    unsigned char output_ctl_down:2;
    unsigned char output_ctl_left:2;
    unsigned char output_ctl_up:2;
    struct Output output_right; 
    struct Output output_down; 
    struct Output output_left; 
    struct Output output_up;
    struct Input inputx;
    struct Input inputy;
    struct Input inputz;
};
2j4z5cfb

2j4z5cfb1#

你不需要把enum嵌入到一个单独的struct中,直接使用它就行了,枚举本身就是位域:

enum Input {
    I_PE_left     = 0, 
    I_PE_up       = 1, 
    I_PE_right    = 2, 
    I_PE_down     = 3,
    I_LSU_left    = 4,
    I_LSU_up      = 5,
    I_LSU_right   = 6,
    I_LSU_down    = 7,
    I_LR_0        = 8,
    I_LR_1        = 9,
    I_LR_2        = 10,
    I_IMMI_0      = 11,
    I_IMMI_1      = 12,
    I_IMMI_2      = 13,
    I_UNUSE       = 15
};

struct Instr {
    enum Input inputx:4;
    enum Input inputy:4;
    enum Input inputz:4;
    enum Input inputzz:4;
};

-fshort-enums编译,enum类型只占1个字节,这样你就给予了一个2字节的结构体,而不用打包。

相关问题