|
1 |
| -from SwiftFloatingPointTypes import all_floating_point_types |
2 |
| - |
3 | 1 | class SwiftMathFunction(object):
|
4 |
| - def __init__(self, name, kind=None, swiftName=None, args="x", comment=None, platforms=None): |
5 |
| - self.name = name |
6 |
| - self.swiftName = swiftName if swiftName is not None else name |
7 |
| - self.kind = kind if kind is not None else "library" |
8 |
| - self.args = args |
9 |
| - self.comment = comment if comment is not None else "/// The " + str(self.swiftName) + " function." |
10 |
| - self.platforms = platforms |
| 2 | + def __init__(self, name, kind=None, swiftName=None, args="x", comment=None, |
| 3 | + platforms=None): |
| 4 | + self.name = name |
| 5 | + self.swiftName = swiftName if swiftName is not None else name |
| 6 | + self.kind = kind if kind is not None else "library" |
| 7 | + self.args = args |
| 8 | + if comment is not None: |
| 9 | + self.comment = comment |
| 10 | + else: |
| 11 | + self.comment = "/// The " + str(self.swiftName) + " function." |
| 12 | + self.platforms = platforms |
| 13 | + |
| 14 | + def params(self, prefix="", suffix=""): |
| 15 | + return ", ".join(map(lambda a: prefix + a + suffix, self.args)) |
| 16 | + |
| 17 | + def decl(self, type): |
| 18 | + return self.swiftName + "(" + self.params("_ ", ": " + type) + \ |
| 19 | + ") -> " + type |
| 20 | + |
| 21 | + def free_decl(self, constraint="T: ElementaryFunctions"): |
| 22 | + return self.swiftName + "<T>(" + self.params("_ ", ": T") + \ |
| 23 | + ") -> T where " + constraint |
| 24 | + |
| 25 | + def impl(self, type): |
| 26 | + if self.kind == "intrinsic": |
| 27 | + builtin = "Builtin.int_" + self.name + "_FPIEEE" + str(type.bits) |
| 28 | + return type.stdlib_name + "(" + builtin + "(" + \ |
| 29 | + self.params("", "._value") + "))" |
| 30 | + return "_swift_stdlib_" + self.name + type.cFuncSuffix + "(" + \ |
| 31 | + self.params() + ")" |
11 | 32 |
|
12 |
| - def params(self, prefix="", suffix=""): |
13 |
| - return ", ".join( |
14 |
| - map(lambda a: prefix + a + suffix, self.args) |
15 |
| - ) |
16 |
| - |
17 |
| - def decl(self, type): |
18 |
| - return self.swiftName + "(" + self.params("_ ", ": " + type) + ") -> " + type |
19 |
| - |
20 |
| - def free_decl(self, constraint="T: ElementaryFunctions"): |
21 |
| - return self.swiftName + "<T>(" + self.params("_ ", ": T") + ") -> T where " + constraint |
22 |
| - |
23 |
| - def impl(self, type): |
24 |
| - if self.kind == "intrinsic": |
25 |
| - builtin = "Builtin.int_" + self.name + "_FPIEEE" + str(type.bits) |
26 |
| - return type.stdlib_name + "(" + builtin + "(" + self.params("","._value") + "))" |
27 |
| - return "_swift_stdlib_" + self.name + type.cFuncSuffix + "(" + self.params() + ")" |
28 | 33 |
|
29 | 34 | ElementaryFunctions = [
|
30 |
| - SwiftMathFunction(name="sqrt", kind="intrinsic", comment="/// The square root of `x`."), |
31 |
| - SwiftMathFunction(name="cos", kind="intrinsic", comment="/// The cosine of `x`."), |
32 |
| - SwiftMathFunction(name="sin", kind="intrinsic", comment="/// The sine of `x`."), |
33 |
| - SwiftMathFunction(name="tan", comment="/// The tangent of `x`."), |
34 |
| - SwiftMathFunction(name="acos"), |
35 |
| - SwiftMathFunction(name="asin"), |
36 |
| - SwiftMathFunction(name="atan"), |
37 |
| - SwiftMathFunction(name="cosh"), |
38 |
| - SwiftMathFunction(name="sinh"), |
39 |
| - SwiftMathFunction(name="tanh"), |
40 |
| - SwiftMathFunction(name="acosh"), |
41 |
| - SwiftMathFunction(name="asinh"), |
42 |
| - SwiftMathFunction(name="atanh"), |
43 |
| - SwiftMathFunction(name="exp", kind="intrinsic"), |
44 |
| - SwiftMathFunction(name="exp2", kind="intrinsic"), |
45 |
| - SwiftMathFunction(name="exp10"), |
46 |
| - SwiftMathFunction(name="expm1"), |
47 |
| - SwiftMathFunction(name="log", kind="intrinsic"), |
48 |
| - SwiftMathFunction(name="log2", kind="intrinsic"), |
49 |
| - SwiftMathFunction(name="log10", kind="intrinsic"), |
50 |
| - SwiftMathFunction(name="log1p"), |
51 |
| -# SwiftMathFunction(name="pow", kind="intrinsic", args="xy"), Handled separately for edge cases. |
52 |
| -# SwiftMathFunction(name="root", args="xn"), Handled separately for implementation. |
| 35 | + SwiftMathFunction(name="sqrt", kind="intrinsic", |
| 36 | + comment="/// The square root of `x`."), |
| 37 | + SwiftMathFunction(name="cos", kind="intrinsic", |
| 38 | + comment="/// The cosine of `x`."), |
| 39 | + SwiftMathFunction(name="sin", kind="intrinsic", |
| 40 | + comment="/// The sine of `x`."), |
| 41 | + SwiftMathFunction(name="tan", |
| 42 | + comment="/// The tangent of `x`."), |
| 43 | + SwiftMathFunction(name="acos"), |
| 44 | + SwiftMathFunction(name="asin"), |
| 45 | + SwiftMathFunction(name="atan"), |
| 46 | + SwiftMathFunction(name="cosh"), |
| 47 | + SwiftMathFunction(name="sinh"), |
| 48 | + SwiftMathFunction(name="tanh"), |
| 49 | + SwiftMathFunction(name="acosh"), |
| 50 | + SwiftMathFunction(name="asinh"), |
| 51 | + SwiftMathFunction(name="atanh"), |
| 52 | + SwiftMathFunction(name="exp", kind="intrinsic"), |
| 53 | + SwiftMathFunction(name="exp2", kind="intrinsic"), |
| 54 | + SwiftMathFunction(name="exp10"), |
| 55 | + SwiftMathFunction(name="expm1"), |
| 56 | + SwiftMathFunction(name="log", kind="intrinsic"), |
| 57 | + SwiftMathFunction(name="log2", kind="intrinsic"), |
| 58 | + SwiftMathFunction(name="log10", kind="intrinsic"), |
| 59 | + SwiftMathFunction(name="log1p"), |
| 60 | + # SwiftMathFunction(name="pow", kind="intrinsic", args="xy"), Handled |
| 61 | + # separately for edge cases. |
| 62 | + # SwiftMathFunction(name="root", args="xn"), Handled separately for |
| 63 | + # implementation. |
53 | 64 | ]
|
54 | 65 |
|
55 | 66 | RealFunctions = [
|
56 |
| -# SwiftMathFunction(name="atan2"), Handled separately for explicit arg labels. |
57 |
| - SwiftMathFunction(name="erf"), |
58 |
| - SwiftMathFunction(name="erfc"), |
59 |
| - SwiftMathFunction(name="hypot", args="xy"), |
60 |
| - SwiftMathFunction(name="tgamma", swiftName="gamma"), |
61 |
| -# SwiftMathFunction(name="lgamma"), Handled separately to handle sign result. |
| 67 | + # SwiftMathFunction(name="atan2"), Handled separately for explicit |
| 68 | + # argument labels. |
| 69 | + SwiftMathFunction(name="erf"), |
| 70 | + SwiftMathFunction(name="erfc"), |
| 71 | + SwiftMathFunction(name="hypot", args="xy"), |
| 72 | + SwiftMathFunction(name="tgamma", swiftName="gamma"), |
| 73 | + # SwiftMathFunction(name="lgamma"), Handled separately for sign result. |
62 | 74 | ]
|
0 commit comments