Skip to content

Add cuda::bit_msb to <cuda/bit> - #9624

Merged
davebayer merged 9 commits into
NVIDIA:mainfrom
temujinkz:cuda-bit-msb
Oct 26, 2026
Merged

davebayer merged 9 commits into
NVIDIA:mainfrom
temujinkz:cuda-bit-msb

Conversation

@temujinkz

Copy link
Copy Markdown
Contributor

Addresses the msb request from #6108 (sibling of bit_ffs in #9609).

bit_msb returns the zero-based index of the most significant set bit, floor(log2(value)), and -1 for zero. It forwards to cuda::std::__bit_log2 so it reuses the existing bfind/clz lowering rather than adding new codegen.

I went with the zero-based convention to match __bit_log2, since a 1-based version would just be bit_width. Easy to flip to 1-based if you'd prefer. Tests and a docs page included.

bit_msb returns the zero-based index of the most significant set bit
(floor(log2(value))), or -1 when value is zero. It forwards to
cuda::std::__bit_log2 so it reuses the optimal bfind/clz lowering, and
exposes that internal primitive type safely on all unsigned integers.

Addresses the msb request in NVIDIA#6108.

Signed-off-by: temujinkz <ttalkenov@gmail.com>
@temujinkz
temujinkz requested review from a team as code owners June 29, 2026 09:20
@temujinkz
temujinkz requested a review from alliepiper June 29, 2026 09:20
@temujinkz
temujinkz requested a review from davebayer June 29, 2026 09:20
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jun 29, 2026
@copy-pr-bot

copy-pr-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

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.

@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jun 29, 2026
@coderabbitai

coderabbitai Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in 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

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: NVIDIA/cccl
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: baaeed7
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: fd7f2306-2d80-458c-90e6-3efe1975186a
Commits

Reviewing files that changed from the base of the PR and between d5103f5 and d95857a.

Files selected for processing: 1
  • docs/libcudacxx/extended_api/bit/bit_msb.rst
Files skipped from review as they are similar to previous changes: 1
  • docs/libcudacxx/extended_api/bit/bit_msb.rst

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

📝 Summary

Summary by CodeRabbit

  • New Features
    • Added cuda::bit_msb to <cuda/bit> for unsigned integer values. It returns the zero-based position of the most significant set bit, or -1 when the value is zero.
  • Documentation
    • Added API documentation describing the function’s input requirements, return values, and relationship to related bit operations, with a CUDA usage example.
    • Listed the API in the bit availability table, including availability for CCCL 3.7.0 and CUDA 13.7.

Adds cuda::bit_msb, a constrained constexpr function that returns the most significant set-bit index or -1 for zero. The API is exposed through cuda/bit, tested across unsigned integer types, and documented with availability metadata and examples.

Changes: cuda::bit_msb

Layer / File(s) Summary
Header implementation and public wiring
libcudacxx/include/cuda/__bit/bit_msb.h, libcudacxx/include/cuda/bit
Defines bit_msb for unsigned integer types as a wrapper over __bit_log2 and includes it from cuda/bit.
Constexpr and runtime validation
libcudacxx/test/libcudacxx/cuda/bit/bit_msb.pass.cpp
Checks accepted and rejected types, plus zero, single-bit, and multi-bit results across supported unsigned integer types.
API reference documentation
docs/libcudacxx/extended_api/bit.rst, docs/libcudacxx/extended_api/bit/bit_msb.rst
Adds the reference page, example, toctree entry, capability description, and CCCL/CUDA availability metadata.

Possibly related issues

  • important: NVIDIA/cccl#11989 — The issue describes a test pattern that can remove runtime coverage on Clang; the new test uses the same assert(test()); static_assert(test()); pattern.

Possibly related PRs

  • important: NVIDIA/cccl#12220 — Changes the __bit_log2 device lowering and preserves the zero sentinel used by bit_msb.

Suggested reviewers: pciolkosz

Priority: Normal

Change: Feature

Merge Risk: Minimal · up to d9585

The documentation does not introduce the reported broken link. No issue requiring a fix before merge was established.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@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: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 172f9c7a-18ac-49a7-bd73-6af7368b9912

📥 Commits

Reviewing files that changed from the base of the PR and between 0d8f162 and 6d37a1f.

📒 Files selected for processing (5)
  • docs/libcudacxx/extended_api/bit.rst
  • docs/libcudacxx/extended_api/bit/bit_msb.rst
  • libcudacxx/include/cuda/__bit/bit_msb.h
  • libcudacxx/include/cuda/bit
  • libcudacxx/test/libcudacxx/cuda/bit/bit_msb.pass.cpp

Comment on lines +41 to +55
#include <cuda/bit>
#include <cuda/std/cassert>
#include <cuda/std/cstdint>

__global__ void bit_msb_kernel() {
assert(cuda::bit_msb(uint32_t{0}) == -1);
assert(cuda::bit_msb(uint32_t{1}) == 0);
assert(cuda::bit_msb(uint32_t{0b10101000}) == 7);
assert(cuda::bit_msb(~uint32_t{0}) == 31);
}

