Skip to content

Commit b8d1481

Browse files
committed
WOW\! 100% less gratuitous type machinery at runtime\!
1 parent 5e5cd76 commit b8d1481

File tree

7 files changed

+28
-118
lines changed

7 files changed

+28
-118
lines changed

stdlib/public/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ if(SWIFT_BUILD_STDLIB)
6161
add_subdirectory(stubs)
6262
add_subdirectory(core)
6363
add_subdirectory(SwiftOnoneSupport)
64-
6564
add_subdirectory(Maths)
6665
endif()
6766

stdlib/public/Maths/Maths.swift.gyb

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,41 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
// MARK: - Free functions defined on FloatingPoint
14-
@_transparent
15-
public func ceil<T>(_ x: T) -> T where T: FloatingPoint {
16-
return x.rounded(.up)
14+
%for constraint in ["T: FloatingPoint", "T: SIMD, T.Scalar: FloatingPoint"]:
15+
% for (func,dir) in [("ceil", ".up"), ("floor", ".down"), ("round", ""), ("trunc", ".towardZero")]:
16+
@_alwaysEmitIntoClient
17+
public func ${func}<T>(_ x: T) -> T where ${constraint} {
18+
return x.rounded(${dir})
1719
}
1820

19-
@_transparent
20-
public func floor<T>(_ x: T) -> T where T: FloatingPoint {
21-
return x.rounded(.down)
22-
}
23-
24-
@_transparent
25-
public func round<T: FloatingPoint>(_ x: T) -> T {
26-
return x.rounded()
27-
}
28-
29-
@_transparent
30-
public func trunc<T>(_ x: T) -> T where T: FloatingPoint {
31-
return x.rounded(.towardZero)
32-
}
33-
34-
@_transparent
35-
public func sqrt<T>(_ x: T) -> T where T: FloatingPoint {
21+
%end
22+
@_alwaysEmitIntoClient
23+
public func sqrt<T>(_ x: T) -> T where ${constraint} {
3624
return x.squareRoot()
3725
}
3826

39-
@_transparent
40-
public func fma<T>(_ x: T, _ y: T, _ z: T) -> T where T: FloatingPoint {
27+
@_alwaysEmitIntoClient
28+
public func fma<T>(_ x: T, _ y: T, _ z: T) -> T where ${constraint} {
4129
return z.addingProduct(x, y)
4230
}
4331

44-
@_transparent
32+
%end
33+
@_alwaysEmitIntoClient
4534
public func remainder<T>(_ x: T, _ y: T) -> T where T: FloatingPoint {
4635
return x.remainder(dividingBy: y)
4736
}
4837

49-
@_transparent
38+
@_alwaysEmitIntoClient
5039
public func fmod<T>(_ x: T, _ y: T) -> T where T: FloatingPoint {
5140
return x.truncatingRemainder(dividingBy: y)
5241
}
5342

5443
// MARK: - Free functions defined on Mathsable
55-
5644
%from SwiftMathsFunctions import *
5745
%for func in MathFunctions:
5846

59-
@_transparent
47+
@_alwaysEmitIntoClient
6048
public func ${func.free_decl()} {
61-
// return T.Maths.${func.name}(${func.params()})
62-
return T.${func.name}(${func.params()})
49+
return T.Maths.${func.name}(${func.params()})
6350
}
6451
%end

stdlib/public/Platform/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_swift_target_library(swiftDarwin ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_
1818
SWIFT_COMPILE_FLAGS -Xfrontend -disable-objc-attr-requires-foundation-module "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
1919
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
2020
TARGET_SDKS ALL_APPLE_PLATFORMS
21+
SWIFT_MODULE_DEPENDS Maths
2122

2223
# This is overly conservative, but we have so few API notes files that
2324
# haven't migrated to the Swift repo that it's probably fine in practice.
@@ -33,6 +34,7 @@ add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_O
3334
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
3435
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
3536
TARGET_SDKS ANDROID CYGWIN FREEBSD LINUX HAIKU
37+
SWIFT_MODULE_DEPENDS Maths
3638
DEPENDS glibc_modulemap)
3739

3840
add_swift_target_library(swiftMSVCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
@@ -44,7 +46,8 @@ add_swift_target_library(swiftMSVCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_
4446

4547
SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
4648
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
47-
TARGET_SDKS WINDOWS)
49+
TARGET_SDKS WINDOWS
50+
SWIFT_MODULE_DEPENDS Maths)
4851

4952
set(glibc_modulemap_target_list)
5053
foreach(sdk ${SWIFT_SDKS})

stdlib/public/Platform/tgmath.swift.gyb

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,6 @@ public func fabs<T: FloatingPoint>(_ x: T) -> T {
2020
return x.magnitude
2121
}
2222

23-
@_transparent
24-
public func sqrt<T: FloatingPoint>(_ x: T) -> T {
25-
return x.squareRoot()
26-
}
27-
28-
@_transparent
29-
public func fma<T: FloatingPoint>(_ x: T, _ y: T, _ z: T) -> T {
30-
return z.addingProduct(x, y)
31-
}
32-
33-
@_transparent
34-
public func remainder<T: FloatingPoint>(_ x: T, _ y: T) -> T {
35-
return x.remainder(dividingBy: y)
36-
}
37-
38-
@_transparent
39-
public func fmod<T: FloatingPoint>(_ x: T, _ y: T) -> T {
40-
return x.truncatingRemainder(dividingBy: y)
41-
}
42-
43-
@_transparent
44-
public func ceil<T: FloatingPoint>(_ x: T) -> T {
45-
return x.rounded(.up)
46-
}
47-
48-
@_transparent
49-
public func floor<T: FloatingPoint>(_ x: T) -> T {
50-
return x.rounded(.down)
51-
}
52-
53-
@_transparent
54-
public func round<T: FloatingPoint>(_ x: T) -> T {
55-
return x.rounded()
56-
}
57-
58-
@_transparent
59-
public func trunc<T: FloatingPoint>(_ x: T) -> T {
60-
return x.rounded(.towardZero)
61-
}
62-
6323
@_transparent
6424
public func scalbn<T: FloatingPoint>(_ x: T, _ n : Int) -> T {
6525
return T(sign: .plus, exponent: T.Exponent(n), significand: x)

stdlib/public/core/Mathsable.swift.gyb

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from SwiftMathsFunctions import *
2525
}%
2626

27-
/* NAMESPACED
2827
public protocol Mathsable {
2928
associatedtype Maths: MathsImplementations where Maths.Value == Self
3029
}
@@ -38,27 +37,28 @@ public protocol MathsImplementations {
3837

3938
%for type in all_floating_point_types():
4039
extension ${type.stdlib_name}: Mathsable {
40+
@_frozen
4141
public enum Maths: MathsImplementations {
4242
public typealias Value = ${type.stdlib_name}
4343
% for func in MathFunctions:
44-
45-
@_transparent
44+
@_alwaysEmitIntoClient
4645
public static func ${func.decl("Value")} {
4746
return ${func.impl(type)}
4847
}
49-
%end
48+
% end
5049
}
5150
}
52-
51+
5352
%end
5453

54+
@_fixed_layout
5555
public struct _SIMDMaths<V>: MathsImplementations
5656
where V: SIMD, V.Scalar: Mathsable {
5757

5858
public typealias Value = V
5959
%for func in MathFunctions:
6060

61-
@_transparent
61+
@inlinable
6262
public static func ${func.decl("V")} {
6363
var r = V()
6464
for i in x.indices {
@@ -76,42 +76,3 @@ extension SIMD where Scalar: Mathsable {
7676
%for n in [2,3,4,8,16,32,64]:
7777
extension SIMD${n}: Mathsable where Scalar: Mathsable { }
7878
%end
79-
*/
80-
81-
/* NO NAMESPACE */
82-
public protocol Mathsable {
83-
%for func in MathFunctions:
84-
static func ${func.decl("Self")}
85-
%end
86-
}
87-
88-
%for type in all_floating_point_types():
89-
extension ${type.stdlib_name}: Mathsable {
90-
% for func in MathFunctions:
91-
92-
@_transparent
93-
public static func ${func.decl(type.stdlib_name)} {
94-
return ${func.impl(type)}
95-
}
96-
%end
97-
}
98-
99-
%end
100-
101-
extension SIMD where Scalar: Mathsable {
102-
%for func in MathFunctions:
103-
104-
@_transparent
105-
public static func ${func.decl("Self")} {
106-
var r = Self()
107-
for i in x.indices {
108-
r[i] = Scalar.${func.name}(${func.params("", "[i]")})
109-
}
110-
return r
111-
}
112-
%end
113-
}
114-
115-
%for n in [2,3,4,8,16,32,64]:
116-
extension SIMD${n}: Mathsable where Scalar: Mathsable { }
117-
%end

stdlib/public/core/SIMDVector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ extension SIMD where Scalar : FloatingPoint {
652652
}
653653

654654
@_transparent
655-
public func rounded(_ rule: FloatingPointRoundingRule) -> Self {
655+
public func rounded(_ rule: FloatingPointRoundingRule = .toNearestOrEven) -> Self {
656656
var result = Self()
657657
for i in result.indices { result[i] = self[i].rounded(rule) }
658658
return result

utils/SwiftMathsFunctions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def params(self, prefix="", suffix=""):
1515
def decl(self, type):
1616
return self.name + "(" + self.params("_ ", ": " + type) + ") -> " + type
1717

18-
def free_decl(self):
19-
return self.name + "<T>(" + self.params("_ ", ": T") + ") -> T where T: Mathsable"
18+
def free_decl(self, protocol="Mathsable"):
19+
return self.name + "<T>(" + self.params("_ ", ": T") + ") -> T where T: " + protocol
2020

2121
def impl(self, type):
2222
if self.intrinsic:

0 commit comments

Comments
 (0)