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

[stdlib] Implement updates to SE-0104 #11203

Merged
merged 6 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions stdlib/private/StdlibUnicodeUnittest/Collation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,13 @@ public struct StringComparisonTest {

public func sortKey(forCollationElements ces: [UInt64]) -> ([UInt16], [UInt16], [UInt16]) {
func L1(_ ce: UInt64) -> UInt16 {
return UInt16(extendingOrTruncating: ce &>> 32)
return UInt16(truncatingIfNeeded: ce &>> 32)
}
func L2(_ ce: UInt64) -> UInt16 {
return UInt16(extendingOrTruncating: ce &>> 16)
return UInt16(truncatingIfNeeded: ce &>> 16)
}
func L3(_ ce: UInt64) -> UInt16 {
return UInt16(extendingOrTruncating: ce)
return UInt16(truncatingIfNeeded: ce)
}

var result1: [UInt16] = []
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/ASCII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension Unicode.ASCII : Unicode.Encoding {
_ source: Unicode.Scalar
) -> EncodedScalar? {
guard source.value < (1&<<7) else { return nil }
return EncodedScalar(UInt8(extendingOrTruncating: source.value))
return EncodedScalar(UInt8(truncatingIfNeeded: source.value))
}

@inline(__always)
Expand Down Expand Up @@ -80,7 +80,7 @@ extension Unicode.ASCII.Parser : Unicode.Parser {
where I.Element == Encoding.CodeUnit {
let n = input.next()
if _fastPath(n != nil), let x = n {
guard _fastPath(Int8(extendingOrTruncating: x) >= 0)
guard _fastPath(Int8(truncatingIfNeeded: x) >= 0)
else { return .error(length: 1) }
return .valid(Unicode.ASCII.EncodedScalar(x))
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/ArrayBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal struct _ArrayBody {
_storage = _SwiftArrayBodyStorage(
count: count,
_capacityAndFlags:
(UInt(extendingOrTruncating: capacity) &<< 1) |
(UInt(truncatingIfNeeded: capacity) &<< 1) |
(elementTypeIsBridgedVerbatim ? 1 : 0))
}

Expand Down
3 changes: 1 addition & 2 deletions stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ func _canBeClass<T>(_: T.Type) -> Int8 {
/// - Value conversion from one integer type to another. Use the destination
/// type's initializer or the `numericCast(_:)` function.
/// - Bitwise conversion from one integer type to another. Use the destination
/// type's `init(extendingOrTruncating:)` or `init(bitPattern:)`
/// initializer.
/// type's `init(truncatingIfNeeded:)` or `init(bitPattern:)` initializer.
/// - Conversion from a pointer to an integer value with the bit pattern of the
/// pointer's address in memory, or vice versa. Use the `init(bitPattern:)`
/// initializer for the destination type.
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public struct Character :
shift += 16
}
}
guard _fastPath(Int64(extendingOrTruncating: bits) >= 0) else {
guard _fastPath(Int64(truncatingIfNeeded: bits) >= 0) else {
break FastPath
}
_representation = .smallUTF16(Builtin.trunc_Int64_Int63(bits._value))
Expand Down Expand Up @@ -224,7 +224,7 @@ public struct Character :

if _fastPath(s._core.count <= 4) {
let b = _UIntBuffer<UInt64, Unicode.UTF16.CodeUnit>(s._core)
if _fastPath(Int64(extendingOrTruncating: b._storage) >= 0) {
if _fastPath(Int64(truncatingIfNeeded: b._storage) >= 0) {
_representation = .smallUTF16(
Builtin.trunc_Int64_Int63(b._storage._value))
return
Expand Down Expand Up @@ -300,7 +300,7 @@ extension Character {
return _UIntBuffer<UInt64, Unicode.UTF16.CodeUnit>(
_storage: bits,
_bitCount: UInt8(
extendingOrTruncating: 16 * Swift.max(1, (minBitWidth + 15) / 16))
truncatingIfNeeded: 16 * Swift.max(1, (minBitWidth + 15) / 16))
)
}

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/CharacterUnicodeScalars.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ extension Character.UnicodeScalarView : Collection {
case .valid(let s):
return Index(
_encodedOffset: startOfNextScalar, _scalar: s,
_stride: UInt8(extendingOrTruncating: s.count))
_stride: UInt8(truncatingIfNeeded: s.count))
case .error:
return Index(
_encodedOffset: startOfNextScalar,
Expand Down Expand Up @@ -138,7 +138,7 @@ extension Character.UnicodeScalarView : BidirectionalCollection {
case .valid(let s):
return Index(
_encodedOffset: i._encodedOffset - s.count, _scalar: s,
_stride: UInt8(extendingOrTruncating: s.count))
_stride: UInt8(truncatingIfNeeded: s.count))
case .error:
return Index(
_encodedOffset: i._encodedOffset - 1,
Expand Down
63 changes: 29 additions & 34 deletions stdlib/public/core/DoubleWidth.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
}
}

public init<T : FloatingPoint>(_ source: T) {
public init<T : BinaryFloatingPoint>(_ source: T) {
fatalError()
}

public init?<T : FloatingPoint>(exactly source: T) {
public init?<T : BinaryFloatingPoint>(exactly source: T) {
fatalError()
}

public init<T : BinaryFloatingPoint>(_ source: T)
where T.RawSignificand : FixedWidthInteger
{
Expand Down Expand Up @@ -269,27 +269,23 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
% for (operator, name) in [('+', 'adding'), ('-', 'subtracting')]:
% highAffectedByLowOverflow = 'Base.max' if operator == '+' else 'Base.min'
public func ${name}ReportingOverflow(_ rhs: DoubleWidth)
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
-> (partialValue: DoubleWidth, overflow: Bool) {
let (low, lowOverflow) =
_storage.low.${name}ReportingOverflow(rhs._storage.low)
let (high, highOverflow) =
_storage.high.${name}ReportingOverflow(rhs._storage.high)
let isLowOverflow = lowOverflow == .overflow
let result = (high &${operator} (isLowOverflow ? 1 : 0), low)
let overflow = ArithmeticOverflow(
highOverflow == .overflow ||
high == ${highAffectedByLowOverflow} && isLowOverflow
)
return (partialValue: DoubleWidth(result),
overflow: overflow)
let result = (high &${operator} (lowOverflow ? 1 : 0), low)
let overflow = highOverflow ||
high == ${highAffectedByLowOverflow} && lowOverflow
return (partialValue: DoubleWidth(result), overflow: overflow)
}
% end

public func multipliedReportingOverflow(
by rhs: DoubleWidth
) -> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
) -> (partialValue: DoubleWidth, overflow: Bool) {
let (carry, product) = multipliedFullWidth(by: rhs)
let result = DoubleWidth(extendingOrTruncating: product)
let result = DoubleWidth(truncatingIfNeeded: product)

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

return (result, ArithmeticOverflow(didCarry || hadPositiveOverflow))
return (result, didCarry || hadPositiveOverflow)
}

public func quotientAndRemainder(dividingBy other: DoubleWidth)
Expand Down Expand Up @@ -350,25 +346,25 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
}

public func dividedReportingOverflow(by other: DoubleWidth)
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
-> (partialValue: DoubleWidth, overflow: Bool) {
if other == (0 as DoubleWidth) ||
(DoubleWidth.isSigned && other == (-1 as Int) && self == .min)
{
return (self, .overflow)
return (self, true)
}

return (quotientAndRemainder(dividingBy: other).quotient, .none)
return (quotientAndRemainder(dividingBy: other).quotient, false)
}

public func remainderReportingOverflow(dividingBy other: DoubleWidth)
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
-> (partialValue: DoubleWidth, overflow: Bool) {
if other == 0 ||
(DoubleWidth.isSigned && other == -1 && self == .min)
{
return (self, .overflow)
return (self, true)
}

return (quotientAndRemainder(dividingBy: other).remainder, .none)
return (quotientAndRemainder(dividingBy: other).remainder, false)
}

public func multipliedFullWidth(by other: DoubleWidth)
Expand All @@ -384,8 +380,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
func sum(_ x: Low, _ y: Low, _ z: Low) -> (partial: Low, carry: Low) {
let (sum1, overflow1) = x.addingReportingOverflow(y)
let (sum2, overflow2) = sum1.addingReportingOverflow(z)
let carry: Low = (overflow1 == .overflow ? 1 : 0) +
(overflow2 == .overflow ? 1 : 0)
let carry: Low = (overflow1 ? 1 : 0) + (overflow2 ? 1 : 0)
return (sum2, carry)
}

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

if isNegative {
let (lowComplement, overflow) = (~low).addingReportingOverflow(1)
return (~high + (overflow == .overflow ? 1 : 0), lowComplement)
return (~high + (overflow ? 1 : 0), lowComplement)
} else {
return (high, low)
}
Expand Down Expand Up @@ -447,7 +442,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :

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

Expand All @@ -472,7 +467,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
if rhs._storage.low == Base.bitWidth {
lhs = DoubleWidth((
lhs < (0 as DoubleWidth) ? ~0 : 0,
Low(extendingOrTruncating: lhs._storage.high)
Low(truncatingIfNeeded: lhs._storage.high)
))
return
}
Expand All @@ -485,10 +480,10 @@ public struct DoubleWidth<Base : FixedWidthInteger> :

lhs._storage.high <<= High(rhs._storage.low)
if Base.bitWidth > rhs._storage.low {
lhs._storage.high |= High(extendingOrTruncating: lhs._storage.low >>
lhs._storage.high |= High(truncatingIfNeeded: lhs._storage.low >>
(numericCast(Base.bitWidth) - rhs._storage.low))
} else {
lhs._storage.high |= High(extendingOrTruncating: lhs._storage.low <<
lhs._storage.high |= High(truncatingIfNeeded: lhs._storage.low <<
(rhs._storage.low - numericCast(Base.bitWidth)))
}
lhs._storage.low <<= rhs._storage.low
Expand All @@ -500,14 +495,14 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
lhs._storage.low >>= rhs._storage.low
if Base.bitWidth > rhs._storage.low {
lhs._storage.low |= Low(
extendingOrTruncating:
truncatingIfNeeded:
lhs._storage.high << (numericCast(Base.bitWidth) - rhs._storage.low))
} else {
lhs._storage.low |= Low(
extendingOrTruncating: lhs._storage.high >>
truncatingIfNeeded: lhs._storage.high >>
(rhs._storage.low - numericCast(Base.bitWidth)))
}
lhs._storage.high >>= High(extendingOrTruncating: rhs._storage.low)
lhs._storage.high >>= High(truncatingIfNeeded: rhs._storage.low)
}

%{
Expand Down Expand Up @@ -535,7 +530,7 @@ binaryOperators = [
lhs: inout DoubleWidth, rhs: DoubleWidth
) {
let (result, overflow) = lhs.${name}ReportingOverflow(${argumentLabel}rhs)
_precondition(overflow == .none, "Overflow in ${operator}=")
_precondition(!overflow, "Overflow in ${operator}=")
lhs = result
}
% end
Expand Down Expand Up @@ -578,8 +573,8 @@ binaryOperators = [

@_transparent
public var byteSwapped: DoubleWidth {
return DoubleWidth((High(extendingOrTruncating: low.byteSwapped),
Low(extendingOrTruncating: high.byteSwapped)))
return DoubleWidth((High(truncatingIfNeeded: low.byteSwapped),
Low(truncatingIfNeeded: high.byteSwapped)))
}
}

Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/core/FloatingPointTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -754,11 +754,11 @@ extension ${Self} : Hashable {
%if bits <= word_bits:
return Int(bitPattern: UInt(bitPattern))
%elif bits == 64: # Double -> 32-bit Int
return Int(extendingOrTruncating: bitPattern &>> 32) ^
Int(extendingOrTruncating: bitPattern)
return Int(truncatingIfNeeded: bitPattern &>> 32) ^
Int(truncatingIfNeeded: bitPattern)
%elif word_bits == 32: # Float80 -> 32-bit Int
return Int(extendingOrTruncating: significandBitPattern &>> 32) ^
Int(extendingOrTruncating: significandBitPattern) ^
return Int(truncatingIfNeeded: significandBitPattern &>> 32) ^
Int(truncatingIfNeeded: significandBitPattern) ^
Int(_representation.signAndExponent)
%else: # Float80 -> 64-bit Int
return Int(bitPattern: UInt(significandBitPattern)) ^
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/core/IntegerParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ internal func _asciiDigit<CodeUnit : UnsignedInteger, Result : BinaryInteger>(
let lower = _ascii16("a")..._ascii16("z")
let upper = _ascii16("A")..._ascii16("Z")

let u = UInt16(extendingOrTruncating: u_)
let u = UInt16(truncatingIfNeeded: u_)
let d: UInt16
if _fastPath(digit ~= u) { d = u &- digit.lowerBound }
else if _fastPath(upper ~= u) { d = u &- upper.lowerBound &+ 10 }
else if _fastPath(lower ~= u) { d = u &- lower.lowerBound &+ 10 }
else { return nil }
guard _fastPath(d < radix) else { return nil }
return Result(extendingOrTruncating: d)
return Result(truncatingIfNeeded: d)
}

@inline(__always)
Expand All @@ -40,7 +40,7 @@ where Rest.Element : UnsignedInteger {
if !positive {
let (result0, overflow0)
= (0 as Result).subtractingReportingOverflow(result)
guard _fastPath(overflow0 == .none) else { return nil }
guard _fastPath(!overflow0) else { return nil }
result = result0
}

Expand All @@ -51,7 +51,7 @@ where Rest.Element : UnsignedInteger {
let (result2, overflow2) = positive
? result1.addingReportingOverflow(d)
: result1.subtractingReportingOverflow(d)
guard _fastPath(overflow1 == .none && overflow2 == .none)
guard _fastPath(!overflow1 && !overflow2)
else { return nil }
result = result2
}
Expand Down
Loading