Skip to content

Commit cc024d0

Browse files
Generic implementations of math functions implementable on FloatingPoint. (#3524)
Replace the non-generic tgmath functions with generic <T: FloatingPoint> implementations where possible, and move the global sqrt() operation into tgmath.
1 parent bcab1aa commit cc024d0

File tree

4 files changed

+52
-67
lines changed

4 files changed

+52
-67
lines changed

stdlib/public/Platform/tgmath.swift.gyb

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,52 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
// Generic functions implementable directly on FloatingPoint.
14+
@_transparent
15+
public func fabs<T: FloatingPoint>(_ x: T) -> T {
16+
return abs(x)
17+
}
18+
19+
@_transparent
20+
public func sqrt<T: FloatingPoint>(_ x: T) -> T {
21+
return x.squareRoot()
22+
}
23+
24+
@_transparent
25+
public func fma<T: FloatingPoint>(_ x: T, _ y: T, _ z: T) -> T {
26+
return z.addingProduct(x,y)
27+
}
28+
29+
@_transparent
30+
public func remainder<T: FloatingPoint>(_ x: T, _ y: T) -> T {
31+
return x.remainder(dividingBy: y)
32+
}
33+
34+
@_transparent
35+
public func fmod<T: FloatingPoint>(_ x: T, _ y: T) -> T {
36+
return x.truncatingRemainder(dividingBy: y)
37+
}
38+
39+
@_transparent
40+
public func ceil<T: FloatingPoint>(_ x: T) -> T {
41+
return x.rounded(.up)
42+
}
43+
44+
@_transparent
45+
public func floor<T: FloatingPoint>(_ x: T) -> T {
46+
return x.rounded(.down)
47+
}
48+
49+
@_transparent
50+
public func round<T: FloatingPoint>(_ x: T) -> T {
51+
return x.rounded()
52+
}
53+
54+
@_transparent
55+
public func trunc<T: FloatingPoint>(_ x: T) -> T {
56+
return x.rounded(.towardZero)
57+
}
58+
1359
%{
1460

1561
# Don't need 64-bit (Double/CDouble) overlays. The ordinary C imports work fine.
@@ -50,7 +96,7 @@ UnaryFunctions = [
5096
'acosh', 'asinh', 'atanh', 'cosh', 'sinh', 'tanh',
5197
'expm1',
5298
'log1p', 'logb',
53-
'cbrt', 'sqrt', 'erf', 'erfc', 'tgamma',
99+
'cbrt', 'erf', 'erfc', 'tgamma',
54100
]
55101

56102
# These functions have a corresponding LLVM intrinsic
@@ -60,22 +106,21 @@ UnaryIntrinsicFunctions = [
60106
'cos', 'sin',
61107
'exp', 'exp2',
62108
'log', 'log10', 'log2',
63-
'fabs',
64-
'ceil', 'floor', 'nearbyint', 'rint', 'round', 'trunc',
109+
'nearbyint', 'rint',
65110
]
66111

67112
# (T, T) -> T
68113
BinaryFunctions = [
69-
'atan2', 'hypot', 'pow', 'fmod',
70-
'remainder', 'copysign', 'nextafter', 'fdim', 'fmax', 'fmin'
114+
'atan2', 'hypot', 'pow',
115+
'copysign', 'nextafter', 'fdim', 'fmax', 'fmin'
71116
]
72117

73118
# These functions have special implementations.
74119
OtherFunctions = [
75120
'fpclassify',
76121
'isnormal', 'isfinite', 'isinf', 'isnan', 'signbit',
77122
'modf', 'ldexp', 'frexp', 'ilogb', 'scalbn', 'lgamma',
78-
'remquo', 'nan', 'fma',
123+
'remquo', 'nan',
79124
'jn', 'yn'
80125
]
81126

@@ -117,7 +162,6 @@ def TypedBinaryFunctions():
117162

118163
}%
119164

120-
121165
// Unary functions
122166
// Note these do not have a corresponding LLVM intrinsic
123167
% for T, CT, f, ufunc in TypedUnaryFunctions():
@@ -319,14 +363,6 @@ public func nan(_ tag: String) -> ${T} {
319363

320364
% end
321365

322-
% for T, CT, f in OverlayFloatTypes():
323-
@_transparent
324-
public func fma(_ x: ${T}, _ y: ${T}, _ z: ${T}) -> ${T} {
325-
return ${T}(fma${f}(${CT}(x), ${CT}(y), ${CT}(z)))
326-
}
327-
328-
% end
329-
330366
% # These C functions only support double. The overlay fixes the Int parameter.
331367
@_transparent
332368
public func jn(_ n: Int, _ x: Double) -> Double {

stdlib/public/SDK/CoreGraphics/CGFloat.swift.gyb

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -605,26 +605,6 @@ public func tgamma(_ x: CGFloat) -> CGFloat {
605605
return CGFloat(tgamma(x.native))
606606
}
607607

608-
@_transparent
609-
public func fabs(_ x: CGFloat) -> CGFloat {
610-
return CGFloat(fabs(x.native))
611-
}
612-
613-
@_transparent
614-
public func sqrt(_ x: CGFloat) -> CGFloat {
615-
return CGFloat(sqrt(x.native))
616-
}
617-
618-
@_transparent
619-
public func ceil(_ x: CGFloat) -> CGFloat {
620-
return CGFloat(ceil(x.native))
621-
}
622-
623-
@_transparent
624-
public func floor(_ x: CGFloat) -> CGFloat {
625-
return CGFloat(floor(x.native))
626-
}
627-
628608
@_transparent
629609
public func nearbyint(_ x: CGFloat) -> CGFloat {
630610
return CGFloat(nearbyint(x.native))
@@ -635,16 +615,6 @@ public func rint(_ x: CGFloat) -> CGFloat {
635615
return CGFloat(rint(x.native))
636616
}
637617

638-
@_transparent
639-
public func round(_ x: CGFloat) -> CGFloat {
640-
return CGFloat(round(x.native))
641-
}
642-
643-
@_transparent
644-
public func trunc(_ x: CGFloat) -> CGFloat {
645-
return CGFloat(trunc(x.native))
646-
}
647-
648618
@_transparent
649619
public func atan2(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
650620
return CGFloat(atan2(lhs.native, rhs.native))
@@ -660,16 +630,6 @@ public func pow(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
660630
return CGFloat(pow(lhs.native, rhs.native))
661631
}
662632

663-
@_transparent
664-
public func fmod(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
665-
return CGFloat(fmod(lhs.native, rhs.native))
666-
}
667-
668-
@_transparent
669-
public func remainder(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
670-
return CGFloat(remainder(lhs.native, rhs.native))
671-
}
672-
673633
@_transparent
674634
public func copysign(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat {
675635
return CGFloat(copysign(lhs.native, rhs.native))
@@ -760,11 +720,6 @@ public func nan(_ tag: String) -> CGFloat {
760720
return CGFloat(nan(tag) as CGFloat.NativeType)
761721
}
762722

763-
@_transparent
764-
public func fma(_ x: CGFloat, _ y: CGFloat, _ z: CGFloat) -> CGFloat {
765-
return CGFloat(fma(x.native, y.native, z.native))
766-
}
767-
768723
@_transparent
769724
public func j0(_ x: CGFloat) -> CGFloat {
770725
return CGFloat(j0(Double(x.native)))

stdlib/public/core/BuiltinMath.swift.gyb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ UnaryIntrinsicFunctions = [
4949
'cos', 'sin',
5050
'exp', 'exp2',
5151
'log', 'log10', 'log2',
52-
'fabs',
53-
'ceil', 'floor', 'nearbyint', 'rint', 'round', 'trunc',
52+
'nearbyint', 'rint',
5453
]
5554

5655
def TypedUnaryIntrinsicFunctions():

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,6 @@ public func ${op[0]}=<T : FloatingPoint>(lhs: inout T, rhs: T) {
572572

573573
%end
574574

575-
@_transparent
576-
public func sqrt<T : FloatingPoint>(_ rhs: T) -> T {
577-
return rhs.squareRoot()
578-
}
579-
580575
@_transparent
581576
public func ==<T : FloatingPoint>(lhs: T, rhs: T) -> Bool {
582577
return lhs.isEqual(to: rhs)

0 commit comments

Comments
 (0)