1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations 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
32 namespace 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
38 template <typename T>
typeToTfLiteType()39 constexpr TfLiteType typeToTfLiteType() {
40 return kTfLiteNoType;
41 }
42 // Map from TfLiteType to the corresponding C++ type.
43 // Example:
44 // TfLiteTypeToType<kTfLiteBool>::Type -> bool
45 template <TfLiteType TFLITE_TYPE_ENUM>
46 struct 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.
61 MATCH_TYPE_AND_TFLITE_TYPE(int32_t, kTfLiteInt32);
62 MATCH_TYPE_AND_TFLITE_TYPE(uint32_t, kTfLiteUInt32);
63 MATCH_TYPE_AND_TFLITE_TYPE(int16_t, kTfLiteInt16);
64 MATCH_TYPE_AND_TFLITE_TYPE(int64_t, kTfLiteInt64);
65 MATCH_TYPE_AND_TFLITE_TYPE(float, kTfLiteFloat32);
66 MATCH_TYPE_AND_TFLITE_TYPE(unsigned char, kTfLiteUInt8);
67 MATCH_TYPE_AND_TFLITE_TYPE(int8_t, kTfLiteInt8);
68 MATCH_TYPE_AND_TFLITE_TYPE(bool, kTfLiteBool);
69 MATCH_TYPE_AND_TFLITE_TYPE(TfLiteFloat16, kTfLiteFloat16);
70 MATCH_TYPE_AND_TFLITE_TYPE(double, kTfLiteFloat64);
71 MATCH_TYPE_AND_TFLITE_TYPE(uint64_t, kTfLiteUInt64);
72
73 } // namespace tflite
74 #endif // TENSORFLOW_LITE_PORTABLE_TYPE_TO_TFLITETYPE_H_
75