2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2018 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "kernels/FloorMod.h"
19 #include "kernels/Utils.h"
21 #include <tensorflow/lite/kernels/internal/reference/binary_function.h>
27 template <typename T> T FloorDivFunc(T input1, T input2)
31 float operator()(const float lhs, const float rhs) const { return std::fmod(lhs, rhs); }
34 typename std::conditional<std::is_integral<T>::value, std::modulus<T>, FloatMod>::type;
36 T trunc_mod = mod_func(input1, input2);
37 return (trunc_mod != 0) && ((input2 < 0) != (trunc_mod < 0)) ? (trunc_mod + input2) : trunc_mod;
42 namespace luci_interpreter
48 FloorMod::FloorMod(const Tensor *x, const Tensor *y, Tensor *output) : Kernel({x, y}, {output}) {}
50 void FloorMod::configure()
52 LUCI_INTERPRETER_CHECK(x()->element_type() == output()->element_type());
53 LUCI_INTERPRETER_CHECK(y()->element_type() == output()->element_type());
55 output()->resize(calculateShapeForBroadcast(x()->shape(), y()->shape()));
58 void FloorMod::execute() const
60 switch (x()->element_type())
62 case DataType::FLOAT32:
66 evalInteger<int8_t>();
69 evalInteger<int16_t>();
72 evalInteger<int32_t>();
75 evalInteger<int64_t>();
78 throw std::runtime_error("Unsupported type.");
82 void FloorMod::evalFloat() const
84 const auto x_data = getTensorData<float>(x());
85 const auto y_data = getTensorData<float>(y());
87 if (x()->shape() != y()->shape())
89 tflite::reference_ops::BroadcastBinaryFunction4DSlow<float, float, float>(
90 getTensorShape(x()), x_data, getTensorShape(y()), y_data, getTensorShape(output()),
91 getTensorData<float>(output()), FloorDivFunc<float>);
95 tflite::reference_ops::BinaryFunction<float, float, float>(
96 getTensorShape(x()), x_data, getTensorShape(y()), y_data, getTensorShape(output()),
97 getTensorData<float>(output()), FloorDivFunc<float>);
101 template <typename T> void FloorMod::evalInteger() const
103 const auto x_data = getTensorData<T>(x());
104 const auto y_data = getTensorData<T>(y());
106 // Check the denominator
107 const auto y_data_type = y()->element_type();
108 if (y_data_type == DataType::S8 || y_data_type == DataType::S16 || y_data_type == DataType::S32 ||
109 y_data_type == DataType::S64)
111 for (int i = 0; i < getTensorShape(y()).FlatSize(); ++i)
113 LUCI_INTERPRETER_CHECK(y_data[i] != 0);
117 if (x()->shape() != y()->shape())
119 tflite::reference_ops::BroadcastBinaryFunction4DSlow<T, T, T>(
120 getTensorShape(x()), x_data, getTensorShape(y()), y_data, getTensorShape(output()),
121 getTensorData<T>(output()), FloorDivFunc<T>);
125 tflite::reference_ops::BinaryFunction<T, T, T>(getTensorShape(x()), x_data, getTensorShape(y()),
126 y_data, getTensorShape(output()),
127 getTensorData<T>(output()), FloorDivFunc<T>);
131 } // namespace kernels
132 } // namespace luci_interpreter