use super::{floor, k_cos, k_sin, log};
const PI: f64 = 3.14159265358979311600e+00;
const A0: f64 = 7.72156649015328655494e-02;
const A1: f64 = 3.22467033424113591611e-01;
const A2: f64 = 6.73523010531292681824e-02;
const A3: f64 = 2.05808084325167332806e-02;
const A4: f64 = 7.38555086081402883957e-03;
const A5: f64 = 2.89051383673415629091e-03;
const A6: f64 = 1.19270763183362067845e-03;
const A7: f64 = 5.10069792153511336608e-04;
const A8: f64 = 2.20862790713908385557e-04;
const A9: f64 = 1.08011567247583939954e-04;
const A10: f64 = 2.52144565451257326939e-05;
const A11: f64 = 4.48640949618915160150e-05;
const TC: f64 = 1.46163214496836224576e+00;
const TF: f64 = -1.21486290535849611461e-01;
const TT: f64 = -3.63867699703950536541e-18;
const T0: f64 = 4.83836122723810047042e-01;
const T1: f64 = -1.47587722994593911752e-01;
const T2: f64 = 6.46249402391333854778e-02;
const T3: f64 = -3.27885410759859649565e-02;
const T4: f64 = 1.79706750811820387126e-02;
const T5: f64 = -1.03142241298341437450e-02;
const T6: f64 = 6.10053870246291332635e-03;
const T7: f64 = -3.68452016781138256760e-03;
const T8: f64 = 2.25964780900612472250e-03;
const T9: f64 = -1.40346469989232843813e-03;
const T10: f64 = 8.81081882437654011382e-04;
const T11: f64 = -5.38595305356740546715e-04;
const T12: f64 = 3.15632070903625950361e-04;
const T13: f64 = -3.12754168375120860518e-04;
const T14: f64 = 3.35529192635519073543e-04;
const U0: f64 = -7.72156649015328655494e-02;
const U1: f64 = 6.32827064025093366517e-01;
const U2: f64 = 1.45492250137234768737e+00;
const U3: f64 = 9.77717527963372745603e-01;
const U4: f64 = 2.28963728064692451092e-01;
const U5: f64 = 1.33810918536787660377e-02;
const V1: f64 = 2.45597793713041134822e+00;
const V2: f64 = 2.12848976379893395361e+00;
const V3: f64 = 7.69285150456672783825e-01;
const V4: f64 = 1.04222645593369134254e-01;
const V5: f64 = 3.21709242282423911810e-03;
const S0: f64 = -7.72156649015328655494e-02;
const S1: f64 = 2.14982415960608852501e-01;
const S2: f64 = 3.25778796408930981787e-01;
const S3: f64 = 1.46350472652464452805e-01;
const S4: f64 = 2.66422703033638609560e-02;
const S5: f64 = 1.84028451407337715652e-03;
const S6: f64 = 3.19475326584100867617e-05;
const R1: f64 = 1.39200533467621045958e+00;
const R2: f64 = 7.21935547567138069525e-01;
const R3: f64 = 1.71933865632803078993e-01;
const R4: f64 = 1.86459191715652901344e-02;
const R5: f64 = 7.77942496381893596434e-04;
const R6: f64 = 7.32668430744625636189e-06;
const W0: f64 = 4.18938533204672725052e-01;
const W1: f64 = 8.33333333333329678849e-02;
const W2: f64 = -2.77777777728775536470e-03;
const W3: f64 = 7.93650558643019558500e-04;
const W4: f64 = -5.95187557450339963135e-04;
const W5: f64 = 8.36339918996282139126e-04;
const W6: f64 = -1.63092934096575273989e-03;
fn sin_pi(mut x: f64) -> f64 {
let mut n: i32;
x = 2.0 * (x * 0.5 - floor(x * 0.5));
n = (x * 4.0) as i32;
n = div!(n + 1, 2);
x -= (n as f64) * 0.5;
x *= PI;
match n {
1 => k_cos(x, 0.0),
2 => k_sin(-x, 0.0, 0),
3 => -k_cos(x, 0.0),
_ => k_sin(x, 0.0, 0),
}
}
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn lgamma_r(mut x: f64) -> (f64, i32) {
let u: u64 = x.to_bits();
let mut t: f64;
let y: f64;
let mut z: f64;
let nadj: f64;
let p: f64;
let p1: f64;
let p2: f64;
let p3: f64;
let q: f64;
let mut r: f64;
let w: f64;
let ix: u32;
let sign: bool;
let i: i32;
let mut signgam: i32;
signgam = 1;
sign = (u >> 63) != 0;
ix = ((u >> 32) as u32) & 0x7fffffff;
if ix >= 0x7ff00000 {
return (x * x, signgam);
}
if ix < (0x3ff - 70) << 20 {
if sign {
x = -x;
signgam = -1;
}
return (-log(x), signgam);
}
if sign {
x = -x;
t = sin_pi(x);
if t == 0.0 {
return (1.0 / (x - x), signgam);
}
if t > 0.0 {
signgam = -1;
} else {
t = -t;
}
nadj = log(PI / (t * x));
} else {
nadj = 0.0;
}
if (ix == 0x3ff00000 || ix == 0x40000000) && (u & 0xffffffff) == 0 {
r = 0.0;
}
else if ix < 0x40000000 {
if ix <= 0x3feccccc {
r = -log(x);
if ix >= 0x3FE76944 {
y = 1.0 - x;
i = 0;
} else if ix >= 0x3FCDA661 {
y = x - (TC - 1.0);
i = 1;
} else {
y = x;
i = 2;
}
} else {
r = 0.0;
if ix >= 0x3FFBB4C3 {
y = 2.0 - x;
i = 0;
} else if ix >= 0x3FF3B4C4 {
y = x - TC;
i = 1;
} else {
y = x - 1.0;
i = 2;
}
}
match i {
0 => {
z = y * y;
p1 = A0 + z * (A2 + z * (A4 + z * (A6 + z * (A8 + z * A10))));
p2 = z * (A1 + z * (A3 + z * (A5 + z * (A7 + z * (A9 + z * A11)))));
p = y * p1 + p2;
r += p - 0.5 * y;
}
1 => {
z = y * y;
w = z * y;
p1 = T0 + w * (T3 + w * (T6 + w * (T9 + w * T12)));
p2 = T1 + w * (T4 + w * (T7 + w * (T10 + w * T13)));
p3 = T2 + w * (T5 + w * (T8 + w * (T11 + w * T14)));
p = z * p1 - (TT - w * (p2 + y * p3));
r += TF + p;
}
2 => {
p1 = y * (U0 + y * (U1 + y * (U2 + y * (U3 + y * (U4 + y * U5)))));
p2 = 1.0 + y * (V1 + y * (V2 + y * (V3 + y * (V4 + y * V5))));
r += -0.5 * y + p1 / p2;
}
#[cfg(debug_assertions)]
_ => unreachable!(),
#[cfg(not(debug_assertions))]
_ => {}
}
} else if ix < 0x40200000 {
i = x as i32;
y = x - (i as f64);
p = y * (S0 + y * (S1 + y * (S2 + y * (S3 + y * (S4 + y * (S5 + y * S6))))));
q = 1.0 + y * (R1 + y * (R2 + y * (R3 + y * (R4 + y * (R5 + y * R6)))));
r = 0.5 * y + p / q;
z = 1.0;
if i >= 7 {
z *= y + 6.0;
}
if i >= 6 {
z *= y + 5.0;
}
if i >= 5 {
z *= y + 4.0;
}
if i >= 4 {
z *= y + 3.0;
}
if i >= 3 {
z *= y + 2.0;
r += log(z);
}
} else if ix < 0x43900000 {
t = log(x);
z = 1.0 / x;
y = z * z;
w = W0 + z * (W1 + y * (W2 + y * (W3 + y * (W4 + y * (W5 + y * W6)))));
r = (x - 0.5) * (t - 1.0) + w;
} else {
r = x * (log(x) - 1.0);
}
if sign {
r = nadj - r;
}
return (r, signgam);
}