Skip to content

Add cuda::bit_ffs to <cuda/bit> - #9609

Merged
miscco merged 8 commits into
NVIDIA:mainfrom
temujinkz:cuda-bit-ffs
Aug 14, 2026
Merged

miscco merged 8 commits into
NVIDIA:mainfrom
temujinkz:cuda-bit-ffs

Conversation

@temujinkz

Copy link
Copy Markdown
Contributor

cuda::bit_ffs(x) returns the 1-based position of the lowest set bit, or 0 if x is zero. The body is x == 0 ? 0 : countr_zero(x) + 1. countr_zero counts trailing zeros, so adding 1 turns it into a 1-based index. The x == 0 guard is the one case countr_zero doesn't define cleanly, and handling it is the whole reason this wrapper exists. It matches __builtin_ffs and CUDA's __ffs. It's constrained to unsigned integers, works on all widths including 128-bit, and is constexpr plus host/device because it just forwards to countr_zero.

Implements a type-safe find-first-set that returns 1 + the index of the
least significant set bit, or 0 for a zero input, matching __builtin_ffs
and CUDA's __ffs (unlike countr_zero). Adds tests and docs.

Closes NVIDIA#6108

Signed-off-by: temujinkz <ttalkenov@gmail.com>
@temujinkz
temujinkz requested review from a team as code owners June 26, 2026 11:06
@temujinkz
temujinkz requested a review from gonidelis June 26, 2026 11:06
@temujinkz
temujinkz requested a review from griwes June 26, 2026 11:06
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jun 26, 2026
@copy-pr-bot

copy-pr-bot Bot commented Jun 26, 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 26, 2026
Comment thread libcudacxx/test/libcudacxx/cuda/bit/bit_ffs.pass.cpp Outdated
Comment thread libcudacxx/test/libcudacxx/cuda/bit/bit_ffs.pass.cpp Outdated
Comment thread libcudacxx/test/libcudacxx/cuda/bit/bit_ffs.pass.cpp Outdated
Comment thread libcudacxx/test/libcudacxx/cuda/bit/bit_ffs.pass.cpp Outdated
Comment thread libcudacxx/test/libcudacxx/cuda/bit/bit_ffs.pass.cpp
@github-project-automation github-project-automation Bot moved this from In Review to In Progress in CCCL Jun 26, 2026
@miscco

miscco commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Thanks a lot for the PR, This already looks great, I have some nits

Comment thread docs/libcudacxx/extended_api/bit/bit_ffs.rst
Comment thread libcudacxx/include/cuda/__bit/bit_ffs.h Outdated
Review feedback from @miscco and @fbusato on NVIDIA#9609:
- route through __builtin_ffs / __builtin_ffsll on host and __ffs / __ffsll on
  device (constexpr fallback) so codegen matches __ffs / __ffsll
- test: use assert instead of static_assert so the runtime path is exercised,
  mark the per-type helper void, [[maybe_unused]] the constant
- test: add a concept check that bit_ffs rejects bool, signed, float, and enums
- docs: add an all-bits-set example

Signed-off-by: temujinkz <ttalkenov@gmail.com>
@coderabbitai

coderabbitai Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added cuda::bit_ffs to <cuda/bit>, returning the 1-based position of the least significant set bit, or 0 for zero.
    • Supports unsigned integer types, including 128-bit integers.
  • Documentation

    • Added API reference documentation, availability details, usage guidance, and examples.
  • Tests

    • Added coverage for supported types, compile-time and runtime evaluation, zero and multi-bit values, and invalid input types.

Walkthrough

Adds cuda::bit_ffs with host, device, and constexpr implementations, transitive inclusion from cuda/bit, extended API documentation, and tests for unsigned inputs and bit-position results.

Changes

Bit FFS API

