Meta tags:
Headings (most frequently used words):
unrolling, loop, example, mips, manual, in, to, assembly, language, contents, advantages, disadvantages, static, dynamic, see, also, references, further, reading, external, links, simple, early, complexity, while, loops, assembler, ibm, 360, or, architecture, converting, the,
Text of the page (most frequently used words):
the (200), loop (95), #unrolling (46), 100 (45), 256 (42), code (33), and (32), array (31), mvc (30), this (28), example (25), for (25), that (25), with (24), from (23), bytes (23), move (22), f10 (22), process (21), edit (20), entry (20), can (20), number (18), instructions (18), entries (16), pointer (15), instruction (15), printf (15), size (14), r15 (14), which (14), analysis (13), each (12), statements (11), compiler (11), f12 (11), will (11), case (11), not (11), program (11), are (11), all (10), optimizing (10), branch (10), address (10), offset (10), optimization (9), variable (9), assembly (9), element (9), int (9), only (9), manual (9), print (9), remove (9), wikipedia (8), may (8), static (8), time (8), language (8), iteration (8), then (8), while (8), more (8), condition (8), elimination (7), constant (7), repeat (7), also (7), one (7), mips (7), could (7), break (7), set (7), dynamic (7), simple (7), sequence (7), maxm1 (7), maximum (7), action (7), add (6), value (6), performance (6), loops (6), cache (6), addi (6), following (6), double (6), two (6), left (6), label (6), assembler (6), arithmetic (6), after (6), variables (6), overhead (6), about (5), use (5), page (5), articles (5), parallel (5), index (5), control (5), jump (5), function (5), other (5), register (5), links (5), compilers (5), unwinding (5), duff (5), device (5), just (5), mul (5), displacement (5), above (5), increment (5), initialize (5), here (5), remaining (5), bunchsize (5), required (5), used (5), where (5), iterations (5), increase (5), have (5), start (5), first (5), these (5), any (5), often (5), might (5), do_odd_stuff (5), do_even_stuff (5), toggle (4), contents (4), search (4), additional (4), using (4), list (4), optimizations (4), data (4), flow (4), technique (4), reading (4), university (4), journal (4), cite (4), computer (4), has (4), server (4), speed (4), memory (4), see (4), loop3 (4), same (4), but (4), note (4), arrays (4), count (4), base (4), drop (4), through (4), main (4), still (4), possible (4), into (4), similar (4), length (4), would (4), executed (4), goto (4), unrolled (4), normal (4), next (4), prediction (4), hide (4), sidebar (4), table (3), view (3), statement (3), text (3), was (3), march (3), 2026 (3), cs1 (3), multiple (3), short (3), computing (3), retrieved (3), execution (3), checking (3), store (3), reduction (3), 2012 (3), programming (3), external (3), modern (3), further (3), model (3), requires (3), cpu (3), eliminating (3), because (3), references (3), continue (3), test (3), dot (3), product (3), below (3), switch (3), calculate (3), times (3), per (3), course (3), make (3), techniques (3), field (3), approximately (3), require (3), original (3), r14 (3), registers (3), end (3), init (3), than (3), al2 (3), ilen (3), pre (3), architecture (3), ibm (3), 360 (3), known (3), instead (3), advantages (3), small (3), values (3), part (3), endwhile (3), consider (3), large (3), therefore (3), such (3), programmer (3), result (3), significant (3), increased (3), article (3), transformation (3), tools (3), languages (2), contact (2), privacy (2), policy (2), available (2), terms (2), you (2), last (2), categories (2), generated (2), maint (2), names (2), authors (2), description (2), matches (2), wikidata (2), dependence (2), inline (2), expansion (2), expression (2), dead (2), compile (2), global (2), call (2), conditional (2), numbering (2), based (2), define (2), induction (2), software (2), pipelining (2), unswitching (2), splitting (2), fusion (2), automatic (2), agner (2), fog (2), subroutines (2), introduction (2), x86 (2), book (2), pages (2), 2001 (2), isbn (2), approach (2), optimized (2), pdf (2), help (2), parallelism (2), link (2), august (2), over (2), much (2), faster (2), lines (2), some (2), portal (2), bgtz (2), implemented (2), again (2), compute (2), vectors (2), converting (2), dotproduct (2), source (2), processed (2), unroll (2), get (2), elements (2), divisible (2), include (2), written (2), generate (2), single (2), library (2), parameters (2), clear (2), rest (2), immediately (2), copied (2), whereas (2), saving (2), execute (2), unwound (2), even (2), there (2), return (2), equ (2), unconditional (2), zero (2), them (2), hexadecimal (2), plus (2), addressing (2), being (2), order (2), previous (2), reduce (2), beyond (2), batch (2), except (2), dynamically (2), 50cl256 (2), programmers (2), including (2), able (2), benefit (2), referenced (2), particular (2), less (2), specified (2), machine (2), run (2), standard (2), relatively (2), requiring (2), tweaked (2), pseudocode (2), performed (2), automatically (2), jumps (2), general (2), content (2), replicating (2), many (2), etc (2), produce (2), reference (2), new (2), later (2), its (2), replaced (2), however (2), need (2), procedure (2), involves (2), merely (2), done (2), early (2), complexity (2), expanded (2), operations (2), become (2), branches (2), potentially (2), should (2), accomplished (2), calls (2), correct (2), executing (2), without (2), penalty (2), pressure (2), systems (2), when (2), inlining (2), due (2), leading (2), manually (2), space (2), misses (2), occur (2), cause (2), storage (2), learn (2), information (2), disadvantages (2), independent (2), well (2), tests (2), reducing (2), appearance (2), upload (2), file (2), changes (2), history (2), read (2), subsection (2), log (2), create (2), account (2), donate (2), menu (2), topic, mobile, cookie, statistics, developers, conduct, legal, safety, contacts, disclaimers, under, apply, site, agree, registered, trademark, non, profit, organization, wikimedia, foundation, inc, creative, commons, attribution, sharealike, license, rendered, parsoid, edited, utc, hidden, disputed, december, 2009, accuracy, disputes, containing, suspected, texts, errors, missing, periodical, https, org, php, title, loop_unrolling, oldid, 1345639607, range, shape, escape, access, alias, profile, guided, partial, evaluation, threading, templates, bounds, interprocedural, tail, deforestation, functional, rematerialization, allocation, selection, scheduling, generation, sparse, propagation, ssa, reaching, definitions, chain, upwards, exposed, uses, live, recognition, folding, common, subexpression, strength, nest, interchange, inversion, invariant, motion, vectorization, parallelization, local, peephole, basic, block, handbook, gives, concise, generalized, graphics, black, michael, abrash, chapter, kennedy, ken, allen, randy, morgan, kaufmann, 55860, 286, architectures, minnesota, adam, horvath, far, away, sarkar, vivek, nested, 581, 3353104, s2cid, 1023, 1012246031671, doi, 545, international, copenhagen, college, engineering, smt, theory, lists, nicolau, alexandru, 1985, quantization, fine, grain, exploitation, dept, science, technical, report, ithaca, cornell, 14638257, oclc, petersen, arbenz, 2004, oxford, press, ullman, jeffrey, aho, alfred, 1977, mass, addison, wesley, pub, 201, 10073, 471, principles, design, tso, ted, 2000, jim, gettys, wonderful, explanation, effect, turns, out, predictions, relative, changing, past, decade, pretty, pointless, fact, instances, shrunk, _half_, _a_, _megabyte_, boot, excess, meant, wasn, thrashing, xfree86, 2014, linux, kernel, mailing, lkml, indiana, edu, patch, input, drivers, word, needed, compilation, level, don, yourself, factor, thus, displacements, decrement, before, implementing, omits, initializations, type, duplication, avoided, writing, parts, together, none, rely, complete, jumping, update, amount, bunches, remainder, most, processing, total, counter, void, constexpr, reflecting, stdio, demonstrates, unlike, full, absolute, indexes, replacement, perfectly, specifying, four, five, operands, alternatively, subroutine, accessed, passing, making, readily, accessible, macro, involved, long, combined, adjusted, accordingly, nulls, byte, added, every, 156, 202, conventional, had, consisted, 108, thousands, defined, starting, starts, point, always, contain, subtract, bnp, positive, meaning, entire, bypassed, multiply, calculated, specific, allowable, f00, character, decreases, avoids, permissible, within, fff, 255, decreasing, moved, bnpr, reload, destroyed, calculation, constants, passed, addresses, pointers, loaded, beginning, actual, elsewhere, acquired, 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, assemblers, assumes, both, having, lengths, section, writers, method, efficient, advantage, greatest, flagged, exceeded, tables, since, benefits, frequently, dependent, until, determine, whether, invoke, individual, flexibility, versus, context, situation, savings, useful, quite, overall, included, once, jit, better, altogether, involving, intricate, indexing, cases, probably, best, innermost, allow, optimisations, yet, yield, gain, unless, compiled, lot, notorious, makes, latter, develop, given, developed, usages, change, mean, staying, derived, carries, forward, becomes, whose, changed, invocation, computation, mod, else, administrative, arranges, productive, itself, contributes, nothing, results, desired, tedium, hundred, been, processor, generating, replications, editor, similarly, replication, programs, easily, track, combinations, find, repetition, boring, mistakes, bloat, hand, expands, produced, checked, debugged, allocate, addition, inside, structure, chosen, carefully, indeed, assuming, already, working, implications, were, amendments, somewhat, complicated, conditions, discuss, dubious, modification, afterwards, taken, represents, decrease, administration, optimal, usually, rather, indexed, referencing, delete, items, collection, normally, means, resources, compared, those, item_number, analyzing, interpreting, contrast, cpus, try, guess, way, waiting, resolve, incorrect, flush, pipeline, lead, mispredictions, lower, hardware, relies, renaming, temporary, across, limiting, reuse, superscalar, conflict, body, contains, prevent, excessive, trade, off, between, reduced, harder, understand, maintain, readability, increases, larger, binaries, consumes, exceeds, frequent, severe, degradation, costly, accesses, higher, requirements, takes, problematic, embedded, limited, microcontrollers, how, message, claims, cited, sources, material, removed, fictitious, research, verified, copyright, violations, hallucinated, incorporate, prohibited, sometimes, perform, upon, request, unknown, earlier, affect, follow, minimized, gains, realized, compensates, caused, tight, consists, offsets, built, directly, individually, certain, bounded, formal, verification, goal, penalties, hiding, latencies, delay, eliminate, repeated, computational, attempts, optimize, expense, undertaken, processors, counterproductive, tradeoff, binary, free, encyclopedia, item, projects, printable, version, download, export, legacy, parser, shortened, url, permanent, related, what, actions, english, talk, українська, türkçe, српски, srpski, русский, polski, 한국어, 日本語, italiano, français, فارسی, español, deutsch, top, personal, special, recent, community, contribute, random, current, events, navigation,
Text of the page (random words):
mple makes reference only to x i and x i 1 in the loop the latter only to develop the new value x i therefore given that there is no later reference to the array x developed here its usages could be replaced by a simple variable such a change would however mean a simple variable whose value is changed whereas if staying with the array the compiler s analysis might note that the array s values are constant each derived from a previous constant and therefore carries forward the constant values so that the code becomes print 2 2 print 3 6 print 4 24 etc in general the content of a loop might be large involving intricate array indexing these cases are probably best left to optimizing compilers to unroll replicating innermost loops might allow many possible optimisations yet yield only a small gain unless n is large unrolling while loops edit consider a pseudocode while loop similar to the following normal loop after loop unrolling unrolled tweaked loop while condition do action endwhile while condition do action if not condition then goto break action if not condition then goto break action endwhile label break if condition then repeat action if not condition then goto break action if not condition then goto break action while condition label break in this case unrolling is faster because the endwhile a jump to the start of the loop will be executed 66 less often even better the tweaked pseudocode example that may be performed automatically by some optimizing compilers eliminating unconditional jumps altogether dynamic unrolling edit since the benefits of loop unrolling are frequently dependent on the size of an array which may often not be known until run time jit compilers for example can determine whether to invoke a standard loop sequence or instead generate a relatively short sequence of individual instructions for each element this flexibility is one of the advantages of just in time techniques versus static or manual optimization in the context of loop unrolling in this situation it is often with relatively small values of n where the savings are still useful requiring quite small if any overall increase in program size that might be included just once as part of a standard library assembly language programmers including optimizing compiler writers are also able to benefit from the technique of dynamic loop unrolling using a method similar to that used for efficient branch tables here the advantage is greatest where the maximum offset of any referenced field in a particular array is less than the maximum offset that can be specified in a machine instruction which will be flagged by the assembler if exceeded assembler example ibm 360 or z architecture edit for an x86 example see the external links section this example is for ibm 360 or z architecture assemblers and assumes a field of 100 bytes at offset zero is to be copied from array from to array to both having 50 entries with element lengths of 256 bytes each the return address is in r14 initialize registers r15 r0 r1 and r2 from data defined at the end of the program starting with label init maxm1 lm r15 r2 init set r15 maximum number of mvc instructions maxm1 16 r0 number of entries of array r1 address of from array and r2 address of to array the loop starts here loop equ define loop label at this point r15 will always contain the number 16 maxm1 sr r15 r0 subtract the remaining number of entries in the array r0 from r15 bnp all if r15 is not positive meaning we have more than 16 remaining entries in the array jump to do the entire mvc sequence and then repeat calculate an offset from start of mvc sequence for unconditional branch to the unwound mvc loop below if the number of remaining entries in the arrays is zero r15 will be 16 so all the mvc instructions will be bypassed mh r15 al2 ilen multiply r15 by the length of one mvc instruction b all r15 jump to all r15 the address of the calculated specific mvc instruction with drop through to the rest of them mvc instruction table first entry has maximum allowable offset with single register hexadecimal f00 15 256 in this example all 16 of the following mvc move character instructions use base plus offset addressing and each to from offset decreases by the length of one array element 256 this avoids pointer arithmetic being required for each element up to a maximum permissible offset within the instruction of hexadecimal fff 15 256 255 the instructions are in order of decreasing offset so the last element in the set is moved first all mvc 15 256 100 r2 15 256 r1 move 100 bytes of 16th entry from array 1 to array 2 with drop through ilen equ all set ilen to the length of the previous mvc instruction mvc 14 256 100 r2 14 256 r1 move 100 bytes of 15th entry mvc 13 256 100 r2 13 256 r1 move 100 bytes of 14th entry mvc 12 256 100 r2 12 256 r1 move 100 bytes of 13th entry mvc 11 256 100 r2 11 256 r1 move 100 bytes of 12th entry mvc 10 256 100 r2 10 256 r1 move 100 bytes of 11th entry mvc 09 256 100 r2 09 256 r1 move 100 bytes of 10th entry mvc 08 256 100 r2 08 256 r1 move 100 bytes of 9th entry mvc 07 256 100 r2 07 256 r1 move 100 bytes of 8th entry mvc 06 256 100 r2 06 256 r1 move 100 bytes of 7th entry mvc 05 256 100 r2 05 256 r1 move 100 bytes of 6th entry mvc 04 256 100 r2 04 256 r1 move 100 bytes of 5th entry mvc 03 256 100 r2 03 256 r1 move 100 bytes of 4th entry mvc 02 256 100 r2 02 256 r1 move 100 bytes of 3rd entry mvc 01 256 100 r2 01 256 r1 move 100 bytes of 2nd entry mvc 00 256 100 r2 00 256 r1 move 100 bytes of 1st entry s r0 maxm1 reduce the number of remaining entries to process bnpr r14 if no more entries to process return to address in r14 ah r1 al2 16 256 increment from array pointer beyond first set ah r2 al2 16 256 increment to array pointer beyond first set l r15 maxm1 reload the maximum number of mvc instructions per batch into r15 destroyed by the calculation in the first instruction of the loop b loop execute loop again static constants and variables these could be passed as parameters except maxm1 init ds 0a 4 addresses pointers to be pre loaded with the lm instruction in the beginning of the program maxm1 dc a 16 maximum number of mvc instructions executed per batch n dc a 50 number of actual entries in array a variable set elsewhere dc a from address of start of array 1 pointer dc a to address of start of array 2 pointer static arrays these could be dynamically acquired from ds 50cl256 array of 50 entries of 256 bytes each to ds 50cl256 array of 50 entries of 256 bytes each in this example approximately 202 instructions would be required with a conventional loop 50 iterations whereas the above dynamic code would require only about 89 instructions or a saving of approximately 56 if the array had consisted of only two entries it would still execute in approximately the same time as the original unwound loop the increase in code size is only about 108 bytes even if there are thousands of entries in the array similar techniques can of course be used where multiple instructions are involved as long as the combined instruction length is adjusted accordingly for example in this same example if it is required to clear the rest of each array entry to nulls immediately after the 100 byte field copied an additional clear instruction xc xx 256 100 156 r1 xx 256 100 r2 can be added immediately after every mvc in the sequence where xx matches the value in the mvc above it it is of course perfectly possible to generate the above code inline using a single assembler macro statement specifying just four or five operands or alternatively make it into a library subroutine accessed by a simple call passing a list of parameters making the optimization readily accessible c example edit the following example demonstrates dynamic loop unrolling for a simple program written in c unlike the assembler example above pointer index arithmetic is still generated by the compiler in this example because a variable i is still used to address the array element full optimization is only possible if absolute indexes are used in the replacement statements include stdio h the number of entries processed per loop iteration note that this number is a constant constant reflecting the code below constexpr int bunchsize 8 int main void int i 0 counter int entries 50 total number to process if the number of elements is not divisible by bunchsize get repeat times required to do most processing in the while loop int repeat entries bunchsize number of times to repeat int left entries bunchsize calculate remainder unroll the loop in bunches of 8 while repeat printf process d n i printf process d n i 1 printf process d n i 2 printf process d n i 3 printf process d n i 4 printf process d n i 5 printf process d n i 6 printf process d n i 7 update the index by amount processed in one go i bunchsize use a switch statement to process remaining by jumping to the case label at the label that will then drop through to complete the set switch left case 7 printf process d n i 6 process and rely on drop through case 6 printf process d n i 5 case 5 printf process d n i 4 case 4 printf process d n i 3 case 3 printf process d n i 2 case 2 printf process d n i 1 two left case 1 printf process d n i just one left to process case 0 break none left code duplication could be avoided by writing the two parts together as in duff s device c to mips assembly language loop unrolling example edit source 9 the following example will compute a dot product of two 100 entry vectors a and b of type double here is the code in c double dotproduct 0 for int i 0 i 100 i dotproduct a i b i converting to mips assembly language edit the following is mips assembly code that will compute the dot product of two 100 entry vectors a and b before implementing loop unrolling the code below omits the loop initializations initialize loop count 7 to 100 initialize dot product f8 to 0 initialize a i pointer 5 to the base address of a initialize b i pointer 6 to the base address of b note that the size of one element of the arrays a double is 8 bytes loop3 l d f10 0 5 f10 a i l d f12 0 6 f12 b i mul d f10 f10 f12 f10 a i b i add d f8 f8 f10 f8 f8 a i b i addi 5 5 8 increment pointer for a i by the size of a double addi 6 6 8 increment pointer for b i by the size of a double addi 7 7 1 decrement loop count test bgtz 7 loop3 continue if loop count 0 unrolling the loop in mips edit the following is the same as above but with loop unrolling implemented at a factor of 4 note again that the size of one element of the arrays a double is 8 bytes thus the 0 8 16 24 displacements and the 32 displacement on each loop loop3 l d f10 0 5 iteration with displacement 0 l d f12 0 6 mul d f10 f10 f12 add d f8 f8 f10 l d f10 8 5 iteration with displacement 8 l d f12 8 6 mul d f10 f10 f12 add d f8 f8 f10 l d f10 16 5 iteration with displacement 16 l d f12 16 6 mul d f10 f10 f12 add d f8 f8 f10 l d f10 24 5 iteration with displacement 24 l d f12 24 6 mul d f10 f10 f12 add d f8 f8 f10 addi 5 5 32 addi 6 6 32 addi 7 7 4 test bgtz 7 loop3 continue loop if 7 0 see also edit computer programming portal don t repeat yourself instruction level parallelism just in time compilation loop fusion loop splitting loop unswitching parallel computing references edit tso ted august 22 2000 re patch re move of input drivers some word needed from you lkml indiana edu linux kernel mailing list retrieved august 22 2014 jim gettys has a wonderful explanation of this effect in the x server it turns out that with branch predictions and the relative speed of cpu vs memory changing over the past decade loop unrolling is pretty much pointless in fact by eliminating all instances of duff s device from the xfree86 4 0 server the server shrunk in size by _half_ _a_ _megabyte_ and was faster to boot because the elimination of all that excess code meant that the x server wasn t thrashing the cache lines as much ullman jeffrey d aho alfred v 1977 principles of compiler design reading mass addison wesley pub co pp 471 2 isbn 0 201 10073 8 petersen w p arbenz p 2004 introduction to parallel computing oxford university press p 10 cite book cs1 maint multiple names authors list link nicolau alexandru 1985 loop quantization unwinding for fine grain parallelism exploitation dept of computer science technical report ithaca ny cornell university oclc 14638257 cite journal cite journal requires journal help model checking using smt and theory of lists fog agner 2012 02 29 optimizing subroutines in assembly language pdf copenhagen university college of engineering p 100 retrieved 2012 09 22 12 11 loop unrolling sarkar vivek 2001 optimized unrolling of nested loops international journal of parallel programming 29 5 545 581 doi 10 1023 a 1012246031671 s2cid 3353104 adam horvath code unwinding performance is far away loop unrolling university of minnesota further reading edit kennedy ken allen randy 2001 optimizing compilers for modern architectures a dependence based approach morgan kaufmann isbn 1 55860 286 0 external links edit chapter 7 pages 8 to 10 of michael abrash s graphics programming black book is about loop unrolling with an example in x86 assembly generalized loop unrolling gives a concise introduction optimizing subroutines in assembly language agner fog s optimizations handbook with the loop unrolling technique 2012 v t e compiler optimizations basic block peephole optimization local value numbering loop automatic parallelization automatic vectorization induction variable loop fusion loop invariant code motion loop inversion loop interchange loop nest optimization loop splitting loop unrolling loop unswitching software pipelining strength reduction data flow analysis available expression common subexpression elimination constant folding dead store elimination induction variable recognition and elimination live variable analysis upwards exposed uses use define chain reaching definitions ssa based global value numbering sparse conditional constant propagation code generation instruction scheduling instruction selection register allocation rematerialization functional deforestation tail call elimination global interprocedural optimization other bounds checking elimination compile time function execution dead code elimination expression templates inline expansion jump threading partial evaluation profile guided optimization static analysis alias analysis array access analysis control flow analysis data flow analysis dependence analysis escape analysis pointer analysis shape analysis value range analysis retrieved from https en wikipedia org w index php title loop_unrolling oldid 1345639607 categories compiler optimizations parallel computing hidden categories articles with short description short description matches wikidata cs1 maint multiple names authors list cs1 errors missing periodical articles containing suspected ai generated texts from march 2026 all accuracy disputes articles with disputed statements from december 2009 articles ...
|