functional.hpp Source File

functional.hpp Source File#

Composable Kernel: functional.hpp Source File
utility/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
9namespace ck {
10
11// TODO: right? wrong?
13{
14 template <typename T>
15 __host__ __device__ constexpr T&& operator()(T&& x) const
16 {
17 return static_cast<T&&>(x);
18 }
19};
20
21struct swallow
22{
23 template <typename... Ts>
24 __host__ __device__ constexpr swallow(Ts&&...)
25 {
26 }
27};
28
29template <typename T>
31{
32 constexpr bool operator()(const T& x, const T& y) const { return x && y; }
33};
34
35template <typename T>
37{
38 constexpr bool operator()(const T& x, const T& y) const { return x || y; }
39};
40
41template <typename T>
43{
44 constexpr bool operator()(const T& x) const { return !x; }
45};
46
47// Emulate if constexpr
48template <bool>
49struct static_if;
50
51template <>
52struct 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
75template <>
76struct 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
99template <bool predicate, class X, class Y>
101
102template <class X, class Y>
103struct conditional<true, X, Y>
104{
105 using type = X;
106};
107
108template <class X, class Y>
109struct conditional<false, X, Y>
110{
111 using type = Y;
112};
113
114template <bool predicate, class X, class Y>
116
117// z = predicate ? x : y
118template <bool predicate, typename X, typename Y>
119constexpr 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:268
typename conditional< predicate, X, Y >::type conditional_t
Definition utility/functional.hpp:115
constexpr auto conditional_expr(X &&x, Y &&y)
Definition utility/functional.hpp:119
Y type
Definition utility/functional.hpp:111
X type
Definition utility/functional.hpp:105
Definition utility/functional.hpp:100
Definition utility/functional.hpp:13
__host__ __device__ constexpr T && operator()(T &&x) const
Definition utility/functional.hpp:15
Definition utility/functional.hpp:31
constexpr bool operator()(const T &x, const T &y) const
Definition utility/functional.hpp:32
Definition utility/functional.hpp:43
constexpr bool operator()(const T &x) const
Definition utility/functional.hpp:44
Definition utility/functional.hpp:37
constexpr bool operator()(const T &x, const T &y) const
Definition utility/functional.hpp:38
static_if< false > Type
Definition utility/functional.hpp:78
__host__ __device__ constexpr auto operator()(F) const
Definition utility/functional.hpp:81
__host__ static __device__ void Else(F f)
Definition utility/functional.hpp:87
static_if< true > Type
Definition utility/functional.hpp:54
__host__ __device__ constexpr auto operator()(F f) const
Definition utility/functional.hpp:57
__host__ static __device__ void Else(F)
Definition utility/functional.hpp:70
Definition utility/functional.hpp:49
__host__ __device__ constexpr swallow(Ts &&...)
Definition utility/functional.hpp:24