Skip to content

[libcu++] Optimize cuda::std::saturating_cast#9724

Open
davebayer wants to merge 1 commit into
NVIDIA:mainfrom
davebayer:optimize_saturating_cast
Open

[libcu++] Optimize cuda::std::saturating_cast#9724
davebayer wants to merge 1 commit into
NVIDIA:mainfrom
davebayer:optimize_saturating_cast

Conversation

@davebayer

Copy link
Copy Markdown
Contributor

We can use inline PTX to make ptxas generate a single I2I instruction in most of the cases.

I've also renamed {op}_sat tests to saturating_{op}, I forgot to do that when renaming those functions.

@davebayer davebayer requested a review from a team as a code owner July 7, 2026 06:56
@davebayer davebayer requested a review from Jacobfaib July 7, 2026 06:56
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jul 7, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jul 7, 2026
@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 6fc2caed-2c56-4302-bc8a-2f83fa313c9e

📥 Commits

Reviewing files that changed from the base of the PR and between 2b4d39f and a23b125.

📒 Files selected for processing (8)
  • libcudacxx/include/cuda/__numeric/saturating_overflow_cast.h
  • libcudacxx/include/cuda/std/__numeric/saturating_cast.h
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_add.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_cast.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_div.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_mul.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_sub.pass.cpp
💤 Files with no reviewable changes (1)
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp
🚧 Files skipped from review as they are similar to previous changes (3)
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_cast.pass.cpp
  • libcudacxx/include/cuda/__numeric/saturating_overflow_cast.h
  • libcudacxx/include/cuda/std/__numeric/saturating_cast.h

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Improved CUDA device-side cuda::std::saturating_cast with optimized conversion paths for common integer size/signedness combinations.
  • Bug Fixes
    • Refined narrowing overflow detection to compile out overflow checks when it’s impossible, preserving identical results for in-range values.
    • Adjusted specific device-side negative-input narrowing corner cases on newer architectures.
  • Tests
    • Added a new saturating_cast pass test covering many integer type combinations with constexpr/runtime validation.
    • Removed an older overlapping saturating-cast test.

Walkthrough

This PR tightens saturating_overflow_cast, adds a device-side PTX-backed saturating_cast path, and replaces the saturating-cast test with broader integer coverage.

Changes

Saturating cast device implementation and tests

Layer / File(s) Summary
Compile-time overflow guard
libcudacxx/include/cuda/__numeric/saturating_overflow_cast.h
Overflow branches now use if constexpr checks around _Tp min and max range comparisons.
Device PTX saturating_cast path
libcudacxx/include/cuda/std/__numeric/saturating_cast.h
New device helper emits cvt.sat.* PTX for supported integer conversions, falls back to saturating_overflow_cast, and public saturating_cast dispatches to the device path on NV_IS_DEVICE outside constant evaluation.
saturating_cast test replacement
libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_cast.pass.cpp
New pass test checks return values, return type, noexcept, signed and unsigned edge cases, and compile-time evaluation across integer type combinations.

Suggested reviewers: miscco, bernhardmgruber

Warning

Tools execution failed with the following error:

Failed to run tools: 13 INTERNAL: Received RST_STREAM with code 2 (Internal server error)


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.

🧹 Nitpick comments (2)
libcudacxx/include/cuda/std/__numeric/saturating_cast.h (1)

34-337: 📐 Maintainability & Code Quality | 🔵 Trivial

suggestion: this branch matrix (4 size classes × up to 4 sign combos) is highly repetitive — same shape of if constexpr + asm(...) + assignment repeated ~30 times. Consider a small helper macro parameterized by destination/source PTX type suffixes and register constraints to cut duplication, if consistent with how similar per-width/per-sign PTX dispatch is done elsewhere in the codebase.

libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_cast.pass.cpp (1)

22-52: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

suggestion: add a device-side call site here, mirroring saturating_div.pass.cpp, so the NV_IS_DEVICE branch in cuda::std::saturating_cast is actually exercised. Right now this file only covers host/static-evaluation paths, leaving __saturating_cast_impl_device untested.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 1a0bf500-19c2-4d07-8bf4-b4fa47f94015

📥 Commits

Reviewing files that changed from the base of the PR and between 48ff8d1 and 6a8d7c5.

📒 Files selected for processing (8)
  • libcudacxx/include/cuda/__numeric/saturating_overflow_cast.h
  • libcudacxx/include/cuda/std/__numeric/saturating_cast.h
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_add.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_cast.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_div.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_mul.pass.cpp
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturating_sub.pass.cpp
💤 Files with no reviewable changes (1)
  • libcudacxx/test/libcudacxx/std/numerics/numeric.ops/numeric.ops.sat/saturate_cast.pass.cpp

@davebayer davebayer force-pushed the optimize_saturating_cast branch from 6a8d7c5 to 2b4d39f Compare July 7, 2026 07:53
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment thread libcudacxx/include/cuda/std/__numeric/saturating_cast.h Outdated
@github-project-automation github-project-automation Bot moved this from In Review to In Progress in CCCL Jul 7, 2026
@davebayer davebayer force-pushed the optimize_saturating_cast branch from 2b4d39f to a23b125 Compare July 7, 2026 16:14

@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. Only one comment on int128

Comment thread libcudacxx/include/cuda/std/__numeric/saturating_cast.h

#if _CCCL_CUDA_COMPILATION()
_CCCL_TEMPLATE(class _Up, class _Tp)
_CCCL_REQUIRES((sizeof(_Tp) == sizeof(int8_t)))

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.

Did you mean sizeof(_Up) here (and the other functions)? The body checks sizeof(_Tp)

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.

Omg yes, thank you!

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

😬 CI Workflow Results

🟥 Finished in 2h 09m: Pass: 50%/120 | Total: 2d 12h | Max: 2h 09m | Hits: 92%/132453

See results here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants