Home | History | Annotate | Download | only in kernels
      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_UTIL_BOUNDS_CHECK_H_
     17 #define TENSORFLOW_UTIL_BOUNDS_CHECK_H_
     18 
     19 #include <type_traits>
     20 
     21 #include "third_party/eigen3/Eigen/Core"
     22 #include "tensorflow/core/platform/macros.h"
     23 
     24 namespace tensorflow {
     25 
     26 // Check that 0 <= index < limit using a single comparison, assuming
     27 // that 0 <= limit if Index is signed.  Intended for use in performance
     28 // critical contexts where 0 <= index < limit is almost always true.
     29 template <typename Ta, typename Tb>
     30 EIGEN_ALWAYS_INLINE EIGEN_DEVICE_FUNC bool FastBoundsCheck(const Ta index,
     31                                                            const Tb limit) {
     32   static_assert(std::is_integral<Ta>::value && std::is_integral<Tb>::value,
     33                 "FastBoundsCheck can only be used on integer types.");
     34   typedef typename std::make_unsigned<decltype(index + limit)>::type UIndex;
     35   return TF_PREDICT_TRUE(static_cast<UIndex>(index) <
     36                          static_cast<UIndex>(limit));
     37 }
     38 
     39 namespace internal {
     40 // Ensure that the compiler cannot elide a copy into a local, for
     41 // bounds checking on source tensors that might be updated asynchronously.
     42 // This function may only be used on primitive integral types (int32, int64,
     43 // etc).  It does not guarantee any atomicity or barriers.
     44 template <typename T>
     45 EIGEN_ALWAYS_INLINE EIGEN_DEVICE_FUNC const T SubtleMustCopy(const T &x) {
     46   static_assert(std::is_integral<T>::value,
     47                 "SubtleMustCopy can only be used on integer types.");
     48   auto *to_x = reinterpret_cast<const volatile T *>(&x);
     49   return *to_x;
     50 }
     51 }  // namespace internal
     52 }  // namespace tensorflow
     53 
     54 #endif  // TENSORFLOW_UTIL_BOUNDS_CHECK_H_
     55