Skip to content

fix(examples): replace switch statement with std::popcount in sudoku example - #2251

Open
alwaysprince05 wants to merge 1 commit into
NVIDIA:mainfrom
alwaysprince05:fix/issue-2105-sudoku-assert
Open

fix(examples): replace switch statement with std::popcount in sudoku example#2251
alwaysprince05 wants to merge 1 commit into
NVIDIA:mainfrom
alwaysprince05:fix/issue-2105-sudoku-assert

Conversation

@alwaysprince05

Copy link
Copy Markdown
Contributor

Fixes #2105 - Replace switch statement with std::popcount in sudoku example to fix assert failure in Debug mode

…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>
@copy-pr-bot

copy-pr-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Comment thread examples/sudoku.cpp
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

@ericniebler

Copy link
Copy Markdown
Collaborator

/ok to test f7055d2

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.

example.sudoku: assert in examine_potentials aborts the example in non-NDEBUG builds

2 participants