summaryrefslogtreecommitdiff
path: root/compute/cker/include/cker/operation/Comparison.h
blob: 4516358de2c2420404a7de317749de3c9564bd8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __NNFW_CKER_COMPARISON_H__
#define __NNFW_CKER_COMPARISON_H__

#include "cker/Shape.h"
#include "cker/Types.h"
#include "cker/Utils.h"

namespace nnfw
{
namespace cker
{

template <typename T> inline bool EqualFn(T lhs, T rhs) { return lhs == rhs; }
template <typename T> inline bool NotEqualFn(T lhs, T rhs) { return lhs != rhs; }
template <typename T> inline bool GreaterFn(T lhs, T rhs) { return lhs > rhs; }
template <typename T> inline bool GreaterEqualFn(T lhs, T rhs) { return lhs >= rhs; }
template <typename T> inline bool LessFn(T lhs, T rhs) { return lhs < rhs; }
template <typename T> inline bool LessEqualFn(T lhs, T rhs) { return lhs <= rhs; }

template <typename T> using ComparisonFn = bool (*)(T, T);

template <typename T, ComparisonFn<T> F>
inline void ComparisonImpl(const Shape &input1_shape, const T *input1_data,
                           const Shape &input2_shape, const T *input2_data,
                           const Shape &output_shape, bool *output_data)
{
  const int64_t flatsize = // number of data....
      MatchingFlatSize(input1_shape, input2_shape, output_shape);
  for (int64_t i = 0; i < flatsize; ++i)
  {
    output_data[i] = F(input1_data[i], input2_data[i]);
  }
}

template <ComparisonFn<float> F>
inline void Comparison(const Shape &input1_shape, const float *input1_data,
                       const Shape &input2_shape, const float *input2_data,
                       const Shape &output_shape, bool *output_data)
{
  ComparisonImpl<float, F>(input1_shape, input1_data, input2_shape, input2_data, output_shape,
                           output_data);
}

template <typename T, ComparisonFn<T> F>
inline void
BroadcastComparison4DSlowImpl(const Shape &unextended_input1_shape, const T *input1_data,
                              const Shape &unextended_input2_shape, const T *input2_data,
                              const Shape &unextended_output_shape, bool *output_data)
{
  assert(unextended_input1_shape.DimensionsCount() <= 4);
  assert(unextended_input2_shape.DimensionsCount() <= 4);
  assert(unextended_output_shape.DimensionsCount() <= 4);
  const Shape output_shape = Shape::ExtendedShape(4, unextended_output_shape);

  NdArrayDesc<4> desc1;
  NdArrayDesc<4> desc2;
  NdArrayDescsForElementwiseBroadcast(unextended_input1_shape, unextended_input2_shape, &desc1,
                                      &desc2);

  for (int b = 0; b < output_shape.Dims(0); ++b)
  {
    for (int y = 0; y < output_shape.Dims(1); ++y)
    {
      for (int x = 0; x < output_shape.Dims(2); ++x)
      {
        for (int c = 0; c < output_shape.Dims(3); ++c)
        {
          output_data[Offset(output_shape, b, y, x, c)] =
              F(input1_data[SubscriptToIndex(desc1, b, y, x, c)],
                input2_data[SubscriptToIndex(desc2, b, y, x, c)]);
        }
      }
    }
  }
}

template <typename T, ComparisonFn<T> F>
inline void BroadcastComparison4DSlow(const Shape &input1_shape, const T *input1_data,
                                      const Shape &input2_shape, const T *input2_data,
                                      const Shape &output_shape, bool *output_data)
{
  BroadcastComparison4DSlowImpl<T, F>(input1_shape, input1_data, input2_shape, input2_data,
                                      output_shape, output_data);
}

#define TFLITE_COMPARISON_OP(name)                                                                 \
  inline void name(const Shape &input1_shape, const float *input1_data, const Shape &input2_shape, \
                   const float *input2_data, const Shape &output_shape, bool *output_data)         \
  {                                                                                                \
    Comparison<name##Fn>(input1_shape, input1_data, input2_shape, input2_data, output_shape,       \
                         output_data);                                                             \
  }                                                                                                \
  template <typename T>                                                                            \
  inline void name##NoScaling(const Shape &input1_shape, const T *input1_data,                     \
                              const Shape &input2_shape, const T *input2_data,                     \
                              const Shape &output_shape, bool *output_data)                        \
  {                                                                                                \
    ComparisonImpl<T, name##Fn>(input1_shape, input1_data, input2_shape, input2_data,              \
                                output_shape, output_data);                                        \
  }                                                                                                \
  template <typename T>                                                                            \
  inline void Broadcast4DSlow##name##NoScaling(const Shape &input1_shape, const T *input1_data,    \
                                               const Shape &input2_shape, const T *input2_data,    \
                                               const Shape &output_shape, bool *output_data)       \
  {                                                                                                \
    BroadcastComparison4DSlowImpl<T, name##Fn>(input1_shape, input1_data, input2_shape,            \
                                               input2_data, output_shape, output_data);            \
  }                                                                                                \
  template <typename T>                                                                            \
  inline void Broadcast4DSlow##name(const Shape &input1_shape, const T *input1_data,               \
                                    const Shape &input2_shape, const T *input2_data,               \
                                    const Shape &output_shape, bool *output_data)                  \
  {                                                                                                \
    BroadcastComparison4DSlow<T, name##Fn>(input1_shape, input1_data, input2_shape, input2_data,   \
                                           output_shape, output_data);                             \
  }

TFLITE_COMPARISON_OP(Equal);
TFLITE_COMPARISON_OP(NotEqual);
TFLITE_COMPARISON_OP(Greater);
TFLITE_COMPARISON_OP(GreaterEqual);
TFLITE_COMPARISON_OP(Less);
TFLITE_COMPARISON_OP(LessEqual);
#undef TFLITE_COMPARISON_OP

} // namespace cker
} // namespace nnfw

#endif