functional.hpp 2.42 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
#pragma once
5

6
7
#include "ck/utility/integral_constant.hpp"
#include "ck/utility/type.hpp"
8

9
10
namespace ck {

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

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

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

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

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

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

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

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

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

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

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

117
} // namespace ck