1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_CORE_FRAMEWORK_BOUNDS_CHECK_H_
17#define TENSORFLOW_CORE_FRAMEWORK_BOUNDS_CHECK_H_
18
19#include <type_traits>
20
21#include "third_party/eigen3/Eigen/Core"
22#include "tensorflow/core/platform/macros.h"
23
24namespace 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.
29template <typename Ta, typename Tb>
30EIGEN_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
39namespace 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.
44template <typename T>
45EIGEN_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_CORE_FRAMEWORK_BOUNDS_CHECK_H_
55