<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=""><div class="">Hi Xiaodi,</div><div class=""><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""> What is the performance penalty as compared to the existing operators?</div></blockquote><div class=""><br class=""></div><div class="">First of all, I don’t have the concrete numbers unfortunately. But…</div><div class="">If we are talking about concrete types, there should be very little difference between the two smart and masking shifts.</div><div class="">For example, the following functions:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Monaco" class=""><span style="font-size: 12px;" class="">public func foo(x: UInt) -> UInt {</span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 12px;" class=""> return x >> 32</span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 12px;" class="">}</span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 12px;" class=""><br class=""></span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 12px;" class="">public func bar(x: UInt) -> UInt {</span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 12px;" class=""> return x &>> 32</span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 12px;" class="">}</span></font></div></div><div class=""><br class=""></div><div class="">Produce equivalent SIL and LLVM IR when compiled with optimizations. Moreover, if you change 32 to 100, then foo will become something like:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span style="font-size: 12px;" class="">foo(x: UInt) { return 0 }</span></font></div><div class=""><br class=""></div><div class="">whereas bar will still call the <font face="Monaco" class=""><span style="font-size: 12px;" class="">llvm.shl</span></font>.</div><div class=""><br class=""></div><blockquote type="cite" class=""><div dir="ltr" class="">Beyond performance, what are the implications for migrating existing bit twiddling algorithms written in Swift 3?</div></blockquote>Let’s take for example the ‘&>>” operator. There are 2 overloads: <font face="Monaco" class=""><span style="font-size: 12px;" class="">&>>(Self, Self)</span></font> and <font face="Monaco" class=""><span style="font-size: 12px;" class="">&>><Other : BinaryInteger>(Self, Other)</span></font></div><div class="">(In Swift 3 there is only one version of >>, that takes both arguments of the same type.)</div><div class=""><br class=""></div><div class="">So, in an expression `x &>> 32`, compiler assumes 32 to have type Int (which is the default type for the integer literals) and prefer the heterogeneous overload. It might not be too bad, as I showed in examples above, but in order to increase chances of success you might want to specify an explicit type context, as in `x &>> (32 as TypeOfX)`.</div><div class=""><br class=""></div><div class="">As always, only concrete benchmarks will tell. We spent many hours optimizing the implementation for our existing benchmark suite.</div><div class=""><br class=""></div><div class="">Max</div><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 13, 2017, at 2:14 PM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">[Resending to list with original message removed for length.]</div><br class="">This is fantastic. Glad to see it take shape. Very neat and insightful to have trailingZeros on BinaryInteger but leadingZeros on FixedWidthInteger. A couple of questions on smart shifts: What is the performance penalty as compared to the existing operators? Beyond performance, what are the implications for migrating existing bit twiddling algorithms written in Swift 3?<br class=""><div class=""><br class=""></div></div>
</div></blockquote></div><br class=""></body></html>