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-7648] Add dedicated bitwise initializers to BinaryInteger #50189

Open
lorentey opened this issue May 10, 2018 · 5 comments
Open

[SR-7648] Add dedicated bitwise initializers to BinaryInteger #50189

lorentey opened this issue May 10, 2018 · 5 comments
Labels
new feature standard library Area: Standard library umbrella

Comments

@lorentey
Copy link
Member

Previous ID SR-7648
Radar None
Original Reporter @lorentey
Type New Feature
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels New Feature
Assignee None
Priority Medium

md5: ee8ac7bc3cc99994b8ee663af146b48e

Issue Description:

To enable custom implementations of FixedWidthInteger, and to speed up and simplify generic code that needs to initialize integer types (especially those that are wider than UInt64) from arbitrary data, we should add dedicated initializers to BinaryInteger that take a specific word or a sequence of words in two's complement representation:

protocol BinaryInteger {
  init(truncatingWordIfNeeded word: UInt)
  init<W: Sequence>(truncatingWordsIfNeeded words: W) where W.Element == UInt
}

The new initializers should be public.

The single-word initializer is only there to provide a potential speed boost for the most common case. For FixedWidthInteger (and possibly also BinaryInteger itself), it seems possible to provide a default implementation for the sequence-based initializer using the single-word initializer and shifts, but it seems better to leave init(truncatingWordsIfNeeded: ) as a required initializer. (The default implementation would be quadratic for most big integer types.)

Note that for FixedWidthInteger, we currently require the underscored initializer FixedWidthInteger.init(_truncatingBits bits: UInt), without providing a default implementation. This makes it impossible to implement FixedWidthInteger using just the public API.

@lorentey
Copy link
Member Author

This will need a swift-evolution pitch & proposal.

@lorentey
Copy link
Member Author

A words-based initializer could also be useful for an improved version of the ExpressibleByIntegerLiteral protocol that would support integer literals of truly arbitrary widths.

@lorentey
Copy link
Member Author

See #16413 (comment) for an example of a generic function where these initializers would come useful.

@xwu
Copy link
Collaborator

xwu commented May 10, 2018

What is the difference between init(truncatingIfNeeded: UInt) and init(truncatingWordIfNeeded: UInt)?

@lorentey
Copy link
Member Author

@xwu: They behave the same, but the former is a specialization of a generic requirement that can be implemented with the latter. (My suggested names above aren't great; perhaps init(word: UInt) and init<S>(words: S) would work just as well.)

(FixedWidthInteger.init<T>(truncatingIfNeeded: T) is currently implemented using .init(_truncatingBits: ), which is the single-word initializer.)

@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
new feature standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants