Skip to content

Commit 3f993ef

Browse files
authored
Merge pull request #11203 from moiseev/integers-revised
[stdlib] Implement updates to SE-0104
2 parents cbdf0ff + 5563ed1 commit 3f993ef

28 files changed

+240
-295
lines changed

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(extendingOrTruncating: ce &>> 32)
341+
return UInt16(truncatingIfNeeded: ce &>> 32)
342342
}
343343
func L2(_ ce: UInt64) -> UInt16 {
344-
return UInt16(extendingOrTruncating: ce &>> 16)
344+
return UInt16(truncatingIfNeeded: ce &>> 16)
345345
}
346346
func L3(_ ce: UInt64) -> UInt16 {
347-
return UInt16(extendingOrTruncating: ce)
347+
return UInt16(truncatingIfNeeded: ce)
348348
}
349349

350350
var result1: [UInt16] = []

stdlib/public/core/ASCII.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension Unicode.ASCII : Unicode.Encoding {
4141
_ source: Unicode.Scalar
4242
) -> EncodedScalar? {
4343
guard source.value < (1&<<7) else { return nil }
44-
return EncodedScalar(UInt8(extendingOrTruncating: source.value))
44+
return EncodedScalar(UInt8(truncatingIfNeeded: source.value))
4545
}
4646

