<div dir="ltr"><div>Hmm, with respect to both endianness and Jordan&#39;s comments re popcount (and by extension, trailing zeros and leading zeros), I think these go to the dual purpose of `BinaryInteger` (and its refinement, `FixedWidthInteger`). Both concede by their very name that they are modeling not merely a numeric type but one that has a certain machine representation. Endian conversions aren&#39;t numeric operations, but they are appropriately operations that can be generic over fixed width integers. I&#39;m not convinced (though I haven&#39;t thought about it for too long yet) of what `LittleEndian&lt;Int&gt;` would gain us.</div><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 21, 2017 at 2:12 PM, John McCall via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><span class=""><blockquote type="cite"><div>On Feb 21, 2017, at 3:08 PM, John McCall via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite"><div>On Feb 21, 2017, at 2:15 PM, Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><div><div dir="auto"><div><div>Sent from my moss-covered three-handled family gradunza</div></div><div><br>On Feb 21, 2017, at 9:04 AM, Jordan Rose &lt;<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="auto" style="word-wrap:break-word">[Proposal:<span class="m_6405813231599184935Apple-converted-space"> </span><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md" target="_blank">https://github.com/<wbr>apple/swift-evolution/blob/<wbr>master/proposals/0104-<wbr>improved-integers.md</a>]<div><br></div><div>Hi, Max (and Dave). I did have some questions about this revision:</div><div><br></div><div><div><blockquote type="cite">Arithmetic and <wbr>SignedArithmetic protocols have been renamed to Number and SignedNumber.<br></blockquote><br></div><div>What happens to NSNumber here? It feels like the same problem as Character and (NS)CharacterSet.</div><div><br></div><div><br></div><div><div><blockquote type="cite">Endian-converting initializers and properties were added to the FixedWidthInteger <wbr>protocol.<br></blockquote><br></div>This is the thing I have the biggest problem with. Endian conversions aren&#39;t numeric operations, and you can&#39;t meaningfully mix numbers of different endianness. That implies to me that numbers with different endianness should have different types. I think there&#39;s a design to explore with LittleEndian&lt;Int&gt; and BigEndian&lt;Int&gt;, and explicitly using those types whenever you need to convert.<span class="m_6405813231599184935Apple-converted-space"> </span></div></div></div></div></blockquote><div><br></div><div>I disagree. Nobody actually wants to compute with numbers in the wrong endianness for the machine. This is just used for corrections at the ends of wire protocols, where static type has no meaning.</div></div></div></blockquote><div><br></div>I think Jordan&#39;s suggestion is not that LittleEndian&lt;Int&gt; or BigEndian&lt;Int&gt; would be artihmetic types, but that they would be different types, primarily opaque, that can be explicitly converted to/from Int.  When you read something off the wire, you ask for the bytes as one of those two types (as appropriate) and then convert to the underlying type.  Ideally, Int doesn&#39;t even conform to the &quot;this type can be read off the wire&quot; protocol, eliminating the common mistake of serializing something using native endianness.</div></div></blockquote><div><br></div></span>Of course, you would not want LittleEndian&lt;Int&gt; to be directly serializable either, because it is not a fixed-size type; but I think the underlying point stands.</div><div><br></div><div>John.</div><div><div class="h5"><div><br><blockquote type="cite"><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">John.</div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br><blockquote type="cite"><div><div dir="auto"><br><blockquote type="cite"><div><div dir="auto" style="word-wrap:break-word"><div><div>Here&#39;s a sketch of such a thing:</div><div><br></div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div dir="auto" style="word-wrap:break-word"><div><div>struct LittleEndian&lt;Value: FixedWidthInteger&gt; {</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>  private var storage: Value</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div><br></div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>  public var value: Value {</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>#if little_endian</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>    return storage</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>#else</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>    return swapBytes(storage)</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>#endif</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>  }</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div><br></div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>  public var bitPattern: Value {</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>    return storage</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>  }</div></div><div><br></div><div>  public var asBigEndian: BigEndian&lt;Value&gt; {</div><div>    return BigEndian(value: self.value)</div><div>  }</div></div><div dir="auto" style="word-wrap:break-word"><div><div><br></div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>  public init(value: Value) {</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>#if little_endian</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>    storage = value</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>#else</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>    storage = swapBytes(value)</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>#endif</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>  }</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div><br></div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>  public init(bitPattern: Value) {</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>    storage = bitPattern</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>  }</div></div></div><div dir="auto" style="word-wrap:break-word"><div><div>}</div></div></div></blockquote><div dir="auto" style="word-wrap:break-word"><div><div><br></div><div>I&#39;m not saying this is the<span class="m_6405813231599184935Apple-converted-space"> </span><i>right</i> solution, just that I suspect adding Self-producing properties that change endianness is the wrong one.</div><br></div><div><blockquote type="cite">  /// The number of bits equal to 1 in this value&#39;s binary representation.<br>  ///<br>  /// For example, in a fixed-width integer type with a `bitWidth` value of 8,<br>  /// the number 31 has five bits equal to 1.<br>  ///<br>  ///     let x: Int8 = 0b0001_1111<br>  ///     // x == 31<br>  ///     // x.popcount == 5<br>  var popcount: Int { get<div> }</div></blockquote><br></div><div>Is this property actually useful enough to put into a protocol? I know it&#39;s defaulted, but it&#39;s already an esoteric operation; it seems unlikely that one would need it in a generic context. (It&#39;s also definable for arbitrary UnsignedIntegers as well as arbitrary FixedWidthIntegers.)</div></div></div></blockquote><div><br></div><div>The whole point is that you want to dispatch down to an LLVM instruction for this and not rely on the optimizer to collapse your loop into one. </div><div><br></div><blockquote type="cite"><div><div dir="auto" style="word-wrap:break-word"><div><br></div><div>(I&#39;m also still not happy with the non-Swifty name, but I see &quot;populationCount&quot; or &quot;numberOfOneBits&quot; would probably be worse.)</div><div><br></div><div><br></div><div>Thanks in advance,</div><div>Jordan</div></div></div></blockquote></div>______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></blockquote></div><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">______________________________<wbr>_________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">swift-evolution mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="mailto:swift-evolution@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-evolution@swift.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a></div></blockquote></div><br></div></div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div></div>