Skip to content

Use C++20 standard bit utilities and std::bit_cast. NFC - #2773

Merged
sbc100 merged 2 commits into
mainfrom
cxx20-bitops
Jun 24, 2026
Merged

sbc100 merged 2 commits into
mainfrom
cxx20-bitops

Conversation

@sbc100

@sbc100 sbc100 commented Jun 23, 2026

Copy link
Copy Markdown
Member
  • Replace custom Clz, Ctz, and Popcount implementations in config.h.in with standard templates using std::countl_zero, std::countr_zero, and std::popcount from .
  • Redefine wabt::Bitcast to use std::bit_cast.
  • This removes a significant amount of compiler-specific boilerplate.

@sbc100
sbc100 requested review from keithw and zherczeg June 23, 2026 18:32
@sbc100
sbc100 force-pushed the cxx20-bitops branch 2 times, most recently from 2b083fc to 39f6b58 Compare June 23, 2026 20:17
@sbc100
sbc100 enabled auto-merge (squash) June 23, 2026 20:23
@sbc100
sbc100 requested a review from dschuff June 23, 2026 23:03
@zherczeg

Copy link
Copy Markdown
Collaborator

Question: it does not seem the general library needs these functions. Are these intrerpreter specific? If yes, we should move these to there.

@sbc100

sbc100 commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

Question: it does not seem the general library needs these functions. Are these intrerpreter specific? If yes, we should move these to there.

I had the same thought but it seems at least Clz is used in literal.cc. Maybe we could split them up be it seem like a good idea to keep them together.

- Replace custom Clz, Ctz, and Popcount implementations in config.h.in
  with standard templates using std::countl_zero, std::countr_zero,
  and std::popcount from <bit>.
- Redefine wabt::Bitcast to use std::bit_cast.

This removes a significant amount of compiler-specific boilerplate.
@sbc100

sbc100 commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

OK, I ended up just inlining these calls.. since they were all operating on unsigned types anyway.

@sbc100
sbc100 force-pushed the cxx20-bitops branch 4 times, most recently from df00608 to ac89a79 Compare June 24, 2026 04:16

@zherczeg zherczeg left a comment

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.

LGTM. I like this code variant.

@sbc100
sbc100 merged commit 274f26b into main Jun 24, 2026
17 checks passed
@sbc100
sbc100 deleted the cxx20-bitops branch June 24, 2026 04:38
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.

2 participants