Skip to content

Commit 767e8f3

Browse files
authored
Merge pull request #3796 from apple/new-integer-protocols
New integer protocols
2 parents 98e6367 + 8ca814c commit 767e8f3

File tree

124 files changed

+8670
-2673
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+8670
-2673
lines changed

benchmark/single-source/ByteSwap.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import TestsUtils
1818

1919
// a naive O(n) implementation of byteswap.
2020
func byteswap_n(_ a: UInt64) -> UInt64 {
21-
return ((a & 0x00000000000000FF) << 56) |
22-
((a & 0x000000000000FF00) << 40) |
23-
((a & 0x0000000000FF0000) << 24) |
24-
((a & 0x00000000FF000000) << 8) |
25-
((a & 0x000000FF00000000) >> 8) |
26-
((a & 0x0000FF0000000000) >> 24) |
27-
((a & 0x00FF000000000000) >> 40) |
28-
((a & 0xFF00000000000000) >> 56)
21+
var result = ((a & 0x00000000000000FF) << 56)
22+
result |= ((a & 0x000000000000FF00) << 40)
23+
result |= ((a & 0x0000000000FF0000) << 24)
24+
result |= ((a & 0x00000000FF000000) << 8)
25+
result |= ((a & 0x000000FF00000000) >> 8)
26+
result |= ((a & 0x0000FF0000000000) >> 24)
27+
result |= ((a & 0x00FF000000000000) >> 40)
28+
result |= ((a & 0xFF00000000000000) >> 56)
29+
return result
2930
}
3031

3132
// a O(logn) implementation of byteswap.

