Skip to content

Make Float16 available for macOS on Apple Silicon #34821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions stdlib/public/core/CTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public typealias CLong = Int
/// The C 'long long' type.
public typealias CLongLong = Int64

#if !(os(macOS) && arch(x86_64))
/// The C '_Float16' type.
@available(iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
public typealias CFloat16 = Float16
#endif

/// The C 'float' type.
public typealias CFloat = Float
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4730,9 +4730,8 @@ extension RawRepresentable where RawValue == Float, Self: Decodable {
}
}

@available(iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
#if !(os(macOS) && arch(x86_64))
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
extension Float16: Codable {
/// Creates a new instance by decoding from the given decoder.
///
Expand Down Expand Up @@ -4763,6 +4762,7 @@ extension Float16: Codable {
try Float(self).encode(to: encoder)
}
}
#endif

extension Int: Codable {
/// Creates a new instance by decoding from the given decoder.
Expand Down
19 changes: 6 additions & 13 deletions stdlib/public/core/FloatingPointParsing.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ internal func _isspace_clocale(_ u: UTF16.CodeUnit) -> Bool {

% if bits == 80:
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
% elif bits == 16:
#if !(os(macOS) && arch(x86_64))
% end

//===--- Parsing ----------------------------------------------------------===//
%if bits == 16:
@available(iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
%end
extension ${Self}: LosslessStringConvertible {
/// Creates a new instance from the given string.
Expand Down Expand Up @@ -136,7 +135,7 @@ extension ${Self}: LosslessStringConvertible {
%if bits == 16:
self.init(Substring(text))
%else:
if #available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tbkka I think we made a change here that hasn't been pushed upstream for whatever reason--do you want to bring the two in sync now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should sync these up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to put up a PR and tag me to review?

self.init(Substring(text))
} else {
self = 0.0
Expand Down Expand Up @@ -166,13 +165,7 @@ extension ${Self}: LosslessStringConvertible {
// In particular, we still have to export
// _swift_stdlib_strtoXYZ_clocale()
// as ABI to support old compiled code that still requires it.
%if bits == 16:
@available(iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
%else:
@available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
%end
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
public init?(_ text: Substring) {
self = 0.0
let success = withUnsafeMutablePointer(to: &self) { p -> Bool in
Expand All @@ -196,7 +189,7 @@ extension ${Self}: LosslessStringConvertible {
}
}

% if bits == 80:
% if bits in [16,80]:
#endif
% end

Expand Down
14 changes: 9 additions & 5 deletions stdlib/public/core/FloatingPointTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ RawSignificand = 'UInt' + str(SignificandSize)

def Availability(bits):
if bits == 16:
return '@available(iOS 14.0, watchOS 7.0, tvOS 14.0, *)\n@available(macOS, unavailable)\n@available(macCatalyst, unavailable)'
return '@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)'
return ''

if Self == 'Float16':
Expand All @@ -65,6 +65,8 @@ else:

% if bits == 80:
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
% elif bits == 16:
#if !(os(macOS) && arch(x86_64))
% end

${SelfDocComment}
Expand Down Expand Up @@ -1106,8 +1108,10 @@ extension ${Self} {
% srcBits = src_type.bits
% That = src_type.stdlib_name

% if (srcBits == 80) and (bits != 80):
% if srcBits == 80:
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
% elif srcBits == 16:
#if !(os(macOS) && arch(x86_64))
% end

% if srcBits == bits:
Expand Down Expand Up @@ -1192,7 +1196,7 @@ extension ${Self} {
}
}

% if (srcBits == 80) and (bits != 80):
% if srcBits in [16,80]:
#endif
% end
% end
Expand Down Expand Up @@ -1343,12 +1347,12 @@ internal struct _${Self}AnyHashableBox: _AnyHashableBox {
// Deprecated operators
//===----------------------------------------------------------------------===//

% if bits == 80:
% if bits in [16,80]:
#else

${SelfDocComment}
@frozen
@available(*, unavailable, message: "Float80 is not available on target platform.")
@available(*, unavailable, message: "${Self} is not available on target platform.")
public struct ${Self} {
/// Creates a value initialized to zero.
@_transparent
Expand Down
12 changes: 5 additions & 7 deletions stdlib/public/core/IntegerTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,8 @@ public struct ${Self}

% if FloatType == 'Float80':
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
% elif FloatType == 'Float16':
#if !(os(macOS) && arch(x86_64))
% end

/// Creates an integer from the given floating-point value, rounding toward
Expand All @@ -1152,9 +1154,7 @@ public struct ${Self}
/// `source` must be representable in this type after rounding toward
/// zero.
% if FloatType == 'Float16':
@available(iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
% end
@_transparent
public init(_ source: ${FloatType}) {
Expand Down Expand Up @@ -1184,9 +1184,7 @@ public struct ${Self}
///
/// - Parameter source: A floating-point value to convert to an integer.
% if FloatType == 'Float16':
@available(iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
% end
@_transparent
public init?(exactly source: ${FloatType}) {
Expand All @@ -1206,7 +1204,7 @@ public struct ${Self}
self._value = Builtin.fpto${u}i_FPIEEE${FloatBits}_${BuiltinName}(source._value)
}

% if FloatType == 'Float80':
% if FloatType in ['Float16', 'Float80']:
#endif
% end

Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ internal struct _Buffer72 {
}
}

#if !(os(macOS) && arch(x86_64))
// Note that this takes a Float32 argument instead of Float16, because clang
// doesn't have _Float16 on all platforms yet.
@_silgen_name("swift_float16ToString")
Expand All @@ -303,9 +304,7 @@ internal func _float16ToStringImpl(
_ debug: Bool
) -> Int

@available(iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
internal func _float16ToString(
_ value: Float16,
debug: Bool
Expand All @@ -317,6 +316,7 @@ internal func _float16ToString(
}
return (buffer, length)
}
#endif

// Returns a UInt64, but that value is the length of the string, so it's
// guaranteed to fit into an Int. This is part of the ABI, so we can't
Expand Down
12 changes: 7 additions & 5 deletions stdlib/public/core/SIMDVectorTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,10 @@ extension ${Self}: SIMDScalar {
%end

%for (Self, bits) in [('Float16',16), ('Float',32), ('Double',64)]:
%if bits == 16:
@available(iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
%end
% if bits == 16:
#if !(os(macOS) && arch(x86_64))
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
% end
extension ${Self} : SIMDScalar {

public typealias SIMDMaskScalar = Int${bits}
Expand Down Expand Up @@ -299,5 +298,8 @@ extension ${Self} : SIMDScalar {

% end
}
% if bits == 16:
#endif
% end

%end
4 changes: 4 additions & 0 deletions test/ClangImporter/simd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ let ll3_value: ll3 = makes_ll3()
let ll8_value: ll8 = makes_ll8()
let ull4_value: ull4 = makes_ull4()
let ull16_value: ull16 = makes_ull16()
#if !(os(macOS) && arch(x86_64))
let half2_value: half2 = makes_half2()
let half3_value: half3 = makes_half3()
let half4_value: half4 = makes_half4()
let half8_value: half8 = makes_half8()
let half16_value: half16 = makes_half16()
let half32_value: half32 = makes_half32()
#endif
let float2_value: float2 = makes_float2()
let float3_value: float3 = makes_float3()
let float4_value: float4 = makes_float4()
Expand Down Expand Up @@ -58,12 +60,14 @@ takes_ll3(ll3_value)
takes_ll8(ll8_value)
takes_ull4(ull4_value)
takes_ull16(ull16_value)
#if !(os(macOS) && arch(x86_64))
takes_half2(half2_value)
takes_half3(half3_value)
takes_half4(half4_value)
takes_half8(half8_value)
takes_half16(half16_value)
takes_half32(half32_value)
#endif
takes_float2(float2_value)
takes_float3(float3_value)
takes_float4(float4_value)
Expand Down
14 changes: 6 additions & 8 deletions test/stdlib/PrintFloat.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,9 @@ let PrintTests = TestSuite("FloatingPointPrinting")

% for FloatType in ['Float16', 'Float', 'Double', 'Float80']:
% if FloatType == 'Float16':
@available(iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@available(macOS, unavailable)
@available(macCatalyst, unavailable)
% end
% if FloatType == 'Float80':
#if !(os(macOS) && arch(x86_64))
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
% elif FloatType == 'Float80':
#if !os(Windows) && (arch(i386) || arch(x86_64))
% end

Expand Down Expand Up @@ -425,7 +423,7 @@ fileprivate func expectAccurateDescription(_ object: ${FloatType},
// that the result is not closer. Note this requires higher-precision
// arithmetic.
}
% if FloatType == 'Float80':
% if FloatType in ['Float16','Float80']:
#endif
% end
% end
Expand Down Expand Up @@ -587,8 +585,8 @@ PrintTests.test("Printable_CDouble") {
expectDescription("-1.0", CDouble(-1.0))
}

#if !os(macOS) && !(os(iOS) && targetEnvironment(macCatalyst))
if #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
#if !(os(macOS) && arch(x86_64))
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
PrintTests.test("Printable_Float16") {
func asFloat16(_ f: Float16) -> Float16 { return f }

Expand Down