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 "HalInterfaces.h"
20 #include "OperationResolver.h"
21 #include "OperationsUtils.h"
22 
23 namespace android {
24 namespace nn {
25 namespace logical_not {
26 
27 constexpr uint32_t kNumInputs = 1;
28 constexpr uint32_t kInputTensor = 0;
29 
30 constexpr uint32_t kNumOutputs = 1;
31 constexpr uint32_t kOutputTensor = 0;
32 
33 namespace {
34 
35 using namespace hal;
36 
compute(const bool8 * input,const Shape & shape,bool8 * output)37 bool compute(const bool8* input, const Shape& shape, bool8* output) {
38     const auto size = getNumberOfElements(shape);
39     for (uint32_t i = 0; i < size; ++i) {
40         output[i] = input[i] == 0;
41     }
42     return true;
43 }
44 
45 }  // namespace
46 
validate(const IOperationValidationContext * context)47 bool validate(const IOperationValidationContext* context) {
48     NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
49     NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
50     OperandType inputType = context->getInputType(kInputTensor);
51     NN_RET_CHECK(inputType == OperandType::TENSOR_BOOL8)
52             << "Unsupported tensor type for LOGICAL_NOT";
53     NN_RET_CHECK(validateInputTypes(context, {inputType}));
54     NN_RET_CHECK(validateOutputTypes(context, {inputType}));
55     return validateHalVersion(context, HalVersion::V1_2);
56 }
57 
prepare(IOperationExecutionContext * context)58 bool prepare(IOperationExecutionContext* context) {
59     Shape input = context->getInputShape(kInputTensor);
60     Shape output = context->getOutputShape(kOutputTensor);
61     NN_RET_CHECK(SetShape(input, &output));
62     return context->setOutputShape(kOutputTensor, output);
63 }
64 
execute(IOperationExecutionContext * context)65 bool execute(IOperationExecutionContext* context) {
66     return compute(context->getInputBuffer<bool8>(kInputTensor),
67                    context->getInputShape(kInputTensor),
68                    context->getOutputBuffer<bool8>(kOutputTensor));
69 }
70 
71 }  // namespace logical_not
72 
73 NN_REGISTER_OPERATION(LOGICAL_NOT, "LOGICAL_NOT", logical_not::validate, logical_not::prepare,
74                       logical_not::execute);
75 
76 }  // namespace nn
77 }  // namespace android
78