Skip to content

Commit 97852af

Browse files
committed
Adjust Float16 availability for Swift 5.4 toolchains.
In older toolchains, Float16 is marked unavailable on all mac targets; starting with the 5.4 toolchain, it is available starting in macOS 11.0 when targeting Apple Silicon.
1 parent 105b59b commit 97852af

File tree

5 files changed

+188
-19
lines changed

5 files changed

+188
-19
lines changed

Sources/RealModule/Float16+Real.swift

Lines changed: 170 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,177 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
#if swift(>=5.3) && !(os(macOS) || os(iOS) && targetEnvironment(macCatalyst))
1312
import _NumericsShims
1413

14+
// When building with a Swift 5.4 or later toolchain, Float16 is available on
15+
// non-x86 macOS from 11.0 onward.
16+
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
17+
18+
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
19+
extension Float16: Real {
20+
@_transparent
21+
public static func cos(_ x: Float16) -> Float16 {
22+
Float16(.cos(Float(x)))
23+
}
24+
25+
@_transparent
26+
public static func sin(_ x: Float16) -> Float16 {
27+
Float16(.sin(Float(x)))
28+
}
29+
30+
@_transparent
31+
public static func tan(_ x: Float16) -> Float16 {
32+
Float16(.tan(Float(x)))
33+
}
34+
35+
@_transparent
36+
public static func acos(_ x: Float16) -> Float16 {
37+
Float16(.acos(Float(x)))
38+
}
39+
40+
@_transparent
41+
public static func asin(_ x: Float16) -> Float16 {
42+
Float16(.asin(Float(x)))
43+
}
44+
45+
@_transparent
46+
public static func atan(_ x: Float16) -> Float16 {
47+
Float16(.atan(Float(x)))
48+
}
49+
50+
@_transparent
51+
public static func cosh(_ x: Float16) -> Float16 {
52+
Float16(.cosh(Float(x)))
53+
}
54+
55+
@_transparent
56+
public static func sinh(_ x: Float16) -> Float16 {
57+
Float16(.sinh(Float(x)))
58+
}
59+
60+
@_transparent
61+
public static func tanh(_ x: Float16) -> Float16 {
62+
Float16(.tanh(Float(x)))
63+
}
64+
65+
@_transparent
66+
public static func acosh(_ x: Float16) -> Float16 {
67+
Float16(.acosh(Float(x)))
68+
}
69+
70+
@_transparent
71+
public static func asinh(_ x: Float16) -> Float16 {
72+
Float16(.asinh(Float(x)))
73+
}
74+
75+
@_transparent
76+
public static func atanh(_ x: Float16) -> Float16 {
77+
Float16(.atanh(Float(x)))
78+
}
79+
80+
@_transparent
81+
public static func exp(_ x: Float16) -> Float16 {
82+
Float16(.exp(Float(x)))
83+
}
84+
85+
@_transparent
86+
public static func expMinusOne(_ x: Float16) -> Float16 {
87+
Float16(.expMinusOne(Float(x)))
88+
}
89+
90+
@_transparent
91+
public static func log(_ x: Float16) -> Float16 {
92+
Float16(.log(Float(x)))
93+
}
94+
95+
@_transparent
96+
public static func log(onePlus x: Float16) -> Float16 {
97+
Float16(.log(onePlus: Float(x)))
98+
}
99+
100+
@_transparent
101+
public static func erf(_ x: Float16) -> Float16 {
102+
Float16(.erf(Float(x)))
103+
}
104+
105+
@_transparent
106+
public static func erfc(_ x: Float16) -> Float16 {
107+
Float16(.erfc(Float(x)))
108+
}
109+
110+
@_transparent
111+
public static func exp2(_ x: Float16) -> Float16 {
112+
Float16(.exp2(Float(x)))
113+
}
114+
115+
@_transparent
116+
public static func exp10(_ x: Float16) -> Float16 {
117+
Float16(.exp10(Float(x)))
118+
}
119+
120+
@_transparent
121+
public static func hypot(_ x: Float16, _ y: Float16) -> Float16 {
122+
if x.isInfinite || y.isInfinite { return .infinity }
123+
let xf = Float(x)
124+
let yf = Float(y)
125+
return Float16(.sqrt(xf*xf + yf*yf))
126+
}
127+
128+
@_transparent
129+
public static func gamma(_ x: Float16) -> Float16 {
130+
Float16(.gamma(Float(x)))
131+
}
132+
133+
@_transparent
134+
public static func log2(_ x: Float16) -> Float16 {
135+
Float16(.log2(Float(x)))
136+
}
137+
138+
@_transparent
139+
public static func log10(_ x: Float16) -> Float16 {
140+
Float16(.log10(Float(x)))
141+
}
142+
143+
@_transparent
144+
public static func pow(_ x: Float16, _ y: Float16) -> Float16 {
145+
Float16(.pow(Float(x), Float(y)))
146+
}
147+
148+
@_transparent
149+
public static func pow(_ x: Float16, _ n: Int) -> Float16 {
150+
// Float16 is simpler than Float or Double, because the range of
151+
// "interesting" exponents is pretty small; anything outside of
152+
// -22707 ... 34061 simply overflows or underflows for every
153+
// x that isn't zero or one. This whole range is representable
154+
// as Float, so we can just use powf as long as we're a little
155+
// bit (get it?) careful to preserve parity.
156+
let clamped = min(max(n, -0x10000), 0x10000) | (n & 1)
157+
return Float16(libm_powf(Float(x), Float(clamped)))
158+
}
159+
160+
@_transparent
161+
public static func root(_ x: Float16, _ n: Int) -> Float16 {
162+
Float16(.root(Float(x), n))
163+
}
164+
165+
@_transparent
166+
public static func atan2(y: Float16, x: Float16) -> Float16 {
167+
Float16(.atan2(y: Float(y), x: Float(x)))
168+
}
169+
170+
#if !os(Windows)
171+
@_transparent
172+
public static func logGamma(_ x: Float16) -> Float16 {
173+
Float16(.logGamma(Float(x)))
174+
}
175+
#endif
176+
}
177+
178+
// When building with older Swift toolchains for macOS, Float16 is not
179+
// available. We will drop support for these older toolchains at some
180+
// future point and remove this duplication.
181+
#elseif swift(>=5.3) && !(os(macOS) || targetEnvironment(macCatalyst))
182+
15183
@available(iOS 14.0, tvOS 14.0, watchOS 7.0, *)
16184
extension Float16: Real {
17185
@_transparent
@@ -171,4 +339,5 @@ extension Float16: Real {
171339
}
172340
#endif
173341
}
342+
174343
#endif

