Skip to content

Commit 4cbc508

Browse files
authored
Merge pull request #165 from thiago-carneiro/main
Fix: correct sliceInRange stop clamp for non-zero origin domains (#164)
2 parents 2becd7a + b5e42fc commit 4cbc508

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

mdio/variable.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -965,13 +965,15 @@ class Variable {
965965

966966
for (size_t idx = 0; idx < labels.size(); ++idx) {
967967
if (labels[idx] == desc.label) {
968-
// Clamp the descriptor to the domain.
969-
return {desc.label, // label
970-
desc.start < domain.origin()[idx] ? domain.origin()[idx]
971-
: desc.start, // start
972-
desc.stop > domain.shape()[idx] ? domain.shape()[idx]
973-
: desc.stop, // stop
974-
desc.step}; // step
968+
// Clamp the descriptor to the domain using absolute coordinates.
969+
// NOTE: domain.shape()[idx] is a size, not an absolute upper bound when
970+
// the origin is non-zero. The correct exclusive upper bound is
971+
// origin + shape.
972+
Index dim_origin = domain.origin()[idx];
973+
Index dim_upper = dim_origin + domain.shape()[idx]; // exclusive
974+
Index clamped_start = desc.start < dim_origin ? dim_origin : desc.start;
975+
Index clamped_stop = desc.stop > dim_upper ? dim_upper : desc.stop;
976+
return {desc.label, clamped_start, clamped_stop, desc.step};
975977
}
976978
}
977979
// We don't slice along a dimension that doesn't exist, so the descriptor is

0 commit comments

Comments
 (0)