[swift-evolution] protocol-oriented integers (take 2)

Stephen Canon scanon at apple.com
Sat Jan 14 16:21:38 CST 2017

> On Jan 14, 2017, at 11:18 AM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> On Fri, Jan 13, 2017 at 7:51 PM, Xiaodi Wu <xiaodi.wu at gmail.com <mailto:xiaodi.wu at gmail.com>> wrote:
> 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.
> 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:
> ```
> func foo() -> UInt32 {
>     return arc4random() >> arc4random()
> }
> ```

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).

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].

– Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170114/e941fbca/attachment.html>

More information about the swift-evolution mailing list