17ff853eb16118f670642c883fea523a3d8cbe47
[platform/core/ml/nnfw.git] / compiler / luci / lang / src / Nodes / CircleConst.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
17 #include "luci/IR/Nodes/CircleConst.h"
18
19 #include <cassert>
20
21 namespace luci
22 {
23
24 template <loco::DataType DT> uint32_t CircleConst::size(void) const
25 {
26   assert(dtype() == DT);
27   assert(_data.size() % sizeof(typename loco::DataTypeImpl<DT>::Type) == 0);
28   return _data.size() / sizeof(typename loco::DataTypeImpl<DT>::Type);
29 }
30
31 template <loco::DataType DT> void CircleConst::size(uint32_t l)
32 {
33   assert(dtype() == DT);
34   _data.resize(l * sizeof(typename loco::DataTypeImpl<DT>::Type));
35 }
36
37 template <loco::DataType DT>
38 const typename loco::DataTypeImpl<DT>::Type &CircleConst::at(uint32_t n) const
39 {
40   assert(dtype() == DT);
41   assert(n < size<DT>());
42   return *(reinterpret_cast<const typename loco::DataTypeImpl<DT>::Type *>(_data.data()) + n);
43 }
44
45 template <loco::DataType DT> typename loco::DataTypeImpl<DT>::Type &CircleConst::at(uint32_t n)
46 {
47   assert(dtype() == DT);
48   assert(n < size<DT>());
49   return *(reinterpret_cast<typename loco::DataTypeImpl<DT>::Type *>(_data.data()) + n);
50 }
51
52 template <loco::DataType DT>
53 const typename loco::DataTypeImpl<DT>::Type &CircleConst::scalar(void) const
54 {
55   assert(dtype() == DT);
56   return *(reinterpret_cast<const typename loco::DataTypeImpl<DT>::Type *>(_data.data()));
57 }
58
59 template <loco::DataType DT> typename loco::DataTypeImpl<DT>::Type &CircleConst::scalar(void)
60 {
61   assert(dtype() == DT);
62   return *(reinterpret_cast<typename loco::DataTypeImpl<DT>::Type *>(_data.data()));
63 }
64
65 #define INSTANTIATE(DT)                                                                      \
66   template uint32_t CircleConst::size<DT>(void) const;                                       \
67   template void CircleConst::size<DT>(uint32_t);                                             \
68   template const typename loco::DataTypeImpl<DT>::Type &CircleConst::at<DT>(uint32_t) const; \
69   template typename loco::DataTypeImpl<DT>::Type &CircleConst::at<DT>(uint32_t);             \
70   template const typename loco::DataTypeImpl<DT>::Type &CircleConst::scalar<DT>(void) const; \
71   template typename loco::DataTypeImpl<DT>::Type &CircleConst::scalar<DT>(void);
72
73 INSTANTIATE(loco::DataType::S64);
74 INSTANTIATE(loco::DataType::S32);
75 INSTANTIATE(loco::DataType::S16);
76 INSTANTIATE(loco::DataType::FLOAT32);
77 INSTANTIATE(loco::DataType::U8);
78 INSTANTIATE(loco::DataType::BOOL);
79
80 #undef INSTANTIATE
81
82 } // namespace luci