Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEA]: Add round up/down to multiple #2377

Closed
1 task done
fbusato opened this issue Sep 5, 2024 · 2 comments · Fixed by #3234
Closed
1 task done

[FEA]: Add round up/down to multiple #2377

fbusato opened this issue Sep 5, 2024 · 2 comments · Fixed by #3234
Assignees
Labels
feature request New feature or request.

Comments

@fbusato
Copy link
Contributor

fbusato commented Sep 5, 2024

Is this a duplicate?

Area

libcu++

Is your feature request related to a problem? Please describe.

The following patterns are very common:

  • floor(a / b) * b
  • ceil(a / b) * b

Introduce two functions to compute round up/down to a given multiple

Describe the solution you'd like

template<typename T>
//HOST_DEVICE_NODISCARD
constexpr T round_up_to_multiple(T value, T mul) {
    static_assert(::cuda::is_integral<T>::value);
    //ASSERT_OR_ASSUME(value >= 0)
    //ASSERT_OR_ASSUME(mul   > 0)
    auto  cdiv = ceil_div(value, mul);
    //ASSERT(!is_mul_overflow(cdiv, mul))
    using U    = ::cuda::std::__make_unsigned_t<T>;
    auto  mul1 = static_cast<U>(mul);  // for optimization purposes
    auto  ret  = static_cast<T>(cdiv * mul1);
    //ASSERT_OR_ASSUME(ret >= value)
    return ret;
}
template<typename T>
//HOST_DEVICE_NODISCARD
constexpr T round_down_to_multiple(T value, T mul) {
    static_assert(::cuda::is_integral<T>::value);
    //ASSERT_OR_ASSUME(value >= 0)
    //ASSERT_OR_ASSUME(mul   > 0)
    //ASSERT_OR_ASSUME(mul <= value)
    using U    = ::cuda::std::__make_unsigned_t<T>;
    auto  value1 = static_cast<U>(value); // for optimization purposes
    auto  mul1 = static_cast<U>(mul);  // for optimization purposes
    auto  ret    = static_cast<T>((value1 / mul1) * mul1);
    //ASSERT_OR_ASSUME(ret >= value - mul)
    return ret;
}

Describe alternatives you've considered

round_up_to_multiple(T, U);
round_down_to_multiple(T, U);

Additional context

#1517
#2375

@fbusato fbusato added the feature request New feature or request. label Sep 5, 2024
@fbusato fbusato self-assigned this Sep 5, 2024
@github-project-automation github-project-automation bot moved this to Todo in CCCL Sep 5, 2024
@fbusato
Copy link
Contributor Author

fbusato commented Sep 5, 2024

@bernhardmgruber @pauleonix for visibility

@bernhardmgruber
Copy link
Contributor

Thx a lot for these suggestion! I would love to see those in libcu++!

@cccl-authenticator-app cccl-authenticator-app bot moved this from Todo to In Review in CCCL Jan 2, 2025
@github-project-automation github-project-automation bot moved this from In Review to Done in CCCL Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants