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 "Neg.h"
20
21 #include <cmath>
22
23 #include "OperationResolver.h"
24 #include "OperationsExecutionUtils.h"
25 #include "Tracing.h"
26
27 namespace android {
28 namespace nn {
29 namespace neg {
30 namespace {
31
32 template <typename T>
compute(const T * input,const Shape & shape,T * output)33 inline bool compute(const T* input, const Shape& shape, T* output) {
34 const auto size = getNumberOfElements(shape);
35 for (uint32_t i = 0; i < size; ++i) {
36 output[i] = -input[i];
37 }
38 return true;
39 }
40
41 } // namespace
42
prepare(IOperationExecutionContext * context)43 bool prepare(IOperationExecutionContext* context) {
44 Shape input = context->getInputShape(kInputTensor);
45 Shape output = context->getOutputShape(kOutputTensor);
46 NN_RET_CHECK(SetShape(input, &output));
47 return context->setOutputShape(kOutputTensor, output);
48 }
49
execute(IOperationExecutionContext * context)50 bool execute(IOperationExecutionContext* context) {
51 switch (context->getInputType(kInputTensor)) {
52 case OperandType::TENSOR_FLOAT16:
53 return compute(context->getInputBuffer<_Float16>(kInputTensor),
54 context->getInputShape(kInputTensor),
55 context->getOutputBuffer<_Float16>(kOutputTensor));
56 case OperandType::TENSOR_FLOAT32:
57 return compute(context->getInputBuffer<float>(kInputTensor),
58 context->getInputShape(kInputTensor),
59 context->getOutputBuffer<float>(kOutputTensor));
60 case OperandType::TENSOR_INT32:
61 return compute(context->getInputBuffer<int32_t>(kInputTensor),
62 context->getInputShape(kInputTensor),
63 context->getOutputBuffer<int32_t>(kOutputTensor));
64 default:
65 NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
66 }
67 }
68
69 } // namespace neg
70
71 NN_REGISTER_OPERATION_DEFAULT_VALIDATION(NEG, neg::prepare, neg::execute);
72
73 } // namespace nn
74 } // namespace android
75