Skip to content

[libcu++] Implement P3104R6 Bit permutations - #10063

Merged
davebayer merged 3 commits into
NVIDIA:mainfrom
davebayer:bit_perms
Jul 30, 2026
Merged

[libcu++] Implement P3104R6 Bit permutations#10063
davebayer merged 3 commits into
NVIDIA:mainfrom
davebayer:bit_perms

Conversation

@davebayer

Copy link
Copy Markdown
Contributor

No description provided.

@davebayer
davebayer requested a review from a team as a code owner July 22, 2026 07:13
@davebayer
davebayer requested a review from ericniebler July 22, 2026 07:13
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jul 22, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jul 22, 2026
@davebayer davebayer linked an issue Jul 22, 2026 that may be closed by this pull request
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Changes

Adds cuda::std bit reversal, repetition, compression, and expansion utilities for unsigned integers. The umbrella header exports them, with constexpr and runtime tests across supported unsigned widths.

Bit operation utilities

Layer / File(s) Summary
Primitive bit operations
libcudacxx/include/cuda/std/__bit/bit_reverse.h, libcudacxx/include/cuda/std/__bit/bit_repeat.h, libcudacxx/test/.../bit_reverse.pass.cpp, libcudacxx/test/.../bit_repeat.pass.cpp
Adds constrained bit_reverse and bit_repeat APIs with type, noexcept, constexpr, runtime, and optional 128-bit coverage.
Masked bit transforms
libcudacxx/include/cuda/std/__bit/bit_compress.h, libcudacxx/include/cuda/std/__bit/bit_expand.h, libcudacxx/include/cuda/std/bit, libcudacxx/test/.../bit_compress.pass.cpp, libcudacxx/test/.../bit_expand.pass.cpp
Adds mask-driven compression and expansion, exports the facilities through cuda/std/bit, and validates both implementations at runtime and compile time.

Suggested reviewers: ericniebler


Comment @coderabbitai help to get the list of available commands.

Comment thread libcudacxx/include/cuda/std/__bit/bit_compress.h
Comment thread libcudacxx/include/cuda/std/__bit/bit_compress.h Outdated
Comment thread libcudacxx/test/libcudacxx/std/numerics/bit/bit.count/bit_compress.pass.cpp Outdated
Comment thread libcudacxx/test/libcudacxx/std/numerics/bit/bit.count/bit_compress.pass.cpp Outdated
@github-actions

This comment has been minimized.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 71b3dca6-5e9a-45ad-8e43-fd768a1f3e9a

📥 Commits

Reviewing files that changed from the base of the PR and between c2f0c18 and dedcdb7.

📒 Files selected for processing (9)
  • libcudacxx/include/cuda/std/__bit/bit_compress.h
  • libcudacxx/include/cuda/std/__bit/bit_expand.h
  • libcudacxx/include/cuda/std/__bit/bit_repeat.h
  • libcudacxx/include/cuda/std/__bit/bit_reverse.h
  • libcudacxx/include/cuda/std/bit
  • libcudacxx/test/libcudacxx/std/numerics/bit/bit.count/bit_compress.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/bit/bit.count/bit_expand.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/bit/bit.count/bit_repeat.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/bit/bit.count/bit_reverse.pass.cpp
🚧 Files skipped from review as they are similar to previous changes (5)
  • libcudacxx/include/cuda/std/__bit/bit_reverse.h
  • libcudacxx/include/cuda/std/bit
  • libcudacxx/include/cuda/std/__bit/bit_repeat.h
  • libcudacxx/include/cuda/std/__bit/bit_expand.h
  • libcudacxx/include/cuda/std/__bit/bit_compress.h

@davebayer
davebayer enabled auto-merge (squash) July 23, 2026 15:24

@fbusato fbusato left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks good. Left a couple of minor issues

Comment thread libcudacxx/include/cuda/std/__bit/bit_repeat.h
Comment thread libcudacxx/include/cuda/std/__bit/bit_reverse.h
Comment thread libcudacxx/test/libcudacxx/std/numerics/bit/bit.count/bit_compress.pass.cpp Outdated
#if _CCCL_HAS_INT128()
test<__uint128_t>();
#endif // _CCCL_HAS_INT128()
return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

note. Other bit operations also test for aliases. I think it makes more sense to only test for builtin types. Just a consistency note.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I know, but I don't see the motivation behind testing the same types, just as aliases, this should cover all cases

Comment thread libcudacxx/test/libcudacxx/std/numerics/bit/bit.count/bit_expand.pass.cpp Outdated
@davebayer
davebayer disabled auto-merge July 23, 2026 17:35
@github-actions

This comment has been minimized.

Comment thread libcudacxx/include/cuda/std/__bit/bit_compress.h
Comment thread libcudacxx/include/cuda/std/__bit/bit_compress.h
const auto __n = ::cuda::std::countl_one(__mask_rev);

// Write __n consecutive bits.
const auto __segment = static_cast<_Tp>(__v & ::cuda::bitmask<_Tp>(0, __n));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we really need a bitmask here, or can we do the simple shift?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

bitmask is more efficient on device, it produces a single BMSK instruction

Comment thread libcudacxx/include/cuda/std/__bit/bit_repeat.h Outdated
Comment on lines +53 to +61
const auto __pattern = static_cast<_Tp>(__v & ::cuda::bitmask<_Tp>(0, __n));
_Tp __ret = __pattern;
for (int __i = __n; __i < __width; __i += __n)
{
__ret <<= __n;
__ret |= __pattern;
}
return __ret;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Question: I am slightly worried about smaller than int sized integers. Wouldnt it be more efficient to promote to 32 bit and then cast back?

The masked bit operations are probably not efficient

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Probably? I wanted to implement the simpler cases first and then we can optimize every algorithm separately

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread libcudacxx/include/cuda/std/__bit/bit_reverse.h
const auto __n = ::cuda::std::countl_one(__mask_rev);

// Write __n consecutive bits.
const auto __segment = static_cast<_Tp>(__v & ::cuda::bitmask<_Tp>(0, __n));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Question: Should we use the shl instruction on device?

At least clang knows some compiler builtins (__builtin_ia32_pext_di and __builtin_ia32_pext_si) for that https://www.mail-archive.com/cfe-commits@lists.llvm.org/msg706509.html

GCC knows _pext_u32 and _pext_u64 but only with -mbmi2 We might want to investigate if it is worth to optimize for clang-cuda here or at least add this as a followup

We will probably need the fallback for constexpr evaluation anyhow

return __v;
}

const auto __pattern = static_cast<_Tp>(__v & ::cuda::bitmask<_Tp>(0, __n));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto we should be able to use compiler builtins for that

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 2h 39m: Pass: 100%/120 | Total: 1d 22h | Max: 1h 48m | Hits: 100%/358474

See results here.

@davebayer
davebayer merged commit 5c1d40f into NVIDIA:main Jul 30, 2026
138 of 140 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Jul 30, 2026
@davebayer
davebayer deleted the bit_perms branch July 30, 2026 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[FEA]: P3104R6 Bit permutations

4 participants