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
use super::{expf, fabsf};
const ERX: f32 = 8.4506291151e-01;
const EFX8: f32 = 1.0270333290e+00;
const PP0: f32 = 1.2837916613e-01;
const PP1: f32 = -3.2504209876e-01;
const PP2: f32 = -2.8481749818e-02;
const PP3: f32 = -5.7702702470e-03;
const PP4: f32 = -2.3763017452e-05;
const QQ1: f32 = 3.9791721106e-01;
const QQ2: f32 = 6.5022252500e-02;
const QQ3: f32 = 5.0813062117e-03;
const QQ4: f32 = 1.3249473704e-04;
const QQ5: f32 = -3.9602282413e-06;
const PA0: f32 = -2.3621185683e-03;
const PA1: f32 = 4.1485610604e-01;
const PA2: f32 = -3.7220788002e-01;
const PA3: f32 = 3.1834661961e-01;
const PA4: f32 = -1.1089469492e-01;
const PA5: f32 = 3.5478305072e-02;
const PA6: f32 = -2.1663755178e-03;
const QA1: f32 = 1.0642088205e-01;
const QA2: f32 = 5.4039794207e-01;
const QA3: f32 = 7.1828655899e-02;
const QA4: f32 = 1.2617121637e-01;
const QA5: f32 = 1.3637083583e-02;
const QA6: f32 = 1.1984500103e-02;
const RA0: f32 = -9.8649440333e-03;
const RA1: f32 = -6.9385856390e-01;
const RA2: f32 = -1.0558626175e+01;
const RA3: f32 = -6.2375331879e+01;
const RA4: f32 = -1.6239666748e+02;
const RA5: f32 = -1.8460508728e+02;
const RA6: f32 = -8.1287437439e+01;
const RA7: f32 = -9.8143291473e+00;
const SA1: f32 = 1.9651271820e+01;
const SA2: f32 = 1.3765776062e+02;
const SA3: f32 = 4.3456588745e+02;
const SA4: f32 = 6.4538726807e+02;
const SA5: f32 = 4.2900814819e+02;
const SA6: f32 = 1.0863500214e+02;
const SA7: f32 = 6.5702495575e+00;
const SA8: f32 = -6.0424413532e-02;
const RB0: f32 = -9.8649431020e-03;
const RB1: f32 = -7.9928326607e-01;
const RB2: f32 = -1.7757955551e+01;
const RB3: f32 = -1.6063638306e+02;
const RB4: f32 = -6.3756646729e+02;
const RB5: f32 = -1.0250950928e+03;
const RB6: f32 = -4.8351919556e+02;
const SB1: f32 = 3.0338060379e+01;
const SB2: f32 = 3.2579251099e+02;
const SB3: f32 = 1.5367296143e+03;
const SB4: f32 = 3.1998581543e+03;
const SB5: f32 = 2.5530502930e+03;
const SB6: f32 = 4.7452853394e+02;
const SB7: f32 = -2.2440952301e+01;
fn erfc1(x: f32) -> f32 {
let s: f32;
let p: f32;
let q: f32;
s = fabsf(x) - 1.0;
p = PA0 + s * (PA1 + s * (PA2 + s * (PA3 + s * (PA4 + s * (PA5 + s * PA6)))));
q = 1.0 + s * (QA1 + s * (QA2 + s * (QA3 + s * (QA4 + s * (QA5 + s * QA6)))));
return 1.0 - ERX - p / q;
}
fn erfc2(mut ix: u32, mut x: f32) -> f32 {
let s: f32;
let r: f32;
let big_s: f32;
let z: f32;
if ix < 0x3fa00000 {
return erfc1(x);
}
x = fabsf(x);
s = 1.0 / (x * x);
if ix < 0x4036db6d {
r = RA0 + s * (RA1 + s * (RA2 + s * (RA3 + s * (RA4 + s * (RA5 + s * (RA6 + s * RA7))))));
big_s = 1.0
+ s * (SA1
+ s * (SA2 + s * (SA3 + s * (SA4 + s * (SA5 + s * (SA6 + s * (SA7 + s * SA8)))))));
} else {
r = RB0 + s * (RB1 + s * (RB2 + s * (RB3 + s * (RB4 + s * (RB5 + s * RB6)))));
big_s =
1.0 + s * (SB1 + s * (SB2 + s * (SB3 + s * (SB4 + s * (SB5 + s * (SB6 + s * SB7))))));
}
ix = x.to_bits();
z = f32::from_bits(ix & 0xffffe000);
expf(-z * z - 0.5625) * expf((z - x) * (z + x) + r / big_s) / x
}
pub fn erff(x: f32) -> f32 {
let r: f32;
let s: f32;
let z: f32;
let y: f32;
let mut ix: u32;
let sign: usize;
ix = x.to_bits();
sign = (ix >> 31) as usize;
ix &= 0x7fffffff;
if ix >= 0x7f800000 {
return 1.0 - 2.0 * (sign as f32) + 1.0 / x;
}
if ix < 0x3f580000 {
if ix < 0x31800000 {
return 0.125 * (8.0 * x + EFX8 * x);
}
z = x * x;
r = PP0 + z * (PP1 + z * (PP2 + z * (PP3 + z * PP4)));
s = 1.0 + z * (QQ1 + z * (QQ2 + z * (QQ3 + z * (QQ4 + z * QQ5))));
y = r / s;
return x + x * y;
}
if ix < 0x40c00000 {
y = 1.0 - erfc2(ix, x);
} else {
let x1p_120 = f32::from_bits(0x03800000);
y = 1.0 - x1p_120;
}
if sign != 0 {
-y
} else {
y
}
}
pub fn erfcf(x: f32) -> f32 {
let r: f32;
let s: f32;
let z: f32;
let y: f32;
let mut ix: u32;
let sign: usize;
ix = x.to_bits();
sign = (ix >> 31) as usize;
ix &= 0x7fffffff;
if ix >= 0x7f800000 {
return 2.0 * (sign as f32) + 1.0 / x;
}
if ix < 0x3f580000 {
if ix < 0x23800000 {
return 1.0 - x;
}
z = x * x;
r = PP0 + z * (PP1 + z * (PP2 + z * (PP3 + z * PP4)));
s = 1.0 + z * (QQ1 + z * (QQ2 + z * (QQ3 + z * (QQ4 + z * QQ5))));
y = r / s;
if sign != 0 || ix < 0x3e800000 {
return 1.0 - (x + x * y);
}
return 0.5 - (x - 0.5 + x * y);
}
if ix < 0x41e00000 {
if sign != 0 {
return 2.0 - erfc2(ix, x);
} else {
return erfc2(ix, x);
}
}
let x1p_120 = f32::from_bits(0x03800000);
if sign != 0 {
2.0 - x1p_120
} else {
x1p_120 * x1p_120
}
}