"vscode:/vscode.git/clone" did not exist on "947bda73fe7cc8d72b31619b532d1d33459cfc4a"
functional.hpp 2.44 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

4
5
6
#ifndef CK_FUNCTIONAL_HPP
#define CK_FUNCTIONAL_HPP

Chao Liu's avatar
Chao Liu committed
7
#include "integral_constant.hpp"
Chao Liu's avatar
Chao Liu committed
8
#include "type.hpp"
9

10
11
namespace ck {

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

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

Chao Liu's avatar
Chao Liu committed
30
31
32
33
34
35
36
37
38
39
40
41
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
42
43
44
45
46
47
template <typename T>
struct logical_not
{
    constexpr bool operator()(const T& x) const { return !x; }
};

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

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

Chao Liu's avatar
Chao Liu committed
57
    template <typename F>
Chao Liu's avatar
Chao Liu committed
58
59
60
    __host__ __device__ constexpr auto operator()(F f) const
    {
        // This is a trick for compiler:
Chao Liu's avatar
Chao Liu committed
61
62
63
64
        //   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
65
        //   instantiated here
Chao Liu's avatar
Chao Liu committed
66
67
68
69
        f(forwarder{});
        return Type{};
    }

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

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

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

Chao Liu's avatar
Chao Liu committed
87
    template <typename F>
Chao Liu's avatar
Chao Liu committed
88
    __host__ __device__ static void Else(F f)
Chao Liu's avatar
Chao Liu committed
89
90
    {
        // This is a trick for compiler:
Chao Liu's avatar
Chao Liu committed
91
92
93
94
        //   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
95
        //   instantiated here
Chao Liu's avatar
Chao Liu committed
96
97
98
        f(forwarder{});
    }
};
99

Chao Liu's avatar
Chao Liu committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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;

118
119
} // namespace ck
#endif