@xort/rcmg 0.0.0-stage → 0.1.0-preview.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-APACHE +176 -0
- package/LICENSE-MIT +21 -0
- package/PROVENANCE.json +1601 -0
- package/README.md +152 -2
- package/licenses/registry/allocator-api2-0.2.22-c19b7c6f923b/LICENSE-APACHE +176 -0
- package/licenses/registry/allocator-api2-0.2.22-c19b7c6f923b/LICENSE-MIT +23 -0
- package/licenses/registry/autocfg-1.5.1-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/autocfg-1.5.1-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/alloc.rs +794 -0
- package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/mod.rs +93 -0
- package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/raw_vec.rs +781 -0
- package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/str/lossy.rs +209 -0
- package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/str/mod.rs +43 -0
- package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/string.rs +2191 -0
- package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/vec.rs +3051 -0
- package/licenses/registry/cfg-if-1.0.5-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/cfg-if-1.0.5-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/equivalent-1.0.3-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/equivalent-1.0.3-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/foldhash-0.2.0-c19b7c6f923b/LICENSE +19 -0
- package/licenses/registry/hashbrown-0.16.1-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/hashbrown-0.16.1-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/LICENSE.txt +258 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/acos.rs +112 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/acosf.rs +79 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/asin.rs +115 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/asinf.rs +68 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/atan.rs +180 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/atan2.rs +131 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/atan2f.rs +90 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/atanf.rs +108 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/cbrt.rs +219 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/cbrtf.rs +73 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/cos.rs +77 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/cosf.rs +86 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/erf.rs +314 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/erff.rs +226 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/exp.rs +156 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/exp2.rs +400 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/exp2f.rs +141 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/expf.rs +103 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/expm1.rs +142 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/expm1f.rs +134 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/fma.rs +171 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/ceil.rs +174 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/floor.rs +157 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fma.rs +278 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fmax.rs +23 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fmaximum.rs +27 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fmaximum_num.rs +29 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fmin.rs +23 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fminimum.rs +27 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fminimum_num.rs +29 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fmod.rs +116 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/rint.rs +130 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/sqrt.rs +541 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/trunc.rs +148 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/j0.rs +426 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/j0f.rs +363 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/j1.rs +418 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/j1f.rs +383 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/jn.rs +339 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/jnf.rs +253 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_cos.rs +62 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_cosf.rs +29 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_sin.rs +57 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_sinf.rs +30 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_tan.rs +105 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_tanf.rs +46 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/lgamma_r.rs +321 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/lgammaf_r.rs +256 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log.rs +118 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log10.rs +116 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log10f.rs +90 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log1p.rs +142 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log1pf.rs +97 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log2.rs +105 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log2f.rs +86 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/logf.rs +66 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/pow.rs +624 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/powf.rs +343 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/rem_pio2.rs +235 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/rem_pio2_large.rs +476 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/rem_pio2f.rs +65 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/sin.rs +95 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/sincos.rs +137 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/sincosf.rs +176 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/sinf.rs +96 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/support/int_traits/narrowing_div.rs +175 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/support/modular.rs +304 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/tan.rs +74 -0
- package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/tanf.rs +81 -0
- package/licenses/registry/num-traits-0.2.19-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/num-traits-0.2.19-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/num-traits-0.2.19-c19b7c6f923b/src/lib.rs +635 -0
- package/licenses/registry/once_cell-1.21.4-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/once_cell-1.21.4-c19b7c6f923b/LICENSE-MIT +23 -0
- package/licenses/registry/proc-macro2-1.0.107-c19b7c6f923b/LICENSE-APACHE +176 -0
- package/licenses/registry/proc-macro2-1.0.107-c19b7c6f923b/LICENSE-MIT +23 -0
- package/licenses/registry/quote-1.0.47-c19b7c6f923b/LICENSE-APACHE +176 -0
- package/licenses/registry/quote-1.0.47-c19b7c6f923b/LICENSE-MIT +23 -0
- package/licenses/registry/robust-1.2.0-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/robust-1.2.0-c19b7c6f923b/LICENSE-MIT +26 -0
- package/licenses/registry/robust-1.2.0-c19b7c6f923b/src/lib.rs +2224 -0
- package/licenses/registry/rustversion-1.0.23-c19b7c6f923b/LICENSE-APACHE +176 -0
- package/licenses/registry/rustversion-1.0.23-c19b7c6f923b/LICENSE-MIT +23 -0
- package/licenses/registry/smallvec-1.16.6-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/smallvec-1.16.6-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/spade-2.15.1-c19b7c6f923b/LICENSE-APACHE +202 -0
- package/licenses/registry/spade-2.15.1-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/syn-2.0.119-c19b7c6f923b/LICENSE-APACHE +176 -0
- package/licenses/registry/syn-2.0.119-c19b7c6f923b/LICENSE-MIT +23 -0
- package/licenses/registry/unicode-ident-1.0.27-c19b7c6f923b/LICENSE-APACHE +176 -0
- package/licenses/registry/unicode-ident-1.0.27-c19b7c6f923b/LICENSE-MIT +23 -0
- package/licenses/registry/unicode-ident-1.0.27-c19b7c6f923b/LICENSE-UNICODE +39 -0
- package/licenses/registry/wasm-bindgen-0.2.118-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/wasm-bindgen-0.2.118-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/wasm-bindgen-macro-0.2.118-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/wasm-bindgen-macro-0.2.118-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/wasm-bindgen-macro-support-0.2.118-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/wasm-bindgen-macro-support-0.2.118-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry/wasm-bindgen-shared-0.2.118-c19b7c6f923b/LICENSE-APACHE +201 -0
- package/licenses/registry/wasm-bindgen-shared-0.2.118-c19b7c6f923b/LICENSE-MIT +25 -0
- package/licenses/registry-licenses.json +1413 -0
- package/licenses/source/LICENSE-APACHE +176 -0
- package/licenses/source/LICENSE-MIT +21 -0
- package/licenses/source/crates/rcmg-mesh/src/isosurface/LICENSE-PyMCubes +27 -0
- package/licenses/source/crates/rcmg-mesh/src/isosurface/NOTICE.md +11 -0
- package/licenses/source/vendor/cadcraft-dxf/LICENSE-APACHE +177 -0
- package/licenses/source/vendor/cadcraft-dxf/LICENSE-MIT +21 -0
- package/node/index.mjs +2 -0
- package/node/package.json +3 -0
- package/node/rcmg_ffi.d.ts +51 -0
- package/node/rcmg_ffi.js +269 -0
- package/node/rcmg_ffi_bg.wasm +0 -0
- package/node/rcmg_ffi_bg.wasm.d.ts +32 -0
- package/package.json +57 -6
- package/web/rcmg_ffi.d.ts +108 -0
- package/web/rcmg_ffi.js +358 -0
- package/web/rcmg_ffi_bg.wasm +0 -0
- package/web/rcmg_ffi_bg.wasm.d.ts +32 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/* origin: FreeBSD /usr/src/lib/msun/src/e_lgamma_r.c */
|
|
2
|
+
/*
|
|
3
|
+
* ====================================================
|
|
4
|
+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
5
|
+
*
|
|
6
|
+
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
|
7
|
+
* Permission to use, copy, modify, and distribute this
|
|
8
|
+
* software is freely granted, provided that this notice
|
|
9
|
+
* is preserved.
|
|
10
|
+
* ====================================================
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
/* lgamma_r(x, signgamp)
|
|
14
|
+
* Reentrant version of the logarithm of the Gamma function
|
|
15
|
+
* with user provide pointer for the sign of Gamma(x).
|
|
16
|
+
*
|
|
17
|
+
* Method:
|
|
18
|
+
* 1. Argument Reduction for 0 < x <= 8
|
|
19
|
+
* Since gamma(1+s)=s*gamma(s), for x in [0,8], we may
|
|
20
|
+
* reduce x to a number in [1.5,2.5] by
|
|
21
|
+
* lgamma(1+s) = log(s) + lgamma(s)
|
|
22
|
+
* for example,
|
|
23
|
+
* lgamma(7.3) = log(6.3) + lgamma(6.3)
|
|
24
|
+
* = log(6.3*5.3) + lgamma(5.3)
|
|
25
|
+
* = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3)
|
|
26
|
+
* 2. Polynomial approximation of lgamma around its
|
|
27
|
+
* minimun ymin=1.461632144968362245 to maintain monotonicity.
|
|
28
|
+
* On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use
|
|
29
|
+
* Let z = x-ymin;
|
|
30
|
+
* lgamma(x) = -1.214862905358496078218 + z^2*poly(z)
|
|
31
|
+
* where
|
|
32
|
+
* poly(z) is a 14 degree polynomial.
|
|
33
|
+
* 2. Rational approximation in the primary interval [2,3]
|
|
34
|
+
* We use the following approximation:
|
|
35
|
+
* s = x-2.0;
|
|
36
|
+
* lgamma(x) = 0.5*s + s*P(s)/Q(s)
|
|
37
|
+
* with accuracy
|
|
38
|
+
* |P/Q - (lgamma(x)-0.5s)| < 2**-61.71
|
|
39
|
+
* Our algorithms are based on the following observation
|
|
40
|
+
*
|
|
41
|
+
* zeta(2)-1 2 zeta(3)-1 3
|
|
42
|
+
* lgamma(2+s) = s*(1-Euler) + --------- * s - --------- * s + ...
|
|
43
|
+
* 2 3
|
|
44
|
+
*
|
|
45
|
+
* where Euler = 0.5771... is the Euler constant, which is very
|
|
46
|
+
* close to 0.5.
|
|
47
|
+
*
|
|
48
|
+
* 3. For x>=8, we have
|
|
49
|
+
* lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+....
|
|
50
|
+
* (better formula:
|
|
51
|
+
* lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...)
|
|
52
|
+
* Let z = 1/x, then we approximation
|
|
53
|
+
* f(z) = lgamma(x) - (x-0.5)(log(x)-1)
|
|
54
|
+
* by
|
|
55
|
+
* 3 5 11
|
|
56
|
+
* w = w0 + w1*z + w2*z + w3*z + ... + w6*z
|
|
57
|
+
* where
|
|
58
|
+
* |w - f(z)| < 2**-58.74
|
|
59
|
+
*
|
|
60
|
+
* 4. For negative x, since (G is gamma function)
|
|
61
|
+
* -x*G(-x)*G(x) = PI/sin(PI*x),
|
|
62
|
+
* we have
|
|
63
|
+
* G(x) = PI/(sin(PI*x)*(-x)*G(-x))
|
|
64
|
+
* since G(-x) is positive, sign(G(x)) = sign(sin(PI*x)) for x<0
|
|
65
|
+
* Hence, for x<0, signgam = sign(sin(PI*x)) and
|
|
66
|
+
* lgamma(x) = log(|Gamma(x)|)
|
|
67
|
+
* = log(PI/(|x*sin(PI*x)|)) - lgamma(-x);
|
|
68
|
+
* Note: one should avoid compute PI*(-x) directly in the
|
|
69
|
+
* computation of sin(PI*(-x)).
|
|
70
|
+
*
|
|
71
|
+
* 5. Special Cases
|
|
72
|
+
* lgamma(2+s) ~ s*(1-Euler) for tiny s
|
|
73
|
+
* lgamma(1) = lgamma(2) = 0
|
|
74
|
+
* lgamma(x) ~ -log(|x|) for tiny x
|
|
75
|
+
* lgamma(0) = lgamma(neg.integer) = inf and raise divide-by-zero
|
|
76
|
+
* lgamma(inf) = inf
|
|
77
|
+
* lgamma(-inf) = inf (bug for bug compatible with C99!?)
|
|
78
|
+
*
|
|
79
|
+
*/
|
|
80
|
+
|
|
81
|
+
use super::{floor, k_cos, k_sin, log};
|
|
82
|
+
|
|
83
|
+
const PI: f64 = 3.14159265358979311600e+00; /* 0x400921FB, 0x54442D18 */
|
|
84
|
+
const A0: f64 = 7.72156649015328655494e-02; /* 0x3FB3C467, 0xE37DB0C8 */
|
|
85
|
+
const A1: f64 = 3.22467033424113591611e-01; /* 0x3FD4A34C, 0xC4A60FAD */
|
|
86
|
+
const A2: f64 = 6.73523010531292681824e-02; /* 0x3FB13E00, 0x1A5562A7 */
|
|
87
|
+
const A3: f64 = 2.05808084325167332806e-02; /* 0x3F951322, 0xAC92547B */
|
|
88
|
+
const A4: f64 = 7.38555086081402883957e-03; /* 0x3F7E404F, 0xB68FEFE8 */
|
|
89
|
+
const A5: f64 = 2.89051383673415629091e-03; /* 0x3F67ADD8, 0xCCB7926B */
|
|
90
|
+
const A6: f64 = 1.19270763183362067845e-03; /* 0x3F538A94, 0x116F3F5D */
|
|
91
|
+
const A7: f64 = 5.10069792153511336608e-04; /* 0x3F40B6C6, 0x89B99C00 */
|
|
92
|
+
const A8: f64 = 2.20862790713908385557e-04; /* 0x3F2CF2EC, 0xED10E54D */
|
|
93
|
+
const A9: f64 = 1.08011567247583939954e-04; /* 0x3F1C5088, 0x987DFB07 */
|
|
94
|
+
const A10: f64 = 2.52144565451257326939e-05; /* 0x3EFA7074, 0x428CFA52 */
|
|
95
|
+
const A11: f64 = 4.48640949618915160150e-05; /* 0x3F07858E, 0x90A45837 */
|
|
96
|
+
const TC: f64 = 1.46163214496836224576e+00; /* 0x3FF762D8, 0x6356BE3F */
|
|
97
|
+
const TF: f64 = -1.21486290535849611461e-01; /* 0xBFBF19B9, 0xBCC38A42 */
|
|
98
|
+
/* tt = -(tail of TF) */
|
|
99
|
+
const TT: f64 = -3.63867699703950536541e-18; /* 0xBC50C7CA, 0xA48A971F */
|
|
100
|
+
const T0: f64 = 4.83836122723810047042e-01; /* 0x3FDEF72B, 0xC8EE38A2 */
|
|
101
|
+
const T1: f64 = -1.47587722994593911752e-01; /* 0xBFC2E427, 0x8DC6C509 */
|
|
102
|
+
const T2: f64 = 6.46249402391333854778e-02; /* 0x3FB08B42, 0x94D5419B */
|
|
103
|
+
const T3: f64 = -3.27885410759859649565e-02; /* 0xBFA0C9A8, 0xDF35B713 */
|
|
104
|
+
const T4: f64 = 1.79706750811820387126e-02; /* 0x3F9266E7, 0x970AF9EC */
|
|
105
|
+
const T5: f64 = -1.03142241298341437450e-02; /* 0xBF851F9F, 0xBA91EC6A */
|
|
106
|
+
const T6: f64 = 6.10053870246291332635e-03; /* 0x3F78FCE0, 0xE370E344 */
|
|
107
|
+
const T7: f64 = -3.68452016781138256760e-03; /* 0xBF6E2EFF, 0xB3E914D7 */
|
|
108
|
+
const T8: f64 = 2.25964780900612472250e-03; /* 0x3F6282D3, 0x2E15C915 */
|
|
109
|
+
const T9: f64 = -1.40346469989232843813e-03; /* 0xBF56FE8E, 0xBF2D1AF1 */
|
|
110
|
+
const T10: f64 = 8.81081882437654011382e-04; /* 0x3F4CDF0C, 0xEF61A8E9 */
|
|
111
|
+
const T11: f64 = -5.38595305356740546715e-04; /* 0xBF41A610, 0x9C73E0EC */
|
|
112
|
+
const T12: f64 = 3.15632070903625950361e-04; /* 0x3F34AF6D, 0x6C0EBBF7 */
|
|
113
|
+
const T13: f64 = -3.12754168375120860518e-04; /* 0xBF347F24, 0xECC38C38 */
|
|
114
|
+
const T14: f64 = 3.35529192635519073543e-04; /* 0x3F35FD3E, 0xE8C2D3F4 */
|
|
115
|
+
const U0: f64 = -7.72156649015328655494e-02; /* 0xBFB3C467, 0xE37DB0C8 */
|
|
116
|
+
const U1: f64 = 6.32827064025093366517e-01; /* 0x3FE4401E, 0x8B005DFF */
|
|
117
|
+
const U2: f64 = 1.45492250137234768737e+00; /* 0x3FF7475C, 0xD119BD6F */
|
|
118
|
+
const U3: f64 = 9.77717527963372745603e-01; /* 0x3FEF4976, 0x44EA8450 */
|
|
119
|
+
const U4: f64 = 2.28963728064692451092e-01; /* 0x3FCD4EAE, 0xF6010924 */
|
|
120
|
+
const U5: f64 = 1.33810918536787660377e-02; /* 0x3F8B678B, 0xBF2BAB09 */
|
|
121
|
+
const V1: f64 = 2.45597793713041134822e+00; /* 0x4003A5D7, 0xC2BD619C */
|
|
122
|
+
const V2: f64 = 2.12848976379893395361e+00; /* 0x40010725, 0xA42B18F5 */
|
|
123
|
+
const V3: f64 = 7.69285150456672783825e-01; /* 0x3FE89DFB, 0xE45050AF */
|
|
124
|
+
const V4: f64 = 1.04222645593369134254e-01; /* 0x3FBAAE55, 0xD6537C88 */
|
|
125
|
+
const V5: f64 = 3.21709242282423911810e-03; /* 0x3F6A5ABB, 0x57D0CF61 */
|
|
126
|
+
const S0: f64 = -7.72156649015328655494e-02; /* 0xBFB3C467, 0xE37DB0C8 */
|
|
127
|
+
const S1: f64 = 2.14982415960608852501e-01; /* 0x3FCB848B, 0x36E20878 */
|
|
128
|
+
const S2: f64 = 3.25778796408930981787e-01; /* 0x3FD4D98F, 0x4F139F59 */
|
|
129
|
+
const S3: f64 = 1.46350472652464452805e-01; /* 0x3FC2BB9C, 0xBEE5F2F7 */
|
|
130
|
+
const S4: f64 = 2.66422703033638609560e-02; /* 0x3F9B481C, 0x7E939961 */
|
|
131
|
+
const S5: f64 = 1.84028451407337715652e-03; /* 0x3F5E26B6, 0x7368F239 */
|
|
132
|
+
const S6: f64 = 3.19475326584100867617e-05; /* 0x3F00BFEC, 0xDD17E945 */
|
|
133
|
+
const R1: f64 = 1.39200533467621045958e+00; /* 0x3FF645A7, 0x62C4AB74 */
|
|
134
|
+
const R2: f64 = 7.21935547567138069525e-01; /* 0x3FE71A18, 0x93D3DCDC */
|
|
135
|
+
const R3: f64 = 1.71933865632803078993e-01; /* 0x3FC601ED, 0xCCFBDF27 */
|
|
136
|
+
const R4: f64 = 1.86459191715652901344e-02; /* 0x3F9317EA, 0x742ED475 */
|
|
137
|
+
const R5: f64 = 7.77942496381893596434e-04; /* 0x3F497DDA, 0xCA41A95B */
|
|
138
|
+
const R6: f64 = 7.32668430744625636189e-06; /* 0x3EDEBAF7, 0xA5B38140 */
|
|
139
|
+
const W0: f64 = 4.18938533204672725052e-01; /* 0x3FDACFE3, 0x90C97D69 */
|
|
140
|
+
const W1: f64 = 8.33333333333329678849e-02; /* 0x3FB55555, 0x5555553B */
|
|
141
|
+
const W2: f64 = -2.77777777728775536470e-03; /* 0xBF66C16C, 0x16B02E5C */
|
|
142
|
+
const W3: f64 = 7.93650558643019558500e-04; /* 0x3F4A019F, 0x98CF38B6 */
|
|
143
|
+
const W4: f64 = -5.95187557450339963135e-04; /* 0xBF4380CB, 0x8C0FE741 */
|
|
144
|
+
const W5: f64 = 8.36339918996282139126e-04; /* 0x3F4B67BA, 0x4CDAD5D1 */
|
|
145
|
+
const W6: f64 = -1.63092934096575273989e-03; /* 0xBF5AB89D, 0x0B9E43E4 */
|
|
146
|
+
|
|
147
|
+
/* sin(PI*x) assuming x > 2^-100, if sin(PI*x)==0 the sign is arbitrary */
|
|
148
|
+
fn sin_pi(mut x: f64) -> f64 {
|
|
149
|
+
let mut n: i32;
|
|
150
|
+
|
|
151
|
+
/* spurious inexact if odd int */
|
|
152
|
+
x = 2.0 * (x * 0.5 - floor(x * 0.5)); /* x mod 2.0 */
|
|
153
|
+
|
|
154
|
+
n = (x * 4.0) as i32;
|
|
155
|
+
n = div!(n + 1, 2);
|
|
156
|
+
x -= (n as f64) * 0.5;
|
|
157
|
+
x *= PI;
|
|
158
|
+
|
|
159
|
+
match n {
|
|
160
|
+
1 => k_cos(x, 0.0),
|
|
161
|
+
2 => k_sin(-x, 0.0, 0),
|
|
162
|
+
3 => -k_cos(x, 0.0),
|
|
163
|
+
// 0
|
|
164
|
+
_ => k_sin(x, 0.0, 0),
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
|
|
169
|
+
pub fn lgamma_r(mut x: f64) -> (f64, i32) {
|
|
170
|
+
let u: u64 = x.to_bits();
|
|
171
|
+
let mut t: f64;
|
|
172
|
+
let y: f64;
|
|
173
|
+
let mut z: f64;
|
|
174
|
+
let nadj: f64;
|
|
175
|
+
let p: f64;
|
|
176
|
+
let p1: f64;
|
|
177
|
+
let p2: f64;
|
|
178
|
+
let p3: f64;
|
|
179
|
+
let q: f64;
|
|
180
|
+
let mut r: f64;
|
|
181
|
+
let w: f64;
|
|
182
|
+
let ix: u32;
|
|
183
|
+
let sign: bool;
|
|
184
|
+
let i: i32;
|
|
185
|
+
let mut signgam: i32;
|
|
186
|
+
|
|
187
|
+
/* purge off +-inf, NaN, +-0, tiny and negative arguments */
|
|
188
|
+
signgam = 1;
|
|
189
|
+
sign = (u >> 63) != 0;
|
|
190
|
+
ix = ((u >> 32) as u32) & 0x7fffffff;
|
|
191
|
+
if ix >= 0x7ff00000 {
|
|
192
|
+
return (x * x, signgam);
|
|
193
|
+
}
|
|
194
|
+
if ix < (0x3ff - 70) << 20 {
|
|
195
|
+
/* |x|<2**-70, return -log(|x|) */
|
|
196
|
+
if sign {
|
|
197
|
+
x = -x;
|
|
198
|
+
signgam = -1;
|
|
199
|
+
}
|
|
200
|
+
return (-log(x), signgam);
|
|
201
|
+
}
|
|
202
|
+
if sign {
|
|
203
|
+
x = -x;
|
|
204
|
+
t = sin_pi(x);
|
|
205
|
+
if t == 0.0 {
|
|
206
|
+
/* -integer */
|
|
207
|
+
return (1.0 / (x - x), signgam);
|
|
208
|
+
}
|
|
209
|
+
if t > 0.0 {
|
|
210
|
+
signgam = -1;
|
|
211
|
+
} else {
|
|
212
|
+
t = -t;
|
|
213
|
+
}
|
|
214
|
+
nadj = log(PI / (t * x));
|
|
215
|
+
} else {
|
|
216
|
+
nadj = 0.0;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/* purge off 1 and 2 */
|
|
220
|
+
if (ix == 0x3ff00000 || ix == 0x40000000) && (u & 0xffffffff) == 0 {
|
|
221
|
+
r = 0.0;
|
|
222
|
+
}
|
|
223
|
+
/* for x < 2.0 */
|
|
224
|
+
else if ix < 0x40000000 {
|
|
225
|
+
if ix <= 0x3feccccc {
|
|
226
|
+
/* lgamma(x) = lgamma(x+1)-log(x) */
|
|
227
|
+
r = -log(x);
|
|
228
|
+
if ix >= 0x3FE76944 {
|
|
229
|
+
y = 1.0 - x;
|
|
230
|
+
i = 0;
|
|
231
|
+
} else if ix >= 0x3FCDA661 {
|
|
232
|
+
y = x - (TC - 1.0);
|
|
233
|
+
i = 1;
|
|
234
|
+
} else {
|
|
235
|
+
y = x;
|
|
236
|
+
i = 2;
|
|
237
|
+
}
|
|
238
|
+
} else {
|
|
239
|
+
r = 0.0;
|
|
240
|
+
if ix >= 0x3FFBB4C3 {
|
|
241
|
+
/* [1.7316,2] */
|
|
242
|
+
y = 2.0 - x;
|
|
243
|
+
i = 0;
|
|
244
|
+
} else if ix >= 0x3FF3B4C4 {
|
|
245
|
+
/* [1.23,1.73] */
|
|
246
|
+
y = x - TC;
|
|
247
|
+
i = 1;
|
|
248
|
+
} else {
|
|
249
|
+
y = x - 1.0;
|
|
250
|
+
i = 2;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
match i {
|
|
254
|
+
0 => {
|
|
255
|
+
z = y * y;
|
|
256
|
+
p1 = A0 + z * (A2 + z * (A4 + z * (A6 + z * (A8 + z * A10))));
|
|
257
|
+
p2 = z * (A1 + z * (A3 + z * (A5 + z * (A7 + z * (A9 + z * A11)))));
|
|
258
|
+
p = y * p1 + p2;
|
|
259
|
+
r += p - 0.5 * y;
|
|
260
|
+
}
|
|
261
|
+
1 => {
|
|
262
|
+
z = y * y;
|
|
263
|
+
w = z * y;
|
|
264
|
+
p1 = T0 + w * (T3 + w * (T6 + w * (T9 + w * T12))); /* parallel comp */
|
|
265
|
+
p2 = T1 + w * (T4 + w * (T7 + w * (T10 + w * T13)));
|
|
266
|
+
p3 = T2 + w * (T5 + w * (T8 + w * (T11 + w * T14)));
|
|
267
|
+
p = z * p1 - (TT - w * (p2 + y * p3));
|
|
268
|
+
r += TF + p;
|
|
269
|
+
}
|
|
270
|
+
2 => {
|
|
271
|
+
p1 = y * (U0 + y * (U1 + y * (U2 + y * (U3 + y * (U4 + y * U5)))));
|
|
272
|
+
p2 = 1.0 + y * (V1 + y * (V2 + y * (V3 + y * (V4 + y * V5))));
|
|
273
|
+
r += -0.5 * y + p1 / p2;
|
|
274
|
+
}
|
|
275
|
+
#[cfg(debug_assertions)]
|
|
276
|
+
_ => unreachable!(),
|
|
277
|
+
#[cfg(not(debug_assertions))]
|
|
278
|
+
_ => {}
|
|
279
|
+
}
|
|
280
|
+
} else if ix < 0x40200000 {
|
|
281
|
+
/* x < 8.0 */
|
|
282
|
+
i = x as i32;
|
|
283
|
+
y = x - (i as f64);
|
|
284
|
+
p = y * (S0 + y * (S1 + y * (S2 + y * (S3 + y * (S4 + y * (S5 + y * S6))))));
|
|
285
|
+
q = 1.0 + y * (R1 + y * (R2 + y * (R3 + y * (R4 + y * (R5 + y * R6)))));
|
|
286
|
+
r = 0.5 * y + p / q;
|
|
287
|
+
z = 1.0; /* lgamma(1+s) = log(s) + lgamma(s) */
|
|
288
|
+
// TODO: In C, this was implemented using switch jumps with fallthrough.
|
|
289
|
+
// Does this implementation have performance problems?
|
|
290
|
+
if i >= 7 {
|
|
291
|
+
z *= y + 6.0;
|
|
292
|
+
}
|
|
293
|
+
if i >= 6 {
|
|
294
|
+
z *= y + 5.0;
|
|
295
|
+
}
|
|
296
|
+
if i >= 5 {
|
|
297
|
+
z *= y + 4.0;
|
|
298
|
+
}
|
|
299
|
+
if i >= 4 {
|
|
300
|
+
z *= y + 3.0;
|
|
301
|
+
}
|
|
302
|
+
if i >= 3 {
|
|
303
|
+
z *= y + 2.0;
|
|
304
|
+
r += log(z);
|
|
305
|
+
}
|
|
306
|
+
} else if ix < 0x43900000 {
|
|
307
|
+
/* 8.0 <= x < 2**58 */
|
|
308
|
+
t = log(x);
|
|
309
|
+
z = 1.0 / x;
|
|
310
|
+
y = z * z;
|
|
311
|
+
w = W0 + z * (W1 + y * (W2 + y * (W3 + y * (W4 + y * (W5 + y * W6)))));
|
|
312
|
+
r = (x - 0.5) * (t - 1.0) + w;
|
|
313
|
+
} else {
|
|
314
|
+
/* 2**58 <= x <= inf */
|
|
315
|
+
r = x * (log(x) - 1.0);
|
|
316
|
+
}
|
|
317
|
+
if sign {
|
|
318
|
+
r = nadj - r;
|
|
319
|
+
}
|
|
320
|
+
return (r, signgam);
|
|
321
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/* origin: FreeBSD /usr/src/lib/msun/src/e_lgammaf_r.c */
|
|
2
|
+
/*
|
|
3
|
+
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
|
|
4
|
+
*/
|
|
5
|
+
/*
|
|
6
|
+
* ====================================================
|
|
7
|
+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
8
|
+
*
|
|
9
|
+
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
10
|
+
* Permission to use, copy, modify, and distribute this
|
|
11
|
+
* software is freely granted, provided that this notice
|
|
12
|
+
* is preserved.
|
|
13
|
+
* ====================================================
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
use super::{floorf, k_cosf, k_sinf, logf};
|
|
17
|
+
|
|
18
|
+
const PI: f32 = 3.1415927410e+00; /* 0x40490fdb */
|
|
19
|
+
const A0: f32 = 7.7215664089e-02; /* 0x3d9e233f */
|
|
20
|
+
const A1: f32 = 3.2246702909e-01; /* 0x3ea51a66 */
|
|
21
|
+
const A2: f32 = 6.7352302372e-02; /* 0x3d89f001 */
|
|
22
|
+
const A3: f32 = 2.0580807701e-02; /* 0x3ca89915 */
|
|
23
|
+
const A4: f32 = 7.3855509982e-03; /* 0x3bf2027e */
|
|
24
|
+
const A5: f32 = 2.8905137442e-03; /* 0x3b3d6ec6 */
|
|
25
|
+
const A6: f32 = 1.1927076848e-03; /* 0x3a9c54a1 */
|
|
26
|
+
const A7: f32 = 5.1006977446e-04; /* 0x3a05b634 */
|
|
27
|
+
const A8: f32 = 2.2086278477e-04; /* 0x39679767 */
|
|
28
|
+
const A9: f32 = 1.0801156895e-04; /* 0x38e28445 */
|
|
29
|
+
const A10: f32 = 2.5214456400e-05; /* 0x37d383a2 */
|
|
30
|
+
const A11: f32 = 4.4864096708e-05; /* 0x383c2c75 */
|
|
31
|
+
const TC: f32 = 1.4616321325e+00; /* 0x3fbb16c3 */
|
|
32
|
+
const TF: f32 = -1.2148628384e-01; /* 0xbdf8cdcd */
|
|
33
|
+
/* TT = -(tail of TF) */
|
|
34
|
+
const TT: f32 = 6.6971006518e-09; /* 0x31e61c52 */
|
|
35
|
+
const T0: f32 = 4.8383611441e-01; /* 0x3ef7b95e */
|
|
36
|
+
const T1: f32 = -1.4758771658e-01; /* 0xbe17213c */
|
|
37
|
+
const T2: f32 = 6.4624942839e-02; /* 0x3d845a15 */
|
|
38
|
+
const T3: f32 = -3.2788541168e-02; /* 0xbd064d47 */
|
|
39
|
+
const T4: f32 = 1.7970675603e-02; /* 0x3c93373d */
|
|
40
|
+
const T5: f32 = -1.0314224288e-02; /* 0xbc28fcfe */
|
|
41
|
+
const T6: f32 = 6.1005386524e-03; /* 0x3bc7e707 */
|
|
42
|
+
const T7: f32 = -3.6845202558e-03; /* 0xbb7177fe */
|
|
43
|
+
const T8: f32 = 2.2596477065e-03; /* 0x3b141699 */
|
|
44
|
+
const T9: f32 = -1.4034647029e-03; /* 0xbab7f476 */
|
|
45
|
+
const T10: f32 = 8.8108185446e-04; /* 0x3a66f867 */
|
|
46
|
+
const T11: f32 = -5.3859531181e-04; /* 0xba0d3085 */
|
|
47
|
+
const T12: f32 = 3.1563205994e-04; /* 0x39a57b6b */
|
|
48
|
+
const T13: f32 = -3.1275415677e-04; /* 0xb9a3f927 */
|
|
49
|
+
const T14: f32 = 3.3552918467e-04; /* 0x39afe9f7 */
|
|
50
|
+
const U0: f32 = -7.7215664089e-02; /* 0xbd9e233f */
|
|
51
|
+
const U1: f32 = 6.3282704353e-01; /* 0x3f2200f4 */
|
|
52
|
+
const U2: f32 = 1.4549225569e+00; /* 0x3fba3ae7 */
|
|
53
|
+
const U3: f32 = 9.7771751881e-01; /* 0x3f7a4bb2 */
|
|
54
|
+
const U4: f32 = 2.2896373272e-01; /* 0x3e6a7578 */
|
|
55
|
+
const U5: f32 = 1.3381091878e-02; /* 0x3c5b3c5e */
|
|
56
|
+
const V1: f32 = 2.4559779167e+00; /* 0x401d2ebe */
|
|
57
|
+
const V2: f32 = 2.1284897327e+00; /* 0x4008392d */
|
|
58
|
+
const V3: f32 = 7.6928514242e-01; /* 0x3f44efdf */
|
|
59
|
+
const V4: f32 = 1.0422264785e-01; /* 0x3dd572af */
|
|
60
|
+
const V5: f32 = 3.2170924824e-03; /* 0x3b52d5db */
|
|
61
|
+
const S0: f32 = -7.7215664089e-02; /* 0xbd9e233f */
|
|
62
|
+
const S1: f32 = 2.1498242021e-01; /* 0x3e5c245a */
|
|
63
|
+
const S2: f32 = 3.2577878237e-01; /* 0x3ea6cc7a */
|
|
64
|
+
const S3: f32 = 1.4635047317e-01; /* 0x3e15dce6 */
|
|
65
|
+
const S4: f32 = 2.6642270386e-02; /* 0x3cda40e4 */
|
|
66
|
+
const S5: f32 = 1.8402845599e-03; /* 0x3af135b4 */
|
|
67
|
+
const S6: f32 = 3.1947532989e-05; /* 0x3805ff67 */
|
|
68
|
+
const R1: f32 = 1.3920053244e+00; /* 0x3fb22d3b */
|
|
69
|
+
const R2: f32 = 7.2193557024e-01; /* 0x3f38d0c5 */
|
|
70
|
+
const R3: f32 = 1.7193385959e-01; /* 0x3e300f6e */
|
|
71
|
+
const R4: f32 = 1.8645919859e-02; /* 0x3c98bf54 */
|
|
72
|
+
const R5: f32 = 7.7794247773e-04; /* 0x3a4beed6 */
|
|
73
|
+
const R6: f32 = 7.3266842264e-06; /* 0x36f5d7bd */
|
|
74
|
+
const W0: f32 = 4.1893854737e-01; /* 0x3ed67f1d */
|
|
75
|
+
const W1: f32 = 8.3333335817e-02; /* 0x3daaaaab */
|
|
76
|
+
const W2: f32 = -2.7777778450e-03; /* 0xbb360b61 */
|
|
77
|
+
const W3: f32 = 7.9365057172e-04; /* 0x3a500cfd */
|
|
78
|
+
const W4: f32 = -5.9518753551e-04; /* 0xba1c065c */
|
|
79
|
+
const W5: f32 = 8.3633989561e-04; /* 0x3a5b3dd2 */
|
|
80
|
+
const W6: f32 = -1.6309292987e-03; /* 0xbad5c4e8 */
|
|
81
|
+
|
|
82
|
+
/* sin(PI*x) assuming x > 2^-100, if sin(PI*x)==0 the sign is arbitrary */
|
|
83
|
+
fn sin_pi(mut x: f32) -> f32 {
|
|
84
|
+
let mut y: f64;
|
|
85
|
+
let mut n: isize;
|
|
86
|
+
|
|
87
|
+
/* spurious inexact if odd int */
|
|
88
|
+
x = 2.0 * (x * 0.5 - floorf(x * 0.5)); /* x mod 2.0 */
|
|
89
|
+
|
|
90
|
+
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
|
|
99
|
+
_ => k_sinf(y),
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
|
|
104
|
+
pub fn lgammaf_r(mut x: f32) -> (f32, i32) {
|
|
105
|
+
let u = x.to_bits();
|
|
106
|
+
let mut t: f32;
|
|
107
|
+
let y: f32;
|
|
108
|
+
let mut z: f32;
|
|
109
|
+
let nadj: f32;
|
|
110
|
+
let p: f32;
|
|
111
|
+
let p1: f32;
|
|
112
|
+
let p2: f32;
|
|
113
|
+
let p3: f32;
|
|
114
|
+
let q: f32;
|
|
115
|
+
let mut r: f32;
|
|
116
|
+
let w: f32;
|
|
117
|
+
let ix: u32;
|
|
118
|
+
let i: i32;
|
|
119
|
+
let sign: bool;
|
|
120
|
+
let mut signgam: i32;
|
|
121
|
+
|
|
122
|
+
/* purge off +-inf, NaN, +-0, tiny and negative arguments */
|
|
123
|
+
signgam = 1;
|
|
124
|
+
sign = (u >> 31) != 0;
|
|
125
|
+
ix = u & 0x7fffffff;
|
|
126
|
+
if ix >= 0x7f800000 {
|
|
127
|
+
return (x * x, signgam);
|
|
128
|
+
}
|
|
129
|
+
if ix < 0x35000000 {
|
|
130
|
+
/* |x| < 2**-21, return -log(|x|) */
|
|
131
|
+
if sign {
|
|
132
|
+
signgam = -1;
|
|
133
|
+
x = -x;
|
|
134
|
+
}
|
|
135
|
+
return (-logf(x), signgam);
|
|
136
|
+
}
|
|
137
|
+
if sign {
|
|
138
|
+
x = -x;
|
|
139
|
+
t = sin_pi(x);
|
|
140
|
+
if t == 0.0 {
|
|
141
|
+
/* -integer */
|
|
142
|
+
return (1.0 / (x - x), signgam);
|
|
143
|
+
}
|
|
144
|
+
if t > 0.0 {
|
|
145
|
+
signgam = -1;
|
|
146
|
+
} else {
|
|
147
|
+
t = -t;
|
|
148
|
+
}
|
|
149
|
+
nadj = logf(PI / (t * x));
|
|
150
|
+
} else {
|
|
151
|
+
nadj = 0.0;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/* purge off 1 and 2 */
|
|
155
|
+
if ix == 0x3f800000 || ix == 0x40000000 {
|
|
156
|
+
r = 0.0;
|
|
157
|
+
}
|
|
158
|
+
/* for x < 2.0 */
|
|
159
|
+
else if ix < 0x40000000 {
|
|
160
|
+
if ix <= 0x3f666666 {
|
|
161
|
+
/* lgamma(x) = lgamma(x+1)-log(x) */
|
|
162
|
+
r = -logf(x);
|
|
163
|
+
if ix >= 0x3f3b4a20 {
|
|
164
|
+
y = 1.0 - x;
|
|
165
|
+
i = 0;
|
|
166
|
+
} else if ix >= 0x3e6d3308 {
|
|
167
|
+
y = x - (TC - 1.0);
|
|
168
|
+
i = 1;
|
|
169
|
+
} else {
|
|
170
|
+
y = x;
|
|
171
|
+
i = 2;
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
r = 0.0;
|
|
175
|
+
if ix >= 0x3fdda618 {
|
|
176
|
+
/* [1.7316,2] */
|
|
177
|
+
y = 2.0 - x;
|
|
178
|
+
i = 0;
|
|
179
|
+
} else if ix >= 0x3F9da620 {
|
|
180
|
+
/* [1.23,1.73] */
|
|
181
|
+
y = x - TC;
|
|
182
|
+
i = 1;
|
|
183
|
+
} else {
|
|
184
|
+
y = x - 1.0;
|
|
185
|
+
i = 2;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
match i {
|
|
189
|
+
0 => {
|
|
190
|
+
z = y * y;
|
|
191
|
+
p1 = A0 + z * (A2 + z * (A4 + z * (A6 + z * (A8 + z * A10))));
|
|
192
|
+
p2 = z * (A1 + z * (A3 + z * (A5 + z * (A7 + z * (A9 + z * A11)))));
|
|
193
|
+
p = y * p1 + p2;
|
|
194
|
+
r += p - 0.5 * y;
|
|
195
|
+
}
|
|
196
|
+
1 => {
|
|
197
|
+
z = y * y;
|
|
198
|
+
w = z * y;
|
|
199
|
+
p1 = T0 + w * (T3 + w * (T6 + w * (T9 + w * T12))); /* parallel comp */
|
|
200
|
+
p2 = T1 + w * (T4 + w * (T7 + w * (T10 + w * T13)));
|
|
201
|
+
p3 = T2 + w * (T5 + w * (T8 + w * (T11 + w * T14)));
|
|
202
|
+
p = z * p1 - (TT - w * (p2 + y * p3));
|
|
203
|
+
r += TF + p;
|
|
204
|
+
}
|
|
205
|
+
2 => {
|
|
206
|
+
p1 = y * (U0 + y * (U1 + y * (U2 + y * (U3 + y * (U4 + y * U5)))));
|
|
207
|
+
p2 = 1.0 + y * (V1 + y * (V2 + y * (V3 + y * (V4 + y * V5))));
|
|
208
|
+
r += -0.5 * y + p1 / p2;
|
|
209
|
+
}
|
|
210
|
+
#[cfg(debug_assertions)]
|
|
211
|
+
_ => unreachable!(),
|
|
212
|
+
#[cfg(not(debug_assertions))]
|
|
213
|
+
_ => {}
|
|
214
|
+
}
|
|
215
|
+
} else if ix < 0x41000000 {
|
|
216
|
+
/* x < 8.0 */
|
|
217
|
+
i = x as i32;
|
|
218
|
+
y = x - (i as f32);
|
|
219
|
+
p = y * (S0 + y * (S1 + y * (S2 + y * (S3 + y * (S4 + y * (S5 + y * S6))))));
|
|
220
|
+
q = 1.0 + y * (R1 + y * (R2 + y * (R3 + y * (R4 + y * (R5 + y * R6)))));
|
|
221
|
+
r = 0.5 * y + p / q;
|
|
222
|
+
z = 1.0; /* lgamma(1+s) = log(s) + lgamma(s) */
|
|
223
|
+
// TODO: In C, this was implemented using switch jumps with fallthrough.
|
|
224
|
+
// Does this implementation have performance problems?
|
|
225
|
+
if i >= 7 {
|
|
226
|
+
z *= y + 6.0;
|
|
227
|
+
}
|
|
228
|
+
if i >= 6 {
|
|
229
|
+
z *= y + 5.0;
|
|
230
|
+
}
|
|
231
|
+
if i >= 5 {
|
|
232
|
+
z *= y + 4.0;
|
|
233
|
+
}
|
|
234
|
+
if i >= 4 {
|
|
235
|
+
z *= y + 3.0;
|
|
236
|
+
}
|
|
237
|
+
if i >= 3 {
|
|
238
|
+
z *= y + 2.0;
|
|
239
|
+
r += logf(z);
|
|
240
|
+
}
|
|
241
|
+
} else if ix < 0x5c800000 {
|
|
242
|
+
/* 8.0 <= x < 2**58 */
|
|
243
|
+
t = logf(x);
|
|
244
|
+
z = 1.0 / x;
|
|
245
|
+
y = z * z;
|
|
246
|
+
w = W0 + z * (W1 + y * (W2 + y * (W3 + y * (W4 + y * (W5 + y * W6)))));
|
|
247
|
+
r = (x - 0.5) * (t - 1.0) + w;
|
|
248
|
+
} else {
|
|
249
|
+
/* 2**58 <= x <= inf */
|
|
250
|
+
r = x * (logf(x) - 1.0);
|
|
251
|
+
}
|
|
252
|
+
if sign {
|
|
253
|
+
r = nadj - r;
|
|
254
|
+
}
|
|
255
|
+
return (r, signgam);
|
|
256
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/* origin: FreeBSD /usr/src/lib/msun/src/e_log.c */
|
|
2
|
+
/*
|
|
3
|
+
* ====================================================
|
|
4
|
+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
5
|
+
*
|
|
6
|
+
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
|
7
|
+
* Permission to use, copy, modify, and distribute this
|
|
8
|
+
* software is freely granted, provided that this notice
|
|
9
|
+
* is preserved.
|
|
10
|
+
* ====================================================
|
|
11
|
+
*/
|
|
12
|
+
/* log(x)
|
|
13
|
+
* Return the logarithm of x
|
|
14
|
+
*
|
|
15
|
+
* Method :
|
|
16
|
+
* 1. Argument Reduction: find k and f such that
|
|
17
|
+
* x = 2^k * (1+f),
|
|
18
|
+
* where sqrt(2)/2 < 1+f < sqrt(2) .
|
|
19
|
+
*
|
|
20
|
+
* 2. Approximation of log(1+f).
|
|
21
|
+
* Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
|
|
22
|
+
* = 2s + 2/3 s**3 + 2/5 s**5 + .....,
|
|
23
|
+
* = 2s + s*R
|
|
24
|
+
* We use a special Remez algorithm on [0,0.1716] to generate
|
|
25
|
+
* a polynomial of degree 14 to approximate R The maximum error
|
|
26
|
+
* of this polynomial approximation is bounded by 2**-58.45. In
|
|
27
|
+
* other words,
|
|
28
|
+
* 2 4 6 8 10 12 14
|
|
29
|
+
* R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
|
|
30
|
+
* (the values of Lg1 to Lg7 are listed in the program)
|
|
31
|
+
* and
|
|
32
|
+
* | 2 14 | -58.45
|
|
33
|
+
* | Lg1*s +...+Lg7*s - R(z) | <= 2
|
|
34
|
+
* | |
|
|
35
|
+
* Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
|
|
36
|
+
* In order to guarantee error in log below 1ulp, we compute log
|
|
37
|
+
* by
|
|
38
|
+
* log(1+f) = f - s*(f - R) (if f is not too large)
|
|
39
|
+
* log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
|
|
40
|
+
*
|
|
41
|
+
* 3. Finally, log(x) = k*ln2 + log(1+f).
|
|
42
|
+
* = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
|
|
43
|
+
* Here ln2 is split into two floating point number:
|
|
44
|
+
* ln2_hi + ln2_lo,
|
|
45
|
+
* where n*ln2_hi is always exact for |n| < 2000.
|
|
46
|
+
*
|
|
47
|
+
* Special cases:
|
|
48
|
+
* log(x) is NaN with signal if x < 0 (including -INF) ;
|
|
49
|
+
* log(+INF) is +INF; log(0) is -INF with signal;
|
|
50
|
+
* log(NaN) is that NaN with no signal.
|
|
51
|
+
*
|
|
52
|
+
* Accuracy:
|
|
53
|
+
* according to an error analysis, the error is always less than
|
|
54
|
+
* 1 ulp (unit in the last place).
|
|
55
|
+
*
|
|
56
|
+
* Constants:
|
|
57
|
+
* The hexadecimal values are the intended ones for the following
|
|
58
|
+
* constants. The decimal values may be used, provided that the
|
|
59
|
+
* compiler will convert from decimal to binary accurately enough
|
|
60
|
+
* to produce the hexadecimal values shown.
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
const LN2_HI: f64 = 6.93147180369123816490e-01; /* 3fe62e42 fee00000 */
|
|
64
|
+
const LN2_LO: f64 = 1.90821492927058770002e-10; /* 3dea39ef 35793c76 */
|
|
65
|
+
const LG1: f64 = 6.666666666666735130e-01; /* 3FE55555 55555593 */
|
|
66
|
+
const LG2: f64 = 3.999999999940941908e-01; /* 3FD99999 9997FA04 */
|
|
67
|
+
const LG3: f64 = 2.857142874366239149e-01; /* 3FD24924 94229359 */
|
|
68
|
+
const LG4: f64 = 2.222219843214978396e-01; /* 3FCC71C5 1D8E78AF */
|
|
69
|
+
const LG5: f64 = 1.818357216161805012e-01; /* 3FC74664 96CB03DE */
|
|
70
|
+
const LG6: f64 = 1.531383769920937332e-01; /* 3FC39A09 D078C69F */
|
|
71
|
+
const LG7: f64 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
|
|
72
|
+
|
|
73
|
+
/// The natural logarithm of `x` (f64).
|
|
74
|
+
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
|
|
75
|
+
pub fn log(mut x: f64) -> f64 {
|
|
76
|
+
let x1p54 = f64::from_bits(0x4350000000000000); // 0x1p54 === 2 ^ 54
|
|
77
|
+
|
|
78
|
+
let mut ui = x.to_bits();
|
|
79
|
+
let mut hx: u32 = (ui >> 32) as u32;
|
|
80
|
+
let mut k: i32 = 0;
|
|
81
|
+
|
|
82
|
+
if (hx < 0x00100000) || ((hx >> 31) != 0) {
|
|
83
|
+
/* x < 2**-126 */
|
|
84
|
+
if ui << 1 == 0 {
|
|
85
|
+
return -1. / (x * x); /* log(+-0)=-inf */
|
|
86
|
+
}
|
|
87
|
+
if hx >> 31 != 0 {
|
|
88
|
+
return (x - x) / 0.0; /* log(-#) = NaN */
|
|
89
|
+
}
|
|
90
|
+
/* subnormal number, scale x up */
|
|
91
|
+
k -= 54;
|
|
92
|
+
x *= x1p54;
|
|
93
|
+
ui = x.to_bits();
|
|
94
|
+
hx = (ui >> 32) as u32;
|
|
95
|
+
} else if hx >= 0x7ff00000 {
|
|
96
|
+
return x;
|
|
97
|
+
} else if hx == 0x3ff00000 && ui << 32 == 0 {
|
|
98
|
+
return 0.;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/* reduce x into [sqrt(2)/2, sqrt(2)] */
|
|
102
|
+
hx += 0x3ff00000 - 0x3fe6a09e;
|
|
103
|
+
k += ((hx >> 20) as i32) - 0x3ff;
|
|
104
|
+
hx = (hx & 0x000fffff) + 0x3fe6a09e;
|
|
105
|
+
ui = ((hx as u64) << 32) | (ui & 0xffffffff);
|
|
106
|
+
x = f64::from_bits(ui);
|
|
107
|
+
|
|
108
|
+
let f: f64 = x - 1.0;
|
|
109
|
+
let hfsq: f64 = 0.5 * f * f;
|
|
110
|
+
let s: f64 = f / (2.0 + f);
|
|
111
|
+
let z: f64 = s * s;
|
|
112
|
+
let w: f64 = z * z;
|
|
113
|
+
let t1: f64 = w * (LG2 + w * (LG4 + w * LG6));
|
|
114
|
+
let t2: f64 = z * (LG1 + w * (LG3 + w * (LG5 + w * LG7)));
|
|
115
|
+
let r: f64 = t2 + t1;
|
|
116
|
+
let dk: f64 = k as f64;
|
|
117
|
+
s * (hfsq + r) + dk * LN2_LO - hfsq + f + dk * LN2_HI
|
|
118
|
+
}
|