4747
@inline(__always)
@@ -80,7 +80,7 @@ extension Unicode.ASCII.Parser : Unicode.Parser {
8080
where I.Element == Encoding.CodeUnit {
8181
let n = input.next()
8282
if _fastPath(n != nil), let x = n {
83-
guard _fastPath(Int8(extendingOrTruncating: x) >= 0)
83+
guard _fastPath(Int8(truncatingIfNeeded: x) >= 0)
8484
else { return .error(length: 1) }
8585
return .valid(Unicode.ASCII.EncodedScalar(x))
8686
}

stdlib/public/core/ArrayBody.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal struct _ArrayBody {
3232
_storage = _SwiftArrayBodyStorage(
3333
count: count,
3434
_capacityAndFlags:
35-
(UInt(extendingOrTruncating: capacity) &<< 1) |
35+
(UInt(truncatingIfNeeded: capacity) &<< 1) |
3636
(elementTypeIsBridgedVerbatim ? 1 : 0))
3737
}
3838

stdlib/public/core/Builtin.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ func _canBeClass<T>(_: T.Type) -> Int8 {
9090
/// - Value conversion from one integer type to another. Use the destination
9191
/// type's initializer or the `numericCast(_:)` function.
9292
/// - Bitwise conversion from one integer type to another. Use the destination
93-
/// type's `init(extendingOrTruncating:)` or `init(bitPattern:)`
94-
/// initializer.
93+
/// type's `init(truncatingIfNeeded:)` or `init(bitPattern:)` initializer.
9594
/// - Conversion from a pointer to an integer value with the bit pattern of the
9695
/// pointer's address in memory, or vice versa. Use the `init(bitPattern:)`
9796
/// initializer for the destination type.

stdlib/public/core/Character.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public struct Character :
130130
shift += 16
131131
}
132132
}
133-
guard _fastPath(Int64(extendingOrTruncating: bits) >= 0) else {
133+
guard _fastPath(Int64(truncatingIfNeeded: bits) >= 0) else {
134134
break FastPath
135135
}
136136
_representation = .smallUTF16(Builtin.trunc_Int64_Int63(bits._value))
@@ -224,7 +224,7 @@ public struct Character :
224224

225225
if _fastPath(s._core.count <= 4) {
226226
let b = _UIntBuffer<UInt64, Unicode.UTF16.CodeUnit>(s._core)
227-
if _fastPath(Int64(extendingOrTruncating: b._storage) >= 0) {
227+
if _fastPath(Int64(truncatingIfNeeded: b._storage) >= 0) {
228228
_representation = .smallUTF16(
229229
Builtin.trunc_Int64_Int63(b._storage._value))
230230
return
@@ -300,7 +300,7 @@ extension Character {
300300
return _UIntBuffer<UInt64, Unicode.UTF16.CodeUnit>(
301301
_storage: bits,
302302
_bitCount: UInt8(
303-
extendingOrTruncating: 16 * Swift.max(1, (minBitWidth + 15) / 16))
303+
truncatingIfNeeded: 16 * Swift.max(1, (minBitWidth + 15) / 16))
304304
)
305305
}
306306

stdlib/public/core/CharacterUnicodeScalars.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ extension Character.UnicodeScalarView : Collection {
101101
case .valid(let s):
102102
return Index(
103103
_encodedOffset: startOfNextScalar, _scalar: s,
104-
_stride: UInt8(extendingOrTruncating: s.count))
104+
_stride: UInt8(truncatingIfNeeded: s.count))
105105
case .error:
106106
return Index(
107107
_encodedOffset: startOfNextScalar,
@@ -138,7 +138,7 @@ extension Character.UnicodeScalarView : BidirectionalCollection {
138138
case .valid(let s):
139139
return Index(
140140
_encodedOffset: i._encodedOffset - s.count, _scalar: s,
141-
_stride: UInt8(extendingOrTruncating: s.count))
141+
_stride: UInt8(truncatingIfNeeded: s.count))
142142
case .error:
143143
return Index(
144144
_encodedOffset: i._encodedOffset - 1,

stdlib/public/core/DoubleWidth.swift.gyb

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
9797
}
9898
}
9999

100-
public init<T : FloatingPoint>(_ source: T) {
100+
public init<T : BinaryFloatingPoint>(_ source: T) {
101101
fatalError()
102102
}
103103

104-
public init?<T : FloatingPoint>(exactly source: T) {
104+
public init?<T : BinaryFloatingPoint>(exactly source: T) {
105105
fatalError()
106106
}
107-
107+
108108
public init<T : BinaryFloatingPoint>(_ source: T)
109109
where T.RawSignificand : FixedWidthInteger
110110
{
@@ -269,27 +269,23 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
269269
% for (operator, name) in [('+', 'adding'), ('-', 'subtracting')]:
270270
% highAffectedByLowOverflow = 'Base.max' if operator == '+' else 'Base.min'
271271
public func ${name}ReportingOverflow(_ rhs: DoubleWidth)
272-
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
272+
-> (partialValue: DoubleWidth, overflow: Bool) {
273273
let (low, lowOverflow) =
274274
_storage.low.${name}ReportingOverflow(rhs._storage.low)
275275
let (high, highOverflow) =
276276
_storage.high.${name}ReportingOverflow(rhs._storage.high)
277-
let isLowOverflow = lowOverflow == .overflow
278-
let result = (high &${operator} (isLowOverflow ? 1 : 0), low)
279-
let overflow = ArithmeticOverflow(
280-
highOverflow == .overflow ||
281-
high == ${highAffectedByLowOverflow} && isLowOverflow
282-
)
283-
return (partialValue: DoubleWidth(result),
284-
overflow: overflow)
277+
let result = (high &${operator} (lowOverflow ? 1 : 0), low)
278+
let overflow = highOverflow ||
279+
high == ${highAffectedByLowOverflow} && lowOverflow
280+
return (partialValue: DoubleWidth(result), overflow: overflow)
285281
}
286282
% end
287283

288284
public func multipliedReportingOverflow(
289285
by rhs: DoubleWidth
290-
) -> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
286+
) -> (partialValue: DoubleWidth, overflow: Bool) {
291287
let (carry, product) = multipliedFullWidth(by: rhs)
292-
let result = DoubleWidth(extendingOrTruncating: product)
288+
let result = DoubleWidth(truncatingIfNeeded: product)
293289

294290
let isNegative = (self < (0 as DoubleWidth)) != (rhs < (0 as DoubleWidth))
295291
let didCarry = isNegative
@@ -298,7 +294,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
298294
let hadPositiveOverflow = !isNegative &&
299295
DoubleWidth.isSigned && product.leadingZeroBitCount == 0
300296

301-
return (result, ArithmeticOverflow(didCarry || hadPositiveOverflow))
297+
return (result, didCarry || hadPositiveOverflow)
302298
}
303299

304300
public func quotientAndRemainder(dividingBy other: DoubleWidth)
@@ -350,25 +346,25 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
350346
}
351347

352348
public func dividedReportingOverflow(by other: DoubleWidth)
353-
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
349+
-> (partialValue: DoubleWidth, overflow: Bool) {
354350
if other == (0 as DoubleWidth) ||
355351
(DoubleWidth.isSigned && other == (-1 as Int) && self == .min)
356352
{
357-
return (self, .overflow)
353+
return (self, true)
358354
}
359355

360-
return (quotientAndRemainder(dividingBy: other).quotient, .none)
356+
return (quotientAndRemainder(dividingBy: other).quotient, false)
361357
}
362358

363359
public func remainderReportingOverflow(dividingBy other: DoubleWidth)
364-
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
360+
-> (partialValue: DoubleWidth, overflow: Bool) {
365361
if other == 0 ||
366362
(DoubleWidth.isSigned && other == -1 && self == .min)
367363
{
368-
return (self, .overflow)
364+
return (self, true)
369365
}
370366

371-
return (quotientAndRemainder(dividingBy: other).remainder, .none)
367+
return (quotientAndRemainder(dividingBy: other).remainder, false)
372368
}
373369

374370
public func multipliedFullWidth(by other: DoubleWidth)
@@ -384,8 +380,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
384380
func sum(_ x: Low, _ y: Low, _ z: Low) -> (partial: Low, carry: Low) {
385381
let (sum1, overflow1) = x.addingReportingOverflow(y)
386382
let (sum2, overflow2) = sum1.addingReportingOverflow(z)
387-
let carry: Low = (overflow1 == .overflow ? 1 : 0) +
388-
(overflow2 == .overflow ? 1 : 0)
383+
let carry: Low = (overflow1 ? 1 : 0) + (overflow2 ? 1 : 0)
389384
return (sum2, carry)
390385
}
391386

@@ -406,7 +401,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
406401

407402
if isNegative {
408403
let (lowComplement, overflow) = (~low).addingReportingOverflow(1)
409-
return (~high + (overflow == .overflow ? 1 : 0), lowComplement)
404+
return (~high + (overflow ? 1 : 0), lowComplement)
410405
} else {
411406
return (high, low)
412407
}
@@ -447,7 +442,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
447442

448443
// Shift is exactly the width of `Base`, so low -> high.
449444
if rhs._storage.low == Base.bitWidth {
450-
lhs = DoubleWidth((High(extendingOrTruncating: lhs._storage.low), 0))
445+
lhs = DoubleWidth((High(truncatingIfNeeded: lhs._storage.low), 0))
451446
return
452447
}
453448

@@ -472,7 +467,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
472467
if rhs._storage.low == Base.bitWidth {
473468
lhs = DoubleWidth((
474469
lhs < (0 as DoubleWidth) ? ~0 : 0,
475-
Low(extendingOrTruncating: lhs._storage.high)
470+
Low(truncatingIfNeeded: lhs._storage.high)
476471
))
477472
return
478473
}
@@ -485,10 +480,10 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
485480

486481
lhs._storage.high <<= High(rhs._storage.low)
487482
if Base.bitWidth > rhs._storage.low {
488-
lhs._storage.high |= High(extendingOrTruncating: lhs._storage.low >>
483+
lhs._storage.high |= High(truncatingIfNeeded: lhs._storage.low >>
489484
(numericCast(Base.bitWidth) - rhs._storage.low))
490485
} else {
491-
lhs._storage.high |= High(extendingOrTruncating: lhs._storage.low <<
486+
lhs._storage.high |= High(truncatingIfNeeded: lhs._storage.low <<
492487
(rhs._storage.low - numericCast(Base.bitWidth)))
493488
}
494489
lhs._storage.low <<= rhs._storage.low
@@ -500,14 +495,14 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
500495
lhs._storage.low >>= rhs._storage.low
501496
if Base.bitWidth > rhs._storage.low {
502497
lhs._storage.low |= Low(
503-
extendingOrTruncating:
498+
truncatingIfNeeded:
504499
lhs._storage.high << (numericCast(Base.bitWidth) - rhs._storage.low))
505500
} else {
506501
lhs._storage.low |= Low(
507-
extendingOrTruncating: lhs._storage.high >>
502+
truncatingIfNeeded: lhs._storage.high >>
508503
(rhs._storage.low - numericCast(Base.bitWidth)))
509504
}
510-
lhs._storage.high >>= High(extendingOrTruncating: rhs._storage.low)
505+
lhs._storage.high >>= High(truncatingIfNeeded: rhs._storage.low)
511506
}
512507

513508
%{
@@ -535,7 +530,7 @@ binaryOperators = [
535530
lhs: inout DoubleWidth, rhs: DoubleWidth
536531
) {
537532
let (result, overflow) = lhs.${name}ReportingOverflow(${argumentLabel}rhs)
538-
_precondition(overflow == .none, "Overflow in ${operator}=")
533+
_precondition(!overflow, "Overflow in ${operator}=")
539534
lhs = result
540535
}
541536
% end
@@ -578,8 +573,8 @@ binaryOperators = [
578573

579574
@_transparent
580575
public var byteSwapped: DoubleWidth {
581-
return DoubleWidth((High(extendingOrTruncating: low.byteSwapped),
582-
Low(extendingOrTruncating: high.byteSwapped)))
576+
return DoubleWidth((High(truncatingIfNeeded: low.byteSwapped),
577+
Low(truncatingIfNeeded: high.byteSwapped)))
583578
}
584579
}
585580

stdlib/public/core/FloatingPointTypes.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,11 +754,11 @@ extension ${Self} : Hashable {
754754
%if bits <= word_bits:
755755
return Int(bitPattern: UInt(bitPattern))
756756
%elif bits == 64: # Double -> 32-bit Int
757-
return Int(extendingOrTruncating: bitPattern &>> 32) ^
758-
Int(extendingOrTruncating: bitPattern)
757+
return Int(truncatingIfNeeded: bitPattern &>> 32) ^
758+
Int(truncatingIfNeeded: bitPattern)
759759
%elif word_bits == 32: # Float80 -> 32-bit Int
760-
return Int(extendingOrTruncating: significandBitPattern &>> 32) ^
761-
Int(extendingOrTruncating: significandBitPattern) ^
760+
return Int(truncatingIfNeeded: significandBitPattern &>> 32) ^
761+
Int(truncatingIfNeeded: significandBitPattern) ^
762762
Int(_representation.signAndExponent)
763763
%else: # Float80 -> 64-bit Int
764764
return Int(bitPattern: UInt(significandBitPattern)) ^

stdlib/public/core/IntegerParsing.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ internal func _asciiDigit<CodeUnit : UnsignedInteger, Result : BinaryInteger>(
1818
let lower = _ascii16("a")..._ascii16("z")
1919
let upper = _ascii16("A")..._ascii16("Z")
2020

21-
let u = UInt16(extendingOrTruncating: u_)
21+
let u = UInt16(truncatingIfNeeded: u_)
2222
let d: UInt16
2323
if _fastPath(digit ~= u) { d = u &- digit.lowerBound }
2424
else if _fastPath(upper ~= u) { d = u &- upper.lowerBound &+ 10 }
2525
else if _fastPath(lower ~= u) { d = u &- lower.lowerBound &+ 10 }
2626
else { return nil }
2727
guard _fastPath(d < radix) else { return nil }
28-
return Result(extendingOrTruncating: d)
28+
return Result(truncatingIfNeeded: d)
2929
}
3030

3131
@inline(__always)
@@ -40,7 +40,7 @@ where Rest.Element : UnsignedInteger {
4040
if !positive {
4141
let (result0, overflow0)
4242
= (0 as Result).subtractingReportingOverflow(result)
43-
guard _fastPath(overflow0 == .none) else { return nil }
43+
guard _fastPath(!overflow0) else { return nil }
4444
result = result0
4545
}
4646

@@ -51,7 +51,7 @@ where Rest.Element : UnsignedInteger {
5151
let (result2, overflow2) = positive
5252
? result1.addingReportingOverflow(d)
5353
: result1.subtractingReportingOverflow(d)
54-
guard _fastPath(overflow1 == .none && overflow2 == .none)
54+
guard _fastPath(!overflow1 && !overflow2)
5555
else { return nil }
5656
result = result2
5757
}

0 commit comments

Comments
 (0)