Layer / File(s) Summary
API surface
libcudacxx/include/cuda/__bit/bit_ffs.h, libcudacxx/include/cuda/bit
Defines cuda::bit_ffs for unsigned integer types. It provides constexpr, host, and device implementations and exposes the API through cuda/bit.
Unsigned-input coverage
libcudacxx/test/libcudacxx/cuda/bit/bit_ffs.pass.cpp
Checks rejected input categories and validates zero, single-bit, and multi-bit behavior across unsigned integer types at runtime and compile time.
Documentation and index
docs/libcudacxx/extended_api/bit.rst, docs/libcudacxx/extended_api/bit/bit_ffs.rst
Documents the API, its 1-based least-significant-set-bit semantics, example usage, and CCCL/CUDA availability. The index links to the new page.

Suggested reviewers: miscco, gonidelis

Merge Risk: ⚪ Minimal · up to 8526d

The PR adds the new bit_ffs API and documentation with no actionable merge-blocking risk remaining; the formatting and documentation follow-ups are non-blocking.


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: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 8617fc92-34c6-45a3-b206-feac28bfbaf5

📥 Commits

Reviewing files that changed from the base of the PR and between 8756dfc and bfb8bd1.

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

Comment thread libcudacxx/test/libcudacxx/cuda/bit/bit_ffs.pass.cpp
@temujinkz

Copy link
Copy Markdown
Contributor Author

Thanks for the fast review, really appreciate it. Pushed an update that should cover everything:

  • @fbusato: bit_ffs now goes through __builtin_ffs / __builtin_ffsll on host and __ffs / __ffsll on device, with a constexpr fallback. I mirrored the host/device/constexpr dispatch from countr_zero so it stays consistent and matches the ffs/ffsll codegen.
  • @miscco: swapped the in-test static_asserts for assert so the runtime path actually runs, made the per-type helper return void, and marked the constant [[maybe_unused]].
  • @miscco: added a concept check so bit_ffs rejects bool, signed integers, float, and enums.
  • @miscco: added the all-bits-set example to the docs.

Let me know if the codegen looks right on your side, happy to tweak anything.

