DPC++ Runtime
Runtime libraries for oneAPI DPC++
functional.hpp
Go to the documentation of this file.
1 //==----------- functional.hpp --- SYCL functional -------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <functional> // for logical_and, logical_or, bit_and, bit_or, bit...
12 #include <type_traits> // for common_type
13 #include <utility> // for forward
14 
15 namespace sycl {
16 inline namespace _V1 {
17 
18 template <typename T = void> using plus = std::plus<T>;
19 template <typename T = void> using multiplies = std::multiplies<T>;
20 template <typename T = void> using bit_and = std::bit_and<T>;
21 template <typename T = void> using bit_or = std::bit_or<T>;
22 template <typename T = void> using bit_xor = std::bit_xor<T>;
23 
24 // std:logical_and/std::logical_or with a non-void type returns bool,
25 // sycl requires returning T.
26 template <typename T = void> struct logical_and {
27  T operator()(const T &lhs, const T &rhs) const { return lhs && rhs; }
28 };
29 
30 template <> struct logical_and<void> : std::logical_and<void> {};
31 
32 template <typename T = void> struct logical_or {
33  T operator()(const T &lhs, const T &rhs) const { return lhs || rhs; }
34 };
35 
36 template <> struct logical_or<void> : std::logical_or<void> {};
37 
38 // sycl::minimum definition should be consistent with std::min
39 template <typename T = void> struct minimum {
40  T operator()(const T &lhs, const T &rhs) const {
41  return (rhs < lhs) ? rhs : lhs;
42  }
43 };
44 
45 template <> struct minimum<void> {
46  struct is_transparent {};
47  template <typename T, typename U>
48  auto operator()(T &&lhs, U &&rhs) const ->
49  typename std::common_type<T &&, U &&>::type {
50  return (std::forward<const U>(rhs) < std::forward<const T>(lhs))
51  ? std::forward<U>(rhs)
52  : std::forward<T>(lhs);
53  }
54 };
55 
56 // sycl::maximum definition should be consistent with std::max
57 template <typename T = void> struct maximum {
58  T operator()(const T &lhs, const T &rhs) const {
59  return (lhs < rhs) ? rhs : lhs;
60  }
61 };
62 
63 template <> struct maximum<void> {
64  struct is_transparent {};
65  template <typename T, typename U>
66  auto operator()(T &&lhs, U &&rhs) const ->
67  typename std::common_type<T &&, U &&>::type {
68  return (std::forward<const T>(lhs) < std::forward<const U>(rhs))
69  ? std::forward<U>(rhs)
70  : std::forward<T>(lhs);
71  }
72 };
73 
74 } // namespace _V1
75 } // namespace sycl
std::bit_and< T > bit_and
Definition: functional.hpp:20
std::multiplies< T > multiplies
Definition: functional.hpp:19
std::bit_xor< T > bit_xor
Definition: functional.hpp:22
std::plus< T > plus
Definition: functional.hpp:18
std::bit_or< T > bit_or
Definition: functional.hpp:21
Definition: access.hpp:18
T operator()(const T &lhs, const T &rhs) const
Definition: functional.hpp:27
T operator()(const T &lhs, const T &rhs) const
Definition: functional.hpp:33
auto operator()(T &&lhs, U &&rhs) const -> typename std::common_type< T &&, U && >::type
Definition: functional.hpp:66
T operator()(const T &lhs, const T &rhs) const
Definition: functional.hpp:58
auto operator()(T &&lhs, U &&rhs) const -> typename std::common_type< T &&, U && >::type
Definition: functional.hpp:48
T operator()(const T &lhs, const T &rhs) const
Definition: functional.hpp:40