Skip to content

Commit d43d5c4

Browse files
[wasm] Fix build failure due to lack of _Float16 support
WebAssembly does not support _Float16 type, so we need to guard the use of the type. Unfortunately, Clang does not provide a good way to detect the support of _Float16 type at compile time, so just disable for wasm targets.
1 parent 70afeef commit d43d5c4

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

include/swift/Runtime/SwiftDtoa.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@
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+
# if !defined(FLT16_MAX) || defined(__wasm__)
100+
# define SWIFT_DTOA_PASS_FLOAT16_AS_FLOAT 1
101+
# else
102+
# define SWIFT_DTOA_PASS_FLOAT16_AS_FLOAT 0
103+
# endif
104+
#endif
105+
96106
//
97107
// IEEE 754 Binary32 support (also known as "single-precision")
98108
//
@@ -239,7 +249,7 @@ extern "C" {
239249

240250
#if SWIFT_DTOA_BINARY16_SUPPORT
241251
size_t swift_dtoa_optimal_binary16_p(const void *, char *dest, size_t length);
242-
#if defined FLT16_MAX
252+
#if !SWIFT_DTOA_PASS_FLOAT16_AS_FLOAT
243253
// If `_Float16` is defined, provide this convenience wrapper.
244254
size_t swift_dtoa_optimal_binary16(_Float16, char *dest, size_t length);
245255
#endif

stdlib/public/core/Runtime.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,20 @@ 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+
typealias _CFloat16Argument = Float32
357+
#else
358+
typealias _CFloat16Argument = Float16
359+
#endif
360+
355361
@available(SwiftStdlib 5.3, *)
356362
@_silgen_name("swift_float16ToString")
357363
internal func _float16ToStringImpl(
358364
_ buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
359365
_ bufferLength: UInt,
360-
_ value: Float16,
366+
_ value: _CFloat16Argument,
361367
_ debug: Bool
362368
) -> Int
363369

@@ -370,7 +376,7 @@ internal func _float16ToString(
370376
_internalInvariant(MemoryLayout<_Buffer32>.size == 32)
371377
var buffer = _Buffer32()
372378
let length = buffer.withBytes { (bufferPtr) in
373-
_float16ToStringImpl(bufferPtr, 32, value, debug)
379+
_float16ToStringImpl(bufferPtr, 32, _CFloat16Argument(value), debug)
374380
}
375381
return (buffer, length)
376382
}

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)