1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "Operations" 18 19 #include "OperationResolver.h" 20 #include "OperationsUtils.h" 21 22 namespace android { 23 namespace nn { 24 namespace logical_not { 25 26 constexpr uint32_t kNumInputs = 1; 27 constexpr uint32_t kInputTensor = 0; 28 29 constexpr uint32_t kNumOutputs = 1; 30 constexpr uint32_t kOutputTensor = 0; 31 32 namespace { 33 34 bool compute(const bool8* input, const Shape& shape, bool8* output) { 35 const auto size = getNumberOfElements(shape); 36 for (uint32_t i = 0; i < size; ++i) { 37 output[i] = input[i] == 0; 38 } 39 return true; 40 } 41 42 } // namespace 43 44 bool validate(const IOperationValidationContext* context) { 45 NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs); 46 NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs); 47 OperandType inputType = context->getInputType(kInputTensor); 48 NN_RET_CHECK(inputType == OperandType::TENSOR_BOOL8) 49 << "Unsupported tensor type for LOGICAL_NOT"; 50 NN_RET_CHECK(validateInputTypes(context, {inputType})); 51 NN_RET_CHECK(validateOutputTypes(context, {inputType})); 52 return validateHalVersion(context, HalVersion::V1_2); 53 } 54 55 bool prepare(IOperationExecutionContext* context) { 56 Shape input = context->getInputShape(kInputTensor); 57 Shape output = context->getOutputShape(kOutputTensor); 58 NN_RET_CHECK(SetShape(input, &output)); 59 return context->setOutputShape(kOutputTensor, output); 60 } 61 62 bool execute(IOperationExecutionContext* context) { 63 return compute(context->getInputBuffer<bool8>(kInputTensor), 64 context->getInputShape(kInputTensor), 65 context->getOutputBuffer<bool8>(kOutputTensor)); 66 } 67 68 } // namespace logical_not 69 70 NN_REGISTER_OPERATION(LOGICAL_NOT, "LOGICAL_NOT", logical_not::validate, logical_not::prepare, 71 logical_not::execute); 72 73 } // namespace nn 74 } // namespace android 75