- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathMathFunctions.swift.gyb
180 lines (157 loc) · 5.55 KB
/
MathFunctions.swift.gyb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//===--- MathFunctions.swift ----------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftShims
%from SwiftMathFunctions import *
%from SwiftFloatingPointTypes import all_floating_point_types
%# Skip `Float16` for now until it's clear how to conform it to
%# `ElementaryFunctions`, i.e. after apple/swift-numerics adds the conformance.
%floating_point_types =[type for type in all_floating_point_types() if type.bits !=16]
/// A type that has elementary functions available.
///
/// An ["elementary function"][elfn] is a function built up from powers, roots,
/// exponentials, logarithms, trigonometric functions (sin, cos, tan) and
/// their inverses, and the hyperbolic functions (sinh, cosh, tanh) and their
/// inverses.
///
/// Conformance to this protocol means that all of these building blocks are
/// available as static functions on the type.
///
/// ```swift
/// let x: Float = 1
/// let y = Float.sin(x) // 0.84147096
/// ```
///
/// [elfn]: http://en.wikipedia.org/wiki/Elementary_function
// SWIFT_ENABLE_TENSORFLOW
// NOTE(TF-796): Make `ElementaryFunctions` available on macOS.
// @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
publicprotocolElementaryFunctions{
%for func in ElementaryFunctions:
${func.comment}
staticfunc ${func.decl("Self")}
%end
/// `exp(y log(x))` computed without loss of intermediate precision.
///
/// For real types, if `x` is negative the result is NaN, even if `y` has
/// an integral value. For complex types, there is a branch cut on the
/// negative real axis.
staticfunc pow(_ x:Self, _ y:Self)-> Self
/// `x` raised to the `n`th power.
static func pow(_ x:Self, _ n:Int)-> Self
/// The `n`th root of `x`.
///
/// For real types, if `x` is negative and `n` is even, the result is NaN.
/// For complex types, there is a branch cut along the negative real axis.
static func root(_ x:Self, _ n:Int)->Self
}
%for type in floating_point_types:
% if type.bits==80:
#if (arch(i386) || arch(x86_64)) && !(os(Windows) || os(Android))
% end
% Self = type.stdlib_name
extension ${Self}: ElementaryFunctions {
% for func in ElementaryFunctions + RealFunctions:
@_alwaysEmitIntoClient
publicstaticfunc ${func.decl(Self)}{
return ${func.impl(type)}
}
% end
@_alwaysEmitIntoClient
publicstaticfunc pow(_ x: ${Self}, _ y: ${Self})-> ${Self}{
guard x >=0else{return.nan }
return ${Self}(Builtin.int_pow_FPIEEE${type.bits}(x._value, y._value))
}
@_alwaysEmitIntoClient
publicstaticfunc pow(_ x: ${Self}, _ n:Int)-> ${Self}{
// TODO: this implementation isn't quite right for n so large that
// the conversion to `${Self}` rounds. We could also consider using
// a multiply-chain implementation for small `n`; this would be faster
// for static `n`, but less accurate on platforms with a good `pow`
// implementation.
return ${Self}(Builtin.int_pow_FPIEEE${type.bits}(x._value, ${Self}(n)._value))
}
@_alwaysEmitIntoClient
publicstaticfunc root(_ x: ${Self}, _ n:Int)-> ${Self}{
guard x >=0 || n %2!=0else{return.nan }
// TODO: this implementation isn't quite right for n so large that
// the conversion to `${Self}` rounds.
return ${Self}(signOf: x, magnitudeOf:pow(x.magnitude,1/${Self}(n)))
}
@_alwaysEmitIntoClient
publicstaticfunc atan2(_ y: ${Self}, _ x: ${Self})-> ${Self}{
return_stdlib_atan2${type.cFuncSuffix}(y, x)
}
#if !os(Windows)
@_alwaysEmitIntoClient
publicstaticfunc logGamma(_ x: ${Self})-> ${Self}{
return_stdlib_lgamma${type.cFuncSuffix}(x)
}
@_alwaysEmitIntoClient
publicstaticfunc signGamma(_ x: ${Self})->FloatingPointSign{
if x >=0{return.plus }
lettrunc= x.rounded(.towardZero)
if x == trunc {return.plus }
lethalfTrunc= trunc/2
if halfTrunc == halfTrunc.rounded(.towardZero){return.minus }
return.plus
}
#endif
}
% if type.bits ==80:
#endif
% end
%end
// SWIFT_ENABLE_TENSORFLOW
// NOTE(TF-796): Make `ElementaryFunctions` available on macOS.
// @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
extensionSIMDwhere Scalar:ElementaryFunctions{
% for func in ElementaryFunctions:
@_alwaysEmitIntoClient
publicstaticfunc ${func.decl("Self")}{
varr=Self()
foriin r.indices {
r[i]=Scalar.${func.swiftName}(${func.params(suffix="[i]")})
}
return r
}
% end
@_alwaysEmitIntoClient
publicstaticfunc pow(_ x:Self, _ y:Self)->Self{
varr=Self()
foriin r.indices {
r[i]=Scalar.pow(x[i],y[i])
}
return r
}
@_alwaysEmitIntoClient
publicstaticfunc pow(_ x:Self, _ n:Int)->Self{
varr=Self()
foriin r.indices {
r[i]=Scalar.pow(x[i], n)
}
return r
}
@_alwaysEmitIntoClient
publicstaticfunc root(_ x:Self, _ n:Int)->Self{
varr=Self()
foriin r.indices {
r[i]=Scalar.root(x[i], n)
}
return r
}
}
%for n in [2,3,4,8,16,32,64]:
// SWIFT_ENABLE_TENSORFLOW
// NOTE(TF-796): Make `ElementaryFunctions` available on macOS.
// @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
extension SIMD${n}: ElementaryFunctions where Scalar: ElementaryFunctions {}
%end