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_STREAM_EXECUTOR_LIB_MATHUTIL_H_
17 #define TENSORFLOW_STREAM_EXECUTOR_LIB_MATHUTIL_H_
18
19 #include <algorithm>
20 #include <cmath>
21 #include <limits>
22 #include <type_traits>
23 #include <vector>
24
25 #include "tensorflow/stream_executor/platform/logging.h"
26 #include "tensorflow/stream_executor/platform/port.h"
27
28 namespace stream_executor {
29 namespace port {
30
31 class MathUtil {
32 public:
33 template <typename IntegralType>
CeilOfRatio(IntegralType numerator,IntegralType denominator)34 static IntegralType CeilOfRatio(IntegralType numerator,
35 IntegralType denominator) {
36 return CeilOrFloorOfRatio<IntegralType, true>(numerator, denominator);
37 }
38 template <typename IntegralType>
FloorOfRatio(IntegralType numerator,IntegralType denominator)39 static IntegralType FloorOfRatio(IntegralType numerator,
40 IntegralType denominator) {
41 return CeilOrFloorOfRatio<IntegralType, false>(numerator, denominator);
42 }
43 template <typename IntegralType, bool ceil>
44 static IntegralType CeilOrFloorOfRatio(IntegralType numerator,
45 IntegralType denominator);
46 };
47
48 // ---- CeilOrFloorOfRatio ----
49 // This is a branching-free, cast-to-double-free implementation.
50 //
51 // Casting to double is in general incorrect because of loss of precision
52 // when casting an int64 into a double.
53 //
54 // There's a bunch of 'recipes' to compute a integer ceil (or floor) on the web,
55 // and most of them are incorrect.
56 template<typename IntegralType, bool ceil>
CeilOrFloorOfRatio(IntegralType numerator,IntegralType denominator)57 IntegralType MathUtil::CeilOrFloorOfRatio(IntegralType numerator,
58 IntegralType denominator) {
59 static_assert(std::is_integral<IntegralType>::value,
60 "CeilOfRatio_is_only_defined_for_integral_types");
61 assert(denominator != 0);
62 // Dividing the smallest signed integer by -1 is not supported: it would
63 // SIGFPE
64 assert(!std::is_signed<IntegralType>::value ||
65 numerator != std::numeric_limits<IntegralType>::min() ||
66 denominator != -1);
67
68 const IntegralType rounded_toward_zero = numerator / denominator;
69 const IntegralType intermediate_product = rounded_toward_zero * denominator;
70
71 if (ceil) { // Compile-time condition: not an actual branching
72 // When rounded_toward_zero is negative, then an adjustment is never needed:
73 // the real ratio is negative, and so rounded toward zero is the ceil.
74 // When rounded_toward_zero is non-negative, an adjustment is needed if the
75 // sign of the difference numerator - intermediate_product is the same as
76 // the sign of the denominator.
77 //
78 // Using a bool and then a static_cast to IntegralType is not strictly
79 // necessary, but it makes the code clear, and anyway the compiler should
80 // get rid of it.
81 const bool needs_adjustment = (rounded_toward_zero >= 0) &&
82 ((denominator > 0 && numerator > intermediate_product) ||
83 (denominator < 0 && numerator < intermediate_product));
84 const IntegralType adjustment = static_cast<IntegralType>(needs_adjustment);
85 const IntegralType ceil_of_ratio = rounded_toward_zero + adjustment;
86 return ceil_of_ratio;
87 } else {
88 // Floor case: symmetrical to the previous one
89 const bool needs_adjustment = (rounded_toward_zero <= 0) &&
90 ((denominator > 0 && numerator < intermediate_product) ||
91 (denominator < 0 && numerator > intermediate_product));
92 const IntegralType adjustment = static_cast<IntegralType>(needs_adjustment);
93 const IntegralType floor_of_ratio = rounded_toward_zero - adjustment;
94 return floor_of_ratio;
95 }
96 }
97
98 } // namespace port
99 } // namespace stream_executor
100
101 #endif // TENSORFLOW_STREAM_EXECUTOR_LIB_MATHUTIL_H_
102