MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / MultiplyWithoutOverflow

Function MultiplyWithoutOverflow

tensorflow/core/util/overflow.h:26–46  ·  view source on GitHub ↗

Multiply two nonnegative int64's, returning negative for overflow

Source from the content-addressed store, hash-verified

24
25// Multiply two nonnegative int64's, returning negative for overflow
26inline int64 MultiplyWithoutOverflow(const int64 x, const int64 y) {
27 // Multiply in uint64 rather than int64 since signed overflow is undefined.
28 // Negative values will wrap around to large unsigned values in the casts
29 // (see section 4.7 [conv.integral] of the C++14 standard).
30 const uint64 ux = x;
31 const uint64 uy = y;
32 const uint64 uxy = ux * uy;
33
34 // Check if we overflow uint64, using a cheap check if both inputs are small
35 if (TF_PREDICT_FALSE((ux | uy) >> 32 != 0)) {
36 // Ensure nonnegativity. Note that negative numbers will appear "large"
37 // to the unsigned comparisons above.
38 CHECK(x >= 0 && y >= 0);
39
40 // Otherwise, detect overflow using a division
41 if (ux != 0 && uxy / ux != uy) return -1;
42 }
43
44 // Cast back to signed. Any negative value will signal an error.
45 return static_cast<int64>(uxy);
46}
47
48} // namespace tensorflow
49

Callers 13

TESTFunction · 0.70
IsValidMethod · 0.50
IsValidShapeMethod · 0.50
RecomputeNumElementsMethod · 0.50
AddDimMethod · 0.50
AddDimWithStatusMethod · 0.50
MakeShapeHelperFunction · 0.50
NumElementsMethod · 0.50
CalculateTensorSizeFunction · 0.50
CalculateTensorSizeMethod · 0.50
CalculateOutputSizeMethod · 0.50

Calls

no outgoing calls

Tested by 1

TESTFunction · 0.56