Skip to content

Commit 91108b4

Browse files
committed
[stdlib] Implement tgmath in Glibc
1 parent 9ce1387 commit 91108b4

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

stdlib/public/Platform/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ set(swift_platform_name)
22
set(swift_platform_flags)
33
set(swift_platform_sources
44
Platform.swift
5-
Misc.c)
5+
Misc.c
6+
tgmath.swift.gyb)
67

78
if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}")
89
set(swift_platform_name swiftDarwin)
910
set(swift_platform_sources
1011
Darwin.swift
1112
${swift_platform_sources}
1213
POSIXError.swift
13-
MachError.swift
14-
tgmath.swift.gyb)
14+
MachError.swift)
1515
set(swift_platform_flags
1616
SWIFT_COMPILE_FLAGS -Xfrontend -disable-objc-attr-requires-foundation-module
1717
API_NOTES_NON_OVERLAY)

stdlib/public/Platform/tgmath.swift.gyb

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ OtherFunctions = [
7979
'jn', 'yn'
8080
]
8181

82-
# These functions are imported correctly as-is.
82+
# These functions are imported correctly as-is.
8383
OkayFunctions = ['j0', 'j1', 'y0', 'y1']
8484

8585
# These functions are not supported for various reasons.
@@ -129,6 +129,7 @@ public func ${ufunc}(x: ${T}) -> ${T} {
129129

130130
% end
131131

132+
#if os(OSX) || os(iOS) || os(tvOS) || os(watchOS)
132133
// Unary intrinsic functions
133134
// Note these have a corresponding LLVM intrinsic
134135
% for T, ufunc in TypedUnaryIntrinsicFunctions():
@@ -139,6 +140,22 @@ public func ${ufunc}(x: ${T}) -> ${T} {
139140
}
140141

141142
% end
143+
#else
144+
// FIXME: As of now, we cannot declare 64-bit (Double/CDouble) overlays here.
145+
// Since CoreFoundation also exports libc functions, they will conflict with
146+
// Swift overlays when building Foundation. For now, just like normal
147+
// UnaryFunctions, we define overlays only for OverlayFloatTypes.
148+
% for ufunc in UnaryIntrinsicFunctions:
149+
% for T, CT, f in OverlayFloatTypes():
150+
@_transparent
151+
@warn_unused_result
152+
public func ${ufunc}(x: ${T}) -> ${T} {
153+
return ${T}(${ufunc}${f}(${CT}(x)))
154+
}
155+
156+
% end
157+
% end
158+
#endif
142159

143160
// Binary functions
144161

@@ -153,11 +170,9 @@ public func ${bfunc}(lhs: ${T}, _ rhs: ${T}) -> ${T} {
153170

154171
// Other functions
155172

156-
% # These are AllFloatTypes not OverlayFloatTypes because of the Int return.
157-
% for T, CT, f in AllFloatTypes():
158-
% if f == '':
159-
% f = 'd'
160-
% end
173+
% # These would be AllFloatTypes not OverlayFloatTypes because of the Int return.
174+
% # ... except we need special treatment for Double.
175+
% for T, CT, f in OverlayFloatTypes():
161176
@_transparent
162177
@warn_unused_result
163178
public func fpclassify(x: ${T}) -> Int {
@@ -166,6 +181,16 @@ public func fpclassify(x: ${T}) -> Int {
166181

167182
% end
168183

184+
@_transparent
185+
@warn_unused_result
186+
public func fpclassify(x: Double) -> Int {
187+
#if os(Linux)
188+
return Int(__fpclassify(CDouble(x)))
189+
#else
190+
return Int(__fpclassifyd(CDouble(x)))
191+
#endif
192+
}
193+
169194
% # These are AllFloatTypes not OverlayFloatTypes because we need to cover
170195
% # them all because C's declarations are compiler builtins.
171196
% for T, CT, f in AllFloatTypes():
@@ -257,6 +282,16 @@ public func scalbn(x: ${T}, _ n: Int) -> ${T} {
257282

258283
% # This is AllFloatTypes not OverlayFloatTypes because of the tuple return.
259284
% for T, CT, f in AllFloatTypes():
285+
#if os(Linux) || os(FreeBSD)
286+
@_transparent
287+
@warn_unused_result
288+
public func lgamma(x: ${T}) -> (${T}, Int) {
289+
var sign = CInt(0)
290+
let value = lgamma${f}_r(${CT}(x), &sign)
291+
return (${T}(value), Int(sign))
292+
}
293+
#else
294+
% # On Darwin platform,
260295
% # The real lgamma_r is not imported because it hides behind macro _REENTRANT.
261296
@warn_unused_result
262297
@_silgen_name("_swift_Darwin_lgamma${f}_r")
@@ -273,6 +308,7 @@ public func lgamma(x: ${T}) -> (${T}, Int) {
273308
}
274309
return (${T}(value), Int(sign))
275310
}
311+
#endif
276312

277313
% end
278314

test/1_stdlib/tgmath.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
// RUN: %target-run-simple-swift | FileCheck %s
22
// REQUIRES: executable_test
33

4-
// XFAIL: linux
5-
4+
#if os(Linux) || os(FreeBSD)
5+
import Glibc
6+
// FIXME: this is a quick hack for non Darwin platforms
7+
// where they doesn't have CoreGraphics module.
8+
#if arch(i386) || arch(arm)
9+
typealias CGFloat = Float
10+
#elseif arch(x86_64) || arch(arm64)
11+
typealias CGFloat = Double
12+
#endif
13+
#else
614
import Darwin.C.tgmath
715
import CoreGraphics
16+
#endif
817

918
// verifiers
1019

@@ -146,8 +155,10 @@ print3("tan", d1, f1, g1)
146155
d1 = acosh(dx)
147156
f1 = acosh(fx)
148157
g1 = acosh(gx)
149-
print3("acosh", d1, f1, g1)
150-
// CHECK-NEXT: acosh nan nan acosh
158+
print3("acosh", d1.isNaN, f1.isNaN, g1.isNaN)
159+
// CHECK-NEXT: acosh true true acosh
160+
// Note: We cannot check the output with "nan nan" using String interpolations
161+
// because glibc on Linux outputs the result as "-nan" instead of "nan".
151162

152163
d1 = asinh(dx)
153164
f1 = asinh(fx)

test/1_stdlib/tgmath_optimized.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
// RUN: %target-run %t/a.out
44
// REQUIRES: executable_test
55

6-
// XFAIL: linux
7-
8-
import Darwin
6+
#if os(Linux) || os(FreeBSD)
7+
import Glibc
8+
#else
9+
import Darwin
10+
#endif
911
import StdlibUnittest
1012

1113
// Also import modules which are used by StdlibUnittest internally. This

0 commit comments

Comments
 (0)