<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>On Fri, Dec 4, 2015, at 02:42 PM, John McCall wrote:<br></div>
<blockquote type="cite"><div><blockquote type="cite"><div>On Dec 4, 2015, at 2:33 PM, Kevin Ballard &lt;<a href="mailto:kevin@sb.org">kevin@sb.org</a>&gt; wrote:<br></div>
<div><div><div>It's  not actually Duff's Device. Duff's Device relies on the fact that C switch statements don't actually introduce a new scope, and so it overlaps a switch with a do-while loop. This lets it only test the number of bytes once, to jump into the middle of the loop, and then it switches over to a while loop that decrements a counter every 8 instructions. Basically, it's a trick for manual loop unrolling that deals with non-multiple-of-8 counts efficiently.<br></div>
</div>
</div>
</blockquote><div>&nbsp;</div>
<div>To be pedantic, C switch statements do introduce a new scope. &nbsp;What Duff’s Device exploits is that switch is allowed to jump into (almost) arbitrary scopes, and cases can appear anywhere recursively inside a switch.<br></div>
</div>
</blockquote><div>&nbsp;</div>
<div>Oh you're right, what I meant to say was that case statements don't introduce a new scope. The do-while loop is contained entirely within the scope of the switch itself, and the case statements act like goto labels so it can jump into the scope of the do-while.<br></div>
<div>&nbsp;</div>
<div>-Kevin Ballard</div>
</body>
</html>