Skip to content

Commit 2f7435d

Browse files
committed
First non-intrinsic functions.
1 parent b8d1481 commit 2f7435d

File tree

6 files changed

+129
-42
lines changed

6 files changed

+129
-42
lines changed

stdlib/public/Maths/Maths.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public func fmod<T>(_ x: T, _ y: T) -> T where T: FloatingPoint {
4242

4343
// MARK: - Free functions defined on Mathsable
4444
%from SwiftMathsFunctions import *
45-
%for func in MathFunctions:
45+
%for func in MathsFunctions:
4646

4747
@_alwaysEmitIntoClient
4848
public func ${func.free_decl()} {

stdlib/public/SwiftShims/LibcShims.h

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,33 +127,108 @@ static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) {
127127

128128
// Math library functions
129129
static inline SWIFT_ALWAYS_INLINE
130-
float _stdlib_remainderf(float _self, float _other) {
130+
float _swift_stdlib_tanf(float x) {
131+
return __builtin_tanf(x);
132+
}
133+
134+
static inline SWIFT_ALWAYS_INLINE
135+
float _swift_stdlib_acosf(float x) {
136+
return __builtin_acosf(x);
137+
}
138+
139+
static inline SWIFT_ALWAYS_INLINE
140+
float _swift_stdlib_asinf(float x) {
141+
return __builtin_asinf(x);
142+
}
143+
144+
static inline SWIFT_ALWAYS_INLINE
145+
float _swift_stdlib_atanf(float x) {
146+
return __builtin_atanf(x);
147+
}
148+
149+
static inline SWIFT_ALWAYS_INLINE
150+
float _swift_stdlib_atan2f(float y, float x) {
151+
return __builtin_atan2f(y, x);
152+
}
153+
154+
static inline SWIFT_ALWAYS_INLINE
155+
float _swift_stdlib_remainderf(float _self, float _other) {
131156
return __builtin_remainderf(_self, _other);
132157
}
133158

134159
static inline SWIFT_ALWAYS_INLINE
135-
float _stdlib_squareRootf(float _self) {
160+
float _swift_stdlib_squareRootf(float _self) {
136161
return __builtin_sqrtf(_self);
137162
}
163+
164+
static inline SWIFT_ALWAYS_INLINE
165+
double _swift_stdlib_tan(double x) {
166+
return __builtin_tan(x);
167+
}
168+
169+
static inline SWIFT_ALWAYS_INLINE
170+
double _swift_stdlib_acos(double x) {
171+
return __builtin_acos(x);
172+
}
173+
174+
static inline SWIFT_ALWAYS_INLINE
175+
double _swift_stdlib_asin(double x) {
176+
return __builtin_asin(x);
177+
}
178+
179+
static inline SWIFT_ALWAYS_INLINE
180+
double _swift_stdlib_atan(double x) {
181+
return __builtin_atan(x);
182+
}
183+
184+
static inline SWIFT_ALWAYS_INLINE
185+
double _swift_stdlib_atan2(double y, double x) {
186+
return __builtin_atan2(y, x);
187+
}
138188

139189
static inline SWIFT_ALWAYS_INLINE
140-
double _stdlib_remainder(double _self, double _other) {
190+
double _swift_stdlib_remainder(double _self, double _other) {
141191
return __builtin_remainder(_self, _other);
142192
}
143193

144194
static inline SWIFT_ALWAYS_INLINE
145-
double _stdlib_squareRoot(double _self) {
195+
double _swift_stdlib_squareRoot(double _self) {
146196
return __builtin_sqrt(_self);
147197
}
148198

149199
#if !defined _WIN32 && (defined __i386__ || defined __x86_64__)
150200
static inline SWIFT_ALWAYS_INLINE
151-
long double _stdlib_remainderl(long double _self, long double _other) {
201+
long double _swift_stdlib_tanl(long double x) {
202+
return __builtin_tanl(x);
203+
}
204+
205+
static inline SWIFT_ALWAYS_INLINE
206+
long double _swift_stdlib_acosl(long double x) {
207+
return __builtin_acosl(x);
208+
}
209+
210+
static inline SWIFT_ALWAYS_INLINE
211+
long double _swift_stdlib_asinl(long double x) {
212+
return __builtin_asinl(x);
213+
}
214+
215+
static inline SWIFT_ALWAYS_INLINE
216+
long double _swift_stdlib_atanl(long double x) {
217+
return __builtin_atanl(x);
218+
}
219+
220+
static inline SWIFT_ALWAYS_INLINE
221+
long double _swift_stdlib_atan2l(long double y, long double x) {
222+
return __builtin_atan2l(y, x);
223+
}
224+
225+
static inline SWIFT_ALWAYS_INLINE
226+
long double _swift_stdlib_remainderl(long double _self, long double _other) {
152227
return __builtin_remainderl(_self, _other);
153228
}
154229

155230
static inline SWIFT_ALWAYS_INLINE
156-
long double _stdlib_squareRootl(long double _self) {
231+
long double _swift_stdlib_squareRootl(long double _self) {
157232
return __builtin_sqrtl(_self);
158233
}
159234
#endif

stdlib/public/core/BuiltinMath.swift.gyb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ def cFuncSuffix(bits):
4545
# These functions have a corresponding LLVM intrinsic
4646
# Note, keep this up to date with Darwin/tgmath.swift.gyb
4747
UnaryIntrinsicFunctions = [
48-
'cos', 'sin',
49-
'exp', 'exp2',
50-
'log', 'log10', 'log2',
51-
'nearbyint', 'rint',
48+
'nearbyint', 'rint',
5249
]
5350

5451
def TypedUnaryIntrinsicFunctions():

stdlib/public/core/FloatingPointTypes.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ extension ${Self}: BinaryFloatingPoint {
11151115
@inlinable // FIXME(inline-always)
11161116
@inline(__always)
11171117
public mutating func formRemainder(dividingBy other: ${Self}) {
1118-
self = _stdlib_remainder${cFuncSuffix}(self, other)
1118+
self = _swift_stdlib_remainder${cFuncSuffix}(self, other)
11191119
}
11201120

11211121
/// Replaces this value with the remainder of itself divided by the given
@@ -1157,7 +1157,7 @@ extension ${Self}: BinaryFloatingPoint {
11571157
/// value.
11581158
@_transparent
11591159
public mutating func formSquareRoot( ) {
1160-
self = _stdlib_squareRoot${cFuncSuffix}(self)
1160+
self = _swift_stdlib_squareRoot${cFuncSuffix}(self)
11611161
}
11621162

11631163
/// Adds the product of the two given values to this value in place, computed

stdlib/public/core/Mathsable.swift.gyb

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,61 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
// We are omitting the following for now, which could also be defined on
14-
// FloatingPoint, as it's not obvious that we want free function
15-
// implementations:
16-
//
17-
// fabs - use Swift.abs instead.
18-
//
19-
// scalbn, frexp, ldexp, ilogb - we should provide a better technique
20-
// for safely rescaling floating-point computations instead of these
21-
// poorly named and easy to misuse functions.
13+
import SwiftShims
2214

2315
%{
2416
from SwiftMathsFunctions import *
2517
}%
2618

19+
/// A type that has basic maths functions available.
20+
///
21+
/// "Basic maths functions" means roughly "the usual transcendental
22+
/// functions provided by `<math.h>` in C-family langauges."
2723
public protocol Mathsable {
2824
associatedtype Maths: MathsImplementations where Maths.Value == Self
2925
}
3026

27+
/// A type that provides the implementation trampolines for maths functions
28+
/// on the related `Value` type.
3129
public protocol MathsImplementations {
3230
associatedtype Value
33-
%for func in MathFunctions:
31+
%for func in MathsFunctions:
3432
static func ${func.decl("Value")}
3533
%end
3634
}
3735

3836
%for type in all_floating_point_types():
37+
% if type.bits == 80:
38+
#if (arch(i386) || arch(x86_64)) && !os(Windows)
39+
% end
3940
extension ${type.stdlib_name}: Mathsable {
41+
/// Defines basic maths functions for ${type.stdlib_name}
4042
@_frozen
4143
public enum Maths: MathsImplementations {
4244
public typealias Value = ${type.stdlib_name}
43-
% for func in MathFunctions:
45+
% for func in MathsFunctions:
4446
@_alwaysEmitIntoClient
4547
public static func ${func.decl("Value")} {
4648
return ${func.impl(type)}
4749
}
4850
% end
4951
}
5052
}
51-
53+
54+
% if type.bits == 80:
55+
#endif
56+
% end
5257
%end
5358

59+
/// Defines basic maths functions operating elementwise on SIMD vector types.
5460
@_fixed_layout
5561
public struct _SIMDMaths<V>: MathsImplementations
5662
where V: SIMD, V.Scalar: Mathsable {
5763

5864
public typealias Value = V
59-
%for func in MathFunctions:
65+
%for func in MathsFunctions:
6066

61-
@inlinable
67+
@_alwaysEmitIntoClient
6268
public static func ${func.decl("V")} {
6369
var r = V()
6470
for i in x.indices {

utils/SwiftMathsFunctions.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from SwiftFloatingPointTypes import all_floating_point_types
22

33
class SwiftMathsFunction(object):
4-
def __init__(self, name, cName=None, intrinsic=False, args="x"):
4+
def __init__(self, name, kind=None, swiftName=None, args="x"):
55
self.name = name
6-
self.cName = cName if cName is not None else name
7-
self.intrinsic = intrinsic
6+
self.swiftName = swiftName if swiftName is not None else name
7+
self.kind = kind if kind is not None else "library"
88
self.args = args
99

1010
def params(self, prefix="", suffix=""):
@@ -13,25 +13,34 @@ def params(self, prefix="", suffix=""):
1313
)
1414

1515
def decl(self, type):
16-
return self.name + "(" + self.params("_ ", ": " + type) + ") -> " + type
16+
return self.swiftName + "(" + self.params("_ ", ": " + type) + ") -> " + type
1717

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

2121
def impl(self, type):
22-
if self.intrinsic:
22+
if self.kind == "intrinsic":
2323
builtin = "Builtin.int_" + self.name + "_FPIEEE" + str(type.bits)
2424
return type.stdlib_name + "(" + builtin + "(" + self.params("","._value") + "))"
25-
return self.cName + type.cFuncSuffix + "(" + self.params() + ")"
25+
if self.kind == "builtin":
26+
builtin = "Builtin." + self.name + "_FPIEEE" + str(type.bits)
27+
return type.stdlib_name + "(" + builtin + "(" + self.params("","._value") + "))"
28+
return "_swift_stdlib_" + self.name + type.cFuncSuffix + "(" + self.params() + ")"
2629

27-
MathFunctions = [
28-
SwiftMathsFunction(name="cos", intrinsic=True),
29-
SwiftMathsFunction(name="sin", intrinsic=True),
30-
SwiftMathsFunction(name="exp", intrinsic=True),
31-
SwiftMathsFunction(name="exp2", intrinsic=True),
32-
SwiftMathsFunction(name="log", intrinsic=True),
33-
SwiftMathsFunction(name="log10", intrinsic=True),
34-
SwiftMathsFunction(name="log2", intrinsic=True),
30+
MathsFunctions = [
31+
SwiftMathsFunction(name="cos", kind="intrinsic"),
32+
SwiftMathsFunction(name="sin", kind="intrinsic"),
33+
SwiftMathsFunction(name="tan"),
34+
SwiftMathsFunction(name="acos"),
35+
SwiftMathsFunction(name="asin"),
36+
SwiftMathsFunction(name="atan"),
37+
SwiftMathsFunction(name="atan2", args="yx"),
38+
SwiftMathsFunction(name="exp", kind="intrinsic"),
39+
SwiftMathsFunction(name="exp2", kind="intrinsic"),
40+
SwiftMathsFunction(name="log", kind="intrinsic"),
41+
SwiftMathsFunction(name="log10", kind="intrinsic"),
42+
SwiftMathsFunction(name="log2", kind="intrinsic"),
43+
SwiftMathsFunction(name="pow", kind="intrinsic", args="xy"),
3544
]
3645

3746

0 commit comments

Comments
 (0)