-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. An unrelated PR a few weeks ago removed the need for the |
||
|
||
public typealias High = Base | ||
public typealias Low = Base.Magnitude | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. This change is also just cleaning up some noise. |
||
return Index(.high(_high.endIndex)) | ||
case let .low(li): | ||
let next = _low.index(after: li) | ||
|
@@ -1004,6 +1003,19 @@ dwTests.test("DivideMinByMinusOne") { | |
_ = f(Int1024.min) | ||
} | ||
|
||
dwTests.test("isMultiple") { | ||
func isMultipleTest<T: FixedWidthInteger>(type: T.Type) { | ||
expectTrue(T.min.isMultiple(of: 2)) | ||
expectFalse(T.max.isMultiple(of: 10)) | ||
// Test that these do not crash. | ||
expectTrue((0 as T).isMultiple(of: 0)) | ||
expectFalse((1 as T).isMultiple(of: 0)) | ||
expectTrue(T.min.isMultiple(of: 0 &- 1)) | ||
} | ||
isMultipleTest(type: Int128.self) | ||
isMultipleTest(type: UInt128.self) | ||
} | ||
|
||
dwTests.test("MultiplyMinByMinusOne") { | ||
func f(_ x: Int1024) -> Int1024 { | ||
return x * -1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,10 @@ extension MockBinaryInteger : BinaryInteger { | |
return _value.trailingZeroBitCount | ||
} | ||
|
||
func isMultiple(of other: MockBinaryInteger<T>) -> Bool { | ||
return _value.isMultiple(of: other._value) | ||
} | ||
|
||
static func + ( | ||
lhs: MockBinaryInteger<T>, rhs: MockBinaryInteger<T> | ||
) -> MockBinaryInteger<T> { | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Minor update to silence warning while we're here. |
||
} | ||
|
||
expectEqualSequence([1, 2, 3], _toArray("1 2 3") as [Int]) | ||
|
@@ -798,6 +802,27 @@ tests.test("DivideMinByMinusOne") { | |
_ = f(Int.min) | ||
} | ||
|
||
tests.test("isMultiple") { | ||
func isMultipleTest<T: FixedWidthInteger>(type: T.Type) { | ||
expectTrue(T.min.isMultiple(of: 2)) | ||
expectFalse(T.max.isMultiple(of: 10)) | ||
// Test that these do not crash. | ||
expectTrue((0 as T).isMultiple(of: 0)) | ||
expectFalse((1 as T).isMultiple(of: 0)) | ||
expectTrue(T.min.isMultiple(of: 0 &- 1)) | ||
} | ||
isMultipleTest(type: Int.self) | ||
isMultipleTest(type: Int8.self) | ||
isMultipleTest(type: Int16.self) | ||
isMultipleTest(type: Int32.self) | ||
isMultipleTest(type: Int64.self) | ||
isMultipleTest(type: UInt.self) | ||
isMultipleTest(type: UInt8.self) | ||
isMultipleTest(type: UInt16.self) | ||
isMultipleTest(type: UInt32.self) | ||
isMultipleTest(type: UInt64.self) | ||
} | ||
|
||
tests.test("MultiplyMinByMinusOne") { | ||
func f(_ x: Int) -> Int { | ||
return x * -1 | ||
|
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.