<div dir="ltr">On Sun, Jan 29, 2017 at 2:30 PM, David Sweeris <span dir="ltr">&lt;<a href="mailto:davesweeris@mac.com" target="_blank">davesweeris@mac.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div><div class="h5"><div><br></div><div>On Jan 29, 2017, at 12:13, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">On Sun, Jan 29, 2017 at 2:03 PM, David Sweeris <span dir="ltr">&lt;<a href="mailto:davesweeris@mac.com" target="_blank">davesweeris@mac.com</a>&gt;</span> wrote:<br><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"><div dir="auto"><span class="m_-2529960263240559010gmail-"><div><br></div><div>On Jan 29, 2017, at 10:36, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite">Hmm, interesting. I might be tempted to use a 40-bit type for large arrays, but the performance hit for any useful computation over a large array would probably tilt heavily in favor of plain 64-bit integers. What&#39;s your use case for such a 40-bit type? And is it common enough to justify such a facility in the stdlib vs. providing the tools to build it yourself?<br><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">
</blockquote></div>
</blockquote><div><br></div></span>I can think of two use-cases. One — saving memory for large #s of allocations — you already mentioned. The other is for easing interactions with on-disk data. For example, if you&#39;re working with some format that has 24-bit ints, you could use &quot;CompoundWhateverItWasCalled&lt;I<wbr>nt8,Int16&gt;&quot;. It doesn&#39;t make much difference when you&#39;re loading the data, but when you&#39;re writing it back out, you wouldn&#39;t have to worry about trimming that last byte or what to do if the value won&#39;t fit in 24 bits. I mean, obviously you&#39;d still have to handle it, but the overflow would happen in the calculation that causes it rather then while you&#39;re busy doing something else.<br><div><br></div><div>In terms of justification, I think probably all I can offer is that I <i>think</i> it wouldn&#39;t be materially harder or less efficient to implement this than it would be to write a &quot;DoubleWidth&lt;T&gt;&quot; type... It&#39;s extra functionality for free, at least in terms of effort. It would increase the API surface, but not by much. Assuming that &quot;DoubleWidth&quot; could just be a typealias, that is. If I&#39;m wrong about it being &quot;that easy&quot;, then I don&#39;t think it&#39;d be worth it. As you noted, it is somewhat niche.</div></div></blockquote><div><br></div><div>Unless I&#39;m mistaken, DoubleWidth would be implemented using doubleWidthMultiply and doubleWidthDivide facilities provided in these new integer protocols. As the protocol documentation says, DoubleWidth is really intended for wider types, not arbitrarily sized narrower ones. There&#39;s no obvious way I can see to implement the same thing for two types of unequal bit width, short of determining dynamically, at each operation, which of your two types has the larger bit width and truncating bits accordingly. So if you want a 24-bit Int, it&#39;s unclear to me why you&#39;d choose what&#39;s effectively DoubleWidth&lt;Int16&gt; minus 8 bits rather than Int32 minus 8 bits.</div></div></div></div>
</div></blockquote><br></div></div><div>It&#39;s a trade-off between computationally efficiency and memory/storage efficiency. If you want your answer sooner, pick Int32, accept that you&#39;re &quot;wasting&quot; a byte, and manually trim the value later. If you&#39;d rather have your 24-bit int only actually take up 24 bits or not have to worry about its value taking more than 24 bits, pick the other one.</div></div></blockquote><div><br></div><div>You shouldn&#39;t have to make any trade-off: these protocols should make it easy for you to make your own Int24 that is both fast and exactly 24 bits!</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>I&#39;ll have to look at the doubleWidthMultiply/Divide functions later when I get home. Don&#39;t know if they&#39;d actually throw a wrench in my crazy scheme, or if it&#39;s all just in the name. If this causes any problems for DoubleWidth, I don&#39;t think it&#39;d be worth doing this.</div></div></blockquote><div><br></div><div>`doubleWidthMultiply` gives you the high and low halves of the result in a tuple. Unless I&#39;m mistaken, getting the high and low halves are machine instructions. Put them together and you have a double-width value.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div><br></div><div>- Dave Sweeris</div></div></blockquote></div><br></div></div>