stdlib/private/StdlibCollectionUnittest/CheckCollectionInstance.swift.gyb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ internal func _checkIncrementalAdvance<Instances, BaseCollection>(
102102
-baseCollection.distance(from: limit, to: i)
103103

104104
var offset: BaseCollection.IndexDistance = 0
105-
for _ in 0...(d * sign).toIntMax() {
105+
for _ in 0...Int64(d * sign) {
106106
let j = baseCollection.index(i, offsetBy: offset)
107107
let k = baseCollection.index(i, offsetBy: offset + sign, limitedBy: limit) ?? limit
108108
let jAtLimit = offset == d
@@ -447,8 +447,7 @@ public func checkOneLevelOf${Traversal}Collection<
447447
let expectedArray = Array(expected)
448448

449449
// Check `count`.
450-
expectEqual(
451-
expectedArray.count.toIntMax(), collection.count.toIntMax(), ${trace})
450+
expectEqual(Int64(expectedArray.count), Int64(collection.count), ${trace})
452451

453452
//===------------------------------------------------------------------===//
454453
// Check Iteration behavior.

stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -790,10 +790,14 @@ public let mapTests = [
790790
[], [],
791791
{ _ -> Int32 in expectUnreachable(); return 0xffff }),
792792

793-
MapTest([ 101 ], [ 1 ], { (x: Int) -> Int32 in Int32(x + 100) }),
794-
MapTest([ 101, 102 ], [ 1, 2 ], { (x: Int) -> Int32 in Int32(x + 100) }),
795-
MapTest([ 101, 102, 103 ], [ 1, 2, 3 ], { (x: Int) -> Int32 in Int32(x + 100) }),
796-
MapTest(Array(101..<200), Array(1..<100), { (x: Int) -> Int32 in Int32(x + 100) }),
793+
MapTest([ 101 ], [ 1 ],
794+
{ (x: Int) -> Int32 in Int32(x + 100) }),
795+
MapTest([ 101, 102 ], [ 1, 2 ],
796+
{ (x: Int) -> Int32 in Int32(x + 100) }),
797+
MapTest([ 101, 102, 103 ], [ 1, 2, 3 ],
798+
{ (x: Int) -> Int32 in Int32(x + 100) }),
799+
MapTest(Array(101..<200), Array(1..<100),
800+
{ (x: Int) -> Int32 in Int32(x + 100) }),
797801
]
798802

799803
public let minMaxTests = [

stdlib/private/StdlibUnicodeUnittest/Collation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,13 @@ public struct StringComparisonTest {
338338

339339
public func sortKey(forCollationElements ces: [UInt64]) -> ([UInt16], [UInt16], [UInt16]) {
340340
func L1(_ ce: UInt64) -> UInt16 {
341-
return UInt16(truncatingBitPattern: ce >> 32)
341+
return UInt16(extendingOrTruncating: ce &>> 32)
342342
}
343343
func L2(_ ce: UInt64) -> UInt16 {
344-
return UInt16(truncatingBitPattern: ce >> 16)
344+
return UInt16(extendingOrTruncating: ce &>> 16)
345345
}
346346
func L3(_ ce: UInt64) -> UInt16 {
347-
return UInt16(truncatingBitPattern: ce)
347+
return UInt16(extendingOrTruncating: ce)
348348
}
349349

350350
var result1: [UInt16] = []

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2447,7 +2447,7 @@ public func expectEqualsUnordered<
24472447
public func expectEqualsUnordered<T : Strideable>(
24482448
_ expected: CountableRange<T>, _ actual: [T], ${TRACE}
24492449
) {
2450-
if numericCast(expected.count) != actual.count {
2450+
if expected.count != actual.count {
24512451
expectationFailure("expected elements: \"\(expected)\"\n"
24522452
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
24532453
trace: ${trace})

stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public struct _stdlib_ShardedAtomicCounter {
7272

7373
public mutating func randomInt() -> Int {
7474
var result = 0
75-
for _ in 0..<Int._sizeInBits {
75+
for _ in 0..<Int.bitWidth {
7676
result = (result << 1) | (_state & 1)
7777
_state = (_state >> 1) ^ (-(_state & 1) & Int(bitPattern: 0xD0000001))
7878
}

stdlib/private/SwiftPrivate/SwiftPrivate.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
import SwiftShims
1414

1515
/// Convert the given numeric value to a hexadecimal string.
16-
public func asHex<T : Integer>(_ x: T) -> String {
17-
return "0x" + String(x.toIntMax(), radix: 16)
16+
// FIXME(integers): support a more general BinaryInteger protocol
17+
public func asHex<T : FixedWidthInteger>(_ x: T) -> String {
18+
return "0x" + String(x, radix: 16)
1819
}
1920

2021
/// Convert the given sequence of numeric values to a string representing
2122
/// their hexadecimal values.
22-
public func asHex<S: Sequence>(_ x: S) -> String
23-
where S.Iterator.Element : Integer {
23+
// FIXME(integers): support a more general BinaryInteger protocol
24+
public func asHex<S : Sequence>(_ x: S) -> String
25+
where
26+
S.Iterator.Element : FixedWidthInteger {
2427
return "[ " + x.lazy.map { asHex($0) }.joined(separator: ", ") + " ]"
2528
}
2629

stdlib/public/Platform/tgmath.swift.gyb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
// Generic functions implementable directly on FloatingPoint.
1414
@_transparent
15-
public func fabs<T: FloatingPoint>(_ x: T) -> T {
16-
return abs(x)
15+
public func fabs<T: FloatingPoint>(_ x: T) -> T
16+
where T.Magnitude == T {
17+
return x.magnitude
1718
}
1819

1920
@_transparent

stdlib/public/SDK/CoreGraphics/CGFloat.swift.gyb

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ public struct CGFloat {
6767
public var native: NativeType
6868
}
6969

70+
extension CGFloat : SignedNumeric {
71+
// FIXME(integers): implement properly
72+
public init?<T : BinaryInteger>(exactly source: T) {
73+
fatalError()
74+
}
75+
76+
@_transparent
77+
public var magnitude: CGFloat {
78+
return CGFloat(Swift.abs(native))
79+
}
80+
81+
}
82+
7083
extension CGFloat : BinaryFloatingPoint {
7184

7285
public typealias RawSignificand = UInt
@@ -196,34 +209,28 @@ extension CGFloat : BinaryFloatingPoint {
196209
return CGFloat(native.nextUp)
197210
}
198211

199-
@_transparent
200-
public var magnitude: CGFloat {
201-
return CGFloat(Swift.abs(native))
202-
}
203-
204-
@_transparent
205212
public mutating func negate() {
206213
native.negate()
207214
}
208215

209216
@_transparent
210-
public mutating func add(_ other: CGFloat) {
211-
native.add(other.native)
217+
public static func +=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
218+
lhs.native += rhs.native
212219
}
213220

214221
@_transparent
215-
public mutating func subtract(_ other: CGFloat) {
216-
native.subtract(other.native)
222+
public static func -=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
223+
lhs.native -= rhs.native
217224
}
218225

219226
@_transparent
220-
public mutating func multiply(by other: CGFloat) {
221-
native.multiply(by: other.native)
227+
public static func *=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
228+
lhs.native *= rhs.native
222229
}
223230

224231
@_transparent
225-
public mutating func divide(by other: CGFloat) {
226-
native.divide(by: other.native)
232+
public static func /=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
233+
lhs.native /= rhs.native
227234
}
228235

229236
@_transparent
@@ -418,44 +425,34 @@ extension Float {
418425
// tweaking the overload resolution rules, or by removing the other
419426
// definitions in the standard lib, or both.
420427

421-
@_transparent
422-
public func +(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
423-
return lhs.adding(rhs)
424-
}
425-
426-
@_transparent
427-
public func -(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
428-
return lhs.subtracting(rhs)
429-
}
430-
431-
@_transparent
432-
public func *(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
433-
return lhs.multiplied(by: rhs)
434-
}
435-
436-
@_transparent
437-
public func /(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
438-
return lhs.divided(by: rhs)
439-
}
440-
441-
@_transparent
442-
public func +=(lhs: inout CGFloat, rhs: CGFloat) {
443-
lhs.add(rhs)
444-
}
428+
extension CGFloat {
429+
@_transparent
430+
public static func +(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
431+
var lhs = lhs
432+
lhs += rhs
433+
return lhs
434+
}
445435

446-
@_transparent
447-
public func -=(lhs: inout CGFloat, rhs: CGFloat) {
448-
lhs.subtract(rhs)
449-
}
436+
@_transparent
437+
public static func -(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
438+
var lhs = lhs
439+
lhs -= rhs
440+
return lhs
441+
}
450442

451-
@_transparent
452-
public func *=(lhs: inout CGFloat, rhs: CGFloat) {
453-
lhs.multiply(by: rhs)
454-
}
443+
@_transparent
444+
public static func *(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
445+
var lhs = lhs
446+
lhs *= rhs
447+
return lhs
448+
}
455449

456-
@_transparent
457-
public func /=(lhs: inout CGFloat, rhs: CGFloat) {
458-
lhs.divide(by: rhs)
450+
@_transparent
451+
public static func /(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
452+
var lhs = lhs
453+
lhs /= rhs
454+
return lhs
455+
}
459456
}
460457

461458
//===----------------------------------------------------------------------===//

stdlib/public/SDK/Dispatch/Time.swift

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ public struct DispatchTime : Comparable {
6969
}
7070
}
7171

72-
public func <(a: DispatchTime, b: DispatchTime) -> Bool {
73-
return a.rawValue < b.rawValue
74-
}
75-
76-
public func ==(a: DispatchTime, b: DispatchTime) -> Bool {
77-
return a.rawValue == b.rawValue
72+
extension DispatchTime {
73+
public static func < (a: DispatchTime, b: DispatchTime) -> Bool {
74+
return a.rawValue < b.rawValue
75+
}
76+
77+
public static func ==(a: DispatchTime, b: DispatchTime) -> Bool {
78+
return a.rawValue == b.rawValue
79+
}
7880
}
7981

8082
public struct DispatchWallTime : Comparable {
@@ -96,17 +98,20 @@ public struct DispatchWallTime : Comparable {
9698
}
9799
}
98100

99-
public func <(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
100-
if b.rawValue == ~0 {
101-
return a.rawValue != ~0
102-
} else if a.rawValue == ~0 {
103-
return false
104-
}
105-
return -Int64(bitPattern: a.rawValue) < -Int64(bitPattern: b.rawValue)
106-
}
107-
108-
public func ==(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
109-
return a.rawValue == b.rawValue
101+
extension DispatchWallTime {
102+
public static func <(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
103+
let negativeOne: dispatch_time_t = ~0
104+
if b.rawValue == negativeOne {
105+
return a.rawValue != negativeOne
106+
} else if a.rawValue == negativeOne {
107+
return false
108+
}
109+
return -Int64(bitPattern: a.rawValue) < -Int64(bitPattern: b.rawValue)
110+
}
111+
112+
public static func ==(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
113+
return a.rawValue == b.rawValue
114+
}
110115
}
111116

112117
public enum DispatchTimeInterval {

stdlib/public/SDK/Foundation/AffineTransform.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,13 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
273273
}
274274

275275
public var hashValue : Int {
276-
return Int(m11 + m12 + m21 + m22 + tX + tY)
276+
// FIXME(integers): the expression was broken into pieces to speed up
277+
// compilation.
278+
// Used to be just: return Int(m11 + m12 + m21 + m22 + tX + tY)
279+
let a: CGFloat = m11 + m12
280+
let b: CGFloat = m21 + m22
281+
let c: CGFloat = tX + tY
282+
return Int(a + b + c)
277283
}
278284

279285
public var description: String {

0 commit comments

Comments
 (0)