summaryrefslogtreecommitdiff
path: root/compiler/loco/include/loco/IR/DataTypeTraits.h
blob: c4479e545f7ee1e456a3d4e18a6157806af2b88d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __LOCO_IR_DATA_TYPE_TRAITS_H__
#define __LOCO_IR_DATA_TYPE_TRAITS_H__

#include "loco/IR/DataType.h"

#include <cassert>
#include <cstdint>

namespace loco
{

/**
 * @brief C++ scalar type corresponding to each DataType
 */
template <DataType DT> struct DataTypeImpl
{
  // using Type = ...
};

// TODO Support other enum values
template <> struct DataTypeImpl<DataType::S8>
{
  // Use C++ int8_t type for 8bit integer
  using Type = int8_t;
};

template <> struct DataTypeImpl<DataType::U8>
{
  // Use C++ uint8_t type for unsigned 8bit integer
  using Type = uint8_t;
};

template <> struct DataTypeImpl<DataType::S32>
{
  // Use C++ int32_t type for 32bit integer
  using Type = int32_t;
};

template <> struct DataTypeImpl<DataType::FLOAT32>
{
  // Use C++ float type for IEEE 32-bit floating-point numbers
  using Type = float;
};

/**
 * @brief Returns the size of the data type.
 * @note If you need the size at compile time, use `sizeof(typename DataTypeImpl<DT>::Type)`.
 */
inline uint32_t size(DataType data_type)
{
  switch (data_type)
  {
    case DataType::S8:
      return sizeof(DataTypeImpl<DataType::S8>::Type);
    case DataType::U8:
      return sizeof(DataTypeImpl<DataType::U8>::Type);
    case DataType::S32:
      return sizeof(DataTypeImpl<DataType::S32>::Type);
    case DataType::FLOAT32:
      return sizeof(DataTypeImpl<DataType::FLOAT32>::Type);
    default:
      // TODO Support remaining data types.
      assert(false);
      return UINT32_MAX; // Avoid compiler warning.
  }
}

} // namespace loco

#endif // __LOCO_IR_DATA_TYPE_TRAITS_H__