-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add cuda::minimum and cuda::maximum
- Loading branch information
Showing
8 changed files
with
204 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of libcu++, the C++ Standard Library for your entire system, | ||
// 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) 2024 NVIDIA CORPORATION & AFFILIATES. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _CUDA_FUNCTIONAL_MAXIMUM_H | ||
#define _CUDA_FUNCTIONAL_MAXIMUM_H | ||
|
||
#include <cuda/std/detail/__config> | ||
|
||
#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 | ||
|
||
#include <cuda/std/__type_traits/common_type.h> | ||
|
||
_LIBCUDACXX_BEGIN_NAMESPACE_CUDA | ||
|
||
template <class _Tp = void> | ||
struct _CCCL_TYPE_VISIBILITY_DEFAULT maximum | ||
{ | ||
_CCCL_EXEC_CHECK_DISABLE | ||
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr _Tp operator()(const _Tp& __lhs, const _Tp& __rhs) const | ||
noexcept(noexcept((__lhs < __rhs) ? __rhs : __lhs)) | ||
{ | ||
return (__lhs < __rhs) ? __rhs : __lhs; | ||
} | ||
}; | ||
_LIBCUDACXX_CTAD_SUPPORTED_FOR_TYPE(maximum); | ||
|
||
template <> | ||
struct _CCCL_TYPE_VISIBILITY_DEFAULT maximum<void> | ||
{ | ||
_CCCL_EXEC_CHECK_DISABLE | ||
template <class _T1, class _T2> | ||
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr _CUDA_VSTD::__common_type_t<_T1, _T2> | ||
operator()(const _T1& __lhs, const _T2& __rhs) const noexcept(noexcept((__lhs < __rhs) ? __rhs : __lhs)) | ||
{ | ||
return (__lhs < __rhs) ? __rhs : __lhs; | ||
} | ||
}; | ||
|
||
_LIBCUDACXX_END_NAMESPACE_CUDA | ||
|
||
#endif // _CUDA_FUNCTIONAL_MAXIMUM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of libcu++, the C++ Standard Library for your entire system, | ||
// 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) 2024 NVIDIA CORPORATION & AFFILIATES. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _CUDA_FUNCTIONAL_MINIMUM_H | ||
#define _CUDA_FUNCTIONAL_MINIMUM_H | ||
|
||
#include <cuda/std/detail/__config> | ||
|
||
#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 | ||
|
||
#include <cuda/std/__type_traits/common_type.h> | ||
|
||
_LIBCUDACXX_BEGIN_NAMESPACE_CUDA | ||
|
||
template <class _Tp = void> | ||
struct _CCCL_TYPE_VISIBILITY_DEFAULT minimum | ||
{ | ||
_CCCL_EXEC_CHECK_DISABLE | ||
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr _Tp operator()(const _Tp& __lhs, const _Tp& __rhs) const | ||
noexcept(noexcept((__lhs < __rhs) ? __lhs : __rhs)) | ||
{ | ||
return (__lhs < __rhs) ? __lhs : __rhs; | ||
} | ||
}; | ||
_LIBCUDACXX_CTAD_SUPPORTED_FOR_TYPE(minimum); | ||
|
||
template <> | ||
struct _CCCL_TYPE_VISIBILITY_DEFAULT minimum<void> | ||
{ | ||
_CCCL_EXEC_CHECK_DISABLE | ||
template <class _T1, class _T2> | ||
_CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI constexpr _CUDA_VSTD::__common_type_t<_T1, _T2> | ||
operator()(const _T1& __lhs, const _T2& __rhs) const noexcept(noexcept((__lhs < __rhs) ? __lhs : __rhs)) | ||
{ | ||
return (__lhs < __rhs) ? __lhs : __rhs; | ||
} | ||
}; | ||
|
||
_LIBCUDACXX_END_NAMESPACE_CUDA | ||
|
||
#endif // _CUDA_FUNCTIONAL_MINIMUM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
libcudacxx/test/libcudacxx/cuda/functional/maximum.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of libcu++, the C++ Standard Library for your entire system, | ||
// 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) 2024 NVIDIA CORPORATION & AFFILIATES. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <cuda/functional> | ||
|
||
#include "test_macros.h" | ||
|
||
template <typename OpT, typename T> | ||
__host__ __device__ constexpr bool test_op(const T lhs, const T rhs, const T expected) | ||
{ | ||
return (OpT{}(lhs, rhs) == expected) && (OpT{}(lhs, rhs) == OpT{}(rhs, lhs)); | ||
} | ||
|
||
template <typename T> | ||
__host__ __device__ constexpr bool test(const T lhs, const T rhs, const T expected) | ||
{ | ||
return test_op<cuda::maximum<T>>(lhs, rhs, expected) && // | ||
test_op<cuda::maximum<>>(lhs, rhs, expected) && // | ||
test_op<cuda::maximum<void>>(lhs, rhs, expected); | ||
} | ||
|
||
__host__ __device__ constexpr bool test() | ||
{ | ||
return test<int>(0, 1, 1) && // | ||
test<int>(1, 0, 1) && // | ||
test<int>(0, 0, 0) && // | ||
test<int>(-1, 1, 1) && // | ||
test<char>('a', 'b', 'b'); | ||
} | ||
|
||
int main(int, char**) | ||
{ | ||
assert(test()); | ||
static_assert(test(), ""); | ||
return 0; | ||
} |
43 changes: 43 additions & 0 deletions
43
libcudacxx/test/libcudacxx/cuda/functional/minimum.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of libcu++, the C++ Standard Library for your entire system, | ||
// 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) 2024 NVIDIA CORPORATION & AFFILIATES. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <cuda/functional> | ||
|
||
#include "test_macros.h" | ||
|
||
template <typename OpT, typename T> | ||
__host__ __device__ constexpr bool test_op(const T lhs, const T rhs, const T expected) | ||
{ | ||
return (OpT{}(lhs, rhs) == expected) && (OpT{}(lhs, rhs) == OpT{}(rhs, lhs)); | ||
} | ||
|
||
template <typename T> | ||
__host__ __device__ constexpr bool test(T lhs, T rhs, T expected) | ||
{ | ||
return test_op<cuda::minimum<T>>(lhs, rhs, expected) && // | ||
test_op<cuda::minimum<>>(lhs, rhs, expected) && // | ||
test_op<cuda::minimum<void>>(lhs, rhs, expected); | ||
} | ||
|
||
__host__ __device__ constexpr bool test() | ||
{ | ||
return test<int>(0, 1, 0) && // | ||
test<int>(1, 0, 0) && // | ||
test<int>(0, 0, 0) && // | ||
test<int>(-1, 1, -1) && // | ||
test<char>('a', 'b', 'a'); | ||
} | ||
|
||
int main(int, char**) | ||
{ | ||
test(); | ||
static_assert(test(), ""); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters