9d080d89b861468d08b361fdd519b1233219c60d
[platform/core/ml/nnfw.git] / compute / cker / include / cker / operation / Elementwise.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2018 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef __NNFW_CKER_ELEMENTWISE_H__
19 #define __NNFW_CKER_ELEMENTWISE_H__
20
21 #include "cker/eigen/Utils.h"
22 #include "cker/Shape.h"
23 #include "cker/Types.h"
24 #include <Eigen/Core>
25
26 namespace nnfw
27 {
28 namespace cker
29 {
30
31 inline void Sin(const Shape &input_shape, const float *input_data, const Shape &output_shape,
32                 float *output_data)
33 {
34   const int size = MatchingFlatSize(input_shape, output_shape);
35   for (int i = 0; i < size; i++)
36   {
37     output_data[i] = std::sin(input_data[i]);
38   }
39 }
40
41 inline void Cos(const Shape &input_shape, const float *input_data, const Shape &output_shape,
42                 float *output_data)
43 {
44   const int size = MatchingFlatSize(input_shape, output_shape);
45   for (int i = 0; i < size; i++)
46   {
47     output_data[i] = std::cos(input_data[i]);
48   }
49 }
50
51 inline void Abs(const Shape &input_shape, const float *input_data, const Shape &output_shape,
52                 float *output_data)
53 {
54   auto input_map = MapAsVector(input_data, input_shape);
55   auto output_map = MapAsVector(output_data, output_shape);
56   output_map.array() = input_map.array().abs();
57 }
58
59 inline void Rsqrt(const Shape &input_shape, const float *input_data, const Shape &output_shape,
60                   float *output_data)
61 {
62   const int size = MatchingFlatSize(input_shape, output_shape);
63   for (int i = 0; i < size; i++)
64   {
65     output_data[i] = 1.f / std::sqrt(input_data[i]);
66   }
67 }
68
69 template <typename T>
70 inline void Neg(const Shape &input_shape, const T *input_data, const Shape &output_shape,
71                 T *output_data)
72 {
73   const int size = MatchingFlatSize(input_shape, output_shape);
74   for (int i = 0; i < size; i++)
75   {
76     output_data[i] = -input_data[i];
77   }
78 }
79
80 inline void Log(const Shape &input_shape, const float *input_data, const Shape &output_shape,
81                 float *output_data)
82 {
83   const int size = MatchingFlatSize(input_shape, output_shape);
84   for (int i = 0; i < size; i++)
85   {
86     output_data[i] = std::log(input_data[i]);
87   }
88 }
89
90 inline void Floor(const Shape &input_shape, const float *input_data, const Shape &output_shape,
91                   float *output_data)
92 {
93   const int flat_size = MatchingFlatSize(input_shape, output_shape);
94
95   for (int i = 0; i < flat_size; i++)
96   {
97     output_data[i] = std::floor(input_data[i]);
98   }
99 }
100
101 } // namespace cker
102 } // namespace nnfw
103
104 #endif // __NNFW_CKER_ELEMENTWISE_H__