Merge pull request #21037 from DougGregor/flatten-abs
[ABI] [stdlib] Remove magnitude-based overload of abs(_:).
diff --git a/stdlib/public/core/Integers.swift b/stdlib/public/core/Integers.swift
index bdf94aa..fdaeeb1 100644
--- a/stdlib/public/core/Integers.swift
+++ b/stdlib/public/core/Integers.swift
@@ -318,17 +318,6 @@
}
}
-
-/// 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
@@ -344,6 +333,10 @@
/// - 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
}
diff --git a/test/SourceKit/CodeComplete/complete_moduleimportdepth.swift b/test/SourceKit/CodeComplete/complete_moduleimportdepth.swift
index d30b140..808ab0d 100644
--- a/test/SourceKit/CodeComplete/complete_moduleimportdepth.swift
+++ b/test/SourceKit/CodeComplete/complete_moduleimportdepth.swift
@@ -23,18 +23,6 @@
// 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#>)",
diff --git a/test/api-digester/Outputs/stability-stdlib-abi.swift.expected b/test/api-digester/Outputs/stability-stdlib-abi.swift.expected
index cb7a2a8..1694783 100644
--- a/test/api-digester/Outputs/stability-stdlib-abi.swift.expected
+++ b/test/api-digester/Outputs/stability-stdlib-abi.swift.expected
@@ -17,6 +17,7 @@
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
diff --git a/test/api-digester/Outputs/stability-stdlib-source.swift.expected b/test/api-digester/Outputs/stability-stdlib-source.swift.expected
index c729fcb..5a618cf 100644
--- a/test/api-digester/Outputs/stability-stdlib-source.swift.expected
+++ b/test/api-digester/Outputs/stability-stdlib-source.swift.expected
@@ -97,6 +97,7 @@
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
diff --git a/test/stdlib/FloatingPoint.swift.gyb b/test/stdlib/FloatingPoint.swift.gyb
index 729f742..79e9715 100644
--- a/test/stdlib/FloatingPoint.swift.gyb
+++ b/test/stdlib/FloatingPoint.swift.gyb
@@ -413,6 +413,11 @@
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))
@@ -533,6 +538,12 @@
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
diff --git a/test/stdlib/IntegerCompatibility.swift b/test/stdlib/IntegerCompatibility.swift
index db935bc..aeb63bb 100644
--- a/test/stdlib/IntegerCompatibility.swift
+++ b/test/stdlib/IntegerCompatibility.swift
@@ -55,3 +55,9 @@
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
+}