mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Merge pull request #33826 from xwu/float-like-a-butterfly
[stdlib] Add another fast path for generic floating-point conversion
This commit is contained in:
@@ -1890,21 +1890,48 @@ extension BinaryFloatingPoint {
|
||||
/// - Parameter value: A floating-point value to be converted.
|
||||
@inlinable
|
||||
public init<Source: BinaryFloatingPoint>(_ value: Source) {
|
||||
// If two IEEE 754 binary interchange formats share the same exponent bit
|
||||
// count and significand bit count, then they must share the same encoding
|
||||
// for finite and infinite values.
|
||||
switch (Source.exponentBitCount, Source.significandBitCount) {
|
||||
#if !os(macOS) && !(os(iOS) && targetEnvironment(macCatalyst))
|
||||
if #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
|
||||
if case let value_ as Float16 = value {
|
||||
self = Self(Float(value_))
|
||||
return
|
||||
case (5, 10):
|
||||
guard #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) else {
|
||||
self = Self._convert(from: value).value
|
||||
break
|
||||
}
|
||||
}
|
||||
let value_ = value as? Float16 ?? Float16(
|
||||
sign: value.sign,
|
||||
exponentBitPattern:
|
||||
UInt(truncatingIfNeeded: value.exponentBitPattern),
|
||||
significandBitPattern:
|
||||
UInt16(truncatingIfNeeded: value.significandBitPattern))
|
||||
self = Self(Float(value_))
|
||||
#endif
|
||||
switch value {
|
||||
case let value_ as Float:
|
||||
case (8, 23):
|
||||
let value_ = value as? Float ?? Float(
|
||||
sign: value.sign,
|
||||
exponentBitPattern:
|
||||
UInt(truncatingIfNeeded: value.exponentBitPattern),
|
||||
significandBitPattern:
|
||||
UInt32(truncatingIfNeeded: value.significandBitPattern))
|
||||
self = Self(value_)
|
||||
case let value_ as Double:
|
||||
case (11, 52):
|
||||
let value_ = value as? Double ?? Double(
|
||||
sign: value.sign,
|
||||
exponentBitPattern:
|
||||
UInt(truncatingIfNeeded: value.exponentBitPattern),
|
||||
significandBitPattern:
|
||||
UInt64(truncatingIfNeeded: value.significandBitPattern))
|
||||
self = Self(value_)
|
||||
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
|
||||
case let value_ as Float80:
|
||||
case (15, 63):
|
||||
let value_ = value as? Float80 ?? Float80(
|
||||
sign: value.sign,
|
||||
exponentBitPattern:
|
||||
UInt(truncatingIfNeeded: value.exponentBitPattern),
|
||||
significandBitPattern:
|
||||
UInt64(truncatingIfNeeded: value.significandBitPattern))
|
||||
self = Self(value_)
|
||||
#endif
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user