/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/functional.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/functional.hpp Source File#

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/functional.hpp Source File
functional.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2018-2025, Advanced Micro Devices, Inc. All rights reserved.
3 
4 #pragma once
5 
7 #include "ck/utility/type.hpp"
8 
9 namespace ck {
10 
11 // TODO: right? wrong?
12 struct forwarder
13 {
14  template <typename T>
15  __host__ __device__ constexpr T&& operator()(T&& x) const
16  {
17  return static_cast<T&&>(x);
18  }
19 };
20 
21 struct swallow
22 {
23  template <typename... Ts>
24  __host__ __device__ constexpr swallow(Ts&&...)
25  {
26  }
27 };
28 
29 template <typename T>
31 {
32  constexpr bool operator()(const T& x, const T& y) const { return x && y; }
33 };
34 
35 template <typename T>
36 struct logical_or
37 {
38  constexpr bool operator()(const T& x, const T& y) const { return x || y; }
39 };
40 
41 template <typename T>
43 {
44  constexpr bool operator()(const T& x) const { return !x; }
45 };
46 
47 // Emulate if constexpr
48 template <bool>
49 struct static_if;
50 
51 template <>
52 struct static_if<true>
53 {
55 
56  template <typename F>
57  __host__ __device__ constexpr auto operator()(F f) const
58  {
59  // This is a trick for compiler:
60  // Pass forwarder to lambda "f" as "auto" argument, and make sure "f" will
61  // use it,
62  // this will make "f" a generic lambda, so that "f" won't be compiled
63  // until being
64  // instantiated here
65  f(forwarder{});
66  return Type{};
67  }
68 
69  template <typename F>
70  __host__ __device__ static void Else(F)
71  {
72  }
73 };
74 
75 template <>
76 struct static_if<false>
77 {
79 
80  template <typename F>
81  __host__ __device__ constexpr auto operator()(F) const
82  {
83  return Type{};
84  }
85 
86  template <typename F>
87  __host__ __device__ static void Else(F f)
88  {
89  // This is a trick for compiler:
90  // Pass forwarder to lambda "f" as "auto" argument, and make sure "f" will
91  // use it,
92  // this will make "f" a generic lambda, so that "f" won't be compiled
93  // until being
94  // instantiated here
95  f(forwarder{});
96  }
97 };
98 
99 template <bool predicate, class X, class Y>
100 struct conditional;
101 
102 template <class X, class Y>
103 struct conditional<true, X, Y>
104 {
105  using type = X;
106 };
107 
108 template <class X, class Y>
109 struct conditional<false, X, Y>
110 {
111  using type = Y;
112 };
113 
114 template <bool predicate, class X, class Y>
116 
117 // z = predicate ? x : y
118 template <bool predicate, typename X, typename Y>
119 constexpr auto conditional_expr(X&& x, Y&& y)
120 {
121  if constexpr(predicate)
122  {
123  return ck::forward<X>(x);
124  }
125  else
126  {
127  return ck::forward<Y>(y);
128  }
129 }
130 
131 } // namespace ck
Definition: ck.hpp:267
constexpr auto conditional_expr(X &&x, Y &&y)
Definition: functional.hpp:119
typename conditional< predicate, X, Y >::type conditional_t
Definition: functional.hpp:115
Y type
Definition: functional.hpp:111
X type
Definition: functional.hpp:105
Definition: functional.hpp:100
Definition: functional.hpp:13
__host__ constexpr __device__ T && operator()(T &&x) const
Definition: functional.hpp:15
Definition: functional.hpp:31
constexpr bool operator()(const T &x, const T &y) const
Definition: functional.hpp:32
Definition: functional.hpp:43
constexpr bool operator()(const T &x) const
Definition: functional.hpp:44
Definition: functional.hpp:37
constexpr bool operator()(const T &x, const T &y) const
Definition: functional.hpp:38
Definition: functional.hpp:77
__host__ constexpr __device__ auto operator()(F) const
Definition: functional.hpp:81
__host__ static __device__ void Else(F f)
Definition: functional.hpp:87
Definition: functional.hpp:53
__host__ constexpr __device__ auto operator()(F f) const
Definition: functional.hpp:57
__host__ static __device__ void Else(F)
Definition: functional.hpp:70
Definition: functional.hpp:49
Definition: functional.hpp:22
__host__ constexpr __device__ swallow(Ts &&...)
Definition: functional.hpp:24