Skip to content

Commit ef07102

Browse files
Merge pull request #72891 from kateinoigakukun/yt/fix-wasm-float16
2 parents 648705e + c32c7f5 commit ef07102

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

include/swift/Runtime/SwiftDtoa.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@
9393
#define SWIFT_DTOA_BINARY16_SUPPORT 1
9494
#endif
9595

96+
/// Does this platform support needs to pass _Float16 as a float in
97+
/// C function?
98+
#ifndef SWIFT_DTOA_PASS_FLOAT16_AS_FLOAT
99+
// Windows does not define FLT16_MAX even though it supports _Float16 as argument.
100+
# if (!defined(FLT16_MAX) || defined(__wasm__)) && !defined(_WIN32)
101+
# define SWIFT_DTOA_PASS_FLOAT16_AS_FLOAT 1
102+
# else
103+
# define SWIFT_DTOA_PASS_FLOAT16_AS_FLOAT 0
104+
# endif
105+
#endif
106+
96107
//
97108
// IEEE 754 Binary32 support (also known as "single-precision")
98109
//
@@ -239,7 +250,7 @@ extern "C" {
239250

240251
#if SWIFT_DTOA_BINARY16_SUPPORT
241252
size_t swift_dtoa_optimal_binary16_p(const void *, char *dest, size_t length);
242-
#if defined FLT16_MAX
253+
#if !SWIFT_DTOA_PASS_FLOAT16_AS_FLOAT
243254
// If `_Float16` is defined, provide this convenience wrapper.
244255
size_t swift_dtoa_optimal_binary16(_Float16, char *dest, size_t length);
245256
#endif

stdlib/public/core/Runtime.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,22 @@ internal struct _Buffer72 {
350350
}
351351

352352
#if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
353+
#if arch(wasm32)
353354
// Note that this takes a Float32 argument instead of Float16, because clang
354355
// doesn't have _Float16 on all platforms yet.
356+
@available(SwiftStdlib 5.3, *)
357+
typealias _CFloat16Argument = Float32
358+
#else
359+
@available(SwiftStdlib 5.3, *)
360+
typealias _CFloat16Argument = Float16
361+
#endif
362+
355363
@available(SwiftStdlib 5.3, *)
356364
@_silgen_name("swift_float16ToString")
357365
internal func _float16ToStringImpl(
358366
_ buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
359367
_ bufferLength: UInt,
360-
_ value: Float16,
368+
_ value: _CFloat16Argument,
361369
_ debug: Bool
362370
) -> Int
363371

@@ -370,7 +378,7 @@ internal func _float16ToString(
370378
_internalInvariant(MemoryLayout<_Buffer32>.size == 32)
371379
var buffer = _Buffer32()
372380
let length = buffer.withBytes { (bufferPtr) in
373-
_float16ToStringImpl(bufferPtr, 32, value, debug)
381+
_float16ToStringImpl(bufferPtr, 32, _CFloat16Argument(value), debug)
374382
}
375383
return (buffer, length)
376384
}

stdlib/public/runtime/SwiftDtoa.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ static size_t nan_details(char *dest, size_t len, int negative, int quiet, uint6
343343

344344

345345
#if SWIFT_DTOA_BINARY16_SUPPORT
346-
#if defined FLT16_MAX
346+
#if !SWIFT_DTOA_PASS_FLOAT16_AS_FLOAT
347347
// Format a C `_Float16`
348348
size_t swift_dtoa_optimal_binary16(_Float16 d, char *dest, size_t length) {
349349
return swift_dtoa_optimal_binary16_p(&d, dest, length);

stdlib/public/stubs/Stubs.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,21 @@ static locale_t getCLocale() {
173173
#endif
174174
#endif // SWIFT_STDLIB_HAS_LOCALE
175175

176+
#if SWIFT_DTOA_PASS_FLOAT16_AS_FLOAT
177+
using _CFloat16Argument = float;
178+
#else
179+
using _CFloat16Argument = _Float16;
180+
#endif
181+
176182
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
177183
__swift_ssize_t swift_float16ToString(char *Buffer, size_t BufferLength,
178-
_Float16 Value, bool Debug) {
184+
_CFloat16Argument Value, bool Debug) {
185+
#if SWIFT_DTOA_PASS_FLOAT16_AS_FLOAT
186+
__fp16 v = Value;
187+
return swift_dtoa_optimal_binary16_p(&v, Buffer, BufferLength);
188+
#else
179189
return swift_dtoa_optimal_binary16_p(&Value, Buffer, BufferLength);
190+
#endif
180191
}
181192

182193
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API

test/stdlib/PrintFloat16.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ PrintTests.test("Printable_Float16") {
127127
expectEqual(Float16.infinity.debugDescription, "inf")
128128
expectEqual((-Float16.infinity).debugDescription, "-inf")
129129

130+
// Platforms without float 16 argument passing can cause NaNs to be changed
131+
// while being passed.
132+
#if !arch(wasm32)
130133
for bitPattern in (0x7c01 as UInt16) ... 0x7fff {
131134
expectEqual(Float16(bitPattern: bitPattern).description, "nan")
132135
expectEqual(Float16(bitPattern: 0x8000 | bitPattern).description, "nan")
@@ -144,6 +147,7 @@ PrintTests.test("Printable_Float16") {
144147
expectEqual(Float16(bitPattern: bitPattern).debugDescription, expected)
145148
expectEqual(Float16(bitPattern: 0x8000 | bitPattern).debugDescription, "-\(expected)")
146149
}
150+
#endif
147151
#endif
148152
}
149153

0 commit comments

Comments
 (0)