Skip to content

Commit 9167406

Browse files
authored
Small improvements for cuda::ipow (#6736)
1 parent f06a597 commit 9167406

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

  • libcudacxx/include/cuda/__cmath

libcudacxx/include/cuda/__cmath/ipow.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
#endif // no system header
2323

2424
#include <cuda/__cmath/ilog.h>
25+
#include <cuda/__cmath/neg.h>
26+
#include <cuda/__cmath/pow2.h>
2527
#include <cuda/__cmath/uabs.h>
2628
#include <cuda/std/__bit/countl.h>
27-
#include <cuda/std/__bit/has_single_bit.h>
2829
#include <cuda/std/__concepts/concept_macros.h>
2930
#include <cuda/std/__type_traits/is_integer.h>
31+
#include <cuda/std/__type_traits/is_signed.h>
3032
#include <cuda/std/__type_traits/is_unsigned.h>
3133
#include <cuda/std/__type_traits/make_unsigned.h>
3234
#include <cuda/std/__utility/cmp.h>
@@ -48,7 +50,7 @@ template <class _Tp, class _Ep>
4850
{
4951
static_assert(::cuda::std::is_unsigned_v<_Tp>);
5052

51-
if (::cuda::std::has_single_bit(__b))
53+
if (::cuda::is_power_of_two(__b))
5254
{
5355
return ::cuda::__cccl_ipow_impl_base_pow2(__b, __e);
5456
}
@@ -92,10 +94,12 @@ _CCCL_REQUIRES(::cuda::std::__cccl_is_integer_v<_Tp> _CCCL_AND ::cuda::std::__cc
9294
return _Tp{0};
9395
}
9496
auto __res = ::cuda::__cccl_ipow_impl(::cuda::uabs(__b), ::cuda::std::__to_unsigned_like(__e));
95-
if (::cuda::std::cmp_less(__b, _Tp{0}) && (__e % 2u == 1))
97+
if constexpr (::cuda::std::is_signed_v<_Tp>)
9698
{
97-
// todo: replace with ::cuda::__neg(__res) when available
98-
__res = (~__res + 1);
99+
if (__b < _Tp{0} && (__e % 2u == 1))
100+
{
101+
__res = cuda::neg(__res);
102+
}
99103
}
100104
return static_cast<_Tp>(__res);
101105
}

0 commit comments

Comments
 (0)