- Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathDouble+Real.swift
232 lines (200 loc) · 6.41 KB
/
Double+Real.swift
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//===--- Double+Real.swift ------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import _NumericsShims
extensionDouble:Real{
@_transparent
publicstaticfunc cos(_ x:Double)->Double{
libm_cos(x)
}
@_transparent
publicstaticfunc sin(_ x:Double)->Double{
libm_sin(x)
}
@_transparent
publicstaticfunc tan(_ x:Double)->Double{
libm_tan(x)
}
@_transparent
publicstaticfunc acos(_ x:Double)->Double{
libm_acos(x)
}
@_transparent
publicstaticfunc asin(_ x:Double)->Double{
libm_asin(x)
}
@_transparent
publicstaticfunc atan(_ x:Double)->Double{
libm_atan(x)
}
@_transparent
publicstaticfunc cosh(_ x:Double)->Double{
libm_cosh(x)
}
@_transparent
publicstaticfunc sinh(_ x:Double)->Double{
libm_sinh(x)
}
@_transparent
publicstaticfunc tanh(_ x:Double)->Double{
libm_tanh(x)
}
@_transparent
publicstaticfunc acosh(_ x:Double)->Double{
libm_acosh(x)
}
@_transparent
publicstaticfunc asinh(_ x:Double)->Double{
libm_asinh(x)
}
@_transparent
publicstaticfunc atanh(_ x:Double)->Double{
libm_atanh(x)
}
@_transparent
publicstaticfunc exp(_ x:Double)->Double{
libm_exp(x)
}
@_transparent
publicstaticfunc expMinusOne(_ x:Double)->Double{
libm_expm1(x)
}
@_transparent
publicstaticfunc log(_ x:Double)->Double{
libm_log(x)
}
@_transparent
publicstaticfunc log(onePlus x:Double)->Double{
libm_log1p(x)
}
@_transparent
publicstaticfunc erf(_ x:Double)->Double{
libm_erf(x)
}
@_transparent
publicstaticfunc erfc(_ x:Double)->Double{
libm_erfc(x)
}
@_transparent
publicstaticfunc exp2(_ x:Double)->Double{
libm_exp2(x)
}
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
@_transparent
publicstaticfunc exp10(_ x:Double)->Double{
libm_exp10(x)
}
#endif
#if os(macOS) && arch(x86_64)
// Workaround for macOS bug (<rdar://problem/56844150>) where hypot can
// overflow for values very close to the overflow boundary of the naive
// algorithm. Since this is only for macOS, we can just unconditionally
// use Float80, which makes the implementation trivial.
publicstaticfunc hypot(_ x:Double, _ y:Double)->Double{
if x.isInfinite || y.isInfinite {return.infinity }
letx80=Float80(x)
lety80=Float80(y)
returnDouble(Float80.sqrt(x80*x80 + y80*y80))
}
#else
@_transparent
publicstaticfunc hypot(_ x:Double, _ y:Double)->Double{
libm_hypot(x, y)
}
#endif
@_transparent
publicstaticfunc gamma(_ x:Double)->Double{
libm_tgamma(x)
}
@_transparent
publicstaticfunc log2(_ x:Double)->Double{
libm_log2(x)
}
@_transparent
publicstaticfunc log10(_ x:Double)->Double{
libm_log10(x)
}
@_transparent
publicstaticfunc pow(_ x:Double, _ y:Double)->Double{
guard x >=0else{return.nan }
if x ==0 && y ==0{return.nan }
returnlibm_pow(x, y)
}
@_transparent
publicstaticfunc pow(_ x:Double, _ n:Int)->Double{
// If n is exactly representable as Double, we can just call pow:
// Note that all calls on a 32b platform go down this path.
iflet y =Double(exactly: n){returnlibm_pow(x, y)}
// n is not representable in Double, so we will split it into two parts,
// low and high, such that (high + low) = n, and use the identity:
//
// x**(high + low) = x**high * x**low.
//
// We put the high-order 32 bits into high, and the remaining 32 bits
// in low.
//
// The exact split isn't important; all we need is that both pieces get
// less than 53 bits (so that they are exact) and that they both have
// the same sign as n.
//
// This second point is a little bit subtle--why is
// it necessary? Consider what would happen if we took x = 2 and
// n = Int.min + Int(UInt32.max), and simply naively split n without
// taking care with the sign. We would end up computing:
//
// 2**n = 2**Int.min * 2**UInt32.max
//
// The first exponent is negative, the second positive, so the first term
// underflows to zero, and the second overflows to infinity, so the final
// result is NaN, when it should be zero. In order to avoid this
// situation, we make sure that high contains n rounded *towards zero*,
// rather than using simple two's-complement truncation (which rounds
// down).
letmask=Int(truncatingIfNeeded:UInt32.max)
letround= n <0? mask :0
// The addition and subtraction below cannot actually overflow (proof:
// round is positive if n is negative, and zero otherwise, so n + round
// is guaranteed to be representable, and n and high have the same sign,
// so n - high is also representable), but it's hard to tell the compiler
// that, so I'm using wrapping operations instead.
lethigh=(n &+ round)&~mask
letlow= n &- high
returnlibm_pow(x,Double(low))* libm_pow(x,Double(high))
}
@_transparent
publicstaticfunc root(_ x:Double, _ n:Int)->Double{
guard x >=0 || n %2!=0else{return.nan }
// Workaround the issue mentioned below for the specific case of n = 3
// where we can fallback on cbrt.
if n ==3{returnlibm_cbrt(x)}
// TODO: this implementation is not quite correct, because either n or
// 1/n may be not be representable as Double.
returnDouble(signOf: x, magnitudeOf:libm_pow(x.magnitude,1/Double(n)))
}
@_transparent
publicstaticfunc atan2(y:Double, x:Double)->Double{
libm_atan2(y, x)
}
#if !os(Windows)
@_transparent
publicstaticfunc logGamma(_ x:Double)->Double{
vardontCare:Int32=0
returnlibm_lgamma(x,&dontCare)
}
#endif
@_transparent
publicstaticfunc _relaxedAdd(_ a:Double, _ b:Double)->Double{
_numerics_relaxed_add(a, b)
}
@_transparent
publicstaticfunc _relaxedMul(_ a:Double, _ b:Double)->Double{
_numerics_relaxed_mul(a, b)
}
}