Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / loco / include / loco / IR / Dimension.h
1 /*
2  * Copyright (c) 2019 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 #ifndef __LOCO_IR_DIMENSION_H__
18 #define __LOCO_IR_DIMENSION_H__
19
20 #include <cstdint>
21
22 namespace loco
23 {
24
25 /**
26  * @brief The value of one dimension in a tensor shape
27  * @note The value may be unknown
28  */
29 class Dimension final
30 {
31 private:
32   enum class Kind
33   {
34     Known,
35     Unknown
36   };
37
38 public:
39   /// @brief Construct an "unknown" dimension
40   Dimension() = default;
41
42   /// @brief Construct a "known" dimension
43   Dimension(uint32_t value) { set(value); }
44
45 public:
46   /// @brief Return whether the value is known (or not)
47   bool known(void) const { return _kind == Kind::Known; }
48
49   /// @brief Return the value
50   /// @note This value is meaningful only for known dimension
51   uint32_t value(void) const { return _value; }
52
53   void set(uint32_t value)
54   {
55     _kind = Kind::Known;
56     _value = value;
57   }
58
59   void unset(void)
60   {
61     _kind = Kind::Unknown;
62     _value = 0;
63   }
64
65 private:
66   Kind _kind{Kind::Unknown};
67   uint32_t _value{0};
68 };
69
70 /**
71  * @brief Equality operator between two Dimensions
72  *
73  * @note Refer to the definition of equality of dimemsion at
74  *       https://www.tensorflow.org/api_docs/python/tf/Dimension#__eq__
75  */
76 bool operator==(const Dimension &, const Dimension &);
77 bool operator==(const Dimension &, uint32_t);
78 bool operator==(uint32_t, const Dimension &);
79
80 /// @brief Make an "unknown" dimension
81 Dimension make_dimension(void);
82
83 } // namespace loco
84
85 #endif // __LOCO_IR_DIMENSION_H__