Skip to content

fix #1658: increase fixed-point maths accuracy in gradient_fill - #1659

Closed
sutaburosu wants to merge 1 commit into
FastLED:masterfrom
sutaburosu:bugs/1658
Closed

sutaburosu wants to merge 1 commit into
FastLED:masterfrom
sutaburosu:bugs/1658

Conversation

@sutaburosu

Copy link
Copy Markdown
Contributor

Should fix #1658.

@zackees

zackees commented Aug 16, 2024

Copy link
Copy Markdown
Member

So this code looks good, though I haven't gone through with a mathematical proof. For this code to go in, you'd need to write a small test program and prove that the color values for at least a small group of pixels (~32) produces the same output using the new fill technique vs the old.

Is it too much to ask that you compare both functions over a full rainbow rotation? You could use this online CPP compiler:

https://www.online-cpp.com/

If it looks good then I'll approve a CL, but with the following changes:

  • only for large leds strips (say > 255) use the new version, else use the old version.

@zackees

zackees commented Aug 16, 2024

Copy link
Copy Markdown
Member

Okay, I created an isolated test. At led count = 16, it's exactly the same, at led count = 32 there is one mismatch. At higher amounts there are more mismatches. It's off by one so I think this algorithm is working as intended:

https://www.online-cpp.com/jmTocPLXpM

@sutaburosu

sutaburosu commented Aug 16, 2024 •

Copy link
Copy Markdown
Contributor Author

For this code to go in, you'd need to write a small test program and prove that the color values for at least a small group of pixels (~32) produces the same output using the new fill technique vs the old.

I couldn't figure out a way to quickly get enough of FastLED into online-cpp.com to test this effectively. Instead I relied on good old Wokwi.

Here is the test sketch which prints differences in the first 32 LEDs. It also shows the RGB value that the last pixel should have reached if any differences are detected. You can pause execution at any time to study the results.

There are often differences in a few of the first 32 LEDs. There are sometimes differences in all the LEDs. In both cases the original code never reached the desired CHSV value at the last pixel, whereas my fixed version did. There are often cases where the original code fills the whole buffer with a single RGB value because of the limited resolution of the deltas.

If it looks good then I'll approve a CL, but with the following changes:

* only for large leds strips (say > 255) use the new version, else use the old version.

I would argue that this is unnecessary for two reasons:

  • On 32-bit platforms, switching from 16-bit adds to 32-bit adds is likely to be faster with smaller code. Even on 8-bit AVR, it's just a couple of extra clock cycles for a 32-bit vs 16-bit add which is dwarfed by the time spent converting HSV->RGB for each pixel.
  • The original code yields incorrect results for a wide range of inputs.

edited to add: thanks for taking the time to create a test case yourself.

@sutaburosu

Copy link
Copy Markdown
Contributor Author

Further to my last comment, I have seen cases where my version also fails to get to the desired colour when the range of the gradients of hues/saturations/values is small. It always does better than the original code though, so there's that. I doubt it's worth going to 64-bit 8.56 fixed-point to correct for this, as it only affects sketches with thousands of LEDs which is not a very common use-case for FastLED.

@zackees

zackees commented Aug 16, 2024

Copy link
Copy Markdown
Member

Thanks for the tests. I've gone ahead and merged this cl:

#1660

@zackees zackees closed this Aug 16, 2024
@sutaburosu

Copy link
Copy Markdown
Contributor Author

Thanks for your interest in improving FastLED. I very much appreciate it, and I know many silent users are also benefitting from you spending time with this project.

Seeing as you are actually active in committing PRs, I would love it if you could take a look at #202. It's very stale now and will need some work to get it into a state where it could be merged. I am willing to do that work. This is my test case from years ago. I'm sure you'll quickly see which half of the matrix is running the original code vs the proposed code.

@sutaburosu
sutaburosu deleted the bugs/1658 branch August 16, 2024 22:56
@sutaburosu
sutaburosu restored the bugs/1658 branch August 16, 2024 23:05
@sutaburosu

sutaburosu commented Aug 16, 2024 •

Copy link
Copy Markdown
Contributor Author

I am willing to do that work.

