22//
33// This source file is part of the Swift.org open source project
44//
5- // Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
5+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
66// Licensed under Apache License v2.0 with Runtime Library Exception
77//
88// See https://swift.org/LICENSE.txt for license information
@@ -108,9 +108,13 @@ extension Duration {
108108 ///
109109 /// - Returns: A `Duration` representing a given number of seconds.
110110 @available ( SwiftStdlib 5 . 7 , * )
111+ @inlinable
111112 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)
114118 }
115119
116120 /// Construct a `Duration` given a duration and scale, taking care so that
@@ -148,11 +152,15 @@ extension Duration {
148152 ///
149153 /// - Returns: A `Duration` representing a given number of milliseconds.
150154 @available ( SwiftStdlib 5 . 7 , * )
155+ @inlinable
151156 public static func milliseconds< T: BinaryInteger > (
152157 _ milliseconds: T
153158 ) -> 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)
156164 }
157165
158166 /// Construct a `Duration` given a number of seconds milliseconds as a
@@ -173,11 +181,15 @@ extension Duration {
173181 ///
174182 /// - Returns: A `Duration` representing a given number of microseconds.
175183 @available ( SwiftStdlib 5 . 7 , * )
184+ @inlinable
176185 public static func microseconds< T: BinaryInteger > (
177186 _ microseconds: T
178187 ) -> 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)
181193 }
182194
183195 /// Construct a `Duration` given a number of seconds microseconds as a
@@ -198,11 +210,15 @@ extension Duration {
198210 ///
199211 /// - Returns: A `Duration` representing a given number of nanoseconds.
200212 @available ( SwiftStdlib 5 . 7 , * )
213+ @inlinable
201214 public static func nanoseconds< T: BinaryInteger > (
202215 _ nanoseconds: T
203216 ) -> 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)
206222 }
207223}
208224
0 commit comments