<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Assuming RawSignificand really is the type you want, I think this does what you’re looking for?<br class=""><br class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> BinaryFloatingPointWithBitPattern: </span><span style="font-variant-ligatures: no-common-ligatures; color: #c35900" class="">BinaryFloatingPoint</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(bitPattern: RawSignificand)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> bitPattern: RawSignificand { </span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">get</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(195, 89, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(54, 86, 138);" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Float</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">BinaryFloatingPointWithBitPattern</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> { }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(195, 89, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(54, 86, 138);" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Double</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">BinaryFloatingPointWithBitPattern</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> { }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(195, 89, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(195, 89, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">BinaryFloatingPointWithBitPattern</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(unitRangeFromRawSignificand s: RawSignificand) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #c35900" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">(bitPattern: </span><span style="font-variant-ligatures: no-common-ligatures; color: #c35900" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">bitPattern</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">|</span><span style="font-variant-ligatures: no-common-ligatures" class=""> s) </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">-</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #36568a" class="">1</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><br class=""><blockquote type="cite" class="">On Aug 26, 2016, at 7:38 PM, Stephen Canon via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:<br class=""><br class="">Where does your RawSignificand input come from? Is that really the type that you want?<br class=""><br class="">I don’t think you really need very much boilerplate at all here.<br class=""><br class=""><blockquote type="cite" class="">On Aug 26, 2016, at 7:30 PM, Jens Persson <<a href="mailto:jens@bitcycle.com" class="">jens@bitcycle.com</a>> wrote:<br class=""><br class="">I understand.<br class="">It's just very tempting to try and use the new static computed properties for eg 23 and 52 etc.<br class="">I guess I'll just have to write a lot of boilerplate, or perhaps a protocol that is just implemented by Double and Float (that will be very similar to BinaryFloatingPoint in a lot of ways).<br class="">/Jens<br class=""><br class="">On Sat, Aug 27, 2016 at 1:25 AM, Stephen Canon <scanon@apple.com> wrote:<br class="">This doesn’t really scale up very well, though. BinaryFloatingPoint needs to also be able to model e.g. Float2048 or similar; we generally don't want to require that RawExponent to be the same type as RawSignificand (which I think is what you’re really suggesting), because in typical bignum usage significands are much larger than exponents.<br class=""><br class="">It sounds like maybe you actually want to be operating directly on bitPatterns, rather than the abstract fields of the types.<br class=""><br class="">– Steve<br class=""><br class=""><blockquote type="cite" class="">On Aug 26, 2016, at 7:21 PM, Jens Persson <jens@bitcycle.com> wrote:<br class=""><br class="">Oh, to more directly answer your question: I don't like having to create a UInt (UInt64) value when all my bit manipulaton code happens in UInt32 (for Float) for example.<br class=""><br class="">The most probable context for using these computed properties and types of BinaryFloatingPoint is one in which specific fixed width types really matters a lot (look at the name of the protocol and the properties and assocated types we are talking about).<br class=""><br class="">/Jens<br class=""><br class=""><br class="">On Sat, Aug 27, 2016 at 1:15 AM, Jens Persson <jens@bitcycle.com> wrote:<br class="">Reason for asking is that I have this:<br class=""><br class="">extension Double {<br class=""> init(unitRangeFromRawSignificand s: RawSignificand) {<br class=""> let bitPattern = s | (1023 << 52)<br class=""> self = unsafeBitCast(bitPattern, to: Double.self) - 1.0<br class=""> }<br class="">}<br class="">extension Float {<br class=""> init(unitRangeFromRawSignificand s: RawSignificand) {<br class=""> let bitPattern = s | (127 << 23)<br class=""> self = unsafeBitCast(bitPattern, to: Float.self) - 1.0<br class=""> }<br class="">}<br class=""><br class="">But they would be better as:<br class="">extension BinaryFloatingPoint {<br class=""> init(unitRangeFromRawSignificand s: RawSignificand) {<br class=""> ... problems here, have to try casting things into RawSignificand's type ...<br class=""> }<br class="">}<br class=""><br class="">Please have a go at that and perhaps you see what I mean or you will come up with a nice solution that I have missed. (Speed is very important btw.)<br class=""><br class="">/Jens<br class=""><br class=""><br class="">On Sat, Aug 27, 2016 at 1:02 AM, Stephen Canon <scanon@apple.com> wrote:<br class="">> On Aug 26, 2016, at 6:06 PM, Jens Persson via swift-dev <swift-dev@swift.org> wrote:<br class="">><br class="">> I can understand why<br class="">> Double.RawSignificand is UInt64<br class="">> and<br class="">> Float.RawSignificand is UInt32<br class="">><br class="">> But I can't understand why both<br class="">> Double.RawExponent<br class="">> and<br class="">> Float.RawExponent<br class="">> should be UInt.<br class="">><br class="">> Why aren't they also just UInt64 and UInt32, resp.?<br class=""><br class="">Let me flip the question: why would they be UInt64 and UInt32? Absent a reason to prefer a specific fixed-with type, Swift integers should generally default to being [U]Int (and ideally Int, but RawExponent is Unsigned).<br class=""><br class="">– Steve<br class=""><br class=""><br class=""></blockquote><br class=""><br class=""></blockquote><br class="">_______________________________________________<br class="">swift-dev mailing list<br class=""><a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-dev<br class=""></blockquote><br class=""></body></html>