0

If a loop is unrolled by N, duplicating the body N times and reducing the trip-count by a factor of N, you may need 0 to N-1 "final iterations" after the loop - the non-zero cases occurring if your original trip-count wasn't a multiple of N.

What are these final iterations called?


As an example, the following loop:

int sum = 0;
for (int i = 0; i < max; i++) {
  sum += i;
}

Can be unrolled by a factor of 4 as follows:

int sum = 0;
for (int i = 0; i + 3 < max; i += 4) {
  sum += i;
  sum += i + 1;
  sum += i + 2;
  sum += i + 3;
}

// "final iterations" handling here

... and of the author will probably simplify the entire body to something like sum += i *4 + 61. You also need some "final iterations" handling which could be about as simple as the original loop:

// handle the final 0 to 3 iterations
for (; i < max; i++) {
  sum += i;
}

1 Indeed the compiler will probably do an even better job of simplifying even the original loop, often removing it completely and calculating the final result directly via multiplication based on a sum formula. This is only an example, after all.

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
  • *Opinion*: They're still called "iterations". Instead of doing something 100 times, you're doing 10 things 10 times. So you're still iterating. – Lasse V. Karlsen Dec 02 '17 at 21:54
  • @LasseVågsætherKarlsen - yes I'm not looking to avoid the word iterations, but there is some succinct way to describe those iterations, like _final_ iterations (but that's not it), or perhaps "tail" iterations, or whatever, but the exact term is escaping me and I'm seeking guidance from the community. – BeeOnRope Dec 02 '17 at 21:56
  • 2
    Epilog or epilogue, but I usually just call it the tail. – harold Dec 03 '17 at 01:47

3 Answers3

1

Just a few suggestions:

Christian Fries
  • 16,175
  • 10
  • 56
  • 67
0

How about "remainder iterations"? It fits the definition of "remainder" well, although I don't know if it's an accepted term.

skiggety
  • 205
  • 2
  • 7
-2

I'd call it "the part after the loop that handles any remaining iterations".

When a loop is unrolled, what are the final iterations called?

The problem is that the question assumes that it has a "standard"/well known name; but that would be silly (if you give every small but common fragment of code a name you'd have so many names that nobody will ever remember them, which would defeat any purpose that assigning names may have had).

Of course (for documentation) this doesn't exclude the use of temporary names or temporary phrases to improve brevity; either defined by glossary, defined inline (see note), or defined by context.

Note: I typically define stuff inline. For example, if I was repeatedly mentioning "the end part" (the part after the loop that handles any remaining iterations), then I might explain it once and then just say "the end part" for each later occurrence and rely on the reader to remember what I mean by "the end part".

Brendan
  • 35,656
  • 2
  • 39
  • 66