Skip to content

[ABI] [stdlib] Remove magnitude-based overload of abs(_:). #21037

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

Merged
merged 2 commits into from
Dec 6, 2018
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
15 changes: 4 additions & 11 deletions stdlib/public/core/Integers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,6 @@ extension SignedNumeric {
}
}


/// Returns the absolute value of the given number.
///
/// - Parameter x: A signed number.
/// - Returns: The absolute value of `x`.
@inlinable
public func abs<T : SignedNumeric>(_ x: T) -> T
where T.Magnitude == T {
return x.magnitude
}

/// Returns the absolute value of the given number.
///
/// The absolute value of `x` must be representable in the same type. In
Expand All @@ -344,6 +333,10 @@ public func abs<T : SignedNumeric>(_ x: T) -> T
/// - Returns: The absolute value of `x`.
@inlinable
public func abs<T : SignedNumeric & Comparable>(_ x: T) -> T {
if T.self == T.Magnitude.self {
return unsafeBitCast(x.magnitude, to: T.self)
}

return x < (0 as T) ? -x : x
}

Expand Down
12 changes: 0 additions & 12 deletions test/SourceKit/CodeComplete/complete_moduleimportdepth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ func test() {
// CHECK: key.modulename: "Swift"
// CHECK-NEXT: },

// CHECK-LABEL: key.name: "abs(:)",
// CHECK-NEXT: key.sourcetext: "abs(<#T##x: Comparable & SignedNumeric##Comparable & SignedNumeric#>)",
// CHECK-NEXT: key.description: "abs(x: Comparable & SignedNumeric)",
// CHECK-NEXT: key.typename: "Comparable & SignedNumeric",
// CHECK-NEXT: key.doc.brief: "Returns the absolute value of the given number.",
// CHECK-NEXT: key.context: source.codecompletion.context.othermodule,
// CHECK-NEXT: key.moduleimportdepth: 1,
// CHECK-NEXT: key.num_bytes_to_erase: 0,
// CHECK-NOT: key.modulename
// CHECK: key.modulename: "Swift"
// CHECK-NEXT: },

// FooHelper.FooHelperExplicit == 1
// CHECK-LABEL: key.name: "fooHelperExplicitFrameworkFunc1(:)",
// CHECK-NEXT: key.sourcetext: "fooHelperExplicitFrameworkFunc1(<#T##a: Int32##Int32#>)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Func _int64ToStringImpl(_:_:_:_:_:) has been removed
Func _uint64ToStringImpl(_:_:_:_:_:) has been removed
Func _typeByMangledName(_:substitutions:) has been removed
Func _withUninitializedString(_:) has been removed
Func abs(_:) has been removed

Func Optional.unwrappedOrError() has been removed
Struct _UnwrappingFailed has been removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Func UnsafeMutableRawPointer.copyBytes(from:count:) has been removed (deprecated
Func UnsafeMutableRawPointer.deallocate(bytes:alignedTo:) has been removed (deprecated)
Func UnsafeMutableRawPointer.initializeMemory(as:at:count:to:) has been removed (deprecated)
Func UnsafeMutableRawPointer.initializeMemory(as:from:) has been removed (deprecated)
Func abs(_:) has been removed
Protocol Collection has generic signature change from <Self : Sequence, Self.Index : Comparable, Self.Index == Self.Indices.Element, Self.Indices : Collection, Self.Indices == Self.Indices.SubSequence, Self.SubSequence : Collection, Self.Indices.Element == Self.Indices.Index, Self.Indices.Index == Self.SubSequence.Index, Self.SubSequence.Index == Self.Indices.Indices.Element, Self.Indices.Indices.Element == Self.Indices.Indices.Index, Self.Indices.Indices.Index == Self.SubSequence.Indices.Element, Self.SubSequence.Indices.Element == Self.SubSequence.Indices.Index, Self.SubSequence.Indices.Index == Self.SubSequence.Indices.Indices.Element, Self.SubSequence.Indices.Indices.Element == Self.SubSequence.Indices.Indices.Index> to <Self : Sequence, Self.Element == Self.SubSequence.Element, Self.Index : Comparable, Self.Index == Self.Indices.Element, Self.Indices : Collection, Self.Indices == Self.Indices.SubSequence, Self.SubSequence : Collection, Self.SubSequence == Self.SubSequence.SubSequence, Self.Indices.Element == Self.Indices.Index, Self.Indices.Index == Self.SubSequence.Index, Self.SubSequence.Index == Self.Indices.Indices.Element, Self.Indices.Indices.Element == Self.Indices.Indices.Index, Self.Indices.Indices.Index == Self.SubSequence.Indices.Element, Self.SubSequence.Indices.Element == Self.SubSequence.Indices.Index, Self.SubSequence.Indices.Index == Self.SubSequence.Indices.Indices.Element, Self.SubSequence.Indices.Indices.Element == Self.SubSequence.Indices.Indices.Index>
Protocol Sequence has generic signature change from <Self.Element == Self.Iterator.Element, Self.Iterator : IteratorProtocol, Self.SubSequence : Sequence, Self.SubSequence == Self.SubSequence.SubSequence, Self.Iterator.Element == Self.SubSequence.Element, Self.SubSequence.Element == Self.SubSequence.Iterator.Element> to <Self.Element == Self.Iterator.Element, Self.Iterator : IteratorProtocol>
Protocol _SequenceWrapper has been removed
Expand Down
11 changes: 11 additions & 0 deletions test/stdlib/FloatingPoint.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ extension FloatingPoint {
public var isQuietNaN: Bool { return isNaN && !isSignalingNaN }
}

func genericAbs<T: SignedNumeric & Comparable>(_ x: T) -> T {
return abs(x)
}


%for Self in ['Float', 'Double', 'Float80']:
% if Self == 'Float80':
#if !os(Windows) && (arch(i386) || arch(x86_64))
Expand Down Expand Up @@ -533,6 +538,12 @@ FloatingPoint.test("${Self}.significandWidth") {
expectEqual(0, ${Self}(2).significandWidth)
expectEqual(1, ${Self}(3).significandWidth)
}

FloatingPoint.test("${Self}.absNegativeZero") {
expectEqual(abs(${Self}(-0.0)), ${Self}(0.0))
expectEqual(genericAbs(${Self}(-0.0)), ${Self}(0.0))
}

% if Self == 'Float80':
#endif
% end
Expand Down
6 changes: 6 additions & 0 deletions test/stdlib/IntegerCompatibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ func sr5176(description: String = "unambiguous Int32.init(bitPattern:)") {
func sr6634(x: UnsafeBufferPointer<UInt8>) -> Int {
return x.lazy.filter { $0 > 127 || $0 == 0 }.count // should be unambiguous
}

// abs of an integer literal
func returnIntAbs() -> Int {
let x = abs(-8)
return x
}