Skip to content

Commit 87a8edb

Browse files
committed
Microoptimize _low and _high and remove else branch
Update UInt128.swift
1 parent f8f7d0c commit 87a8edb

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

stdlib/public/core/Int128.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ public struct Int128: Sendable {
3131
@available(SwiftStdlib 6.0, *)
3232
@_transparent
3333
public var _low: UInt64 {
34-
UInt64(truncatingIfNeeded: self)
34+
UInt64(Builtin.trunc_Int128_Int64(_value))
3535
}
3636

3737
@available(SwiftStdlib 6.0, *)
3838
@_transparent
3939
public var _high: Int64 {
40-
Int64(truncatingIfNeeded: self &>> 64)
40+
let shifted: Int128 = self &>> 64
41+
return Int64(Builtin.trunc_Int128_Int64(shifted._value))
4142
}
4243

4344
@available(SwiftStdlib 6.0, *)
@@ -450,7 +451,7 @@ extension Int128: BinaryInteger {
450451
#elseif _pointerBitWidth(_32)
451452
UInt(Builtin.trunc_Int128_Int32(_value))
452453
#else
453-
UInt(truncatingIfNeeded: self)
454+
#error("Unsupported platform")
454455
#endif
455456
}
456457
}

stdlib/public/core/UInt128.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ public struct UInt128: Sendable {
3131
@available(SwiftStdlib 6.0, *)
3232
@_transparent
3333
public var _low: UInt64 {
34-
UInt64(truncatingIfNeeded: self)
34+
UInt64(Builtin.trunc_Int128_Int64(_value))
3535
}
3636

3737
@available(SwiftStdlib 6.0, *)
38-
@_transparent @usableFromInline
38+
@_transparent
3939
public var _high: UInt64 {
40-
UInt64(truncatingIfNeeded: self &>> 64)
40+
let shifted: UInt128 = self &>> 64
41+
return UInt64(Builtin.trunc_Int128_Int64(shifted._value))
4142
}
4243

4344
@available(SwiftStdlib 6.0, *)
@@ -385,7 +386,7 @@ extension UInt128: BinaryInteger {
385386

386387
@available(SwiftStdlib 6.0, *)
387388
@_transparent
388-
init(_value: UInt128) {
389+
public init(_value: UInt128) {
389390
self._value = _value
390391
}
391392
}
@@ -442,7 +443,7 @@ extension UInt128: BinaryInteger {
442443
#elseif _pointerBitWidth(_32)
443444
UInt(Builtin.trunc_Int128_Int32(_value))
444445
#else
445-
UInt(truncatingIfNeeded: self)
446+
#error("Unsupported platform")
446447
#endif
447448
}
448449
}

0 commit comments

Comments
 (0)