Skip to content

Commit 643834e

Browse files
committed
[stdlib] Simplify generic floating-point conversion fast paths
1 parent 45d69d5 commit 643834e

File tree

1 file changed

+32
-70
lines changed

1 file changed

+32
-70
lines changed

stdlib/public/core/FloatingPoint.swift

Lines changed: 32 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,85 +1896,47 @@ extension BinaryFloatingPoint {
18961896
switch (Source.exponentBitCount, Source.significandBitCount) {
18971897
#if !os(macOS) && !(os(iOS) && targetEnvironment(macCatalyst))
18981898
case (5, 10):
1899-
if #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
1900-
if case let value_ as Float16 = value {
1901-
self = Self(Float(value_))
1902-
return
1903-
}
1904-
if !value.isNaN {
1905-
let value_ = Float16(
1906-
sign: value.sign,
1907-
exponentBitPattern:
1908-
UInt(truncatingIfNeeded: value.exponentBitPattern),
1909-
significandBitPattern:
1910-
UInt16(truncatingIfNeeded: value.significandBitPattern))
1911-
self = Self(Float(value_))
1912-
return
1913-
}
1899+
guard #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) else {
1900+
self = Self._convert(from: value).value
1901+
break
19141902
}
1903+
let value_ = value as? Float16 ?? Float16(
1904+
sign: value.sign,
1905+
exponentBitPattern:
1906+
UInt(truncatingIfNeeded: value.exponentBitPattern),
1907+
significandBitPattern:
1908+
UInt16(truncatingIfNeeded: value.significandBitPattern))
1909+
self = Self(Float(value_))
19151910
#endif
1916-
case (8, 7):
1917-
if !value.isNaN {
1918-
let value_ = Float(
1919-
sign: value.sign,
1920-
exponentBitPattern:
1921-
UInt(truncatingIfNeeded: value.exponentBitPattern),
1922-
significandBitPattern:
1923-
UInt32(truncatingIfNeeded: value.significandBitPattern) &<< 16)
1924-
self = Self(value_)
1925-
return
1926-
}
19271911
case (8, 23):
1928-
if case let value_ as Float = value {
1929-
self = Self(value_)
1930-
return
1931-
}
1932-
if !value.isNaN {
1933-
let value_ = Float(
1934-
sign: value.sign,
1935-
exponentBitPattern:
1936-
UInt(truncatingIfNeeded: value.exponentBitPattern),
1937-
significandBitPattern:
1938-
UInt32(truncatingIfNeeded: value.significandBitPattern))
1939-
self = Self(value_)
1940-
return
1941-
}
1912+
let value_ = value as? Float ?? Float(
1913+
sign: value.sign,
1914+
exponentBitPattern:
1915+
UInt(truncatingIfNeeded: value.exponentBitPattern),
1916+
significandBitPattern:
1917+
UInt32(truncatingIfNeeded: value.significandBitPattern))
1918+
self = Self(value_)
19421919
case (11, 52):
1943-
if case let value_ as Double = value {
1944-
self = Self(value_)
1945-
return
1946-
}
1947-
if !value.isNaN {
1948-
let value_ = Double(
1949-
sign: value.sign,
1950-
exponentBitPattern:
1951-
UInt(truncatingIfNeeded: value.exponentBitPattern),
1952-
significandBitPattern:
1953-
UInt64(truncatingIfNeeded: value.significandBitPattern))
1954-
self = Self(value_)
1955-
return
1956-
}
1920+
let value_ = value as? Double ?? Double(
1921+
sign: value.sign,
1922+
exponentBitPattern:
1923+
UInt(truncatingIfNeeded: value.exponentBitPattern),
1924+
significandBitPattern:
1925+
UInt64(truncatingIfNeeded: value.significandBitPattern))
1926+
self = Self(value_)
19571927
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
19581928
case (15, 63):
1959-
if case let value_ as Float80 = value {
1960-
self = Self(value_)
1961-
return
1962-
}
1963-
if !value.isNaN {
1964-
let value_ = Float80(
1965-
sign: value.sign,
1966-
exponentBitPattern:
1967-
UInt(truncatingIfNeeded: value.exponentBitPattern),
1968-
significandBitPattern:
1969-
UInt64(truncatingIfNeeded: value.significandBitPattern))
1970-
self = Self(value_)
1971-
return
1972-
}
1929+
let value_ = value as? Float80 ?? Float80(
1930+
sign: value.sign,
1931+
exponentBitPattern:
1932+
UInt(truncatingIfNeeded: value.exponentBitPattern),
1933+
significandBitPattern:
1934+
UInt64(truncatingIfNeeded: value.significandBitPattern))
1935+
self = Self(value_)
19731936
#endif
19741937
default:
1975-
break
1938+
self = Self._convert(from: value).value
19761939
}
1977-
self = Self._convert(from: value).value
19781940
}
19791941

19801942
/// Creates a new instance from the given value, if it can be represented

0 commit comments

Comments
 (0)