{"id":91,"date":"2013-06-18T08:47:06","date_gmt":"2013-06-17T22:47:06","guid":{"rendered":"http:\/\/www.eyemonline.com\/?p=91"},"modified":"2017-11-03T11:27:15","modified_gmt":"2017-11-03T01:27:15","slug":"beautiful-code","status":"publish","type":"post","link":"http:\/\/www.eyemonline.com\/?p=91","title":{"rendered":"Beautiful Code"},"content":{"rendered":"<p>In the mid 1990&#8217;s I was shown a snippet of C source code which remains what I consider to be the most beautiful code I have ever seen. The code was introduced to me as:<\/p>\n<blockquote><p>It&#8217;s a devastatingly deviously unrolled byte-copying loop, devised by Tom Duff while he was at Lucasfilm. In it&#8217;s &#8220;classic&#8221; form, it looks like:<\/p><\/blockquote>\n<p><!--more--><\/p>\n<pre class=\"lang:default decode:true\">register n = (count + 7) \/ 8; \/* count &gt; 0 assumed *\/\r\nswitch (count % 8) {\r\n    case 0: do { *to = *from++;\r\n    case 7:      *to = *from++;\r\n    case 6:      *to = *from++;\r\n    case 5:      *to = *from++;\r\n    case 4:      *to = *from++;\r\n    case 3:      *to = *from++;\r\n    case 2:      *to = *from++;\r\n    case 1:      *to = *from++;\r\n            } while (--n &gt; 0);\r\n}<\/pre>\n<p>The code is described elsewhere on the internet as <a title=\"Duff's Device\" href=\"http:\/\/en.wikipedia.org\/wiki\/Duff's_device\">Duff&#8217;s Device<\/a>.<\/p>\n<p>The code implements an 8 ways unrolled loop which copies a block of data to a single memory address which is likely to be a port address. Typically, unrolled loops do cleanup after the main body of the loop has executed. This code uses the fall-through behaviour of C&#8217;s switch statement to do the cleanup on the way into the loop which makes the code more compact.<\/p>\n<pre class=\"lang:default decode:true\">switch(count % 8) {<\/pre>\n<p>C&#8217;s post increment operator is used to identify the address of the source data for copying and then advances the next address after the copy. This is done in one expression.<\/p>\n<pre class=\"lang:default decode:true\">*from++;<\/pre>\n<p>The number of times needed through the body of the loop is calculated once before entering the loop.<\/p>\n<pre class=\"lang:default decode:true\">register n = (count + 7) \/ 8; \/* count &gt; 0 assumed *\/<\/pre>\n<p>And using C&#8217;s pre decrement operator, decrements and tests the count each time through the loop.<\/p>\n<pre class=\"lang:default decode:true\">while (--n &gt; 0);<\/pre>\n<p>Given the tight coupling between C and the hardware that it is compiled to run on, this code should be amazingly fast on most architectures.<\/p>\n<p>I tip my hat to Tom Duff for his beautiful, and fast, code to copy data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the mid 1990&#8217;s I was shown a snippet of C source code which remains what I consider to be the most beautiful code I have ever seen. The code was introduced to me as: It&#8217;s a devastatingly deviously unrolled &hellip; <a href=\"http:\/\/www.eyemonline.com\/?p=91\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[19],"tags":[17],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3FgN7-1t","_links":{"self":[{"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/posts\/91"}],"collection":[{"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91"}],"version-history":[{"count":15,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":383,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions\/383"}],"wp:attachment":[{"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.eyemonline.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}