| 45 | namespace internal { |
| 46 | |
| 47 | Status ComputeRowMajorStrides(const FixedWidthType& type, |
| 48 | const std::vector<int64_t>& shape, |
| 49 | std::vector<int64_t>* strides) { |
| 50 | const int byte_width = type.byte_width(); |
| 51 | const size_t ndim = shape.size(); |
| 52 | |
| 53 | int64_t remaining = 0; |
| 54 | if (!shape.empty() && shape.front() > 0) { |
| 55 | remaining = byte_width; |
| 56 | for (size_t i = 1; i < ndim; ++i) { |
| 57 | if (internal::MultiplyWithOverflow(remaining, shape[i], &remaining)) { |
| 58 | return Status::Invalid( |
| 59 | "Row-major strides computed from shape would not fit in 64-bit integer"); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (remaining == 0) { |
| 65 | strides->assign(shape.size(), byte_width); |
| 66 | return Status::OK(); |
| 67 | } |
| 68 | |
| 69 | strides->push_back(remaining); |
| 70 | for (size_t i = 1; i < ndim; ++i) { |
| 71 | remaining /= shape[i]; |
| 72 | strides->push_back(remaining); |
| 73 | } |
| 74 | |
| 75 | return Status::OK(); |
| 76 | } |
| 77 | |
| 78 | Status ComputeColumnMajorStrides(const FixedWidthType& type, |
| 79 | const std::vector<int64_t>& shape, |