Files
project_6/cccl_upstream/docs/libcudacxx/extended_api/numeric/narrow.rst
muh-bot 2a7ca101d7 feat(cccl): integrate missing CCCL directories — python/, ci/, .agent/, docs/, test/
Sparse-checkout from NVIDIA/cccl main branch to complete cccl_upstream:

Added:
- python/cuda_cccl/ (226 files) — Python bindings for device-level algorithms
  Critical for muh toolchain: cuda.compute.reduce_into, scan, radix_sort, etc.
  Includes 204 .py files with full test coverage for all 27 algorithms
- ci/ (163 files) — Build/test infrastructure
  build_cub.sh, test_cub.sh, build_and_test_targets.sh, matrix.yaml
  Directly maps to our [INFRA-CI] and [INFRA-BUILD] items
- .agent/skills/ (7 files) — NVIDIA's own agent skills for CCCL
  cccl-style/SKILL.md, cccl-test/SKILL.md, sass-diff/SKILL.md
- docs/ (491 files) — Official CCCL documentation
  CI references, CMake guides, Python compute docs, libcudacxx PTX docs
- test/ (12 files) — Top-level integration tests (cuda_smoke, stdpar)
- Root configs: .clang-format, .clang-tidy, CONTRIBUTING.md, pyproject.toml
- CLAUDE.md symlink → AGENTS.md (NVIDIA's standard)

cccl_upstream now mirrors full NVIDIA/cccl structure:
  Before: 42M (cub + thrust + libcudacxx + cudax + c + examples + benchmarks)
  After:  53M (+python +ci +docs +.agent +test +configs)

This completes the CCCL base needed for:
- [muh-bench] items: ci/util/build_and_test_targets.sh for targeted builds
- [CCCL-verify] items: python/cuda_cccl/tests/ as reference implementations
- [CCCL-test] items: ci/test_cub.sh, ci/test_thrust.sh
- Agent workflow: .agent/skills/ for consistent style and test patterns
2026-08-07 02:34:33 +00:00

56 lines
1.9 KiB
ReStructuredText

.. _libcudacxx-extended-api-numeric-narrow:
``cuda::narrow``
=====================
.. code:: cpp
struct narrowing_error;
template <typename To, typename From>
[[nodiscard]] constexpr
To narrow(From from);
template <typename To, typename From>
[[nodiscard]] constexpr
To narrow_cast(From&& __from) noexcept;
Both functions use a ``static_cast`` to cast the value ``from`` to type ``To``.
``From`` needs to be convertible to ``To``, and implement ``operator!=``.
``cuda::narrow`` additionally checks whether the value has changed,
and if so, throws ``cuda::narrowing_error`` in host code and traps in device code.
In this case, ``To`` additionally needs to be convertible to ``From``.
``cuda::narrow_cast`` does not perform such a check (it's a plain cast) and is just intended to show
that narrowing and a potential change of the value is intended.
The functions are modelled after ``gsl::narrow`` and ``gsl::narrow_cast``.
See also the C++ Core Guidelines
`ES.46 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-narrowing>`_ and
`ES.49 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts-named>`_.
Example
-------
.. code:: cpp
#include <cuda/numeric>
__global__ void kernel(size_t n) {
unsigned int r1 = cuda::narrow<unsigned int>(n); // traps
unsigned int r2 = cuda::narrow_cast<unsigned int>(n); // truncation of value is intended
}
void host() {
unsigned char r1 = cuda::narrow<unsigned char>( 200); // ok
unsigned char r2 = cuda::narrow<unsigned char>( 300); // throws narrowing_error
unsigned int r3 = cuda::narrow<unsigned int >(-100); // throws narrowing_error
unsigned char r4 = cuda::narrow_cast<unsigned char>(300); // truncation of value is intended
kernel<<<1, 1>>>(2LL << 35); // size larger than unsigned int
}
`See it on Godbolt 🔗 <https://godbolt.org/z/ahcqv6joY>`_