Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-8189] Better document the twos-complementyness of FixedWidthInteger #50721

Open
stephentyrone opened this issue Jul 5, 2018 · 5 comments
Assignees
Labels
improvement standard library Area: Standard library umbrella

Comments

@stephentyrone
Copy link
Member

Previous ID SR-8189
Radar rdar://problem/41846353
Original Reporter @stephentyrone
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Improvement
Assignee @stephentyrone
Priority Medium

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.

@stephentyrone
Copy link
Member Author

@moiseev @natecook1000, @xwu you all likely have some thoughts here, too.

@stephentyrone
Copy link
Member Author

@swift-ci create

@xwu
Copy link
Collaborator

xwu commented Jul 5, 2018

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.

@stephentyrone
Copy link
Member Author

@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."

@xwu
Copy link
Collaborator

xwu commented Jul 5, 2018

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.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants