OLD | NEW |
1 #include "libm.h" | 1 #include "libm.h" |
2 | 2 |
3 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 | 3 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 |
4 long double tanhl(long double x) | 4 long double tanhl(long double x) { |
5 { | 5 return tanh(x); |
6 » return tanh(x); | |
7 } | 6 } |
8 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 | 7 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 |
9 long double tanhl(long double x) | 8 long double tanhl(long double x) { |
10 { | 9 union ldshape u = {x}; |
11 » union ldshape u = {x}; | 10 unsigned ex = u.i.se & 0x7fff; |
12 » unsigned ex = u.i.se & 0x7fff; | 11 unsigned sign = u.i.se & 0x8000; |
13 » unsigned sign = u.i.se & 0x8000; | 12 uint32_t w; |
14 » uint32_t w; | 13 long double t; |
15 » long double t; | |
16 | 14 |
17 » /* x = |x| */ | 15 /* x = |x| */ |
18 » u.i.se = ex; | 16 u.i.se = ex; |
19 » x = u.f; | 17 x = u.f; |
20 » w = u.i.m >> 32; | 18 w = u.i.m >> 32; |
21 | 19 |
22 » if (ex > 0x3ffe || (ex == 0x3ffe && w > 0x8c9f53d5)) { | 20 if (ex > 0x3ffe || (ex == 0x3ffe && w > 0x8c9f53d5)) { |
23 » » /* |x| > log(3)/2 ~= 0.5493 or nan */ | 21 /* |x| > log(3)/2 ~= 0.5493 or nan */ |
24 » » if (ex >= 0x3fff+5) { | 22 if (ex >= 0x3fff + 5) { |
25 » » » /* |x| >= 32 */ | 23 /* |x| >= 32 */ |
26 » » » t = 1 + 0/(x + 0x1p-120f); | 24 t = 1 + 0 / (x + 0x1p-120f); |
27 » » } else { | 25 } else { |
28 » » » t = expm1l(2*x); | 26 t = expm1l(2 * x); |
29 » » » t = 1 - 2/(t+2); | 27 t = 1 - 2 / (t + 2); |
30 » » } | 28 } |
31 » } else if (ex > 0x3ffd || (ex == 0x3ffd && w > 0x82c577d4)) { | 29 } else if (ex > 0x3ffd || (ex == 0x3ffd && w > 0x82c577d4)) { |
32 » » /* |x| > log(5/3)/2 ~= 0.2554 */ | 30 /* |x| > log(5/3)/2 ~= 0.2554 */ |
33 » » t = expm1l(2*x); | 31 t = expm1l(2 * x); |
34 » » t = t/(t+2); | 32 t = t / (t + 2); |
35 » } else { | 33 } else { |
36 » » /* |x| is small */ | 34 /* |x| is small */ |
37 » » t = expm1l(-2*x); | 35 t = expm1l(-2 * x); |
38 » » t = -t/(t+2); | 36 t = -t / (t + 2); |
39 » } | 37 } |
40 » return sign ? -t : t; | 38 return sign ? -t : t; |
41 } | 39 } |
42 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 | 40 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 |
43 // TODO: broken implementation to make things compile | 41 // TODO: broken implementation to make things compile |
44 long double tanhl(long double x) | 42 long double tanhl(long double x) { |
45 { | 43 return tanh(x); |
46 » return tanh(x); | |
47 } | 44 } |
48 #endif | 45 #endif |
OLD | NEW |