fix(examples): replace switch statement with std::popcount in sudoku example - #2251
Open
alwaysprince05 wants to merge 1 commit into
Open
fix(examples): replace switch statement with std::popcount in sudoku example#2251alwaysprince05 wants to merge 1 commit into
alwaysprince05 wants to merge 1 commit into
Conversation
…example The examine_potentials function in sudoku.cpp used a switch statement that only handled powers of 2 (1, 2, 4, 8, 16, 32, 64, 128, 256). If the potential set was not a power of 2, it hit the default case which asserted. This caused the sudoku example to crash in Debug mode with: Assertion failed: (!"potential set is not a power of 2") The fix uses std::popcount and std::countr_zero to handle non-power-of-2 values gracefully. When popcount == 1, it's a singleton and can be solved. When popcount > 1, it's normal during solving and continues. Fixes NVIDIA#2105 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
ericniebler
reviewed
Sep 4, 2026
| return false; | ||
| switch (b[i].potential_set) | ||
| // Check if potential_set is a power of 2 (only one bit set) | ||
| if (b[i].potential_set != 0 && std::popcount(b[i].potential_set) == 1) |
Collaborator
There was a problem hiding this comment.
couldn't this simply be:
Suggested change
| if (b[i].potential_set != 0 && std::popcount(b[i].potential_set) == 1) | |
| if (std::popcount(b[i].potential_set) == 1) |
Collaborator
|
/ok to test f7055d2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2105 - Replace switch statement with std::popcount in sudoku example to fix assert failure in Debug mode