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