• 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 #include "tensorflow/core/util/overflow.h"
17 
18 #include <cmath>
19 
20 #ifdef PLATFORM_WINDOWS
21 #include <Windows.h>
22 #endif
23 
24 #include "tensorflow/core/platform/macros.h"
25 #include "tensorflow/core/platform/test.h"
26 
27 namespace tensorflow {
28 namespace {
29 
HasOverflow(int64_t x,int64_t y)30 bool HasOverflow(int64_t x, int64_t y) {
31 #ifdef PLATFORM_WINDOWS
32   // `long double` on MSVC is 64 bits not 80 bits - use a windows specific API
33   // for this test.
34   return ::MultiplyHigh(x, y) != 0;
35 #else
36   long double dxy = static_cast<long double>(x) * static_cast<long double>(y);
37   return dxy > std::numeric_limits<int64_t>::max();
38 #endif
39 }
40 
TEST(OverflowTest,Nonnegative)41 TEST(OverflowTest, Nonnegative) {
42   // Various interesting values
43   std::vector<int64_t> interesting = {
44       0,
45       std::numeric_limits<int64_t>::max(),
46   };
47 
48   for (int i = 0; i < 63; i++) {
49     int64_t bit = static_cast<int64_t>(1) << i;
50     interesting.push_back(bit);
51     interesting.push_back(bit + 1);
52     interesting.push_back(bit - 1);
53   }
54 
55   for (const int64_t mid : {static_cast<int64_t>(1) << 32,
56                             static_cast<int64_t>(std::pow(2, 63.0 / 2))}) {
57     for (int i = -5; i < 5; i++) {
58       interesting.push_back(mid + i);
59     }
60   }
61 
62   // Check all pairs
63   for (int64_t x : interesting) {
64     for (int64_t y : interesting) {
65       int64_t xy = MultiplyWithoutOverflow(x, y);
66       if (HasOverflow(x, y)) {
67         EXPECT_LT(xy, 0) << x << " " << y;
68       } else {
69         EXPECT_EQ(x * y, xy) << x << " " << y;
70       }
71     }
72   }
73 }
74 
TEST(OverflowTest,Negative)75 TEST(OverflowTest, Negative) {
76   const int64_t negatives[] = {-1, std::numeric_limits<int64_t>::min()};
77   for (const int64_t n : negatives) {
78     EXPECT_LT(MultiplyWithoutOverflow(n, 0), 0) << n;
79     EXPECT_LT(MultiplyWithoutOverflow(0, n), 0) << n;
80     EXPECT_LT(MultiplyWithoutOverflow(n, n), 0) << n;
81   }
82 }
83 
84 }  // namespace
85 }  // namespace tensorflow
86