libm/math/
lgammaf_r.rs
1use super::{floorf, k_cosf, k_sinf, logf};
17
18const PI: f32 = 3.1415927410e+00; const A0: f32 = 7.7215664089e-02; const A1: f32 = 3.2246702909e-01; const A2: f32 = 6.7352302372e-02; const A3: f32 = 2.0580807701e-02; const A4: f32 = 7.3855509982e-03; const A5: f32 = 2.8905137442e-03; const A6: f32 = 1.1927076848e-03; const A7: f32 = 5.1006977446e-04; const A8: f32 = 2.2086278477e-04; const A9: f32 = 1.0801156895e-04; const A10: f32 = 2.5214456400e-05; const A11: f32 = 4.4864096708e-05; const TC: f32 = 1.4616321325e+00; const TF: f32 = -1.2148628384e-01; const TT: f32 = 6.6971006518e-09; const T0: f32 = 4.8383611441e-01; const T1: f32 = -1.4758771658e-01; const T2: f32 = 6.4624942839e-02; const T3: f32 = -3.2788541168e-02; const T4: f32 = 1.7970675603e-02; const T5: f32 = -1.0314224288e-02; const T6: f32 = 6.1005386524e-03; const T7: f32 = -3.6845202558e-03; const T8: f32 = 2.2596477065e-03; const T9: f32 = -1.4034647029e-03; const T10: f32 = 8.8108185446e-04; const T11: f32 = -5.3859531181e-04; const T12: f32 = 3.1563205994e-04; const T13: f32 = -3.1275415677e-04; const T14: f32 = 3.3552918467e-04; const U0: f32 = -7.7215664089e-02; const U1: f32 = 6.3282704353e-01; const U2: f32 = 1.4549225569e+00; const U3: f32 = 9.7771751881e-01; const U4: f32 = 2.2896373272e-01; const U5: f32 = 1.3381091878e-02; const V1: f32 = 2.4559779167e+00; const V2: f32 = 2.1284897327e+00; const V3: f32 = 7.6928514242e-01; const V4: f32 = 1.0422264785e-01; const V5: f32 = 3.2170924824e-03; const S0: f32 = -7.7215664089e-02; const S1: f32 = 2.1498242021e-01; const S2: f32 = 3.2577878237e-01; const S3: f32 = 1.4635047317e-01; const S4: f32 = 2.6642270386e-02; const S5: f32 = 1.8402845599e-03; const S6: f32 = 3.1947532989e-05; const R1: f32 = 1.3920053244e+00; const R2: f32 = 7.2193557024e-01; const R3: f32 = 1.7193385959e-01; const R4: f32 = 1.8645919859e-02; const R5: f32 = 7.7794247773e-04; const R6: f32 = 7.3266842264e-06; const W0: f32 = 4.1893854737e-01; const W1: f32 = 8.3333335817e-02; const W2: f32 = -2.7777778450e-03; const W3: f32 = 7.9365057172e-04; const W4: f32 = -5.9518753551e-04; const W5: f32 = 8.3633989561e-04; const W6: f32 = -1.6309292987e-03; fn sin_pi(mut x: f32) -> f32 {
84 let mut y: f64;
85 let mut n: isize;
86
87 x = 2.0 * (x * 0.5 - floorf(x * 0.5)); n = (x * 4.0) as isize;
91 n = div!(n + 1, 2);
92 y = (x as f64) - (n as f64) * 0.5;
93 y *= 3.14159265358979323846;
94 match n {
95 1 => k_cosf(y),
96 2 => k_sinf(-y),
97 3 => -k_cosf(y),
98 0 | _ => k_sinf(y),
99 }
100}
101
102#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
103pub fn lgammaf_r(mut x: f32) -> (f32, i32) {
104 let u = x.to_bits();
105 let mut t: f32;
106 let y: f32;
107 let mut z: f32;
108 let nadj: f32;
109 let p: f32;
110 let p1: f32;
111 let p2: f32;
112 let p3: f32;
113 let q: f32;
114 let mut r: f32;
115 let w: f32;
116 let ix: u32;
117 let i: i32;
118 let sign: bool;
119 let mut signgam: i32;
120
121 signgam = 1;
123 sign = (u >> 31) != 0;
124 ix = u & 0x7fffffff;
125 if ix >= 0x7f800000 {
126 return (x * x, signgam);
127 }
128 if ix < 0x35000000 {
129 if sign {
131 signgam = -1;
132 x = -x;
133 }
134 return (-logf(x), signgam);
135 }
136 if sign {
137 x = -x;
138 t = sin_pi(x);
139 if t == 0.0 {
140 return (1.0 / (x - x), signgam);
142 }
143 if t > 0.0 {
144 signgam = -1;
145 } else {
146 t = -t;
147 }
148 nadj = logf(PI / (t * x));
149 } else {
150 nadj = 0.0;
151 }
152
153 if ix == 0x3f800000 || ix == 0x40000000 {
155 r = 0.0;
156 }
157 else if ix < 0x40000000 {
159 if ix <= 0x3f666666 {
160 r = -logf(x);
162 if ix >= 0x3f3b4a20 {
163 y = 1.0 - x;
164 i = 0;
165 } else if ix >= 0x3e6d3308 {
166 y = x - (TC - 1.0);
167 i = 1;
168 } else {
169 y = x;
170 i = 2;
171 }
172 } else {
173 r = 0.0;
174 if ix >= 0x3fdda618 {
175 y = 2.0 - x;
177 i = 0;
178 } else if ix >= 0x3F9da620 {
179 y = x - TC;
181 i = 1;
182 } else {
183 y = x - 1.0;
184 i = 2;
185 }
186 }
187 match i {
188 0 => {
189 z = y * y;
190 p1 = A0 + z * (A2 + z * (A4 + z * (A6 + z * (A8 + z * A10))));
191 p2 = z * (A1 + z * (A3 + z * (A5 + z * (A7 + z * (A9 + z * A11)))));
192 p = y * p1 + p2;
193 r += p - 0.5 * y;
194 }
195 1 => {
196 z = y * y;
197 w = z * y;
198 p1 = T0 + w * (T3 + w * (T6 + w * (T9 + w * T12))); p2 = T1 + w * (T4 + w * (T7 + w * (T10 + w * T13)));
200 p3 = T2 + w * (T5 + w * (T8 + w * (T11 + w * T14)));
201 p = z * p1 - (TT - w * (p2 + y * p3));
202 r += TF + p;
203 }
204 2 => {
205 p1 = y * (U0 + y * (U1 + y * (U2 + y * (U3 + y * (U4 + y * U5)))));
206 p2 = 1.0 + y * (V1 + y * (V2 + y * (V3 + y * (V4 + y * V5))));
207 r += -0.5 * y + p1 / p2;
208 }
209 #[cfg(debug_assertions)]
210 _ => unreachable!(),
211 #[cfg(not(debug_assertions))]
212 _ => {}
213 }
214 } else if ix < 0x41000000 {
215 i = x as i32;
217 y = x - (i as f32);
218 p = y * (S0 + y * (S1 + y * (S2 + y * (S3 + y * (S4 + y * (S5 + y * S6))))));
219 q = 1.0 + y * (R1 + y * (R2 + y * (R3 + y * (R4 + y * (R5 + y * R6)))));
220 r = 0.5 * y + p / q;
221 z = 1.0; if i >= 7 {
225 z *= y + 6.0;
226 }
227 if i >= 6 {
228 z *= y + 5.0;
229 }
230 if i >= 5 {
231 z *= y + 4.0;
232 }
233 if i >= 4 {
234 z *= y + 3.0;
235 }
236 if i >= 3 {
237 z *= y + 2.0;
238 r += logf(z);
239 }
240 } else if ix < 0x5c800000 {
241 t = logf(x);
243 z = 1.0 / x;
244 y = z * z;
245 w = W0 + z * (W1 + y * (W2 + y * (W3 + y * (W4 + y * (W5 + y * W6)))));
246 r = (x - 0.5) * (t - 1.0) + w;
247 } else {
248 r = x * (logf(x) - 1.0);
250 }
251 if sign {
252 r = nadj - r;
253 }
254 return (r, signgam);
255}