<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 14, 2017, at 11:18 AM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">On Fri, Jan 13, 2017 at 7:51 PM, Xiaodi Wu <span dir="ltr" class="">&lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank" class="">xiaodi.wu@gmail.com</a>&gt;</span> wrote:<br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Thanks, that's very helpful. Yes, my question was more directed to those situations that can't be optimized away. It's good to know that the maximum total cost is an and and a shift, which is what it sounded like but wasn't made explicit.</blockquote><div class=""><br class=""></div><div class="">Oy, I misread your reply; the maximum total cost for a _masking_ shift is two very cheap instructions; the maximum total cost for a _smart_ shift is potentially higher. I know it's unrealistic, but what will we be looking at when it comes to this:</div><div class=""><br class=""></div><div class="">```</div><div class=""><div class="">func foo() -&gt; UInt32 {</div><div class="">&nbsp; &nbsp; return arc4random() &gt;&gt; arc4random()</div><div class="">}</div></div><div class="">```</div></div></div></div></div></blockquote><div><br class=""></div><div>This specific example generates a compare, shift, and conditional move of zero. Still very cheap (a branch would arguably be even better, since the result is almost always zero in this particular case).</div><div><br class=""></div><div>If the RHS is not unsigned, so that it might also be negative, the very rare absolute worst case codegen for basic integer types should be something like [add, compare, negate, 2xshift, 2xselect].</div><div><br class=""></div><div>– Steve</div></div></body></html>