Skip to content

Conversation

@davebayer
Copy link
Contributor

If once wants to compute sin and cos of a number, computing both operations at the same time allows some optimizations.

This PR introduces cuda::sincos that tries to use compiler builtins and device intrinsics to provide this functionality and fallbacks to separate sin and cos calculation.

@davebayer davebayer requested a review from a team as a code owner November 23, 2025 11:40
@davebayer davebayer requested a review from griwes November 23, 2025 11:40
@github-project-automation github-project-automation bot moved this to Todo in CCCL Nov 23, 2025
@cccl-authenticator-app cccl-authenticator-app bot moved this from Todo to In Review in CCCL Nov 23, 2025
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@davebayer davebayer requested a review from miscco November 24, 2025 12:33
@davebayer davebayer self-assigned this Nov 24, 2025
@davebayer davebayer requested a review from s-oboyle November 24, 2025 17:29
@davebayer
Copy link
Contributor Author

@s-oboyle I've applied the function to even more places, could you confirm it is fine?

@github-actions
Copy link
Contributor

🥳 CI Workflow Results

🟩 Finished in 1h 36m: Pass: 100%/90 | Total: 1d 02h | Max: 1h 18m | Hits: 96%/201620

See results here.

template <class Integral>
[[nodiscard]] __host__ __device__
sincos_result<double> sincos(Integral value) noexcept; // (A)
Copy link
Contributor

Choose a reason for hiding this comment

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

using 1 and A looks a typo

Suggested change
sincos_result<double> sincos(Integral value) noexcept; // (A)
sincos_result<double> sincos(Integral value) noexcept; // (2)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is not, it's how they define these overloads on cppreference. Will (2) be better?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, using 1. and A. is not very clear. I never noted in cppreference honestly


**Performance considerations**

- If available, the functionality is implemented by compiler builtins, otherwise fallbacks to ``cuda::std::sin(value)`` and ``cuda::std::cos(value)``.
Copy link
Contributor

Choose a reason for hiding this comment

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

we can be more precise about the types where the optimization applies

auto neg_result = cuda::sincos(-cuda::std::numeric_limits<T>::quiet_NaN());
assert(cuda::std::isnan(neg_result.sin));
assert(cuda::std::isnan(neg_result.cos));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add another test case with a generic values, e.g. 0.3 and test if it matches (or approx) separate calls to cos/sin

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hahah, for sure we can, I wanted not to deal with the floating point comparisons and just test whether it works 😅

Comment on lines +112 to +114
__ret.sin = ::cuda::std::sin(__v);
__ret.cos = ::cuda::std::cos(__v);
return __ret;
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if all compilers like it

Suggested change
__ret.sin = ::cuda::std::sin(__v);
__ret.cos = ::cuda::std::cos(__v);
return __ret;
return {::cuda::std::sin(__v), ::cuda::std::cos(__v)};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think this is an improvement, the return structure is defined anyway

Copy link
Contributor

Choose a reason for hiding this comment

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

it is an improvement in the sense the code is shorter 😅. Anyway, up to you

@github-project-automation github-project-automation bot moved this from In Review to In Progress in CCCL Nov 24, 2025
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