Skip to content

Commit 32424ab

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 32424ab

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

include/swift/Runtime/SwiftDtoa.h

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

96+
/// Does this platform support _Float16 type?
97+
#ifndef SWIFT_STDLIB_HAS_FLOAT16_TYPE
98+
# if defined(FLT16_MAX) && !defined(__wasm__)
99+
# define SWIFT_STDLIB_HAS_FLOAT16_TYPE 1
100+
# else
101+
# define SWIFT_STDLIB_HAS_FLOAT16_TYPE 0
102+
# endif
103+
#endif
104+
96105
//
97106
// IEEE 754 Binary32 support (also known as "single-precision")
98107
//
@@ -239,7 +248,7 @@ extern "C" {
239248

240249
#if SWIFT_DTOA_BINARY16_SUPPORT
241250
size_t swift_dtoa_optimal_binary16_p(const void *, char *dest, size_t length);
242-
#if defined FLT16_MAX
251+
#if SWIFT_STDLIB_HAS_FLOAT16_TYPE
243252
// If `_Float16` is defined, provide this convenience wrapper.
244253
size_t swift_dtoa_optimal_binary16(_Float16, char *dest, size_t length);
245254
#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 _Float16Storage = Float32
357+
#else
358+
typealias _Float16Storage = 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: _Float16Storage,
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, _Float16Storage(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_STDLIB_HAS_FLOAT16_TYPE
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_STDLIB_HAS_FLOAT16_TYPE
177+
using _Float16Storage = _Float16;
178+
#else
179+
using _Float16Storage = float;
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+
_Float16Storage Value, bool Debug) {
185+
#if SWIFT_STDLIB_HAS_FLOAT16_TYPE
179186
return swift_dtoa_optimal_binary16_p(&Value, Buffer, BufferLength);
187+
#else
188+
__fp16 v = Value;
189+
return swift_dtoa_optimal_binary16_p(&v, 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)