/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "operators/kernel/logical_kernel.h" namespace paddle_mobile { namespace operators { template struct LogicalAndFunctor { bool operator()(const T& a, const T& b) const { return a && b; } }; template struct LogicalOrFunctor { bool operator()(const T& a, const T& b) const { return a || b; } }; template struct LogicalNotFunctor { bool operator()(const T& a) const { return !a; } }; template struct LogicalXorFunctor { bool operator()(const T& a, const T& b) const { return (a || b) && !(a && b); } }; template void UnaryLogicalCompute(const Tensor* inputX, Tensor* output) { Functor func; std::transform(inputX->data(), inputX->data() + inputX->numel(), output->data(), func); } template void BinaryLogicalCompute(const Tensor* inputX, const Tensor* inputY, Tensor* output) { Functor func; std::transform(inputX->data(), inputX->data() + inputX->numel(), inputY->data(), output->data(), func); } #ifdef LOGICAL_AND_OP template <> bool LogicalAndKernel::Init(LogicalBinaryParam* param) { return true; } template <> void LogicalAndKernel::Compute( const LogicalBinaryParam& param) { auto* inputX = param.InputX(); auto* inputY = param.InputY(); auto* out = param.Out(); out->mutable_data(); BinaryLogicalCompute>(inputX, inputY, out); } #endif #ifdef LOGICAL_OR_OP template <> bool LogicalOrKernel::Init(LogicalBinaryParam* param) { return true; } template <> void LogicalOrKernel::Compute( const LogicalBinaryParam& param) { auto* inputX = param.InputX(); auto* inputY = param.InputY(); auto* out = param.Out(); out->mutable_data(); BinaryLogicalCompute>(inputX, inputY, out); } #endif #ifdef LOGICAL_NOT_OP template <> bool LogicalNotKernel::Init(LogicalUnaryParam* param) { return true; } template <> void LogicalNotKernel::Compute( const LogicalUnaryParam& param) { auto* inputX = param.InputX(); auto* out = param.Out(); out->mutable_data(); UnaryLogicalCompute>(inputX, out); } #endif #ifdef LOGICAL_XOR_OP template <> bool LogicalXorKernel::Init(LogicalBinaryParam* param) { return true; } template <> void LogicalXorKernel::Compute( const LogicalBinaryParam& param) { auto* inputX = param.InputX(); auto* inputY = param.InputY(); auto* out = param.Out(); out->mutable_data(); BinaryLogicalCompute>(inputX, inputY, out); } #endif } // namespace operators } // namespace paddle_mobile