1 /* Copyright 2015 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
16 #ifndef TENSORFLOW_COMPILER_XLA_OVERFLOW_UTIL_H_
17 #define TENSORFLOW_COMPILER_XLA_OVERFLOW_UTIL_H_
18
19 #include <optional>
20 #include <type_traits>
21
22 #include "tensorflow/compiler/xla/xla_data.pb.h"
23 #include "tensorflow/core/platform/logging.h"
24
25 namespace xla {
26
27 // Multiply two nonnegative int64_t's, returning negative for overflow
MultiplyWithoutOverflow(const int64_t x,const int64_t y)28 inline int64_t MultiplyWithoutOverflow(const int64_t x, const int64_t y) {
29 // Multiply in uint64_t rather than int64_t since signed overflow is
30 // undefined. Negative values will wrap around to large unsigned values in the
31 // casts (see section 4.7 [conv.integral] of the C++14 standard).
32 const uint64_t ux = x;
33 const uint64_t uy = y;
34 const uint64_t uxy = ux * uy;
35
36 // Check if we overflow uint64_t, using a cheap check if both inputs are small
37 if (ABSL_PREDICT_FALSE((ux | uy) >> 32 != 0)) {
38 // Ensure nonnegativity. Note that negative numbers will appear "large"
39 // to the unsigned comparisons above.
40 CHECK(x >= 0 && y >= 0);
41
42 // Otherwise, detect overflow using a division
43 if (ux != 0 && uxy / ux != uy) return -1;
44 }
45
46 // Cast back to signed. Any negative value will signal an error.
47 return static_cast<int64_t>(uxy);
48 }
49
50 // Computes x + y and returns nullopt if it overflows.
51 //
52 // x and y must be signed integers.
53 template <typename T>
OverflowSafeAdd(T x,T y)54 inline std::optional<T> OverflowSafeAdd(T x, T y) {
55 static_assert(std::is_signed<T>::value,
56 "Only implemented for signed numbers T.");
57 static_assert(std::is_integral<T>::value, "Only implemented for integers T.");
58 // "Signed integer overflow occurs on integer addition iff the operands have
59 // the same sign and the sum has a sign opposite to that of the operands."
60 // Hacker's Delight 2nd ed, p 28.
61 using U = typename std::make_unsigned<T>::type;
62 const U ux = x;
63 const U uy = y;
64 const U usum = ux + uy;
65 const T sum = usum;
66 if (x >= 0 == y >= 0 && sum >= 0 != x >= 0) {
67 return std::nullopt;
68 }
69 return sum;
70 }
71
FitsInIntegralType(int64_t x,PrimitiveType ty)72 inline bool FitsInIntegralType(int64_t x, PrimitiveType ty) {
73 switch (ty) {
74 case S8:
75 return std::numeric_limits<int8_t>::min() <= x &&
76 std::numeric_limits<int8_t>::max() >= x;
77 case S16:
78 return std::numeric_limits<int16_t>::min() <= x &&
79 std::numeric_limits<int16_t>::max() >= x;
80 case S32:
81 return std::numeric_limits<int32_t>::min() <= x &&
82 std::numeric_limits<int32_t>::max() >= x;
83 case S64:
84 return true;
85 case U8:
86 return 0 <= x && std::numeric_limits<uint8_t>::max() >= x;
87 case U16:
88 return 0 <= x && std::numeric_limits<uint16_t>::max() >= x;
89 case U32:
90 return 0 <= x && std::numeric_limits<uint32_t>::max() >= x;
91 case U64:
92 return 0 <= x;
93 default:
94 LOG(FATAL) << "Invalid primitive type " << PrimitiveType_Name(ty);
95 }
96 }
97
98 } // namespace xla
99
100 #endif // TENSORFLOW_COMPILER_XLA_OVERFLOW_UTIL_H_
101