forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexingUtils.cpp
More file actions
33 lines (26 loc) · 797 Bytes
/
IndexingUtils.cpp
File metadata and controls
33 lines (26 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <ATen/native/IndexingUtils.h>
namespace at { namespace native {
bool canUse32BitIndexMath(const TensorBase& t, int64_t max_elem) {
int64_t elements = t.numel();
if (elements >= max_elem) {
return false;
}
if (elements == 0) {
return max_elem > 0;
}
int64_t offset = 0;
int64_t linearId = elements - 1;
// NOTE: Assumes all strides are positive, which is true for now
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
for (int i = t.dim() - 1; i >= 0; --i) {
int64_t curDimIndex = linearId % t.size(i);
int64_t curDimOffset = curDimIndex * t.stride(i);
offset += curDimOffset;
linearId /= t.size(i);
}
if (offset >= max_elem) {
return false;
}
return true;
}
}} // namespace at::native