The standard library file FloatingPointTypes.swift.gyb has the declaration _significandMask
for concrete types:
extension ${Self}: BinaryFloatingPoint {
@inlinable // FIXME(inline-always) was usableFromInline
internal static var _significandMask: ${RawSignificand} {
@inline(__always) get {
return 1 &<< ${RawSignificand}(significandBitCount) - 1
}
}
}
I would like to move that property into the FloatingPoint.swift file, constrained by RawSignificand: FixedWidthInteger
(alongside the _convert
and random
methods):
extension BinaryFloatingPoint where RawSignificand: FixedWidthInteger {
@inlinable
internal static var _significandMask: RawSignificand {
return (1 << significandBitCount) &- 1
}
}
There is already one place in FloatingPoint.swift where this could be used right away:
let significandMask = ((1 as RawSignificand) << Self.significandBitCount) &- 1
And my work to improve random(in:using:)
will benefit from it as well.
Is this an acceptable change? Should anything be different about the declaration? Maybe @_transparent
?
I will note that there is one minor difference in the implementations: the existing version uses &<<
because the concrete types all have more bits in RawSignificand
than are used by significandBitCount
. However an arbitrary third-party type might use all the bits in the type for its significand, so the replacement uses <<
.
For the same reason, the replacement uses &-
, because if all the bits are used for the significand, the result should have all bits set to 1.
I wasn’t sure where to ask about this, hopefully I’m in the right place. If this change is acceptable, I’d be happy to raise a pull request for it.