1/* Copyright 2017 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_TOCO_TOCO_PORT_H_
16#define TENSORFLOW_LITE_TOCO_TOCO_PORT_H_
17
18// Portability layer for toco tool. Mainly, abstract filesystem access so we
19// can build and use on google internal environments and on OSX.
20
21#include <string>
22#include "google/protobuf/text_format.h"
23#include "tensorflow/lite/toco/format_port.h"
24#include "tensorflow/core/lib/core/status.h"
25#include "tensorflow/core/platform/logging.h"
26#include "tensorflow/core/platform/platform.h"
27#if defined(PLATFORM_GOOGLE)
28#include "absl/strings/cord.h"
29#endif // PLATFORM_GOOGLE
30
31#ifdef PLATFORM_GOOGLE
32#define TFLITE_PROTO_NS proto2
33#else
34#define TFLITE_PROTO_NS google::protobuf
35#endif
36
37#ifdef __ANDROID__
38#include <sstream>
39namespace std {
40
41template <typename T>
42std::string to_string(T value)
43{
44 std::ostringstream os ;
45 os << value ;
46 return os.str() ;
47}
48
49#ifdef __ARM_ARCH_7A__
50double round(double x);
51#endif
52}
53#endif
54
55namespace toco {
56namespace port {
57
58// Things like tests use other initialization routines that need control
59// of flags. However, for testing we still want to use toco_port.h facilities.
60// This function sets initialized flag trivially.
61void InitGoogleWasDoneElsewhere();
62void InitGoogle(const char* usage, int* argc, char*** argv, bool remove_flags);
63void CheckInitGoogleIsDone(const char* message);
64
65namespace file {
66class Options {};
67inline Options Defaults() {
68 Options o;
69 return o;
70}
71tensorflow::Status GetContents(const std::string& filename,
72 std::string* contents, const Options& options);
73tensorflow::Status SetContents(const std::string& filename,
74 const std::string& contents,
75 const Options& options);
76std::string JoinPath(const std::string& a, const std::string& b);
77tensorflow::Status Writable(const std::string& filename);
78tensorflow::Status Readable(const std::string& filename,
79 const Options& options);
80tensorflow::Status Exists(const std::string& filename, const Options& options);
81} // namespace file
82
83// Copy `src` string to `dest`. User must ensure `dest` has enough space.
84#if defined(PLATFORM_GOOGLE)
85void CopyToBuffer(const ::absl::Cord& src, char* dest);
86#endif // PLATFORM_GOOGLE
87void CopyToBuffer(const std::string& src, char* dest);
88
89inline uint32 ReverseBits32(uint32 n) {
90 n = ((n >> 1) & 0x55555555) | ((n & 0x55555555) << 1);
91 n = ((n >> 2) & 0x33333333) | ((n & 0x33333333) << 2);
92 n = ((n >> 4) & 0x0F0F0F0F) | ((n & 0x0F0F0F0F) << 4);
93 return (((n & 0xFF) << 24) | ((n & 0xFF00) << 8) | ((n & 0xFF0000) >> 8) |
94 ((n & 0xFF000000) >> 24));
95}
96} // namespace port
97
98inline bool ParseFromStringOverload(const std::string& in,
99 TFLITE_PROTO_NS::Message* proto) {
100 return TFLITE_PROTO_NS::TextFormat::ParseFromString(in, proto);
101}
102
103template <typename Proto>
104bool ParseFromStringEitherTextOrBinary(const std::string& input_file_contents,
105 Proto* proto) {
106 if (proto->ParseFromString(input_file_contents)) {
107 return true;
108 }
109
110 if (ParseFromStringOverload(input_file_contents, proto)) {
111 return true;
112 }
113
114 return false;
115}
116
117} // namespace toco
118
119#endif // TENSORFLOW_LITE_TOCO_TOCO_PORT_H_
120