2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
5
- // Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
@@ -108,9 +108,13 @@ extension Duration {
108
108
///
109
109
/// - Returns: A `Duration` representing a given number of seconds.
110
110
@available ( SwiftStdlib 5 . 7 , * )
111
+ @inlinable
111
112
public static func seconds< T: BinaryInteger > ( _ seconds: T ) -> Duration {
112
- return Duration ( _attoseconds:
113
- _Int128 ( seconds) . multiplied ( by: 1_000_000_000_000_000_000 as UInt64 ) )
113
+ guard let high = Int64 ( exactly: seconds >> 64 ) else { fatalError ( ) }
114
+ let low = UInt64 ( truncatingIfNeeded: seconds)
115
+ var lowScaled = low. multipliedFullWidth ( by: 1_000_000_000_000_000_000 )
116
+ var highScaled = high * 1_000_000_000_000_000_000
117
+ return Duration ( _high: highScaled + Int64( lowScaled. high) , low: lowScaled. low)
114
118
}
115
119
116
120
/// Construct a `Duration` given a duration and scale, taking care so that
@@ -148,11 +152,15 @@ extension Duration {
148
152
///
149
153
/// - Returns: A `Duration` representing a given number of milliseconds.
150
154
@available ( SwiftStdlib 5 . 7 , * )
155
+ @inlinable
151
156
public static func milliseconds< T: BinaryInteger > (
152
157
_ milliseconds: T
153
158
) -> Duration {
154
- return Duration ( _attoseconds:
155
- _Int128 ( milliseconds) . multiplied ( by: 1_000_000_000_000_000 as UInt64 ) )
159
+ guard let high = Int64 ( exactly: milliseconds >> 64 ) else { fatalError ( ) }
160
+ let low = UInt64 ( truncatingIfNeeded: milliseconds)
161
+ var lowScaled = low. multipliedFullWidth ( by: 1_000_000_000_000_000 )
162
+ var highScaled = high * 1_000_000_000_000_000
163
+ return Duration ( _high: highScaled + Int64( lowScaled. high) , low: lowScaled. low)
156
164
}
157
165
158
166
/// Construct a `Duration` given a number of seconds milliseconds as a
@@ -173,11 +181,15 @@ extension Duration {
173
181
///
174
182
/// - Returns: A `Duration` representing a given number of microseconds.
175
183
@available ( SwiftStdlib 5 . 7 , * )
184
+ @inlinable
176
185
public static func microseconds< T: BinaryInteger > (
177
186
_ microseconds: T
178
187
) -> Duration {
179
- return Duration ( _attoseconds:
180
- _Int128 ( microseconds) . multiplied ( by: 1_000_000_000_000 as UInt64 ) )
188
+ guard let high = Int64 ( exactly: microseconds >> 64 ) else { fatalError ( ) }
189
+ let low = UInt64 ( truncatingIfNeeded: microseconds)
190
+ var lowScaled = low. multipliedFullWidth ( by: 1_000_000_000_000 )
191
+ var highScaled = high * 1_000_000_000_000
192
+ return Duration ( _high: highScaled + Int64( lowScaled. high) , low: lowScaled. low)
181
193
}
182
194
183
195
/// Construct a `Duration` given a number of seconds microseconds as a
@@ -198,11 +210,15 @@ extension Duration {
198
210
///
199
211
/// - Returns: A `Duration` representing a given number of nanoseconds.
200
212
@available ( SwiftStdlib 5 . 7 , * )
213
+ @inlinable
201
214
public static func nanoseconds< T: BinaryInteger > (
202
215
_ nanoseconds: T
203
216
) -> Duration {
204
- return Duration ( _attoseconds:
205
- _Int128 ( nanoseconds) . multiplied ( by: 1_000_000_000 ) )
217
+ guard let high = Int64 ( exactly: nanoseconds >> 64 ) else { fatalError ( ) }
218
+ let low = UInt64 ( truncatingIfNeeded: nanoseconds)
219
+ var lowScaled = low. multipliedFullWidth ( by: 1_000_000_000 )
220
+ var highScaled = high * 1_000_000_000
221
+ return Duration ( _high: highScaled + Int64( lowScaled. high) , low: lowScaled. low)
206
222
}
207
223
}
208
224
0 commit comments