functional.hpp 2.78 KB
Newer Older
Umang Yadav's avatar
Umang Yadav committed
1
2
3

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
Chao Liu's avatar
Chao Liu committed
4
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
5
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
6

7
#pragma once
8

9
10
#include "ck/utility/integral_constant.hpp"
#include "ck/utility/type.hpp"
11

12
13
namespace ck {

Chao Liu's avatar
Chao Liu committed
14
// TODO: right? wrong?
Chao Liu's avatar
Chao Liu committed
15
16
17
struct forwarder
{
    template <typename T>
Chao Liu's avatar
Chao Liu committed
18
    __host__ __device__ constexpr T&& operator()(T&& x) const
Chao Liu's avatar
Chao Liu committed
19
    {
Chao Liu's avatar
Chao Liu committed
20
        return static_cast<T&&>(x);
Chao Liu's avatar
Chao Liu committed
21
22
23
    }
};

Chao Liu's avatar
Chao Liu committed
24
25
struct swallow
{
Chao Liu's avatar
Chao Liu committed
26
    template <typename... Ts>
Chao Liu's avatar
Chao Liu committed
27
    __host__ __device__ constexpr swallow(Ts&&...)
Chao Liu's avatar
Chao Liu committed
28
29
30
31
    {
    }
};

Chao Liu's avatar
Chao Liu committed
32
33
34
35
36
37
38
39
40
41
42
43
template <typename T>
struct logical_and
{
    constexpr bool operator()(const T& x, const T& y) const { return x && y; }
};

template <typename T>
struct logical_or
{
    constexpr bool operator()(const T& x, const T& y) const { return x || y; }
};

Chao Liu's avatar
Chao Liu committed
44
45
46
47
48
49
template <typename T>
struct logical_not
{
    constexpr bool operator()(const T& x) const { return !x; }
};

Chao Liu's avatar
Chao Liu committed
50
// Emulate if constexpr
Chao Liu's avatar
Chao Liu committed
51
52
template <bool>
struct static_if;
Chao Liu's avatar
Chao Liu committed
53
54
55
56
57
58

template <>
struct static_if<true>
{
    using Type = static_if<true>;

Chao Liu's avatar
Chao Liu committed
59
    template <typename F>
Chao Liu's avatar
Chao Liu committed
60
61
62
    __host__ __device__ constexpr auto operator()(F f) const
    {
        // This is a trick for compiler:
Chao Liu's avatar
Chao Liu committed
63
64
65
66
        //   Pass forwarder to lambda "f" as "auto" argument, and make sure "f" will
        //   use it,
        //   this will make "f" a generic lambda, so that "f" won't be compiled
        //   until being
Chao Liu's avatar
Chao Liu committed
67
        //   instantiated here
Chao Liu's avatar
Chao Liu committed
68
69
70
71
        f(forwarder{});
        return Type{};
    }

Chao Liu's avatar
Chao Liu committed
72
    template <typename F>
Chao Liu's avatar
Chao Liu committed
73
    __host__ __device__ static void Else(F)
Chao Liu's avatar
Chao Liu committed
74
75
76
77
78
79
80
81
82
    {
    }
};

template <>
struct static_if<false>
{
    using Type = static_if<false>;

Chao Liu's avatar
Chao Liu committed
83
    template <typename F>
Chao Liu's avatar
Chao Liu committed
84
85
86
87
88
    __host__ __device__ constexpr auto operator()(F) const
    {
        return Type{};
    }

Chao Liu's avatar
Chao Liu committed
89
    template <typename F>
Chao Liu's avatar
Chao Liu committed
90
    __host__ __device__ static void Else(F f)
Chao Liu's avatar
Chao Liu committed
91
92
    {
        // This is a trick for compiler:
Chao Liu's avatar
Chao Liu committed
93
94
95
96
        //   Pass forwarder to lambda "f" as "auto" argument, and make sure "f" will
        //   use it,
        //   this will make "f" a generic lambda, so that "f" won't be compiled
        //   until being
Chao Liu's avatar
Chao Liu committed
97
        //   instantiated here
Chao Liu's avatar
Chao Liu committed
98
99
100
        f(forwarder{});
    }
};
101

Chao Liu's avatar
Chao Liu committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
template <bool predicate, class X, class Y>
struct conditional;

template <class X, class Y>
struct conditional<true, X, Y>
{
    using type = X;
};

template <class X, class Y>
struct conditional<false, X, Y>
{
    using type = Y;
};

template <bool predicate, class X, class Y>
using conditional_t = typename conditional<predicate, X, Y>::type;

120
121
122
123
124
125
126
127
128
129
130
131
132
133
// z = predicate ? x : y
template <bool predicate, typename X, typename Y>
constexpr auto conditional_expr(X&& x, Y&& y)
{
    if constexpr(predicate)
    {
        return std::forward<X>(x);
    }
    else
    {
        return std::forward<Y>(y);
    }
}

134
} // namespace ck
Umang Yadav's avatar
Umang Yadav committed
135
136

#pragma clang diagnostic pop