aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/lib/math/math_util.h
diff options
context:
space:
mode:
authorGravatar Sanjoy Das <sanjoy@google.com>2017-11-30 14:26:58 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-11-30 14:30:23 -0800
commit6bfc73a0b3c6810725a5eb0020470457cc5cc23e (patch)
treebbd11451f4c7ac5ae4ffa8233e6a2ffaa541d0a8 /tensorflow/core/lib/math/math_util.h
parent6ebb6d6465ddf2380430de7aa287676e9440df7e (diff)
Extract out a MathUtil::GCD helper
This fixes a TODO. PiperOrigin-RevId: 177508258
Diffstat (limited to 'tensorflow/core/lib/math/math_util.h')
-rw-r--r--tensorflow/core/lib/math/math_util.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/tensorflow/core/lib/math/math_util.h b/tensorflow/core/lib/math/math_util.h
index 6f279865e7..9e71598622 100644
--- a/tensorflow/core/lib/math/math_util.h
+++ b/tensorflow/core/lib/math/math_util.h
@@ -16,6 +16,8 @@ limitations under the License.
#ifndef TENSORFLOW_LIB_MATH_MATH_UTIL_H_
#define TENSORFLOW_LIB_MATH_MATH_UTIL_H_
+#include <type_traits>
+
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/types.h"
@@ -59,6 +61,9 @@ class MathUtil {
template <typename IntegralType, bool ceil>
static IntegralType CeilOrFloorOfRatio(IntegralType numerator,
IntegralType denominator);
+
+ template <typename IntegralType>
+ static IntegralType GCD(IntegralType x, IntegralType y);
};
// ---- CeilOrFloorOfRatio ----
@@ -107,6 +112,18 @@ IntegralType MathUtil::CeilOrFloorOfRatio(IntegralType numerator,
}
}
+template <typename IntegralType>
+IntegralType MathUtil::GCD(IntegralType a, IntegralType b) {
+ static_assert(std::is_unsigned<IntegralType>::value,
+ "signed GCD not supported!");
+ while (b != 0) {
+ IntegralType r = a % b;
+ a = b;
+ b = r;
+ }
+ return a;
+}
+
} // namespace tensorflow
#endif // TENSORFLOW_LIB_MATH_MATH_UTIL_H_