Skip to content

Conversation

@fbusato
Copy link
Contributor

@fbusato fbusato commented Oct 1, 2025

Description

Checking if a pointer is in a given range is very common. The utility has already been proposed in WG21 P3234. It is already provided by Boost Core and part of libc++/libstdc++ as internal function.

template <typename T>
[[nodiscard]] _CCCL_API inline bool ptr_in_range(T* ptr, T* start,  T* end) noexcept

@fbusato fbusato self-assigned this Oct 1, 2025
@fbusato fbusato requested review from a team as code owners October 1, 2025 00:06
@fbusato fbusato added this to CCCL Oct 1, 2025
@fbusato fbusato added the 3.2.0 Targeted for 3.2.0 release label Oct 1, 2025
@fbusato fbusato requested a review from gonidelis October 1, 2025 00:06
@github-project-automation github-project-automation bot moved this to Todo in CCCL Oct 1, 2025
@fbusato fbusato requested a review from griwes October 1, 2025 00:06
@cccl-authenticator-app cccl-authenticator-app bot moved this from Todo to In Review in CCCL Oct 1, 2025
@fbusato
Copy link
Contributor Author

fbusato commented Oct 1, 2025

next step is to aggregate memory/pointer utilities in cuda/memory. Right now, they are spread out throughout the library, e.g. ptr overlapping in memcpy.h, or address space in host/device mdspan.

@github-actions

This comment has been minimized.

@github-project-automation github-project-automation bot moved this from In Review to In Progress in CCCL Oct 1, 2025
@fbusato fbusato changed the title Provide cuda::pointer_in_range Provide cuda::ptr_in_range Oct 1, 2025
@fbusato
Copy link
Contributor Author

fbusato commented Oct 1, 2025

@davebayer @miscco open question, we don't do anything special in cuda::std::less_equal and similar. Would make sense to use builtin operators and const void* instead of a template to decrease the impact on compile time?

@fbusato
Copy link
Contributor Author

fbusato commented Oct 1, 2025

I was also tempted to optimize the function in the same way of cuda::in_range but I'm scared about UB with pointers difference.

@fbusato fbusato requested a review from davebayer October 1, 2025 18:40
@davebayer
Copy link
Contributor

pre-commit.ci autofix

@copy-pr-bot
Copy link
Contributor

copy-pr-bot bot commented Oct 1, 2025

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.

@davebayer
Copy link
Contributor

/ok to test 829f9e0

@github-project-automation github-project-automation bot moved this from In Progress to In Review in CCCL Oct 1, 2025
@github-actions

This comment has been minimized.

@davebayer
Copy link
Contributor

/ok to test a506e84

@github-actions

This comment has been minimized.

@fbusato fbusato requested review from davebayer and miscco October 3, 2025 18:19
@fbusato
Copy link
Contributor Author

fbusato commented Oct 3, 2025

I got scared enough by https://quuxplusone.github.io/blog/2019/01/20/std-less-nightmare/ to use a safer version of the function.

[[nodiscard]] _CCCL_API bool __ptr_in_range_host(_Tp* __ptr, _Tp* __start, _Tp* __end) noexcept
{
_CCCL_ASSERT(::std::greater_equal<>{}(__end, __start), "__ptr_in_range_host: __end must be greater than __start");
return ::std::greater_equal<>{}(__ptr, __start) && ::std::less<>{}(__ptr, __end);
Copy link
Member

Choose a reason for hiding this comment

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

just out of curiosity, is our common practice to use ::std::* within HOST_COMPILATION instead of universally relying on ::cuda::std::less (in this case) to do the host/device dispatching?

Copy link
Contributor

@bernhardmgruber bernhardmgruber Oct 7, 2025

Choose a reason for hiding this comment

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

In this case we really rely on a special blessing of std::greater_equal etc. in the C++ standard that allows pointers to be compared and establishes a total order. operator< on raw pointers only yields a partial order, which the compiler abuses in some situations. E.g.:

int a[10];
int b[10];
int* p = a +5;
if (a < p && p < a + 10) // this is legal
if (a < p && p < b) // this is NOT legal, pointers from different arrays cannot be compared. invokes UB
 if (std::less{}(a, p) && std::less{}(p, b)) // this IS legal, pointer addresses are numerically compared

Don't ask me why :D I really don't know.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I read about it before reviewing here. My questions was why we don't use cuda::std::* instead of just std::. Not why we do not use the < operator. But I guess you answered by saying "special blessing of std::greater_equal" which kinda implies that the cuda::std counterpart does not partake of that blessing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, correct. The host compiler recognizes std::greater_equal but not cuda::std::greater_equal

Copy link
Contributor

@bernhardmgruber bernhardmgruber left a comment

Choose a reason for hiding this comment

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

LGTM, just:

@fbusato fbusato enabled auto-merge (squash) October 7, 2025 17:33
@alliepiper
Copy link
Contributor

/ok to test ddd33e5

@fbusato
Copy link
Contributor Author

fbusato commented Oct 8, 2025

/ok to test d164dd5

@github-actions

This comment has been minimized.

@fbusato fbusato added the CI 100% All CI tests passed label Oct 8, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Oct 8, 2025

🥳 CI Workflow Results

🟩 Finished in 3h 43m: Pass: 100%/242 | Total: 3d 02h | Max: 2h 00m | Hits: 97%/304639

See results here.

@fbusato fbusato merged commit ccc252e into NVIDIA:main Oct 8, 2025
498 of 503 checks passed
@github-project-automation github-project-automation bot moved this from In Review to Done in CCCL Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.2.0 Targeted for 3.2.0 release CI 100% All CI tests passed

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants