jgarff / rpi_ws281x

Userspace Raspberry Pi PWM library for WS281X LEDs
BSD 2-Clause "Simplified" License
1.76k stars 615 forks source link

Wrong color calculation #522

Closed NBoumakis closed 9 months ago

NBoumakis commented 11 months ago

Hi,

after discussing with @Gadgetoid at #499 I spent a lot of time trying to understand the code for ws2811_render. Using GDB I discovered that the color array has some weird values. After disabling optimizations back to -O0, the values are correct. I highly doubt that this is GCC's fault (though not impossible), so I can only assume some kind of undefined behavior is present in there. Any suggestions are welcome, because to me everything seems normal.

I took the initiative to look into the the decompiled version of the optimized program. It seems to me that it should still be correct, but it isn't. Same goes for the disassembled code.

Any ideas?

UPDATE: Trying different optimization levels suggests that this occurs at -O2. -O1 still has the expected behavior.

NBoumakis commented 9 months ago

Closing this because I realized that since the output is correct, it's probably me that made a mistake. What I think happens is that the compiler emits code to do part of the calculation in registers, leaving some part of the matrix in memory and some there. Sorry for the inconvenience.