Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compute / cker / include / cker / operation / LogicalAnd.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 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_LOGICAL_AND_H__
19 #define __NNFW_CKER_LOGICAL_AND_H__
20
21 #include "cker/Shape.h"
22 #include "cker/Utils.h"
23
24 namespace nnfw
25 {
26 namespace cker
27 {
28
29 template <typename T>
30 inline void LogicalAndBroadcast(const Shape &unextended_input1_shape, const T *input1_data,
31                                 const Shape &unextended_input2_shape, const T *input2_data,
32                                 const Shape &unextended_output_shape, T *output_data)
33 {
34   assert(unextended_input1_shape.DimensionsCount() <= 4);
35   assert(unextended_input2_shape.DimensionsCount() <= 4);
36   assert(unextended_output_shape.DimensionsCount() <= 4);
37   const Shape output_shape = Shape::ExtendedShape(4, unextended_output_shape);
38
39   NdArrayDesc<4> desc1;
40   NdArrayDesc<4> desc2;
41   NdArrayDescsForElementwiseBroadcast(unextended_input1_shape, unextended_input2_shape, &desc1,
42                                       &desc2);
43
44   for (int b = 0; b < output_shape.Dims(0); ++b)
45   {
46     for (int y = 0; y < output_shape.Dims(1); ++y)
47     {
48       for (int x = 0; x < output_shape.Dims(2); ++x)
49       {
50         for (int c = 0; c < output_shape.Dims(3); ++c)
51         {
52           auto out_idx = Offset(output_shape, b, y, x, c);
53           auto in1_idx = SubscriptToIndex(desc1, b, y, x, c);
54           auto in2_idx = SubscriptToIndex(desc2, b, y, x, c);
55           auto in1_val = input1_data[in1_idx];
56           auto in2_val = input2_data[in2_idx];
57           output_data[out_idx] = in1_val && in2_val;
58         }
59       }
60     }
61   }
62 }
63
64 template <typename T>
65 inline void LogicalAndElementwise(const Shape &shape, const T *input1_data, const T *input2_data,
66                                   T *output_data)
67 {
68
69   int num_elements = shape.FlatSize();
70
71   for (int t = 0; t < num_elements; t++)
72   {
73     output_data[t] = input1_data[t] && input2_data[t];
74   }
75 }
76
77 } // namespace cker
78 } // namespace nnfw
79
80 #endif // __NNFW_CKER_LOGICAL_AND_H__