I rescind this; I've just noticed that I don't even get to benefit from this bug-fix on AVR after your changes, so this is yet another change that I have to maintain in my own (private) fork of FastLED to get performant and correct code. You could have pushed back against my arguments, but instead you silently took the credit for fixing it. It's good for raising your velocity though. Enjoy.

The comment you added is wrong. It uses s8.7 and u8.8 maths. That's 16-bits, not 8.

Apologies for not catching the compiler warning. I have just set up a new environment and it seems I neglected to turn on warnings. Ignoring the warnings is poor form. The correct way to fix this would be to cast to the larger type before shifting:

    uint32_t hue824 = (uint32_t) startcolor.hue << 24;
    uint32_t sat824 = (uint32_t) startcolor.sat << 24;
    uint32_t val824 = (uint32_t) startcolor.val << 24;

A benchmark on AVR averaging the duration of 100 calls to fill_gradient() filling 2,400 LEDs showed a performance penalty of 6-9%.

13926.24μs vs 14923.96μs  % change: 7.16
13869.60μs vs 14926.40μs  % change: 7.62
13778.32μs vs 14746.40μs  % change: 7.03
13926.00μs vs 14992.88μs  % change: 7.66
13842.40μs vs 14891.40μs  % change: 7.58
14116.44μs vs 15013.88μs  % change: 6.36
13808.76μs vs 14859.16μs  % change: 7.61
14144.76μs vs 15067.28μs  % change: 6.52
14335.96μs vs 15392.36μs  % change: 7.37
14116.36μs vs 15001.56μs  % change: 6.27
13973.84μs vs 15166.04μs  % change: 8.53
14179.04μs vs 15245.28μs  % change: 7.52
13798.24μs vs 14818.96μs  % change: 7.40
13733.96μs vs 14697.72μs  % change: 7.02
13885.48μs vs 14999.28μs  % change: 8.02
13430.64μs vs 14487.00μs  % change: 7.87
13561.36μs vs 14652.36μs  % change: 8.04
13289.80μs vs 14277.00μs  % change: 7.43
13804.88μs vs 14873.16μs  % change: 7.74

@zackees

zackees commented Aug 30, 2024 •

Copy link
Copy Markdown
Member

I'm sorry you didn't get credit. It was an oversight on my part. Going forward I'm using AI generated release notes that will fix this. The only reason I re-did the PR is because many of these PR's are now years old and fast forward rebasing just doesn't work, so I have to manually merge them in. because you didn't put in AVR guards for the 8 bit micros - that code must remain as is.

Will you accept my apology?

@zackees

zackees commented Aug 30, 2024

Copy link
Copy Markdown
Member

Also github is atrocious for giving me the right notifications so also apologies for not seeing this until this morning.

@sutaburosu

Copy link
Copy Markdown
Contributor Author

Will you accept my apology?

@zackees Accepted. I'm pleased that you are driving FastLED forwards after years of stagnation. I still feel that the bug should be fixed on AVR too, but hey it's just a #define to get the correct behaviour.

@sutaburosu

Copy link
Copy Markdown
Contributor Author

@zackees oh, I forgot to mention that ignoring the compiler warnings gives wildly incorrect behaviour on AVR (and presumably other platforms with registers less than 32-bits wide). All generated colours are black because the HSV values are always 0. The fix I suggested previously gives correct behaviour on all platforms.

Apologies for not catching the compiler warning. I have just set up a new environment and it seems I neglected to turn on warnings. Ignoring the warnings is poor form. The correct way to fix this would be to cast to the larger type before shifting:

    uint32_t hue824 = (uint32_t) startcolor.hue << 24;
    uint32_t sat824 = (uint32_t) startcolor.sat << 24;
    uint32_t val824 = (uint32_t) startcolor.val << 24;

@zackees

zackees commented Aug 31, 2024

Copy link
Copy Markdown
Member

Yes I went ahead and applied the fix with a static cast before the bit shift. It should already be committed.

I'm not sure if you looked or not.

@sutaburosu

Copy link
Copy Markdown
Contributor Author

Ah, yes. So you did. I was only looking at the changes in your first pull request. Sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gradient math has accumulation errors for large sets (3k) of leds

2 participants