Skip to content

Reimplement overload-and-remove palette-packing algorithm with undivided fractions to avoid integer overflow - #2250

Merged
ISSOtm merged 3 commits into
gbdev:masterfrom
Rangi42:pal-fractions
Nov 29, 2026
Merged

ISSOtm merged 3 commits into
gbdev:masterfrom
Rangi42:pal-fractions

Conversation

@Rangi42

@Rangi42 Rangi42 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #2245, supersedes #2246.

This should be reviewed one commit at a time and rebase-merged, not squash-merged. The first two commits are preliminary: I wanted the MAX_NB_PALETTES constant for this PR, and noticed while adding it that our CLI arg handling was off, e.g. treating -d 258 like a valid -d 2. The last commit is a refactor I'm not entirely sure is worthwhile.

The justification for this PR is explained in the issue. Many thanks to @aaaaaa123456789 for feedback on that problem, confirming that the previous UB was expected and can be avoided; and to @vulcandth for the Astra PR that this one is based on.

I had the initial idea for Fractions that Astra implemented, and in this PR, I independently rederived the logic about maximum limits which prove that overflow can't occur. (I actually only skimmed Astra's explanation, so after calculating the numeric values of my MAX_PAL_COLORS/MAX_NB_PALETTES/MAX_NB_COLOR_SETS formulas and seeing the exact same numbers in Astra's analysis, it raises my confidence that both are correct.)

I did base the Fraction::compare implementation on Astra's, after re-deriving and commenting it to trust that it's correct. Two test cases are also by Astra -- they're important because they would fail if I'd just used the "minimal fix" shortcuts suggested by my initial analysis with DeepSeek 4.1 Flash. (I manually -reversed all the test cases to make sure the .output is correct.)

Argubaly I overdid it with the comments and assertions, but this is the third reimplementation of the relative-size/efficiency calculation (after we discovered issues with floating-point and then with LCM), so I want to err on the side of being clear and certain. @ISSOtm Feel free to reword or trim them though!


In addition to the committed test cases, I tried the maximal 768-tile foo.png (which just barely fits into 256 palettes):

foo

With a regular optimized build, time ./rgbgfx -n 256 -o foo.2bpp -OPQT foo.png takes just real 0m1.736s on my machine. However, with a develop build, it takes real 2m53.344s, which would be prohibitively slow for our CI.

This relatively slow performance is expected; from the original paper:

In the worst case, a given [color set] might successively overload and be removed from all [palettes], whose total number is at most |T|. Each trial requires one set intersection on each [palette]. Hence, the overall complexity is O(|T|^3 Card(T)).

You can see how that O(768^3) pans out with -vvvvv verbose logging. It's a very unrealistic case though, so not something to worry about. :P

@Rangi42 Rangi42 added this to the 1.0.5 milestone Nov 21, 2026
@Rangi42
Rangi42 requested a review from ISSOtm November 21, 2026 14:58
@Rangi42 Rangi42 added bug Unexpected behavior / crashes; to be fixed ASAP! rgbgfx This affects RGBGFX labels Nov 21, 2026
@Rangi42

Rangi42 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Confirmed that this works on Apple OSes older than what CI provides (macOS 11 with clang++ 22, and Mac OS X 10.8 with g++ 14, both available on MacPorts).

@ISSOtm

ISSOtm commented Nov 25, 2026

Copy link
Copy Markdown
Member

Could be nice to have a those “overly slow tests” still in the repo so they are less likely to get lost to time than in a PR or issue thread, even if they aren't tested (at least by default). Idk.

@ISSOtm ISSOtm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked at the meat of the changes yet, but here are already some comments.

Comment thread src/gfx/pal_packing.cpp Resolved
Comment thread src/gfx/pal_packing.cpp Resolved
Comment thread src/gfx/pal_packing.cpp Resolved
Comment thread src/gfx/pal_packing.cpp Resolved
Comment thread src/gfx/pal_packing.cpp Resolved
Comment thread src/gfx/pal_packing.cpp Resolved
@Rangi42
Rangi42 requested a review from ISSOtm November 26, 2026 15:46

@ISSOtm ISSOtm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you for thinking all of this through :) Just a few relatively minor things left, the core logic is a-OK to me!

Comment thread src/gfx/pal_packing.cpp Resolved
Comment thread src/gfx/pal_packing.cpp Resolved
Comment thread src/gfx/pal_packing.cpp Resolved
Comment thread src/gfx/pal_packing.cpp Resolved
Comment thread src/gfx/pal_packing.cpp Resolved
This matches the design of the other three programs, and avoids
silent overflow of `uint16_t` or `uint8_t` bounds.
…ded fractions to avoid integer overflow

Assisted-by: Codex:GPT-6

@ISSOtm ISSOtm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Thanks <3

@ISSOtm
ISSOtm merged commit c2c8f78 into gbdev:master Nov 29, 2026
31 checks passed
@Rangi42
Rangi42 deleted the pal-fractions branch November 29, 2026 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected behavior / crashes; to be fixed ASAP! rgbgfx This affects RGBGFX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Computing the "relative size" of a color set has a wrong assumption

2 participants