functional.hip.hpp 2.5 KB
Newer Older
1
#pragma once
Chao Liu's avatar
Chao Liu committed
2
#include "integral_constant.hip.hpp"
Chao Liu's avatar
Chao Liu committed
3
#include "Sequence.hip.hpp"
4

Chao Liu's avatar
Chao Liu committed
5
6
7
struct forwarder
{
    template <typename T>
Chao Liu's avatar
Chao Liu committed
8
    __host__ __device__ constexpr T&& operator()(T&& x) const
Chao Liu's avatar
Chao Liu committed
9
    {
Chao Liu's avatar
Chao Liu committed
10
        return static_cast<T&&>(x);
Chao Liu's avatar
Chao Liu committed
11
12
13
    }
};

Chao Liu's avatar
Chao Liu committed
14
15
16
17
18
19
20
21
struct swallow
{
    template <class... Ts>
    __host__ __device__ constexpr swallow(Ts&&... ts)
    {
    }
};

Chao Liu's avatar
Chao Liu committed
22
23
24
25
26
27
28
29
#if 0
template<class F>
__host__ __device__ constexpr auto unpacker(F f)
{
    return [=](auto xs_array){ f(xs...); };
}
#endif

Chao Liu's avatar
Chao Liu committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Emulate compile time if statement for C++14
//   Get the idea from
//   "https://baptiste-wicht.com/posts/2015/07/simulate-static_if-with-c11c14.html"
// TODO: use if constexpr, when C++17 is supported
template <bool Predicate>
struct static_if
{
};

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

    template <class F>
    __host__ __device__ constexpr auto operator()(F f) const
    {
        // This is a trick for compiler:
        //   Pass forwarder to lambda "f" as "auto" argument, and maks sure "f" will use it,
Chao Liu's avatar
Chao Liu committed
49
50
        //   this will make "f" a generic lambda, so that "f" won't be compiled until being
        //   instantiated here
Chao Liu's avatar
Chao Liu committed
51
52
53
54
55
        f(forwarder{});
        return Type{};
    }

    template <class F>
56
    __host__ __device__ static constexpr auto Else(F)
Chao Liu's avatar
Chao Liu committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    {
        return Type{};
    }
};

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

    template <class F>
    __host__ __device__ constexpr auto operator()(F) const
    {
        return Type{};
    }

    template <class F>
74
    __host__ __device__ static constexpr auto Else(F f)
Chao Liu's avatar
Chao Liu committed
75
76
77
    {
        // This is a trick for compiler:
        //   Pass forwarder to lambda "f" as "auto" argument, and maks sure "f" will use it,
Chao Liu's avatar
Chao Liu committed
78
79
        //   this will make "f" a generic lambda, so that "f" won't be compiled until being
        //   instantiated here
Chao Liu's avatar
Chao Liu committed
80
81
82
83
        f(forwarder{});
        return Type{};
    }
};
84

Chao Liu's avatar
Chao Liu committed
85
template <index_t NLoop>
86
87
struct static_const_reduce_n
{
Chao Liu's avatar
Chao Liu committed
88
    // signature of F: F(Number<I>)
89
90
91
92
93
94
    template <class F, class Reduce>
    __host__ __device__ constexpr auto operator()(F f, Reduce r) const
    {
        static_assert(NLoop > 1, "out-of-range");

        constexpr auto a = f(Number<NLoop - 1>{});
95
        auto b = static_const_reduce_n<NLoop - 1>{}(f, r); // TODO: cannot use constexpr here, weird
96
97
98
99
100
101
102
103
104
105
106
107
108
        return r(a, b);
    }
};

template <>
struct static_const_reduce_n<1>
{
    template <class F, class Reduce>
    __host__ __device__ constexpr auto operator()(F f, Reduce) const
    {
        return f(Number<0>{});
    }
};