Daniel Konegen / MNIST_example

Dependencies:   mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers logical.cc Source File

logical.cc

00001 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
00002 
00003 Licensed under the Apache License, Version 2.0 (the "License");
00004 you may not use this file except in compliance with the License.
00005 You may obtain a copy of the License at
00006 
00007     http://www.apache.org/licenses/LICENSE-2.0
00008 
00009 Unless required by applicable law or agreed to in writing, software
00010 distributed under the License is distributed on an "AS IS" BASIS,
00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 See the License for the specific language governing permissions and
00013 limitations under the License.
00014 ==============================================================================*/
00015 #include "tensorflow/lite/c/c_api_internal.h"
00016 #include "tensorflow/lite/kernels/internal/reference/binary_function.h"
00017 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
00018 #include "tensorflow/lite/kernels/kernel_util.h"
00019 #include "tensorflow/lite/kernels/op_macros.h"
00020 
00021 namespace tflite {
00022 namespace ops {
00023 namespace micro {
00024 namespace logical {
00025 namespace {
00026 
00027 // Input/output tensor index.
00028 constexpr int kInputTensor1 = 0;
00029 constexpr int kInputTensor2 = 1;
00030 constexpr int kOutputTensor = 0;
00031 
00032 TfLiteStatus LogicalImpl(TfLiteContext* context, TfLiteNode* node,
00033                          bool (*func)(bool, bool)) {
00034   const TfLiteTensor* input1 = GetInput(context, node, kInputTensor1);
00035   const TfLiteTensor* input2 = GetInput(context, node, kInputTensor2);
00036   TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
00037 
00038   if (HaveSameShapes(input1, input2)) {
00039     reference_ops::BinaryFunction<bool, bool, bool>(
00040         GetTensorShape(input1), GetTensorData<bool>(input1),
00041         GetTensorShape(input2), GetTensorData<bool>(input2),
00042         GetTensorShape(output), GetTensorData<bool>(output), func);
00043   } else {
00044     reference_ops::BroadcastBinaryFunction4DSlow<bool, bool, bool>(
00045         GetTensorShape(input1), GetTensorData<bool>(input1),
00046         GetTensorShape(input2), GetTensorData<bool>(input2),
00047         GetTensorShape(output), GetTensorData<bool>(output), func);
00048   }
00049 
00050   return kTfLiteOk;
00051 }
00052 
00053 bool LogicalOr(bool x, bool y) { return x || y; }
00054 
00055 TfLiteStatus LogicalOrEval(TfLiteContext* context, TfLiteNode* node) {
00056   return LogicalImpl(context, node, LogicalOr);
00057 }
00058 
00059 bool LogicalAnd(bool x, bool y) { return x && y; }
00060 
00061 TfLiteStatus LogicalAndEval(TfLiteContext* context, TfLiteNode* node) {
00062   return LogicalImpl(context, node, LogicalAnd);
00063 }
00064 
00065 }  // namespace
00066 }  // namespace logical
00067 
00068 TfLiteRegistration* Register_LOGICAL_OR() {
00069   // Init, Free, Prepare, Eval are satisfying the Interface required by
00070   // TfLiteRegistration.
00071   static TfLiteRegistration r = {/* init */ nullptr, /* free */ nullptr,
00072                                  /* prepare */ nullptr, logical::LogicalOrEval};
00073   return &r;
00074 }
00075 
00076 TfLiteRegistration* Register_LOGICAL_AND() {
00077   // Init, Free, Prepare, Eval are satisfying the Interface required by
00078   // TfLiteRegistration.
00079   static TfLiteRegistration r = {/* init */ nullptr, /* free */ nullptr,
00080                                  /* prepare */ nullptr,
00081                                  logical::LogicalAndEval};
00082   return &r;
00083 }
00084 
00085 }  // namespace micro
00086 }  // namespace ops
00087 }  // namespace tflite