1/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#ifndef TENSORFLOW_LITE_PORTABLE_TYPE_TO_TFLITETYPE_H_
16#define TENSORFLOW_LITE_PORTABLE_TYPE_TO_TFLITETYPE_H_
17
18// Most of the definitions have been moved to this subheader so that Micro
19// can include it without relying on <string> and <complex>, which isn't
20// available on all platforms.
21
22// Arduino build defines abs as a macro here. That is invalid C++, and breaks
23// libc++'s <complex> header, undefine it.
24#ifdef abs
25#undef abs
26#endif
27
28#include <stdint.h>
29
30#include "tensorflow/lite/c/common.h"
31
32namespace tflite {
33
34// Map statically from a C++ type to a TfLiteType. Used in interpreter for
35// safe casts.
36// Example:
37// typeToTfLiteType<bool>() -> kTfLiteBool
38template <typename T>
39constexpr TfLiteType typeToTfLiteType() {
40 return kTfLiteNoType;
41}
42// Map from TfLiteType to the corresponding C++ type.
43// Example:
44// TfLiteTypeToType<kTfLiteBool>::Type -> bool
45template <TfLiteType TFLITE_TYPE_ENUM>
46struct TfLiteTypeToType {}; // Specializations below
47
48// Template specialization for both typeToTfLiteType and TfLiteTypeToType.
49#define MATCH_TYPE_AND_TFLITE_TYPE(CPP_TYPE, TFLITE_TYPE_ENUM) \
50 template <> \
51 constexpr TfLiteType typeToTfLiteType<CPP_TYPE>() { \
52 return TFLITE_TYPE_ENUM; \
53 } \
54 template <> \
55 struct TfLiteTypeToType<TFLITE_TYPE_ENUM> { \
56 using Type = CPP_TYPE; \
57 }
58
59// No string mapping is included here, since the TF Lite packed representation
60// doesn't correspond to a C++ type well.
61MATCH_TYPE_AND_TFLITE_TYPE(int32_t, kTfLiteInt32);
62MATCH_TYPE_AND_TFLITE_TYPE(uint32_t, kTfLiteUInt32);
63MATCH_TYPE_AND_TFLITE_TYPE(int16_t, kTfLiteInt16);
64MATCH_TYPE_AND_TFLITE_TYPE(uint16_t, kTfLiteUInt16);
65MATCH_TYPE_AND_TFLITE_TYPE(int64_t, kTfLiteInt64);
66MATCH_TYPE_AND_TFLITE_TYPE(float, kTfLiteFloat32);
67MATCH_TYPE_AND_TFLITE_TYPE(unsigned char, kTfLiteUInt8);
68MATCH_TYPE_AND_TFLITE_TYPE(int8_t, kTfLiteInt8);
69MATCH_TYPE_AND_TFLITE_TYPE(bool, kTfLiteBool);
70MATCH_TYPE_AND_TFLITE_TYPE(TfLiteFloat16, kTfLiteFloat16);
71MATCH_TYPE_AND_TFLITE_TYPE(double, kTfLiteFloat64);
72MATCH_TYPE_AND_TFLITE_TYPE(uint64_t, kTfLiteUInt64);
73
74} // namespace tflite
75#endif // TENSORFLOW_LITE_PORTABLE_TYPE_TO_TFLITETYPE_H_
76