{"id":10,"date":"2006-05-20T14:53:21","date_gmt":"2006-05-20T22:53:21","guid":{"rendered":"http:\/\/www.pagetable.com\/?p=10"},"modified":"2006-05-20T14:53:21","modified_gmt":"2006-05-20T22:53:21","slug":"simple-compiler-optimization","status":"publish","type":"post","link":"https:\/\/www.pagetable.com\/?p=10","title":{"rendered":"Simple compiler optimization"},"content":{"rendered":"<p>I thought of an optimization that compilers for most CPUs could do that I think should be implemented.  Let&#8217;s say you have C code like this:<\/p>\n<p>if ((variable == 4) || (variable == 6)) { stuff }<\/p>\n<p>Any existing compiler I know of would compile into something like this (eax is variable):<\/p>\n<p>cmp eax, 4<br \/>\njz label<br \/>\ncmp eax, 6<br \/>\njz label<\/p>\n<p>That&#8217;s a bad way to do it.  It should be implemented more like this:<\/p>\n<p>or eax, 2<br \/>\ncmp eax, 6<br \/>\njz label<\/p>\n<p>This saves one instruction, but more importantly, it saves a conditional branch.  Conditional branches can be expensive on modern hardware.  This will work on any similar &#8220;if&#8221; statement whose constants differ by a single bit.<\/p>\n<p>Even if you must copy eax to another register first, it removes a conditional branch, which is probably a win.  The situations in which to do this are more complicated and must take into account many things.<\/p>\n<p>For constants that are 1 apart but not 1 bit apart, like 7 and 8, you can do:<\/p>\n<p>sub eax, 7 \/\/ use dec eax in 1\/2 case<br \/>\ntest eax, 0xFFFFFFFE \/\/ this is a 3 byte opcode<br \/>\njz label<\/p>\n<p>This also works for 0 and -1: increment eax first.<\/p>\n<p>myria<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought of an optimization that compilers for most CPUs could do that I think should be implemented. Let&#8217;s say you have C code like this: if ((variable == 4) || (variable == 6)) { stuff } Any existing compiler I know of would compile into something like this (eax is variable): cmp eax, 4 &#8230; <a title=\"Simple compiler optimization\" class=\"read-more\" href=\"https:\/\/www.pagetable.com\/?p=10\" aria-label=\"Read more about Simple compiler optimization\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,38],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-tricks","category-x86"],"_links":{"self":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10"}],"version-history":[{"count":0,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}