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 "LogicalAndOr.h"
20
21 #include <functional>
22 #include <vector>
23
24 #include "IndexedShapeWrapper.h"
25 #include "OperationResolver.h"
26 #include "OperationsExecutionUtils.h"
27
28 namespace android {
29 namespace nn {
30 namespace logical {
31 namespace {
32
compute(const std::function<bool (bool,bool)> & func,const bool8 * aData,const Shape & aShape,const bool8 * bData,const Shape & bShape,bool8 * outputData,const Shape & outputShape)33 bool compute(const std::function<bool(bool, bool)>& func, const bool8* aData, const Shape& aShape,
34 const bool8* bData, const Shape& bShape, bool8* outputData, const Shape& outputShape) {
35 IndexedShapeWrapper aShapeIndexed(aShape);
36 IndexedShapeWrapper bShapeIndexed(bShape);
37 IndexedShapeWrapper outputShapeIndexed(outputShape);
38 std::vector<uint32_t> curIndex(outputShape.dimensions.size(), 0);
39 bool lastIndex = false;
40 do {
41 uint32_t outputFlatIndex;
42 NN_RET_CHECK(outputShapeIndexed.indexToFlatIndex(curIndex, &outputFlatIndex));
43 uint32_t aFlatIndex;
44 NN_RET_CHECK(aShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &aFlatIndex));
45 uint32_t bFlatIndex;
46 NN_RET_CHECK(bShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &bFlatIndex));
47
48 outputData[outputFlatIndex] = func(aData[aFlatIndex], bData[bFlatIndex]);
49
50 NN_RET_CHECK(outputShapeIndexed.nextIndexInplace(&curIndex, &lastIndex));
51 } while (!lastIndex);
52 return true;
53 }
54
55 } // namespace
56
prepare(IOperationExecutionContext * context)57 bool prepare(IOperationExecutionContext* context) {
58 Shape input1 = context->getInputShape(kInputTensor1);
59 Shape input2 = context->getInputShape(kInputTensor2);
60 Shape output = context->getOutputShape(kOutputTensor);
61 NN_RET_CHECK(calculateBroadcastedShape(input1, input2, &output));
62 return context->setOutputShape(kOutputTensor, output);
63 }
64
executeAnd(IOperationExecutionContext * context)65 bool executeAnd(IOperationExecutionContext* context) {
66 return compute(
67 std::logical_and<bool>(), context->getInputBuffer<bool8>(kInputTensor1),
68 context->getInputShape(kInputTensor1), context->getInputBuffer<bool8>(kInputTensor2),
69 context->getInputShape(kInputTensor2), context->getOutputBuffer<bool8>(kOutputTensor),
70 context->getOutputShape(kOutputTensor));
71 }
72
executeOr(IOperationExecutionContext * context)73 bool executeOr(IOperationExecutionContext* context) {
74 return compute(
75 std::logical_or<bool>(), context->getInputBuffer<bool8>(kInputTensor1),
76 context->getInputShape(kInputTensor1), context->getInputBuffer<bool8>(kInputTensor2),
77 context->getInputShape(kInputTensor2), context->getOutputBuffer<bool8>(kOutputTensor),
78 context->getOutputShape(kOutputTensor));
79 }
80
81 } // namespace logical
82
83 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(LOGICAL_AND, logical::prepare, logical::executeAnd);
84 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(LOGICAL_OR, logical::prepare, logical::executeOr);
85
86 } // namespace nn
87 } // namespace android
88