1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_MOD_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_MOD_H_
17 
18 #include <functional>
19 
20 namespace tflite {
21 
22 namespace reference_ops {
23 
24 template <typename T>
FloorMod(T input1,T input2)25 T FloorMod(T input1, T input2) {
26   struct FloatMod {
27     float operator()(const float lhs, const float rhs) const {
28       return std::fmod(lhs, rhs);
29     }
30   };
31   using ModFunc = typename std::conditional<std::is_integral<T>::value,
32                                             std::modulus<T>, FloatMod>::type;
33   ModFunc mod_func;
34   T trunc_mod = mod_func(input1, input2);
35   return (trunc_mod != 0) && ((input2 < 0) != (trunc_mod < 0))
36              ? (trunc_mod + input2)
37              : trunc_mod;
38 }
39 
40 }  // namespace reference_ops
41 }  // namespace tflite
42 
43 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_FLOOR_MOD_H_
44