Skip to content

Commit 3bbcb48

Browse files
CaoZhongZvpirogov
authored andcommitted
common: nd_iterator: avoid idiv in a loop
1 parent 225984e commit 3bbcb48

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/common/utils.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,10 @@ inline bool nd_iterator_step() {
334334
template <typename U, typename W, typename... Args>
335335
inline bool nd_iterator_step(U &x, const W &X, Args &&... tuple) {
336336
if (nd_iterator_step(utils::forward<Args>(tuple)...)) {
337-
x = (x + 1) % X;
338-
return x == 0;
337+
if (++x - X == 0) {
338+
x = 0;
339+
return true;
340+
}
339341
}
340342
return false;
341343
}
@@ -358,8 +360,10 @@ template <typename U, typename W, typename Y, typename... Args>
358360
inline bool nd_iterator_jump(
359361
U &cur, const U end, W &x, const Y &X, Args &&... tuple) {
360362
if (nd_iterator_jump(cur, end, utils::forward<Args>(tuple)...)) {
361-
x = (x + 1) % X;
362-
return x == 0;
363+
if (++x - X == 0) {
364+
x = 0;
365+
return true;
366+
}
363367
}
364368
return false;
365369
}

0 commit comments

Comments
 (0)