You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A type that conforms to FixedWidthInteger & SignedInteger is necessarily "twos-complementy"; the API and documented behavior require that it effectively pretends to be twos-complement in its observable behavior, regardless of what the underlying representation actually is. We should find some way to document this more explicitly to discourage people from attempting to shoehorn sign-magnitude representations into this particular box.
We could just explicitly say that such types "use a twos-complement" representation. This would be a lie, but a little white one--they should as a matter of implementation convenience, even if it's not a hard requirement. Alternatively we can add a more correct but also more complex explanation.
The text was updated successfully, but these errors were encountered:
I question whether there are realistic (or any) scenarios for which the desired solution is a fixed-width integer type that (a) behaves like it's got two's complement behavior in all observable respects; and (b) doesn't actually use two's complement. While this is fairly common for extant BigInt implementations, I'm not familiar with any such fixed-width types, and I really don't see how it gains us anything to word it in such a way.
@xwu Right. It's only a matter of precision, and I am perhaps being overcautious.
The scenario in which it could reasonably happen is something like "I have an existing highly optimized 256-bit integer library in assembly that uses sign-magnitude, and I want to make it available in Swift as a FixedWidthInteger type."
Conformance to FixedWidthInteger would not be an enjoyable experience for such a signed type, I'd imagine, even if not forbidden. Down the road, hopefully, the standard library will be able to offer Int128 (which LLVM already supports?). Then, if the compiler can be evolved to a point to support DoubleWidth properly, we can get to a point where all useful conforming types are vended by the standard library itself.
Additional Detail from JIRA
md5: 37fca8a2286c0fa9f851a68b21231967
Issue Description:
A type that conforms to FixedWidthInteger & SignedInteger is necessarily "twos-complementy"; the API and documented behavior require that it effectively pretends to be twos-complement in its observable behavior, regardless of what the underlying representation actually is. We should find some way to document this more explicitly to discourage people from attempting to shoehorn sign-magnitude representations into this particular box.
We could just explicitly say that such types "use a twos-complement" representation. This would be a lie, but a little white one--they should as a matter of implementation convenience, even if it's not a hard requirement. Alternatively we can add a more correct but also more complex explanation.
The text was updated successfully, but these errors were encountered: