STM32 gcc 优化产生不同的结果

问题描述 投票:0回答:1

我正在尝试使用 STM32F4(SW4STM32 IDE 中的 gcc)将 RGB 缓冲区解码为适合 LED RGB 矩阵的行。 当设置编译器优化 -O0 时,下面的代码可以完美运行。

使用 -O1 优化编译时,代码会产生不同的结果。 (还有-O2、-O3)。 当使用 -O1 将 (attribute((packed)) 添加到 struct color_t 定义时,代码也会产生不同的结果。

仅对这个.c文件设置优化,其他文件仍然是-O0。

有人能发现为什么优化改变了代码逻辑/行为吗?

uint8_t badr[24576] = { 0xff , 0x2a, .... };

struct s_color_t
{
    uint8_t r;
    uint8_t g;
    uint8_t b;
} ;
//__attribute__((packed))

typedef struct s_color_t color_t;

#define WIDTH       128
#define HEIGHT      64
#define COLOR_SIZE  sizeof(color_t)

#define R0_POS      0x01
#define G0_POS      0x02
#define B0_POS      0x04
#define R1_POS      0x08
#define G1_POS      0x10
#define B1_POS      0x20

#define enc_color(c, s, r)  (c & s)? r : 0

color_t dispBuf[ WIDTH * HEIGHT];
uint8_t dispLine[WIDTH];

void fillBuf()
{
    uint8_t *dbuf = (uint8_t *) dispBuf;
    memcpy(dbuf, badr, WIDTH * HEIGHT * COLOR_SIZE);
}

void enc_row(uint8_t slice, uint16_t row, uint8_t *rBuf)
{
    uint8_t reg;

    color_t *upPtr = dispBuf + row * WIDTH * COLOR_SIZE;
    color_t *dnPtr = dispBuf + (row + (HEIGHT / 2)) * WIDTH * COLOR_SIZE;
    uint8_t *destPtr = rBuf;

    uint8_t sn = (1 << slice);
    for (int i=0; i < WIDTH; i++) {
        reg = 0;
        reg |= enc_color((*upPtr).r, sn, R0_POS);
        reg |= enc_color((*upPtr).g, sn, G0_POS);
        reg |= enc_color((*upPtr).b, sn, B0_POS);
        reg |= enc_color((*dnPtr).r, sn, R1_POS);
        reg |= enc_color((*dnPtr).g, sn, G1_POS);
        reg |= enc_color((*dnPtr).b, sn, B1_POS);

        *destPtr = reg;
        upPtr ++;
        dnPtr ++;
        destPtr ++;
    }
}

void do_func() 
{
    uint8_t *ptrLine = dispLine;

    fillBuf();
    for (int s=0; s < 8; s++) {
        for (int i=0; i< (HEIGHT/2); i++) {
            enc_row(s, i, ptrLine);
            printf("line %3i ", i);

            for(int c=0; c<WIDTH; c++) {
                printf("%02X, ", ptrLine[c] & 0xFF);
            }
            printf("\r\n");
        }
    }
}
gcc optimization stm32
1个回答
0
投票

谢谢赞·林克斯。 事实上,如果一个人计划稍后使用优化,他应该设置检查点来验证代码是否通过优化产生相同的结果。

上面的错误结果是由指针计算引起的。我替换了上面代码中的两条语句:

color_t *upPtr = dispBuf + row * WIDTH * COLOR_SIZE;
color_t *dnPtr = dispBuf + (row + (HEIGHT / 2)) * WIDTH * COLOR_SIZE;

与:

upPtr = &dispBuf[row * WIDTH];
dnPtr = &dispBuf[(row + (HEIGHT / 2)) * WIDTH];

在调试期间(打开优化),删除了几个函数(即操作由编译器完成。)

就嵌入式而言,其他设备的接口信号也可能会改变速度。您可能会超频或更短的脉冲。

使用 -O1 后代码执行时间减少了 60%。

© www.soinside.com 2019 - 2024. All rights reserved.