Skip to content

refactor(imagepacker): Replace bit counting loop with std::has_single_bit - #3722

Draft
CryoTheRenegade wants to merge 1 commit into
TheSuperHackers:mainfrom
CryoTheRenegade:refactor/std-has-single-bit
Draft

CryoTheRenegade wants to merge 1 commit into
TheSuperHackers:mainfrom
CryoTheRenegade:refactor/std-has-single-bit

Conversation

@CryoTheRenegade

Copy link
Copy Markdown

Follow-up to #3652 in the C++20 adapter series. ImagePacker counts the set bits of the target size in a loop to check for a power of two, which is what C++20 std::has_single_bit does.

ImagePacker::getSettingsFromDialog calls std::has_single_bit, and bit_adapter.h gets a VC6 fallback for it next to bit_ceil.

 // ImagePacker.cpp
-UnsignedInt size, val;
-Int bitCount = 0;
+UnsignedInt size;
 size = GetDlgItemInt( dialog, EDIT_WIDTH, nullptr, FALSE );
-for( val = size; val; val >>= 1 )
-	if( BitIsSet( val, 0x1 ) )
-		bitCount++;
-if( bitCount != 1 )
+if( !std::has_single_bit( size ) )
// bit_adapter.h, VC6 only
template <class T>
inline bool has_single_bit(T x)
{
	return x != 0 && (x & (x - 1)) == 0;
}

A size of zero is still rejected, as the existing comment says.

Evidence

Disassembly of ImagePacker.cpp, main against this branch, ClangCL and VC6 Release, both games. Only getSettingsFromDialog changes, with the same float arithmetic and calls:

ClangCL VC6
Instructions 253 -> 246 273 -> 277
Power-of-two check loop removed; lea, xor, cmp/ja loop removed; x != 0, then lea, test

The VC6 count grows only by alignment nops. ImagePacker is a tool, so no game code changes.

AI Assistance:
Claude Opus 5.5 was used to assist implementation and disassembly

…_bit

The target size check counted the set bits in a loop to test for a power
of two. std::has_single_bit does the same test, including the rejection
of zero. bit_adapter.h adds std::has_single_bit for VC6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The VC6 utility fallback now provides std::has_single_bit. ImagePacker uses it to check whether a custom target size is a power of two and retains the existing error path for invalid sizes, including zero.

Changes

ImagePacker size validation

Layer / File(s) Summary
Add the VC6 has_single_bit fallback
Dependencies/Utility/Utility/bit_adapter.h
The VC6 fallback adds has_single_bit(T x) and documents its lowest-set-bit check and type-check limitation.
Use has_single_bit for target-size validation
Core/Tools/ImagePacker/Source/ImagePacker.cpp
ImagePacker replaces its set-bit counting loop with std::has_single_bit. The existing error path still rejects values that are not powers of two, including zero.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Refactor

Possibly related PRs

Suggested reviewers: xezon, skyaero42

Merge Risk: Minimal · up to 42af5

This change swaps a bit-counting loop for a standard power-of-two check in ImagePacker and adds a VC6 fallback. Behavior should be unchanged, including rejection of zero. No actionable merge risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: ImagePacker's bit-counting loop is replaced with std::has_single_bit. The raw summary and objectives confirm the same change, so the title matches the changeset.
Description check Passed The description covers the change. It explains the loop-to-std::has_single_bit replacement in ImagePacker.cpp, the VC6 fallback in bit_adapter.h, zero rejection, and disassembly evidence. It is relate…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description covers the change. It explains the loop-to-std::has_single_bit replacement in ImagePacker.cpp, the VC6 fallback in bit_adapter.h, zero rejection, and disassembly evidence. It is related to the changeset and detailed.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5f31323a-2ef1-4d6a-a660-8bf6440d6e95
Commits

Reviewing files that changed from the base of the PR and between 967a361 and 42af510.

Files selected for processing: 2

  • Core/Tools/ImagePacker/Source/ImagePacker.cpp
  • Dependencies/Utility/Utility/bit_adapter.h

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

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.

1 participant