1use crate::common::{dd_fmla, f_fmla};
30use crate::double_double::DoubleDouble;
31use crate::exponents::{EXP_REDUCE_T0, EXP_REDUCE_T1};
32use crate::hyperbolic::acosh::lpoly_xd_generic;
33use crate::hyperbolic::sinh::hyperbolic_exp_accurate;
34
35#[cold]
36fn as_tanh_zero(x: f64) -> f64 {
37 static CH: [(u64, u64); 10] = [
38 (0xbc75555555555555, 0xbfd5555555555555),
39 (0x3c41111111110916, 0x3fc1111111111111),
40 (0x3c47917917a46f2c, 0xbfaba1ba1ba1ba1c),
41 (0xbc09a52a06f1e599, 0x3f9664f4882c10fa),
42 (0x3c2c297394c24e38, 0xbf8226e355e6c23d),
43 (0xbc0311087e5b1526, 0x3f6d6d3d0e157de0),
44 (0xbbe2868cde54ea0c, 0xbf57da36452b75e1),
45 (0x3bd2cd8fc406c3f7, 0x3f4355824803667b),
46 (0x3b9da22861b4ca80, 0xbf2f57d7734c821d),
47 (0xbbb0831108273a74, 0x3f1967e18ad3facf),
48 ];
49 let dx2 = DoubleDouble::from_exact_mult(x, x);
50 const CL: [u64; 6] = [
51 0xbf0497d8e6462927,
52 0x3ef0b1318c243bd7,
53 0xbedb0f2935e9a120,
54 0x3ec5e9444536e654,
55 0xbeb174ff2a31908c,
56 0x3e9749698c8d338d,
57 ];
58
59 let yw0 = f_fmla(dx2.hi, f64::from_bits(CL[5]), f64::from_bits(CL[4]));
60 let yw1 = f_fmla(dx2.hi, yw0, f64::from_bits(CL[3]));
61 let yw2 = f_fmla(dx2.hi, yw1, f64::from_bits(CL[2]));
62 let yw3 = f_fmla(dx2.hi, yw2, f64::from_bits(CL[1]));
63 let yw4 = f_fmla(dx2.hi, yw3, f64::from_bits(CL[0]));
64
65 let y2 = dx2.hi * yw4;
66
67 let mut y1 = lpoly_xd_generic(dx2, CH, y2);
68 y1 = DoubleDouble::quick_mult_f64(y1, x);
69 y1 = DoubleDouble::quick_mult(y1, dx2); let y0 = DoubleDouble::from_exact_add(x, y1.hi); let mut p = DoubleDouble::from_exact_add(y0.lo, y1.lo);
72 let mut t = p.hi.to_bits();
73 if (t & 0x000fffffffffffff) == 0 {
74 let w = p.lo.to_bits();
75 if ((w ^ t) >> 63) != 0 {
76 t = t.wrapping_sub(1);
77 } else {
78 t = t.wrapping_add(1);
79 }
80 p.hi = f64::from_bits(t);
81 }
82 y0.hi + p.hi
83}
84
85pub fn f_tanh(x: f64) -> f64 {
89 let ax = x.abs();
97 let ix = ax.to_bits();
98 let aix: u64 = ix;
99 if aix >= 0x40330fc1931f09cau64 {
102 if aix > 0x7ff0000000000000u64 {
103 return x + x;
104 } let f = f64::copysign(1.0, x);
106 if aix == 0x7ff0000000000000u64 {
107 return f;
108 }
109 let df = f64::copysign(f64::from_bits(0x3c80000000000000), x);
110 return f - df;
111 }
112 const S: f64 = f64::from_bits(0xc0c71547652b82fe);
113 let v0 = dd_fmla(ax, S, f64::from_bits(0x4188000004000000));
114 let jt = v0.to_bits();
115 let v = v0.to_bits() & 0xfffffffff8000000;
116 let t = f64::from_bits(v) - f64::from_bits(0x4188000000000000);
117
118 let i1: i64 = ((jt >> 27) & 0x3f) as i64;
119 let i0 = (jt >> 33) & 0x3f;
120 let ie = ((jt.wrapping_shl(13)) >> 52) as i64;
121 let sp = (1023i64.wrapping_add(ie) as u64).wrapping_shl(52);
122 const CH: [u64; 4] = [
123 0x4000000000000000,
124 0x4000000000000000,
125 0x3ff55555557e54ff,
126 0x3fe55555553a12f4,
127 ];
128 let t0h = f64::from_bits(EXP_REDUCE_T0[i0 as usize].1);
129 let t1h = f64::from_bits(EXP_REDUCE_T1[i1 as usize].1);
130 let mut th = t0h * t1h;
131 let mut tl;
132 if aix < 0x400d76c8b4395810u64 {
133 if aix < 0x3fd0000000000000u64 {
135 if aix < 0x3e10000000000000u64 {
137 if aix < 0x3df0000000000000u64 {
139 if aix == 0 {
141 return x;
142 }
143 let res = dd_fmla(x, f64::from_bits(0xbc80000000000000), x);
146 return res;
147 }
148 let x3 = x * x * x;
149 return x - x3 / 3.;
150 }
151
152 const C: [u64; 8] = [
153 0xbfd5555555555554,
154 0x3fc1111111110d61,
155 0xbfaba1ba1b983d8b,
156 0x3f9664f4820e99f0,
157 0xbf8226e11e4ac7cf,
158 0x3f6d6c4ab70668b6,
159 0xbf57bbecb57ce996,
160 0x3f41451443697dd8,
161 ];
162 let x2 = x * x;
163 let x3 = x2 * x;
164 let x4 = x2 * x2;
165 let x8 = x4 * x4;
166
167 let p1w0 = f_fmla(x2, f64::from_bits(C[7]), f64::from_bits(C[6]));
168 let p1w1 = f_fmla(x2, f64::from_bits(C[5]), f64::from_bits(C[4]));
169
170 let p0w0 = f_fmla(x2, f64::from_bits(C[3]), f64::from_bits(C[2]));
171 let p0w1 = f_fmla(x2, f64::from_bits(C[1]), f64::from_bits(C[0]));
172
173 let p1 = f_fmla(x4, p1w0, p1w1);
174 let mut p0 = f_fmla(x4, p0w0, p0w1);
175 p0 += x8 * p1;
176 p0 *= x3;
177 let r = DoubleDouble::from_exact_add(x, p0);
178 let e = x3 * f64::from_bits(0x3cba000000000000);
179 let lb = r.hi + (r.lo - e);
180 let ub = r.hi + (r.lo + e);
181 if lb == ub {
182 return lb;
183 }
184 return as_tanh_zero(x);
185 }
186
187 let t0l = f64::from_bits(EXP_REDUCE_T0[i0 as usize].0);
188 let t1l = f64::from_bits(EXP_REDUCE_T1[i1 as usize].0);
189 tl = f_fmla(t0h, t1l, t1h * t0l) + dd_fmla(t0h, t1h, -th);
190 th *= f64::from_bits(sp);
191 tl *= f64::from_bits(sp);
192 const L2H: f64 = f64::from_bits(0xbf162e42ff000000);
193 const L2L: f64 = f64::from_bits(0xbcf718432a1b0e26);
194 let dx = f_fmla(-L2L, t, f_fmla(L2H, t, -ax));
195 let dx2 = dx * dx;
196
197 let pw0 = f_fmla(dx, f64::from_bits(CH[3]), f64::from_bits(CH[2]));
198 let pw1 = f_fmla(dx, f64::from_bits(CH[1]), f64::from_bits(CH[0]));
199
200 let p = dx * f_fmla(dx2, pw0, pw1);
201 let mut rh = th;
202 let mut rl = tl + rh * p;
203 let mut r = DoubleDouble::from_exact_add(rh, rl);
204
205 let ph = r.hi;
206 let pl = r.lo;
207 let mut qh = r.hi;
208 let mut ql = r.lo;
209 let qq = DoubleDouble::from_exact_add(1.0, qh);
210 qh = qq.hi;
211 ql += qq.lo;
212
213 let rqh = 1.0 / qh;
214 let rql = f_fmla(ql, rqh, dd_fmla(rqh, qh, -1.)) * -rqh;
215 let p = DoubleDouble::mult(DoubleDouble::new(pl, ph), DoubleDouble::new(rql, rqh));
216
217 let e = r.hi * f64::from_bits(0x3c10000000000000);
218 r = DoubleDouble::from_exact_sub(0.5, p.hi);
219 r.lo -= p.lo;
220 rh = r.hi * f64::copysign(2., x);
221 rl = r.lo * f64::copysign(2., x);
222 let lb = rh + (rl - e);
223 let ub = rh + (rl + e);
224 if lb == ub {
225 return lb;
226 }
227 } else {
228 const L2: f64 = f64::from_bits(0xbf162e42fefa39ef);
229 let dx = dd_fmla(L2, t, -ax);
230 let dx2 = dx * dx;
231
232 let pw0 = f_fmla(dx, f64::from_bits(CH[3]), f64::from_bits(CH[2]));
233 let pw1 = f_fmla(dx, f64::from_bits(CH[1]), f64::from_bits(CH[0]));
234
235 let p = dx * f_fmla(dx2, pw0, pw1);
236 let mut rh = th * f64::from_bits(sp);
237 rh += (p + ((2. * f64::from_bits(0x3c83000000000000)) * ax)) * rh;
238 let e = rh * f64::from_bits(0x3ce1000000000000);
239 rh = (2. * rh) / (1. + rh);
240 let one = f64::copysign(1., x);
241 rh = f64::copysign(rh, x);
242 let lb = one - (rh + e);
243 let ub = one - (rh - e);
244 if lb == ub {
245 return lb;
246 }
247
248 let t0l = f64::from_bits(EXP_REDUCE_T0[i0 as usize].0);
249 let t1l = f64::from_bits(EXP_REDUCE_T1[i1 as usize].0);
250 tl = f_fmla(t0h, t1l, t1h * t0l) + dd_fmla(t0h, t1h, -th);
251 th *= f64::from_bits(sp);
252 tl *= f64::from_bits(sp);
253 }
254
255 let mut r = hyperbolic_exp_accurate(-2. * ax, t, DoubleDouble::new(tl, th));
256 let mut q = DoubleDouble::from_exact_add(1.0, r.hi);
257 q.lo += r.lo;
258 q = DoubleDouble::from_exact_add(q.hi, q.lo);
259 let rqh = 1. / q.hi;
260 let rql = f_fmla(q.lo, rqh, dd_fmla(rqh, q.hi, -1.)) * -rqh;
261 let p = DoubleDouble::mult(r, DoubleDouble::new(rql, rqh));
262 r = DoubleDouble::from_exact_sub(0.5, p.hi);
263 r.lo -= p.lo;
264 r = DoubleDouble::from_exact_add(r.hi, r.lo);
265 f_fmla(f64::copysign(2., x), r.hi, f64::copysign(2., x) * r.lo)
266}
267
268#[cfg(test)]
269mod tests {
270 use super::*;
271
272 #[test]
273 fn test_tanh() {
274 assert_eq!(f_tanh(4.799980150947863), 0.9998645463239773);
275 assert_eq!(f_tanh(2.549980150947863), 0.9878799187977153);
276 }
277}