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 #include "tensorflow/lite/c/common.h"
16 #include "tensorflow/lite/kernels/internal/reference/binary_function.h"
17 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
18 #include "tensorflow/lite/kernels/op_macros.h"
19 #include "tensorflow/lite/micro/kernels/kernel_util.h"
20
21 namespace tflite {
22 namespace ops {
23 namespace micro {
24 namespace logical {
25 namespace {
26
27 // Input/output tensor index.
28 constexpr int kInputTensor1 = 0;
29 constexpr int kInputTensor2 = 1;
30 constexpr int kOutputTensor = 0;
31
LogicalImpl(TfLiteContext * context,TfLiteNode * node,bool (* func)(bool,bool))32 TfLiteStatus LogicalImpl(TfLiteContext* context, TfLiteNode* node,
33 bool (*func)(bool, bool)) {
34 const TfLiteEvalTensor* input1 =
35 tflite::micro::GetEvalInput(context, node, kInputTensor1);
36 const TfLiteEvalTensor* input2 =
37 tflite::micro::GetEvalInput(context, node, kInputTensor2);
38 TfLiteEvalTensor* output =
39 tflite::micro::GetEvalOutput(context, node, kOutputTensor);
40
41 if (tflite::micro::HaveSameShapes(input1, input2)) {
42 reference_ops::BinaryFunction<bool, bool, bool>(
43 tflite::micro::GetTensorShape(input1),
44 tflite::micro::GetTensorData<bool>(input1),
45 tflite::micro::GetTensorShape(input2),
46 tflite::micro::GetTensorData<bool>(input2),
47 tflite::micro::GetTensorShape(output),
48 tflite::micro::GetTensorData<bool>(output), func);
49 } else {
50 reference_ops::BroadcastBinaryFunction4DSlow<bool, bool, bool>(
51 tflite::micro::GetTensorShape(input1),
52 tflite::micro::GetTensorData<bool>(input1),
53 tflite::micro::GetTensorShape(input2),
54 tflite::micro::GetTensorData<bool>(input2),
55 tflite::micro::GetTensorShape(output),
56 tflite::micro::GetTensorData<bool>(output), func);
57 }
58
59 return kTfLiteOk;
60 }
61
LogicalOr(bool x,bool y)62 bool LogicalOr(bool x, bool y) { return x || y; }
63
LogicalOrEval(TfLiteContext * context,TfLiteNode * node)64 TfLiteStatus LogicalOrEval(TfLiteContext* context, TfLiteNode* node) {
65 return LogicalImpl(context, node, LogicalOr);
66 }
67
LogicalAnd(bool x,bool y)68 bool LogicalAnd(bool x, bool y) { return x && y; }
69
LogicalAndEval(TfLiteContext * context,TfLiteNode * node)70 TfLiteStatus LogicalAndEval(TfLiteContext* context, TfLiteNode* node) {
71 return LogicalImpl(context, node, LogicalAnd);
72 }
73
74 } // namespace
75 } // namespace logical
76
Register_LOGICAL_OR()77 TfLiteRegistration Register_LOGICAL_OR() {
78 // Init, Free, Prepare, Eval are satisfying the Interface required by
79 // TfLiteRegistration.
80 return {/*init=*/nullptr,
81 /*free=*/nullptr,
82 /*prepare=*/nullptr,
83 /*invoke=*/logical::LogicalOrEval,
84 /*profiling_string=*/nullptr,
85 /*builtin_code=*/0,
86 /*custom_name=*/nullptr,
87 /*version=*/0};
88 }
89
Register_LOGICAL_AND()90 TfLiteRegistration Register_LOGICAL_AND() {
91 // Init, Free, Prepare, Eval are satisfying the Interface required by
92 // TfLiteRegistration.
93 return {/*init=*/nullptr,
94 /*free=*/nullptr,
95 /*prepare=*/nullptr,
96 /*invoke=*/logical::LogicalAndEval,
97 /*profiling_string=*/nullptr,
98 /*builtin_code=*/0,
99 /*custom_name=*/nullptr,
100 /*version=*/0};
101 }
102
103 } // namespace micro
104 } // namespace ops
105 } // namespace tflite
106