|
9 | 9 | //
|
10 | 10 | //===----------------------------------------------------------------------===//
|
11 | 11 |
|
12 |
| -#if swift(>=5.3) && !(os(macOS) || os(iOS) && targetEnvironment(macCatalyst)) |
13 | 12 | import _NumericsShims
|
14 | 13 |
|
| 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 | + |
15 | 183 | @available(iOS 14.0, tvOS 14.0, watchOS 7.0, *)
|
16 | 184 | extension Float16: Real {
|
17 | 185 | @_transparent
|
@@ -171,4 +339,5 @@ extension Float16: Real {
|
171 | 339 | }
|
172 | 340 | #endif
|
173 | 341 | }
|
| 342 | + |
174 | 343 | #endif
|
0 commit comments