Skip to content

Commit

Permalink
Prepare more algorithms (#1161)
Browse files Browse the repository at this point in the history
* Move `std::copy` to its own file

* Move `std::copy_backward` to its own file

* Move `std::copy_if` to its own file

* Move `std::copy_n` to its own file

* Move `std::move` to its own file

* Move `std::move_backward` to its own file

* Move `std::transform` to its own file

* Move `std::replace` to its own file

* Move `std::replace_if` to its own file

* Move `std::replace_copy` to its own file

* Move `std::replace_copy_if` to its own file

* Move `std::generate` to its own file

* Move `std::generate_n` to its own file

* Move `std::remove` to its own file

* Move `std::remove_if` to its own file

* Move `std::remove_copy` to its own file

* Move `std::remove_copy_if` to its own file
  • Loading branch information
miscco authored Dec 12, 2023
1 parent 58a527e commit bf098ef
Show file tree
Hide file tree
Showing 39 changed files with 3,182 additions and 487 deletions.
17 changes: 17 additions & 0 deletions libcudacxx/include/cuda/std/detail/libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ set(files
__algorithm/any_of.h
__algorithm/comp_ref_type.h
__algorithm/comp.h
__algorithm/copy_backward.h
__algorithm/copy_if.h
__algorithm/copy_n.h
__algorithm/copy.h
__algorithm/count.h
__algorithm/count_if.h
__algorithm/equal.h
Expand All @@ -17,14 +21,27 @@ set(files
__algorithm/find.h
__algorithm/for_each_n.h
__algorithm/for_each.h
__algorithm/generate_n.h
__algorithm/generate.h
__algorithm/half_positive.h
__algorithm/is_permutation.h
__algorithm/lexicographical_compare.h
__algorithm/mismatch.h
__algorithm/move_backward.h
__algorithm/move.h
__algorithm/none_of.h
__algorithm/remove_copy_if.h
__algorithm/remove_copy.h
__algorithm/remove_if.h
__algorithm/remove.h
__algorithm/replace_copy_if.h
__algorithm/replace_copy.h
__algorithm/replace_if.h
__algorithm/replace.h
__algorithm/search_n.h
__algorithm/search.h
__algorithm/swap_ranges.h
__algorithm/transform.h
__availability
__bit_reference
__bsd_locale_defaults.h
Expand Down
102 changes: 102 additions & 0 deletions libcudacxx/include/cuda/std/detail/libcxx/include/__algorithm/copy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCUDACXX___ALGORITHM_COPY_H
#define _LIBCUDACXX___ALGORITHM_COPY_H

#ifndef __cuda_std__
# include <__config>
#endif // __cuda_std__

#include "../__algorithm/unwrap_iter.h"
#include "../__type_traits/enable_if.h"
#include "../__type_traits/is_constant_evaluated.h"
#include "../__type_traits/is_same.h"
#include "../__type_traits/is_trivially_copy_assignable.h"
#include "../__type_traits/remove_const.h"
#include "../cstdlib"
#include "../cstring"

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header

_LIBCUDACXX_BEGIN_NAMESPACE_STD

template <class _InputIterator, class _OutputIterator>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX11 _OutputIterator
__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
{
for (; __first != __last; ++__first, (void) ++__result)
{
*__result = *__first;
}
return __result;
}

template <class _Tp, class _Up>
inline _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 bool
__dispatch_memmove(_Up* __result, _Tp* __first, const size_t __n)
{
#if _LIBCUDACXX_STD_VER >= 20
if (_CUDA_VSTD::is_constant_evaluated())
{
return false;
}
else
#endif // _LIBCUDACXX_STD_VER >= 20
{
// For now, we only ever use memmove on host
// clang-format off
NV_IF_ELSE_TARGET(NV_IS_HOST, (
_CUDA_VSTD::memmove(__result, __first, __n * sizeof(_Up));
return true;
),(
return false;
))
// clang-format on
}
}

template <class _Tp,
class _Up,
__enable_if_t<_LIBCUDACXX_TRAIT(is_same, __remove_const_t<_Tp>, _Up), int> = 0,
__enable_if_t<_LIBCUDACXX_TRAIT(is_trivially_copy_assignable, _Up), int> = 0>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 _Up*
__copy(_Tp* __first, _Tp* __last, _Up* __result)
{
const ptrdiff_t __n = __last - __first;
if (__n > 0)
{
if (__dispatch_memmove(__result, __first, __n))
{
return __result + __n;
}
for (ptrdiff_t __i = 0; __i < __n; ++__i)
{
*(__result + __i) = *(__first + __i);
}
}
return __result + __n;
}

template <class _InputIterator, class _OutputIterator>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 _OutputIterator
copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
{
return _CUDA_VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
}

_LIBCUDACXX_END_NAMESPACE_STD

#endif // _LIBCUDACXX___ALGORITHM_COPY_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCUDACXX___ALGORITHM_COPY_BACKWARD_H
#define _LIBCUDACXX___ALGORITHM_COPY_BACKWARD_H

#ifndef __cuda_std__
# include <__config>
#endif // __cuda_std__

#include "../__algorithm/copy.h"
#include "../__algorithm/unwrap_iter.h"
#include "../__type_traits/enable_if.h"
#include "../__type_traits/is_same.h"
#include "../__type_traits/is_trivially_copy_assignable.h"
#include "../__type_traits/remove_const.h"

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header

_LIBCUDACXX_BEGIN_NAMESPACE_STD

template <class _BidirectionalIterator, class _OutputIterator>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX11 _OutputIterator
__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
{
while (__first != __last)
{
*--__result = *--__last;
}
return __result;
}

template <class _Tp,
class _Up,
__enable_if_t<_LIBCUDACXX_TRAIT(is_same, __remove_const_t<_Tp>, _Up), int> = 0,
__enable_if_t<_LIBCUDACXX_TRAIT(is_trivially_copy_assignable, _Up), int> = 0>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 _Up*
__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
{
const ptrdiff_t __n = __last - __first;
if (__n > 0)
{
if (__dispatch_memmove(__result - __n, __first, __n))
{
return __result - __n;
}
for (ptrdiff_t __i = 1; __i <= __n; ++__i)
{
*(__result - __i) = *(__last - __i);
}
}
return __result - __n;
}

template <class _BidirectionalIterator1, class _BidirectionalIterator2>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator2
copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, _BidirectionalIterator2 __result)
{
return _CUDA_VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
}

_LIBCUDACXX_END_NAMESPACE_STD

#endif // _LIBCUDACXX___ALGORITHM_COPY_BACKWARD_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCUDACXX___ALGORITHM_COPY_IF_H
#define _LIBCUDACXX___ALGORITHM_COPY_IF_H

#ifndef __cuda_std__
# include <__config>
#endif // __cuda_std__

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header

_LIBCUDACXX_BEGIN_NAMESPACE_STD

template<class _InputIterator, class _OutputIterator, class _Predicate>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX11 _OutputIterator
copy_if(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _Predicate __pred)
{
for (; __first != __last; ++__first)
{
if (__pred(*__first))
{
*__result = *__first;
++__result;
}
}
return __result;
}

_LIBCUDACXX_END_NAMESPACE_STD

#endif // _LIBCUDACXX___ALGORITHM_COPY_IF_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCUDACXX___ALGORITHM_COPY_N_H
#define _LIBCUDACXX___ALGORITHM_COPY_N_H

#ifndef __cuda_std__
# include <__config>
#endif // __cuda_std__

#include "../__algorithm/copy.h"
#include "../__iterator/iterator_traits.h"
#include "../__type_traits/enable_if.h"
#include "../__utility/convert_to_integral.h"

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header

_LIBCUDACXX_BEGIN_NAMESPACE_STD

template <class _InputIterator,
class _Size,
class _OutputIterator,
__enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, int> = 0,
__enable_if_t<!__is_cpp17_random_access_iterator<_InputIterator>::value, int> = 0>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 _OutputIterator
copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
{
using _IntegralSize = decltype(__convert_to_integral(__orig_n));
_IntegralSize __n = static_cast<_IntegralSize>(__orig_n);
if (__n > 0)
{
*__result = *__first;
++__result;
for (--__n; __n > 0; --__n)
{
++__first;
*__result = *__first;
++__result;
}
}
return __result;
}

template <class _InputIterator,
class _Size,
class _OutputIterator,
__enable_if_t<__is_cpp17_random_access_iterator<_InputIterator>::value, int> = 0>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX17 _OutputIterator
copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
{
using _IntegralSize = decltype(__convert_to_integral(__orig_n));
_IntegralSize __n = static_cast<_IntegralSize>(__orig_n);
return _CUDA_VSTD::copy(__first, __first + __n, __result);
}

_LIBCUDACXX_END_NAMESPACE_STD

#endif // _LIBCUDACXX___ALGORITHM_COPY_N_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCUDACXX___ALGORITHM_GENERATE_H
#define _LIBCUDACXX___ALGORITHM_GENERATE_H

#ifndef __cuda_std__
# include <__config>
#endif // __cuda_std__

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header

_LIBCUDACXX_BEGIN_NAMESPACE_STD

template <class _ForwardIterator, class _Generator>
inline _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_AFTER_CXX11 void
generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
{
for (; __first != __last; ++__first)
{
*__first = __gen();
}
}

_LIBCUDACXX_END_NAMESPACE_STD

#endif // _LIBCUDACXX___ALGORITHM_GENERATE_H
Loading

0 comments on commit bf098ef

Please sign in to comment.