Skip to content

Commit 00d2acd

Browse files
authored
Merge pull request #21037 from DougGregor/flatten-abs
[ABI] [stdlib] Remove magnitude-based overload of abs(_:).
2 parents b0d5b9b + ab75577 commit 00d2acd

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

stdlib/public/core/Integers.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,6 @@ extension SignedNumeric {
318318
}
319319
}
320320

321-
322-
/// Returns the absolute value of the given number.
323-
///
324-
/// - Parameter x: A signed number.
325-
/// - Returns: The absolute value of `x`.
326-
@inlinable
327-
public func abs<T : SignedNumeric>(_ x: T) -> T
328-
where T.Magnitude == T {
329-
return x.magnitude
330-
}
331-
332321
/// Returns the absolute value of the given number.
333322
///
334323
/// The absolute value of `x` must be representable in the same type. In
@@ -344,6 +333,10 @@ public func abs<T : SignedNumeric>(_ x: T) -> T
344333
/// - Returns: The absolute value of `x`.
345334
@inlinable
346335
public func abs<T : SignedNumeric & Comparable>(_ x: T) -> T {
336+
if T.self == T.Magnitude.self {
337+
return unsafeBitCast(x.magnitude, to: T.self)
338+
}
339+
347340
return x < (0 as T) ? -x : x
348341
}
349342

test/SourceKit/CodeComplete/complete_moduleimportdepth.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ func test() {
2323
// CHECK: key.modulename: "Swift"
2424
// CHECK-NEXT: },
2525

26-
// CHECK-LABEL: key.name: "abs(:)",
27-
// CHECK-NEXT: key.sourcetext: "abs(<#T##x: Comparable & SignedNumeric##Comparable & SignedNumeric#>)",
28-
// CHECK-NEXT: key.description: "abs(x: Comparable & SignedNumeric)",
29-
// CHECK-NEXT: key.typename: "Comparable & SignedNumeric",
30-
// CHECK-NEXT: key.doc.brief: "Returns the absolute value of the given number.",
31-
// CHECK-NEXT: key.context: source.codecompletion.context.othermodule,
32-
// CHECK-NEXT: key.moduleimportdepth: 1,
33-
// CHECK-NEXT: key.num_bytes_to_erase: 0,
34-
// CHECK-NOT: key.modulename
35-
// CHECK: key.modulename: "Swift"
36-
// CHECK-NEXT: },
37-
3826
// FooHelper.FooHelperExplicit == 1
3927
// CHECK-LABEL: key.name: "fooHelperExplicitFrameworkFunc1(:)",
4028
// CHECK-NEXT: key.sourcetext: "fooHelperExplicitFrameworkFunc1(<#T##a: Int32##Int32#>)",

test/api-digester/Outputs/stability-stdlib-abi.swift.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Func _int64ToStringImpl(_:_:_:_:_:) has been removed
1717
Func _uint64ToStringImpl(_:_:_:_:_:) has been removed
1818
Func _typeByMangledName(_:substitutions:) has been removed
1919
Func _withUninitializedString(_:) has been removed
20+
Func abs(_:) has been removed
2021

2122
Func Optional.unwrappedOrError() has been removed
2223
Struct _UnwrappingFailed has been removed

test/api-digester/Outputs/stability-stdlib-source.swift.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Func UnsafeMutableRawPointer.copyBytes(from:count:) has been removed (deprecated
9797
Func UnsafeMutableRawPointer.deallocate(bytes:alignedTo:) has been removed (deprecated)
9898
Func UnsafeMutableRawPointer.initializeMemory(as:at:count:to:) has been removed (deprecated)
9999
Func UnsafeMutableRawPointer.initializeMemory(as:from:) has been removed (deprecated)
100+
Func abs(_:) has been removed
100101
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>
101102
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>
102103
Protocol _SequenceWrapper has been removed

test/stdlib/FloatingPoint.swift.gyb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ extension FloatingPoint {
413413
public var isQuietNaN: Bool { return isNaN && !isSignalingNaN }
414414
}
415415

416+
func genericAbs<T: SignedNumeric & Comparable>(_ x: T) -> T {
417+
return abs(x)
418+
}
419+
420+
416421
%for Self in ['Float', 'Double', 'Float80']:
417422
% if Self == 'Float80':
418423
#if !os(Windows) && (arch(i386) || arch(x86_64))
@@ -533,6 +538,12 @@ FloatingPoint.test("${Self}.significandWidth") {
533538
expectEqual(0, ${Self}(2).significandWidth)
534539
expectEqual(1, ${Self}(3).significandWidth)
535540
}
541+
542+
FloatingPoint.test("${Self}.absNegativeZero") {
543+
expectEqual(abs(${Self}(-0.0)), ${Self}(0.0))
544+
expectEqual(genericAbs(${Self}(-0.0)), ${Self}(0.0))
545+
}
546+
536547
% if Self == 'Float80':
537548
#endif
538549
% end

test/stdlib/IntegerCompatibility.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,9 @@ func sr5176(description: String = "unambiguous Int32.init(bitPattern:)") {
5555
func sr6634(x: UnsafeBufferPointer<UInt8>) -> Int {
5656
return x.lazy.filter { $0 > 127 || $0 == 0 }.count // should be unambiguous
5757
}
58+
59+
// abs of an integer literal
60+
func returnIntAbs() -> Int {
61+
let x = abs(-8)
62+
return x
63+
}

0 commit comments

Comments
 (0)