blob: 1a19f629e0182482e8499fc504d49f3389bb730a [file] [log] [blame]
Eric Kunze2364dcd2021-04-26 11:06:57 -07001
2// Copyright (c) 2020-2021, ARM Limited.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef _TOSA_SERIALIZATION_ATTRIBUTE_H
17#define _TOSA_SERIALIZATION_ATTRIBUTE_H
18#include "flatbuffers/idl.h"
19#include "flatbuffers/util.h"
20#include "tosa_generated.h"
21
22using std::string;
23
24namespace tosa
25{
26
27class TosaAttributeBase
28{
29public:
30 virtual ~TosaAttributeBase()
31 {}
32};
33
34class TosaNoneAttribute : public TosaAttributeBase
35{
36public:
37 TosaNoneAttribute()
38 {}
39 TosaNoneAttribute(TosaNoneAttribute* p)
40 {}
41};
42
James Wardc15f7d52022-12-07 15:38:01 +000043inline int convertFlatbuffersU8toF32(const flatbuffers::Vector<uint8_t>& in, uint32_t out_size, std::vector<float>& out)
44{
45 out.clear();
46 if (in.size() < out_size * sizeof(float))
47 {
48 printf("convertFlatbuffersU8toF32(): uint8 Flatbuffers buffer size %u must be >= target size %ld\n", in.size(),
49 out_size * sizeof(float));
50 return 1;
51 }
52 for (uint32_t i = 0; i < out_size; i++)
53 {
54 uint32_t byte0 = in[i * sizeof(float)];
55 uint32_t byte1 = in[i * sizeof(float) + 1];
56 uint32_t byte2 = in[i * sizeof(float) + 2];
57 uint32_t byte3 = in[i * sizeof(float) + 3];
58 uint32_t val_u32 = byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
59 float* val_fp32 = reinterpret_cast<float*>(&val_u32);
60 out.push_back(*val_fp32);
61 }
62 return 0;
63}
64
Eric Kunze2364dcd2021-04-26 11:06:57 -070065#define DEF_ARGS_VER0_S_STR(V) _##V = p->V()->str();
66#define DEF_ARGS_VER0_S_DEFAULT(V) _##V = p->V();
James Wardc15f7d52022-12-07 15:38:01 +000067#define DEF_ARGS_VER0_S_float_as_bytes(V) \
68 { \
69 std::vector<float> attr_vec; \
Won Jeon0df9a1c2023-06-07 18:09:40 +000070 if (p->V() && convertFlatbuffersU8toF32(*(p->V()), 1, attr_vec)) \
Tai Ly9fccf932023-02-27 19:06:37 +000071 assert(0 && "Failed to convert u8 buffer to f32"); \
72 _##V = (!attr_vec.empty()) ? attr_vec[0] : 0.0f; \
James Wardc15f7d52022-12-07 15:38:01 +000073 }
Eric Kunze2364dcd2021-04-26 11:06:57 -070074
75#define DEF_ARGS_VER0_S_int32_t(V) DEF_ARGS_VER0_S_DEFAULT(V)
James Wardc15f7d52022-12-07 15:38:01 +000076#define DEF_ARGS_VER0_S_float(V) DEF_ARGS_VER0_S_float_as_bytes(V)
Eric Kunze2364dcd2021-04-26 11:06:57 -070077#define DEF_ARGS_VER0_S_bool(V) DEF_ARGS_VER0_S_DEFAULT(V)
78#define DEF_ARGS_VER0_S_ResizeMode(V) DEF_ARGS_VER0_S_DEFAULT(V)
James Ward485a11d2022-08-05 13:48:37 +010079#define DEF_ARGS_VER0_S_DType(V) DEF_ARGS_VER0_S_DEFAULT(V)
Eric Kunze2364dcd2021-04-26 11:06:57 -070080#define DEF_ARGS_VER0_S_string(V) DEF_ARGS_VER0_S_STR(V)
81
82#define DEF_ARGS_VER0_S(T, V) DEF_ARGS_VER0_S_##T(V)
83#define DEF_ARGS_VER0_V(T, V) _##V = std::vector<T>(p->V()->begin(), p->V()->end());
84
85#define DEF_ARGS_VER1_S(T, V) const T& V
86#define DEF_ARGS_VER1_V(T, V) const std::vector<T>& V
87#define DEF_ARGS_VER2_S(T, V) _##V = V;
88#define DEF_ARGS_VER2_V(T, V) _##V = V;
89#define DEF_ARGS_VER3_S(T, V) \
90 T V() const \
91 { \
92 return _##V; \
93 }
94#define DEF_ARGS_VER3_V(T, V) \
95 std::vector<T> V() const \
96 { \
97 return _##V; \
98 }
99#define DEF_ARGS_VER4_S(T, V) T _##V;
100#define DEF_ARGS_VER4_V(T, V) std::vector<T> _##V;
101
102// another level of preprocessor indirection to handle ", " as function's input argument
103#define DEF_ARGS_VER1_TRUE(T, F, V) DEF_ARGS_VER1_##F(T, V)
104#define DEF_ARGS_VER1_FALSE(T, F, V) , DEF_ARGS_VER1_##F(T, V)
105
106#define DEF_ARGS_VER0(FIRST, T, F, V) DEF_ARGS_VER0_##F(T, V)
107#define DEF_ARGS_VER1(FIRST, T, F, V) DEF_ARGS_VER1_##FIRST(T, F, V)
108#define DEF_ARGS_VER2(FIRST, T, F, V) DEF_ARGS_VER2_##F(T, V)
109#define DEF_ARGS_VER3(FIRST, T, F, V) DEF_ARGS_VER3_##F(T, V)
110#define DEF_ARGS_VER4(FIRST, T, F, V) DEF_ARGS_VER4_##F(T, V)
111
112#define DEF_ARGS_0(VER, ...)
113#define DEF_ARGS_1(VER, T0, F0, V0) DEF_ARGS_##VER(TRUE, T0, F0, V0)
114#define DEF_ARGS_2(VER, T0, F0, V0, T1, F1, V1) DEF_ARGS_##VER(TRUE, T0, F0, V0) DEF_ARGS_##VER(FALSE, T1, F1, V1)
115#define DEF_ARGS_3(VER, T0, F0, V0, T1, F1, V1, T2, F2, V2) \
116 DEF_ARGS_##VER(TRUE, T0, F0, V0) DEF_ARGS_##VER(FALSE, T1, F1, V1) DEF_ARGS_##VER(FALSE, T2, F2, V2)
117#define DEF_ARGS_4(VER, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3) \
118 DEF_ARGS_##VER(TRUE, T0, F0, V0) DEF_ARGS_##VER(FALSE, T1, F1, V1) DEF_ARGS_##VER(FALSE, T2, F2, V2) \
119 DEF_ARGS_##VER(FALSE, T3, F3, V3)
120#define DEF_ARGS_5(VER, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4) \
121 DEF_ARGS_##VER(TRUE, T0, F0, V0) DEF_ARGS_##VER(FALSE, T1, F1, V1) DEF_ARGS_##VER(FALSE, T2, F2, V2) \
122 DEF_ARGS_##VER(FALSE, T3, F3, V3) DEF_ARGS_##VER(FALSE, T4, F4, V4)
123
124#define DEF_ARGS_6(VER, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5) \
125 DEF_ARGS_##VER(TRUE, T0, F0, V0) DEF_ARGS_##VER(FALSE, T1, F1, V1) DEF_ARGS_##VER(FALSE, T2, F2, V2) \
126 DEF_ARGS_##VER(FALSE, T3, F3, V3) DEF_ARGS_##VER(FALSE, T4, F4, V4) DEF_ARGS_##VER(FALSE, T5, F5, V5)
127
128#define DEF_ARGS_7(VER, T0, F0, V0, T1, F1, V1, T2, F2, V2, T3, F3, V3, T4, F4, V4, T5, F5, V5, T6, F6, V6) \
129 DEF_ARGS_##VER(TRUE, T0, F0, V0) DEF_ARGS_##VER(FALSE, T1, F1, V1) DEF_ARGS_##VER(FALSE, T2, F2, V2) \
130 DEF_ARGS_##VER(FALSE, T3, F3, V3) DEF_ARGS_##VER(FALSE, T4, F4, V4) DEF_ARGS_##VER(FALSE, T5, F5, V5) \
131 DEF_ARGS_##VER(FALSE, T6, F6, V6)
132
133#define DEF_VER0_VAR_DECL_PTR(NAME) const NAME* p = static_cast<const NAME*>(options);
134#define DEF_VER0_VAR_0(NAME)
135#define DEF_VER0_VAR_1(NAME) DEF_VER0_VAR_DECL_PTR(NAME)
136#define DEF_VER0_VAR_2(NAME) DEF_VER0_VAR_DECL_PTR(NAME)
137#define DEF_VER0_VAR_3(NAME) DEF_VER0_VAR_DECL_PTR(NAME)
138#define DEF_VER0_VAR_4(NAME) DEF_VER0_VAR_DECL_PTR(NAME)
139#define DEF_VER0_VAR_5(NAME) DEF_VER0_VAR_DECL_PTR(NAME)
140#define DEF_VER0_VAR_6(NAME) DEF_VER0_VAR_DECL_PTR(NAME)
141#define DEF_VER0_VAR_7(NAME) DEF_VER0_VAR_DECL_PTR(NAME)
142
143#define DEF_ATTRIBUTE(NAME, NUM_ARGS, ...) \
144 class Tosa##NAME##Attribute : public TosaAttributeBase \
145 { \
146 public: \
147 Tosa##NAME##Attribute(const TosaAttributeBase* options) \
148 { \
Kevin Cheng55b363b2021-11-06 01:24:24 +0000149 const Tosa##NAME##Attribute* p = static_cast<const Tosa##NAME##Attribute*>(options); \
Eric Kunze2364dcd2021-04-26 11:06:57 -0700150 *this = *p; \
151 } \
152 Tosa##NAME##Attribute(const Tosa##NAME##Attribute* p) \
153 { \
154 *this = *p; \
155 } \
156 Tosa##NAME##Attribute(const void* options){ DEF_VER0_VAR_##NUM_ARGS(NAME##Attribute) \
157 DEF_ARGS_##NUM_ARGS(VER0, __VA_ARGS__) } Tosa##NAME \
158 ##Attribute(DEF_ARGS_##NUM_ARGS(VER1, __VA_ARGS__)) \
159 { \
160 DEF_ARGS_##NUM_ARGS(VER2, __VA_ARGS__) \
161 } \
162 virtual ~Tosa##NAME##Attribute() \
163 {} \
164 DEF_ARGS_##NUM_ARGS(VER3, __VA_ARGS__) private : DEF_ARGS_##NUM_ARGS(VER4, __VA_ARGS__) \
165 };
166
167#include "attribute.def"
168#undef DEF_ATTRIBUTE
169#undef DEF_ARGS_0
170#undef DEF_ARGS_1
171#undef DEF_ARGS_2
172#undef DEF_ARGS_3
173#undef DEF_ARGS_4
174#undef DEF_ARGS_5
175#undef DEF_ARGS_6
176#undef DEF_ARGS_7
177#undef DEF_ARGS_VER0
178#undef DEF_ARGS_VER1
179#undef DEF_ARGS_VER2
180#undef DEF_ARGS_VER3
181#undef DEF_ARGS_VER4
182#undef DEF_ARGS_VER0_S_int32_t
183#undef DEF_ARGS_VER0_S_float
184#undef DEF_ARGS_VER0_S_bool
185#undef DEF_ARGS_VER0_S_ResizeMode
James Ward485a11d2022-08-05 13:48:37 +0100186#undef DEF_ARGS_VER0_S_DType
Eric Kunze2364dcd2021-04-26 11:06:57 -0700187#undef DEF_ARGS_VER0_S_string
188#undef DEF_ARGS_VER0_S_STR
189#undef DEF_ARGS_VER0_S_DEFAULT
190#undef DEF_ARGS_VER1_TRUE
191#undef DEF_ARGS_VER1_FALSE
192#undef DEF_ARGS_VER0_S
193#undef DEF_ARGS_VER0_V
194#undef DEF_ARGS_VER1_S
195#undef DEF_ARGS_VER1_V
196#undef DEF_ARGS_VER2_S
197#undef DEF_ARGS_VER2_V
198#undef DEF_ARGS_VER3_S
199#undef DEF_ARGS_VER3_V
200#undef DEF_ARGS_VER4_S
201#undef DEF_ARGS_VER4_V
202#undef DEF_VER0_VAR_0
203#undef DEF_VER0_VAR_1
204#undef DEF_VER0_VAR_2
205#undef DEF_VER0_VAR_3
206#undef DEF_VER0_VAR_4
207#undef DEF_VER0_VAR_5
208#undef DEF_VER0_VAR_DECL_PTR
209
210} // namespace tosa
211
212#endif // _TOSA_SERIALIZATION_ATTRIBUTE_H