1 | /* Compute cubic root of double value. |
2 | Copyright (C) 1997-2023 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <math.h> |
20 | #include <libm-alias-ldouble.h> |
21 | |
22 | |
23 | #define CBRT2 1.2599210498948731648 /* 2^(1/3) */ |
24 | #define SQR_CBRT2 1.5874010519681994748 /* 2^(2/3) */ |
25 | |
26 | /* We don't use long double values here since U need not be computed |
27 | with full precision. */ |
28 | static const double factor[5] = |
29 | { |
30 | 1.0 / SQR_CBRT2, |
31 | 1.0 / CBRT2, |
32 | 1.0, |
33 | CBRT2, |
34 | SQR_CBRT2 |
35 | }; |
36 | |
37 | static const long double third = 0.3333333333333333333333333L; |
38 | |
39 | long double |
40 | __cbrtl (long double x) |
41 | { |
42 | long double xm, u; |
43 | int xe; |
44 | |
45 | /* Reduce X. XM now is an range 1.0 to 0.5. */ |
46 | xm = __frexpl (fabsl (x), &xe); |
47 | |
48 | /* If X is not finite or is null return it (with raising exceptions |
49 | if necessary. |
50 | Note: *Our* version of `frexp' sets XE to zero if the argument is |
51 | Inf or NaN. This is not portable but faster. */ |
52 | if (xe == 0 && fpclassify (x) <= FP_ZERO) |
53 | return x + x; |
54 | |
55 | u = (((-1.34661104733595206551E-1 * xm |
56 | + 5.46646013663955245034E-1) * xm |
57 | - 9.54382247715094465250E-1) * xm |
58 | + 1.13999833547172932737E0) * xm |
59 | + 4.02389795645447521269E-1; |
60 | |
61 | u *= factor[2 + xe % 3]; |
62 | u = __ldexpl (x > 0.0 ? u : -u, xe / 3); |
63 | |
64 | u -= (u - (x / (u * u))) * third; |
65 | u -= (u - (x / (u * u))) * third; |
66 | return u; |
67 | } |
68 | libm_alias_ldouble (__cbrt, cbrt) |
69 | |