Skip to content

Commit ea10e15

Browse files
committed
[stdlib] Duration: use optimized methods for multiplication/division by constants
1 parent 524f499 commit ea10e15

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

stdlib/public/core/Duration.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,18 @@ public struct Duration: Sendable {
8686
internal var _attoseconds: _Int128 {
8787
_Int128(high: _high, low: _low)
8888
}
89+
}
8990

91+
@available(SwiftStdlib 5.7, *)
92+
extension Duration {
9093
/// The composite components of the `Duration`.
9194
///
9295
/// This is intended for facilitating conversions to existing time types. The
9396
/// attoseconds value will not exceed 1e18 or be lower than -1e18.
97+
@available(SwiftStdlib 5.7, *)
9498
public var components: (seconds: Int64, attoseconds: Int64) {
95-
let seconds = _attoseconds / 1_000_000_000_000_000_000
96-
let attoseconds =
97-
Int64((_attoseconds - seconds * 1_000_000_000_000_000_000))
98-
return (Int64(seconds), attoseconds)
99+
let (seconds, attoseconds) = _attoseconds.dividedBy1e18()
100+
return (Int64(seconds), Int64(attoseconds))
99101
}
100102
}
101103

@@ -109,8 +111,8 @@ extension Duration {
109111
/// - Returns: A `Duration` representing a given number of seconds.
110112
@available(SwiftStdlib 5.7, *)
111113
public static func seconds<T: BinaryInteger>(_ seconds: T) -> Duration {
112-
return Duration(_attoseconds: _Int128(seconds) *
113-
1_000_000_000_000_000_000)
114+
return Duration(_attoseconds:
115+
_Int128(seconds).multiplied(by: 1_000_000_000_000_000_000 as UInt64))
114116
}
115117

116118
/// Construct a `Duration` given a number of seconds represented as a
@@ -121,8 +123,7 @@ extension Duration {
121123
/// - Returns: A `Duration` representing a given number of seconds.
122124
@available(SwiftStdlib 5.7, *)
123125
public static func seconds(_ seconds: Double) -> Duration {
124-
return Duration(_attoseconds: _Int128(seconds *
125-
1_000_000_000_000_000_000))
126+
return Duration(_attoseconds: _Int128(seconds * 1_000_000_000_000_000_000))
126127
}
127128

128129
/// Construct a `Duration` given a number of milliseconds represented as a
@@ -135,8 +136,8 @@ extension Duration {
135136
public static func milliseconds<T: BinaryInteger>(
136137
_ milliseconds: T
137138
) -> Duration {
138-
return Duration(_attoseconds: _Int128(milliseconds) *
139-
1_000_000_000_000_000)
139+
return Duration(_attoseconds:
140+
_Int128(milliseconds).multiplied(by: 1_000_000_000_000_000 as UInt64))
140141
}
141142

142143
/// Construct a `Duration` given a number of seconds milliseconds as a
@@ -147,8 +148,8 @@ extension Duration {
147148
/// - Returns: A `Duration` representing a given number of milliseconds.
148149
@available(SwiftStdlib 5.7, *)
149150
public static func milliseconds(_ milliseconds: Double) -> Duration {
150-
return Duration(_attoseconds: _Int128(milliseconds *
151-
1_000_000_000_000_000))
151+
return Duration(_attoseconds:
152+
_Int128(milliseconds * 1_000_000_000_000_000))
152153
}
153154

154155
/// Construct a `Duration` given a number of microseconds represented as a
@@ -161,8 +162,8 @@ extension Duration {
161162
public static func microseconds<T: BinaryInteger>(
162163
_ microseconds: T
163164
) -> Duration {
164-
return Duration(_attoseconds: _Int128(microseconds) *
165-
1_000_000_000_000)
165+
return Duration(_attoseconds:
166+
_Int128(microseconds).multiplied(by: 1_000_000_000_000 as UInt64))
166167
}
167168

168169
/// Construct a `Duration` given a number of seconds microseconds as a
@@ -173,8 +174,8 @@ extension Duration {
173174
/// - Returns: A `Duration` representing a given number of microseconds.
174175
@available(SwiftStdlib 5.7, *)
175176
public static func microseconds(_ microseconds: Double) -> Duration {
176-
return Duration(_attoseconds: _Int128(microseconds *
177-
1_000_000_000_000))
177+
return Duration(_attoseconds:
178+
_Int128(microseconds * 1_000_000_000_000))
178179
}
179180

180181
/// Construct a `Duration` given a number of nanoseconds represented as a
@@ -187,8 +188,8 @@ extension Duration {
187188
public static func nanoseconds<T: BinaryInteger>(
188189
_ nanoseconds: T
189190
) -> Duration {
190-
return Duration(_attoseconds: _Int128(nanoseconds) *
191-
1_000_000_000)
191+
return Duration(_attoseconds:
192+
_Int128(nanoseconds).multiplied(by: 1_000_000_000))
192193
}
193194
}
194195

0 commit comments

Comments
 (0)