MU Library
call_main.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2024 XCENA Inc.
3 
4 #pragma once
5 
6 #include <cstdint>
7 
8 #include "call_main_template.hpp"
9 #include "mu/ndarray/ndarray.hpp"
10 namespace mu
11 {
12 
13 bool isNormalMode(uint32_t mode);
14 
15 template <typename Function>
16 struct function_traits : public function_traits_helper<typename add_pointer<Function>::type>
17 {
18 };
19 
20 template <typename F, unsigned Arity = function_traits<F>::arity>
21 class CallHelper;
22 #define GET_ARG(num) \
23  using remove_reference##num##_t = typename remove_reference<typename function_traits<F>::arg##num##_type>::type; \
24  remove_reference##num##_t* a##num; \
25  a##num = *((remove_reference##num##_t**)(buffer)); \
26  \
27  if constexpr (is_ndarray_v<remove_reference##num##_t>) \
28  { \
29  /* NDArray type - direct cast */ \
30  void* argAddress = *(void**)buffer; \
31  a##num = *((remove_reference##num##_t**)(argAddress)); \
32  } \
33  else if constexpr (is_pointer<remove_reference##num##_t>::value) \
34  { \
35  /* Pointer type - handle NDArray bit */ \
36  if ((ndarrayValueBit & (1ull << (num - 1))) != 0) \
37  { \
38  auto ndarray = reinterpret_cast<mu::NDArray<char>*>(buffer); \
39  void* data_ptr = ndarray->data(); \
40  a##num = *reinterpret_cast<remove_reference##num##_t**>(data_ptr); \
41  } \
42  } \
43  else \
44  { \
45  /* Non-pointer, non-NDArray type */ \
46  if (isNormalMode(mode) == false) \
47  { \
48  void* argAddress = *(void**)buffer; \
49  if ((ndarrayValueBit & (1ull << (num - 1))) != 0) \
50  { \
51  auto ndarray = reinterpret_cast<mu::NDArray<char>*>(argAddress); \
52  void* data_ptr = ndarray->data(); \
53  a##num = *reinterpret_cast<remove_reference##num##_t**>(data_ptr); \
54  } \
55  else \
56  { \
57  a##num = *((remove_reference##num##_t**)(argAddress)); \
58  } \
59  } \
60  } \
61  buffer += sizeof(void*);
62 
63 #define PRIMITIVE_CAT(a, ...) a##__VA_ARGS__
64 #define ARGS(x) PRIMITIVE_CAT(ARGS_, x)
65 #define ARGS_0
66 #define ARGS_1 GET_ARG(1);
67 #define ARGS_2 ARGS_1 GET_ARG(2);
68 #define ARGS_3 ARGS_2 GET_ARG(3);
69 #define ARGS_4 ARGS_3 GET_ARG(4);
70 #define ARGS_5 ARGS_4 GET_ARG(5);
71 #define ARGS_6 ARGS_5 GET_ARG(6);
72 #define ARGS_7 ARGS_6 GET_ARG(7);
73 #define ARGS_8 ARGS_7 GET_ARG(8);
74 #define ARGS_9 ARGS_8 GET_ARG(9);
75 
76 #define CALL_PARAMS(x) PRIMITIVE_CAT(CALL_PARAMS_, x)
77 #define CALL_PARAMS_0
78 #define CALL_PARAMS_1 *a1
79 #define CALL_PARAMS_2 CALL_PARAMS_1, *a2
80 #define CALL_PARAMS_3 CALL_PARAMS_2, *a3
81 #define CALL_PARAMS_4 CALL_PARAMS_3, *a4
82 #define CALL_PARAMS_5 CALL_PARAMS_4, *a5
83 #define CALL_PARAMS_6 CALL_PARAMS_5, *a6
84 #define CALL_PARAMS_7 CALL_PARAMS_6, *a7
85 #define CALL_PARAMS_8 CALL_PARAMS_7, *a8
86 #define CALL_PARAMS_9 CALL_PARAMS_8, *a9
87 #define CALL_FUNC(x) fp(CALL_PARAMS(x))
88 
89 #define CALL_HELPER(_NUM) \
90  template <typename F> \
91  class CallHelper<F, _NUM> \
92  { \
93  public: \
94  template <typename FP> \
95  static void invoke(FP fp, [[maybe_unused]] uint32_t mode, uint32_t ndarrayValueBit, [[maybe_unused]] char* buffer) \
96  { \
97  ARGS(_NUM); \
98  CALL_FUNC(_NUM); \
99  } \
100  }
101 
112 
113 #undef GET_ARG
114 #undef PRIMITIVE_CAT
115 #undef ARGS
116 #undef ARGS_0
117 #undef ARGS_1
118 #undef ARGS_2
119 #undef ARGS_3
120 #undef ARGS_4
121 #undef ARGS_5
122 #undef ARGS_6
123 #undef ARGS_7
124 #undef ARGS_8
125 #undef ARGS_9
126 #undef CALL_PARAMS
127 #undef CALL_PARAMS_0
128 #undef CALL_PARAMS_1
129 #undef CALL_PARAMS_2
130 #undef CALL_PARAMS_3
131 #undef CALL_PARAMS_4
132 #undef CALL_PARAMS_5
133 #undef CALL_PARAMS_6
134 #undef CALL_PARAMS_7
135 #undef CALL_PARAMS_8
136 #undef CALL_PARAMS_9
137 #undef CALL_FUNC
138 #undef CALL_HELPER
139 
140 template <typename FP>
141 inline void callMain(FP func, uint32_t mode, uint32_t ndarrayValueBit, char* buffer)
142 {
143  CallHelper<typename remove_pointer<FP>::type>::invoke(func, mode, ndarrayValueBit, buffer);
144 }
145 
146 #undef GET_ARG
147 
148 } // namespace mu
Definition: assert.hpp:9
void callMain(FP func, uint32_t mode, uint32_t ndarrayValueBit, char *buffer)
Definition: call_main.hpp:141
CALL_HELPER(0)
bool isNormalMode(uint32_t mode)