From 31f925c7783fb8fa58278b31585dcf7bdb4cfd8c Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Wed, 18 Apr 2018 11:46:07 -0700 Subject: [PATCH] Change operands of subtraction expression to have well-defined behaviour. At present, signed arithmetic overflows (i.e. has undefined behaviour) in general, e.g. when computing 0 - INT_MIN or INT_MAX - INT_MIN. The fact that we want the result in the unsigned type does not help us here. The fix is to convert the operands to the corresponding unsigned type first and then perform the operation in unsigned arithmetic, which is well-defined and has the correct subtraction behaviour. PiperOrigin-RevId: 193391813 --- tensorflow/core/lib/random/random_distributions.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tensorflow/core/lib/random/random_distributions.h b/tensorflow/core/lib/random/random_distributions.h index ad16dbf..4cf3a99 100644 --- a/tensorflow/core/lib/random/random_distributions.h +++ b/tensorflow/core/lib/random/random_distributions.h @@ -164,7 +164,8 @@ class UniformDistribution { typedef int32 ResultElementType; // Must have lo < hi - UniformDistribution(int32 lo, int32 hi) : lo_(lo), range_(hi - lo) {} + UniformDistribution(int32 lo, int32 hi) + : lo_(lo), range_(static_cast(hi) - static_cast(lo)) {} PHILOX_DEVICE_INLINE ResultType operator()(Generator* gen) { @@ -198,7 +199,8 @@ class UniformDistribution { typedef int64 ResultElementType; // Must have lo < hi - UniformDistribution(int64 lo, int64 hi) : lo_(lo), range_(hi - lo) {} + UniformDistribution(int64 lo, int64 hi) + : lo_(lo), range_(static_cast(hi) - static_cast(lo)) {} PHILOX_DEVICE_INLINE ResultType operator()(Generator* gen) { -- 2.7.4