-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
[SE-0225] Implementation of isMultiple for BinaryInteger. #18689
[SE-0225] Implementation of isMultiple for BinaryInteger. #18689
Conversation
@swift-ci please test. |
@swift-ci please smoke test |
@@ -560,7 +564,7 @@ tests.test("truncatingIfNeeded") { | |||
|
|||
tests.test("Parsing/LosslessStringConvertible") { | |||
func _toArray<T: LosslessStringConvertible>(_ text: String) -> [T] { | |||
return text.split(separator: " ").map { T(String($0)) }.flatMap { $0 } | |||
return text.split(separator: " ").map { T(String($0)) }.compactMap { $0 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor update to silence warning while we're here.
@inlinable | ||
public func isMultiple(of other: Self) -> Bool { | ||
// Nothing but zero is a multiple of zero. | ||
if other == 0 { return self == 0 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use of guard
for an early return?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case I think it reads a lot more clearly as an if
, because it avoids the double-negative.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense! @stephentyrone thanks for your response 😉
@swift-ci Please smoke test |
@swift-ci Please smoke test. |
@@ -39,8 +39,7 @@ import StdlibUnittest | |||
/// integer type. Nesting `DoubleWidth` instances, in particular, may result in | |||
/// undesirable performance. | |||
public struct DoubleWidth<Base : FixedWidthInteger> | |||
where Base.Magnitude : UnsignedInteger, | |||
Base.Words : Collection, Base.Magnitude.Words : Collection { | |||
where Base.Words : Collection, Base.Magnitude.Words : Collection { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An unrelated PR a few weeks ago removed the need for the .Magnitude : UnsignedInteger
conformance. Removing it to get rid of warning in tests.
@@ -282,7 +281,7 @@ extension DoubleWidth.Words: Collection { | |||
|
|||
public func index(after i: Index) -> Index { | |||
switch i._value { | |||
case let .low(li) where Base.bitWidth < UInt.bitWidth: | |||
case .low where Base.bitWidth < UInt.bitWidth: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is also just cleaning up some noise.
SE-0225 has been approved with revisions. I will update this PR to match what was approved, but I'm not going to pursue merging it until two much more involved changes to integer protocols land, to avoid causing any merge issues for them: |
A default implementation is provided for FixedWidthInteger. Very basic test coverage included as well.
eaa9f82
to
03d8ce5
Compare
@swift-ci please test |
@natecook1000 Either notes on documentation or a follow-on commit appreciated. |
Build failed |
Build failed |
@swift-ci please benchmark |
Build comment file:Performance: -O
Code size: -O
Performance: -Osize
Code size: -Osize
Performance: -Onone
How to read the dataThe tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.If you see any unexpected regressions, you should consider fixing the regressions before you merge the PR. Noise: Sometimes the performance results (not code size!) contain false alarms. Unexpected regressions which are marked with '(?)' are probably noise. If you see regressions which you cannot explain you can try to run the benchmarks again. If regressions still show up, please consult with the performance team (@eeckstein). Hardware Overview
|
stdlib/public/core/Integers.swift
Outdated
// Nothing but zero is a multiple of zero. | ||
if other == 0 { return self == 0 } | ||
// Special case to avoid overflow on .min / -1 for signed types. | ||
if Self.isSigned && self == .min && other == -1 { return true } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be curious if performance changes if the two conditionals were replaced with a check of the magnitude, i.e. return other.magnitude <= 1 || self % other == 0
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For concrete types, there should basically be no difference in the face of optimization. We should be able to teach the optimizer to have only a single test on the fast-path, but that's follow-on work after the API is in.
@swift-ci please smoke test |
Implementation tracking proposal for SE-0225.
Preliminary discussion here: https://forums.swift.org/t/even-and-odd-integers/11774/129
Proposal here: swiftlang/swift-evolution#891
Final decision: https://forums.swift.org/t/accepted-with-modifications-se-0225-adding-iseven-isodd-ismultiple-to-binaryinteger/15689