Sources/_TestSupport/RealTestSupport.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public protocol FixedWidthFloatingPoint: BinaryFloatingPoint
1515
where Exponent: FixedWidthInteger,
1616
RawSignificand: FixedWidthInteger { }
1717

18-
#if swift(>=5.3) && !(os(macOS) || os(iOS) && targetEnvironment(macCatalyst))
19-
@available(iOS 14.0, watchOS 14.0, tvOS 7.0, *)
18+
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
19+
@available(macOS 11.0, iOS 14.0, watchOS 14.0, tvOS 7.0, *)
2020
extension Float16: FixedWidthFloatingPoint { }
2121
#endif
2222

Tests/RealTests/ElementaryFunctionChecks.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ internal extension Real where Self: BinaryFloatingPoint {
130130

131131
final class ElementaryFunctionChecks: XCTestCase {
132132

133-
#if swift(>=5.3) && !(os(macOS) || os(iOS) && targetEnvironment(macCatalyst))
133+
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
134134
func testFloat16() {
135-
if #available(iOS 14.0, watchOS 14.0, tvOS 7.0, *) {
135+
if #available(macOS 11.0, iOS 14.0, watchOS 14.0, tvOS 7.0, *) {
136136
Float16.elementaryFunctionChecks()
137137
Float16.realFunctionChecks()
138138
}

Tests/RealTests/IntegerExponentTests.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ internal extension Real where Self: FixedWidthFloatingPoint {
7777
}
7878
}
7979

80-
#if swift(>=5.3) && !(os(macOS) || os(iOS) && targetEnvironment(macCatalyst))
81-
@available(iOS 14.0, watchOS 14.0, tvOS 7.0, *)
80+
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
81+
@available(macOS 11.0, iOS 14.0, watchOS 14.0, tvOS 7.0, *)
8282
extension Float16 {
8383
static func testIntegerExponent() {
8484
testIntegerExponentCommon()
@@ -174,9 +174,11 @@ extension Double {
174174

175175
final class IntegerExponentTests: XCTestCase {
176176

177-
#if swift(>=5.3) && !(os(macOS) || os(iOS) && targetEnvironment(macCatalyst))
177+
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
178178
func testFloat16() {
179-
Float16.testIntegerExponent()
179+
if #available(macOS 11.0, iOS 14.0, watchOS 14.0, tvOS 7.0, *) {
180+
Float16.testIntegerExponent()
181+
}
180182
}
181183
#endif
182184

Tests/WindowsMain.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,15 @@ extension RealTests.ApproximateEqualityTests {
3232
])
3333
}
3434

35-
#if swift(>=5.3) && !(os(macOS) || os(iOS) && targetEnvironment(macCatalyst))
35+
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
3636
extension ElementaryFunctionChecks {
3737
static var all = testCase([
3838
("testFloat16", ElementaryFunctionChecks.testFloat16),
3939
("testFloat", ElementaryFunctionChecks.testFloat),
4040
("testDouble", ElementaryFunctionChecks.testDouble),
4141
])
4242
}
43-
#else
44-
extension ElementaryFunctionChecks {
45-
static var all = testCase([
46-
("testFloat", ElementaryFunctionChecks.testFloat),
47-
("testDouble", ElementaryFunctionChecks.testDouble),
48-
])
49-
}
50-
#endif
5143

52-
#if swift(>=5.3) && !(os(macOS) || os(iOS) && targetEnvironment(macCatalyst))
5344
extension IntegerExponentTests {
5445
static var all = testCase([
5546
("testFloat16", IntegerExponentTests.testFloat16),
@@ -58,6 +49,13 @@ extension IntegerExponentTests {
5849
])
5950
}
6051
#else
52+
extension ElementaryFunctionChecks {
53+
static var all = testCase([
54+
("testFloat", ElementaryFunctionChecks.testFloat),
55+
("testDouble", ElementaryFunctionChecks.testDouble),
56+
])
57+
}
58+
6159
extension IntegerExponentTests {
6260
static var all = testCase([
6361
("testFloat", IntegerExponentTests.testFloat),

0 commit comments

Comments
 (0)