From 94e2213b3823ec1141077a7a7cc4f0b0af11876b Mon Sep 17 00:00:00 2001 From: Rasmus Munk Larsen Date: Fri, 8 Sep 2017 15:49:55 -0700 Subject: Avoid undefined behavior in Eigen::TensorCostModel::numThreads. If the cost is large enough then the thread count can be larger than the maximum representable int, so just casting it to an int is undefined behavior. Contributed by phurst@google.com. --- unsupported/Eigen/CXX11/src/Tensor/TensorCostModel.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'unsupported/Eigen/CXX11/src/Tensor/TensorCostModel.h') diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorCostModel.h b/unsupported/Eigen/CXX11/src/Tensor/TensorCostModel.h index 83c449cf1..ac6717977 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorCostModel.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorCostModel.h @@ -174,8 +174,9 @@ class TensorCostModel { static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE int numThreads( double output_size, const TensorOpCost& cost_per_coeff, int max_threads) { double cost = totalCost(output_size, cost_per_coeff); - int threads = (cost - kStartupCycles) / kPerThreadCycles + 0.9; - return numext::mini(max_threads, numext::maxi(1, threads)); + // Make sure we don't invoke undefined behavior when we convert to an int. + threads = numext::mini(threads, GenericNumTraits::highest()); + return numext::mini(max_threads, numext::maxi(1, threads)); } // taskSize assesses parallel task size. -- cgit v1.2.3