temujinkz added a commit to temujinkz/cccl that referenced this pull request Jun 27, 2026
Pre-empt the same review feedback as bit_ffs (NVIDIA#9609):
- implement bit_msb by forwarding to cuda::std::bit_width, which already lowers to
  the optimal clz based code
- test: use assert instead of static_assert, per-type helper returns void,
  [[maybe_unused]] the constant, and add a concept check rejecting bool, signed,
  float, and enums
- docs: add an all-bits-set example

Signed-off-by: temujinkz <ttalkenov@gmail.com>
Per the IWYU coding guideline (and CodeRabbit), the test used numeric_limits and
size_t through transitive includes. Add the direct headers.

Signed-off-by: temujinkz <ttalkenov@gmail.com>
@temujinkz

Copy link
Copy Markdown
Contributor Author

Good catch, fixed. Added <cuda/std/limits> and <cuda/std/cstddef> directly to the test instead of leaning on transitive includes.

@temujinkz temujinkz left a comment

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.

went back through everything from the last round, should all be covered now. thanks for the thorough pass

Comment thread docs/libcudacxx/extended_api/bit.rst Outdated
Comment thread libcudacxx/include/cuda/__bit/bit_ffs.h Outdated
Comment thread libcudacxx/include/cuda/__bit/bit_ffs.h Outdated
Comment thread libcudacxx/include/cuda/__bit/bit_ffs.h Outdated
Comment thread libcudacxx/include/cuda/__bit/bit_ffs.h Outdated
Comment thread libcudacxx/include/cuda/__bit/bit_ffs.h Outdated
Comment thread libcudacxx/include/cuda/__bit/bit_ffs.h Outdated
Comment thread libcudacxx/include/cuda/__bit/bit_ffs.h Outdated
- detect __builtin_ffs and __builtin_ffsll under separate conditions
  and drop the redundant clang/nvhpc checks; keep the gcc<10 fallback
  since gcc only gained __has_builtin in gcc 10, like the other bit
  headers
- also undef the ffs builtins under nvrtc
- rename __bit_ffs_impl_constexpr to __bit_ffs_impl_generic
- take parameters by const value
- drop the redundant intermediate casts and use
  cuda::std::uint32_t/uint64_t in the size dispatches
- docs: bit_ffs ships in CUDA 13.5
@temujinkz
temujinkz requested review from fbusato and miscco July 15, 2026 10:11
Mark the signature __tile__ and include cuda_runtime_api.h in the
example so it builds as written.
@fbusato

fbusato commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

/ok to test 5698ecb

Comment thread libcudacxx/include/cuda/__bit/bit_ffs.h
Comment thread libcudacxx/include/cuda/__bit/bit_ffs.h Outdated
@github-actions

This comment has been minimized.

Signed-off-by: temujinkz <ttalkenov@gmail.com>
@temujinkz
temujinkz requested a review from fbusato July 17, 2026 08:09

@griwes griwes 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.

This looks good, tests seem appropriate. Kinda wild we didn't have a native wrapper for __ffs yet.

@miscco

miscco commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

/ok to test 8526d61

@coderabbitai

coderabbitai Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@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.

🧹 Nitpick comments (2)
libcudacxx/include/cuda/__bit/bit_ffs.h (2)

35-41: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

suggestion: Update both #endif comments to repeat their complete opening conditions. Lines 37 and 41 omit || _CCCL_COMPILER(GCC, <, 10).

Based on learnings, each annotated #endif comment must repeat the exact verbatim condition from its corresponding #if.

Source: Learnings


119-124: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

suggestion: Convert the bit_ffs API comment to the required Doxygen form. Add //! @brief``, @param[in] __value, and `@return` documentation.

As per coding guidelines, documented functions must include //! @brief``, parameter documentation, and @return for non-void functions.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: cff4a5f2-d179-45ba-a298-83a5e503f268

📥 Commits

Reviewing files that changed from the base of the PR and between 3ae0e48 and 8526d61.

📒 Files selected for processing (5)
  • docs/libcudacxx/extended_api/bit.rst
  • docs/libcudacxx/extended_api/bit/bit_ffs.rst
  • libcudacxx/include/cuda/__bit/bit_ffs.h
  • libcudacxx/include/cuda/bit
  • libcudacxx/test/libcudacxx/cuda/bit/bit_ffs.pass.cpp
🚧 Files skipped from review as they are similar to previous changes (3)
  • libcudacxx/include/cuda/bit
  • docs/libcudacxx/extended_api/bit/bit_ffs.rst
  • docs/libcudacxx/extended_api/bit.rst

@github-actions

Copy link
Copy Markdown
Contributor

⏱️ CCCL compile-time benchmark comparison: Public headers compile-time bench

Result: 0 regression row(s), 1 improvement row(s) above threshold.

Run Value
Config public-headers-gcc13
Baseline origin/main
Preset all-dev
Targets cub.headers.base, thrust.cpp.cuda.headers.base, libcudacxx.test.public_headers
GPU / launch args rtx2080 / --cuda 13.3 --host gcc13

Artifacts: reports and traces

Direct file processing

-f file-processing exclusive --sort total

🟢 Direct file processing — Improvements
Rank Improvement impact Selected Δ Baseline Current Event Matched traces
1 0.629197 -0.629197 5.370743 4.741546 Processing Header File: libcudacxx/include/cuda/std/__cccl/prologue.h 552

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 1h 35m: Pass: 100%/117 | Total: 23h 27m | Max: 48m 03s | Hits: 98%/338174

See results here.

@github-project-automation github-project-automation Bot moved this from In Progress to In Review in CCCL Aug 14, 2026
@miscco
miscco merged commit 2deb899 into NVIDIA:main Aug 14, 2026
145 checks passed
@miscco

miscco commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Thanks a lot for implementing this 🎉

Efficient bit fiddling is dear to my heart

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.

4 participants