int main() {
bit_msb_kernel<<<1, 1>>>();
cudaDeviceSynchronize();
return 0;

@coderabbitai coderabbitai Bot Jun 29, 2026 •

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

important: Make the example self-contained.

The snippet calls cudaDeviceSynchronize() but never includes a CUDA runtime header, so it is not buildable as written. Add the appropriate runtime include (for example, the project-preferred CUDA runtime API header) or drop the synchronization call from the example. As per path instructions, documentation changes should prioritize buildable examples and technical accuracy.

Source: Path instructions

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.

@temujinkz this comment makes sense

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.

@fbusato Sounds good — leaving this for temujinkz to update the example with the missing CUDA runtime include (or drop cudaDeviceSynchronize()).

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.

Added <cuda_runtime_api.h> to the example includes, same header is_pointer_accessible.rst uses. Applied the same fix to the bit_ffs example in #9609 since it had the identical pattern.

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.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

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.

Please, remove the include, this header is always included during CUDA compilation by the compiler

Comment thread libcudacxx/include/cuda/__bit/bit_msb.h Outdated
@fbusato

fbusato commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

/ok to test 6d37a1f

@github-actions

This comment has been minimized.

Comment thread libcudacxx/include/cuda/__bit/bit_msb.h Outdated
Comment thread libcudacxx/test/libcudacxx/cuda/bit/bit_msb.pass.cpp Outdated

@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. A few minor suggestions

Comment thread docs/libcudacxx/extended_api/bit.rst Outdated
Comment thread libcudacxx/include/cuda/__bit/bit_msb.h Outdated
Comment thread docs/libcudacxx/extended_api/bit/bit_msb.rst Outdated
Comment thread libcudacxx/test/libcudacxx/cuda/bit/bit_msb.pass.cpp
Comment thread libcudacxx/test/libcudacxx/cuda/bit/bit_msb.pass.cpp Outdated
Comment thread docs/libcudacxx/extended_api/bit/bit_msb.rst Outdated
- convert the header comment to a doxygen docstring and take the
  argument by const value
- test: trim the type list to the fundamental unsigned types plus
  __uint128_t, replace the concept detector with a plain SFINAE
  variable template, and fix a copy-paste comment
- docs: use math notation for the floor(log2) formula, say unsigned
  integer types in the note, and set the CUDA availability to 13.5
@fbusato

fbusato commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

/ok to test 88bbfe2

Comment thread docs/libcudacxx/extended_api/bit/bit_msb.rst Outdated
Comment thread libcudacxx/include/cuda/__bit/bit_msb.h Outdated
@github-actions

This comment has been minimized.

The bit_ffs page lives in a separate PR, so the :ref: label does not
exist on this branch and the docs build treats the dangling reference
as an error. Use plain text for now; the link can be restored once
both pages are in main.
- assume the result range after __bit_log2 so the optimizer can rely
  on it
- docs: mark the signature __tile__ and include cuda_runtime_api.h in
  the example so it builds as written
@temujinkz

Copy link
Copy Markdown
Contributor Author

Went through everything from the last rounds: doxygen docstring, const argument, math notation, the unsigned wording in the note, CUDA 13.5, the trimmed type list, and the SFINAE detector (one tweak: dectype in the snippet fixed to decltype). Today's items too: __tile__ in the docs signature, <cuda_runtime_api.h> in the example, and the _CCCL_ASSUME range hint.

@temujinkz

Copy link
Copy Markdown
Contributor Author

Two extra fixes after the red CI on 88bbfe2: the docs build was failing because the bit_ffs cross reference points at a page that only exists in #9609, so I unlinked it to plain text for now, and I merged main to pick up the clang-tidy-22 suppressions the clang-tidy job needed.

@fbusato

fbusato commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

/ok to test 295df5d

@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
  • Source mode: Auto
  • Repository: NVIDIA/cccl
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: baaeed7
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 70c55bbe-20b8-474f-8b32-64552b4eaaea
📥 Commits

Reviewing files that changed from the base of the PR and between 88bbfe2 and 92ab995.

📒 Files selected for processing: 4
  • docs/libcudacxx/extended_api/bit.rst
  • docs/libcudacxx/extended_api/bit/bit_msb.rst
  • libcudacxx/include/cuda/__bit/bit_msb.h
  • libcudacxx/include/cuda/bit

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

Comment thread libcudacxx/include/cuda/__bit/bit_msb.h
@fbusato

fbusato commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

/ok to test 92ab995

@github-actions

This comment has been minimized.

Comment thread docs/libcudacxx/extended_api/bit/bit_msb.rst Outdated
Comment thread docs/libcudacxx/extended_api/bit/bit_msb.rst
@davebayer

Copy link
Copy Markdown
Contributor

/ok to test d95857a

@davebayer
davebayer enabled auto-merge (squash) October 26, 2026 07:02
@davebayer

Copy link
Copy Markdown
Contributor

pre-commit.ci autofix

@davebayer

Copy link
Copy Markdown
Contributor

/ok to test 63228c6

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 2h 43m: Pass: 100%/228 | Total: 3d 02h | Max: 1h 47m | Hits: 99%/623210

See results here.

@davebayer
davebayer merged commit c4451c1 into NVIDIA:main Oct 26, 2026
255 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants