<s>
Loop	B-Operating_System
unrolling	I-Operating_System
,	O
also	O
known	O
as	O
loop	B-Operating_System
unwinding	I-Operating_System
,	O
is	O
a	O
loop	O
transformation	O
technique	O
that	O
attempts	O
to	O
optimize	O
a	O
program	O
's	O
execution	O
speed	O
at	O
the	O
expense	O
of	O
its	O
binary	O
size	O
,	O
which	O
is	O
an	O
approach	O
known	O
as	O
space	O
–	O
time	O
tradeoff	O
.	O
</s>
<s>
The	O
transformation	O
can	O
be	O
undertaken	O
manually	O
by	O
the	O
programmer	O
or	O
by	O
an	O
optimizing	B-Application
compiler	I-Application
.	O
</s>
<s>
On	O
modern	O
processors	O
,	O
loop	B-Operating_System
unrolling	I-Operating_System
is	O
often	O
counterproductive	O
,	O
as	O
the	O
increased	O
code	O
size	O
can	O
cause	O
more	O
cache	O
misses	O
;	O
cf	O
.	O
</s>
<s>
The	O
goal	O
of	O
loop	B-Operating_System
unwinding	I-Operating_System
is	O
to	O
increase	O
a	O
program	O
's	O
speed	O
by	O
reducing	O
or	O
eliminating	O
instructions	O
that	O
control	O
the	O
loop	O
,	O
such	O
as	O
pointer	O
arithmetic	O
and	O
"	O
end	O
of	O
loop	O
"	O
tests	O
on	O
each	O
iteration	O
;	O
reducing	O
branch	O
penalties	O
;	O
as	O
well	O
as	O
hiding	O
latencies	O
,	O
including	O
the	O
delay	O
in	O
reading	O
data	O
from	O
memory	O
.	O
</s>
<s>
Loop	B-Operating_System
unrolling	I-Operating_System
is	O
also	O
part	O
of	O
certain	O
formal	O
verification	O
techniques	O
,	O
in	O
particular	O
bounded	O
model	B-Application
checking	I-Application
.	O
</s>
<s>
If	O
an	O
optimizing	B-Application
compiler	I-Application
or	O
assembler	B-Language
is	O
able	O
to	O
pre-calculate	O
offsets	O
to	O
each	O
individually	O
referenced	O
array	O
variable	O
,	O
these	O
can	O
be	O
built	O
into	O
the	O
machine	B-Language
code	I-Language
instructions	O
directly	O
,	O
therefore	O
requiring	O
no	O
additional	O
arithmetic	O
operations	O
at	O
run	O
time	O
.	O
</s>
<s>
Branch	B-General_Concept
penalty	I-General_Concept
is	O
minimized	O
.	O
</s>
<s>
where	O
statements	O
that	O
occur	O
earlier	O
in	O
the	O
loop	O
do	O
not	O
affect	O
statements	O
that	O
follow	O
them	O
)	O
,	O
the	O
statements	O
can	O
potentially	O
be	O
executed	O
in	O
parallel	B-Operating_System
.	O
</s>
<s>
Optimizing	B-Application
compilers	I-Application
will	O
sometimes	O
perform	O
the	O
unrolling	O
automatically	O
,	O
or	O
upon	O
request	O
.	O
</s>
<s>
Unless	O
performed	O
transparently	O
by	O
an	O
optimizing	B-Application
compiler	I-Application
,	O
the	O
code	O
may	O
become	O
less	O
readable	O
.	O
</s>
<s>
Manual	O
(	O
or	O
static	O
)	O
loop	B-Operating_System
unrolling	I-Operating_System
involves	O
the	O
programmer	O
analyzing	O
the	O
loop	O
and	O
interpreting	O
the	O
iterations	O
into	O
a	O
sequence	O
of	O
instructions	O
which	O
will	O
reduce	O
the	O
loop	O
overhead	O
.	O
</s>
<s>
Normal	O
loopAfter	O
loop	B-Operating_System
unrolling	I-Operating_System
int	O
x	O
;	O
</s>
<s>
This	O
usually	O
requires	O
"	O
base	B-General_Concept
plus	O
offset	O
"	O
addressing	O
,	O
rather	O
than	O
indexed	O
referencing	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
this	O
manual	O
loop	B-Operating_System
unrolling	I-Operating_System
expands	O
the	O
source	O
code	O
size	O
from	O
3	O
lines	O
to	O
7	O
,	O
that	O
have	O
to	O
be	O
produced	O
,	O
checked	O
,	O
and	O
debugged	O
,	O
and	O
the	O
compiler	O
may	O
have	O
to	O
allocate	O
more	O
registers	O
to	O
store	O
variables	O
in	O
the	O
expanded	O
loop	O
iteration	O
.	O
</s>
<s>
Normal	O
loopAfter	O
loop	B-Operating_System
unrolling	I-Operating_System
x(1 )	O
:=	O
1	O
;	O
</s>
<s>
These	O
cases	O
are	O
probably	O
best	O
left	O
to	O
optimizing	B-Application
compilers	I-Application
to	O
unroll	O
.	O
</s>
<s>
Even	O
better	O
,	O
the	O
"	O
tweaked	O
"	O
pseudocode	O
example	O
,	O
that	O
may	O
be	O
performed	O
automatically	O
by	O
some	O
optimizing	B-Application
compilers	I-Application
,	O
eliminating	O
unconditional	O
jumps	O
altogether	O
.	O
</s>
<s>
Since	O
the	O
benefits	O
of	O
loop	B-Operating_System
unrolling	I-Operating_System
are	O
frequently	O
dependent	O
on	O
the	O
size	O
of	O
an	O
arraywhich	O
may	O
often	O
not	O
be	O
known	O
until	O
run	O
timeJIT	O
compilers	O
(	O
for	O
example	O
)	O
can	O
determine	O
whether	O
to	O
invoke	O
a	O
"	O
standard	O
"	O
loop	O
sequence	O
or	O
instead	O
generate	O
a	O
(	O
relatively	O
short	O
)	O
sequence	O
of	O
individual	O
instructions	O
for	O
each	O
element	O
.	O
</s>
<s>
This	O
flexibility	O
is	O
one	O
of	O
the	O
advantages	O
of	O
just-in-time	O
techniques	O
versus	O
static	O
or	O
manual	O
optimization	O
in	O
the	O
context	O
of	O
loop	B-Operating_System
unrolling	I-Operating_System
.	O
</s>
<s>
Assembly	B-Language
language	I-Language
programmers	O
(	O
including	O
optimizing	B-Application
compiler	I-Application
writers	O
)	O
are	O
also	O
able	O
to	O
benefit	O
from	O
the	O
technique	O
of	O
dynamic	O
loop	B-Operating_System
unrolling	I-Operating_System
,	O
using	O
a	O
method	O
similar	O
to	O
that	O
used	O
for	O
efficient	O
branch	O
tables	O
.	O
</s>
<s>
Here	O
,	O
the	O
advantage	O
is	O
greatest	O
where	O
the	O
maximum	O
offset	O
of	O
any	O
referenced	O
field	O
in	O
a	O
particular	O
array	O
is	O
less	O
than	O
the	O
maximum	O
offset	O
that	O
can	O
be	O
specified	O
in	O
a	O
machine	B-Language
instruction	I-Language
(	O
which	O
will	O
be	O
flagged	O
by	O
the	O
assembler	B-Language
if	O
exceeded	O
)	O
.	O
</s>
<s>
This	O
example	O
is	O
for	O
IBM/360	B-Application
or	O
Z/Architecture	B-Device
assemblers	B-Language
and	O
assumes	O
a	O
field	O
of	O
100	O
bytes	O
(	O
at	O
offset	O
zero	O
)	O
is	O
to	O
be	O
copied	O
from	O
array	O
FROM	O
to	O
array	O
TOboth	O
having	O
50	O
entries	O
with	O
element	O
lengths	O
of	O
256	O
bytes	O
each	O
.	O
</s>
<s>
It	O
is	O
,	O
of	O
course	O
,	O
perfectly	O
possible	O
to	O
generate	O
the	O
above	O
code	O
"	O
inline	O
"	O
using	O
a	O
single	O
assembler	B-Language
macro	O
statement	O
,	O
specifying	O
just	O
four	O
or	O
five	O
operands	O
(	O
or	O
alternatively	O
,	O
make	O
it	O
into	O
a	O
library	O
subroutine	O
,	O
accessed	O
by	O
a	O
simple	O
call	O
,	O
passing	O
a	O
list	O
of	O
parameters	O
)	O
,	O
making	O
the	O
optimization	O
readily	O
accessible	O
.	O
</s>
<s>
The	O
following	O
example	O
demonstrates	O
dynamic	O
loop	B-Operating_System
unrolling	I-Operating_System
for	O
a	O
simple	O
program	O
written	O
in	O
C	B-Language
.	O
Unlike	O
the	O
assembler	B-Language
example	O
above	O
,	O
pointer/index	O
arithmetic	O
is	O
still	O
generated	O
by	O
the	O
compiler	O
in	O
this	O
example	O
because	O
a	O
variable	O
(	O
i	O
)	O
is	O
still	O
used	O
to	O
address	O
the	O
array	O
element	O
.	O
</s>
<s>
Here	O
is	O
the	O
code	O
in	O
C	B-Language
:	O
</s>
<s>
The	O
following	O
is	O
MIPS	O
assembly	B-Language
code	I-Language
that	O
will	O
compute	O
the	O
dot	O
product	O
of	O
two	O
100-entry	O
vectors	O
,	O
A	O
and	O
B	O
,	O
before	O
implementing	O
loop	B-Operating_System
unrolling	I-Operating_System
.	O
</s>
<s>
Initialize	O
A[i]	O
pointer	O
( $5	O
)	O
to	O
the	O
base	B-General_Concept
address	I-General_Concept
of	O
A	O
.	O
</s>
<s>
Initialize	O
B[i]	O
pointer	O
( $6	O
)	O
to	O
the	O
base	B-General_Concept
address	I-General_Concept
of	O
B	O
.	O
</s>
<s>
The	O
following	O
is	O
the	O
same	O
as	O
above	O
,	O
but	O
with	O
loop	B-Operating_System
unrolling	I-Operating_System
implemented	O
at	O
a	O
factor	O
of	O
4	O
.	O
</s>
