]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/cmath.rs
30b0c54dc2dc212124199b385619820f06545c73
[rust.git] / src / libcore / num / cmath.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #[doc(hidden)]; // FIXME #3538
12
13 // function names are almost identical to C's libmath, a few have been
14 // renamed, grep for "rename:"
15
16 pub mod c_double_utils {
17     use libc::{c_double, c_int};
18
19     #[link_name = "m"]
20     #[abi = "cdecl"]
21     pub extern {
22         // Alpabetically sorted by link_name
23
24         unsafe fn acos(n: c_double) -> c_double;
25         unsafe fn asin(n: c_double) -> c_double;
26         unsafe fn atan(n: c_double) -> c_double;
27         unsafe fn atan2(a: c_double, b: c_double) -> c_double;
28         unsafe fn cbrt(n: c_double) -> c_double;
29         unsafe fn ceil(n: c_double) -> c_double;
30         unsafe fn copysign(x: c_double, y: c_double) -> c_double;
31         unsafe fn cos(n: c_double) -> c_double;
32         unsafe fn cosh(n: c_double) -> c_double;
33         unsafe fn erf(n: c_double) -> c_double;
34         unsafe fn erfc(n: c_double) -> c_double;
35         unsafe fn exp(n: c_double) -> c_double;
36         unsafe fn expm1(n: c_double) -> c_double;
37         unsafe fn exp2(n: c_double) -> c_double;
38         #[link_name="fabs"] unsafe fn abs(n: c_double) -> c_double;
39         // rename: for clarity and consistency with add/sub/mul/div
40         #[link_name="fdim"]
41         unsafe fn abs_sub(a: c_double, b: c_double) -> c_double;
42         unsafe fn floor(n: c_double) -> c_double;
43         // rename: for clarity and consistency with add/sub/mul/div
44         #[link_name="fma"]
45         unsafe fn mul_add(a: c_double, b: c_double, c: c_double) -> c_double;
46         #[link_name="fmax"]
47         unsafe fn fmax(a: c_double, b: c_double) -> c_double;
48         #[link_name="fmin"]
49         unsafe fn fmin(a: c_double, b: c_double) -> c_double;
50         #[link_name="nextafter"]
51         unsafe fn next_after(x: c_double, y: c_double) -> c_double;
52         unsafe fn frexp(n: c_double, value: &mut c_int) -> c_double;
53         unsafe fn hypot(x: c_double, y: c_double) -> c_double;
54         unsafe fn ldexp(x: c_double, n: c_int) -> c_double;
55         #[cfg(unix)]
56         #[link_name="lgamma_r"]
57         unsafe fn lgamma(n: c_double, sign: &mut c_int) -> c_double;
58         #[cfg(windows)]
59         #[link_name="__lgamma_r"]
60         unsafe fn lgamma(n: c_double, sign: &mut c_int) -> c_double;
61         // renamed: log is a reserved keyword; ln seems more natural, too
62         #[link_name="log"] unsafe fn ln(n: c_double) -> c_double;
63         // renamed: "logb" /often/ is confused for log2 by beginners
64         #[link_name="logb"] unsafe fn log_radix(n: c_double) -> c_double;
65         // renamed: to be consitent with log as ln
66         #[link_name="log1p"] unsafe fn ln1p(n: c_double) -> c_double;
67         unsafe fn log10(n: c_double) -> c_double;
68         unsafe fn log2(n: c_double) -> c_double;
69         #[link_name="ilogb"] unsafe fn ilog_radix(n: c_double) -> c_int;
70         unsafe fn modf(n: c_double, iptr: &mut c_double) -> c_double;
71         unsafe fn pow(n: c_double, e: c_double) -> c_double;
72     // FIXME (#1379): enable when rounding modes become available
73     //    unsafe fn rint(n: c_double) -> c_double;
74         unsafe fn round(n: c_double) -> c_double;
75         // rename: for consistency with logradix
76         #[link_name="scalbn"] unsafe fn ldexp_radix(n: c_double, i: c_int) ->
77             c_double;
78         unsafe fn sin(n: c_double) -> c_double;
79         unsafe fn sinh(n: c_double) -> c_double;
80         unsafe fn sqrt(n: c_double) -> c_double;
81         unsafe fn tan(n: c_double) -> c_double;
82         unsafe fn tanh(n: c_double) -> c_double;
83         unsafe fn tgamma(n: c_double) -> c_double;
84         unsafe fn trunc(n: c_double) -> c_double;
85
86         // These are commonly only available for doubles
87
88         unsafe fn j0(n: c_double) -> c_double;
89         unsafe fn j1(n: c_double) -> c_double;
90         unsafe fn jn(i: c_int, n: c_double) -> c_double;
91
92         unsafe fn y0(n: c_double) -> c_double;
93         unsafe fn y1(n: c_double) -> c_double;
94         unsafe fn yn(i: c_int, n: c_double) -> c_double;
95     }
96 }
97
98 pub mod c_float_utils {
99     use libc::{c_float, c_int};
100
101     #[link_name = "m"]
102     #[abi = "cdecl"]
103     pub extern {
104         // Alpabetically sorted by link_name
105
106         #[link_name="acosf"] unsafe fn acos(n: c_float) -> c_float;
107         #[link_name="asinf"] unsafe fn asin(n: c_float) -> c_float;
108         #[link_name="atanf"] unsafe fn atan(n: c_float) -> c_float;
109         #[link_name="atan2f"]
110         unsafe fn atan2(a: c_float, b: c_float) -> c_float;
111         #[link_name="cbrtf"] unsafe fn cbrt(n: c_float) -> c_float;
112         #[link_name="ceilf"] unsafe fn ceil(n: c_float) -> c_float;
113         #[link_name="copysignf"] unsafe fn copysign(x: c_float,
114                                                   y: c_float) -> c_float;
115         #[link_name="cosf"] unsafe fn cos(n: c_float) -> c_float;
116         #[link_name="coshf"] unsafe fn cosh(n: c_float) -> c_float;
117         #[link_name="erff"] unsafe fn erf(n: c_float) -> c_float;
118         #[link_name="erfcf"] unsafe fn erfc(n: c_float) -> c_float;
119         #[link_name="expf"] unsafe fn exp(n: c_float) -> c_float;
120         #[link_name="expm1f"]unsafe fn expm1(n: c_float) -> c_float;
121         #[link_name="exp2f"] unsafe fn exp2(n: c_float) -> c_float;
122         #[link_name="fabsf"] unsafe fn abs(n: c_float) -> c_float;
123         #[link_name="fdimf"]
124         unsafe fn abs_sub(a: c_float, b: c_float) -> c_float;
125         #[link_name="floorf"] unsafe fn floor(n: c_float) -> c_float;
126         #[link_name="frexpf"] unsafe fn frexp(n: c_float,
127                                             value: &mut c_int) -> c_float;
128         #[link_name="fmaf"]
129         unsafe fn mul_add(a: c_float, b: c_float, c: c_float) -> c_float;
130         #[link_name="fmaxf"]
131         unsafe fn fmax(a: c_float, b: c_float) -> c_float;
132         #[link_name="fminf"]
133         unsafe fn fmin(a: c_float, b: c_float) -> c_float;
134         #[link_name="nextafterf"]
135         unsafe fn next_after(x: c_float, y: c_float) -> c_float;
136         #[link_name="hypotf"]
137         unsafe fn hypot(x: c_float, y: c_float) -> c_float;
138         #[link_name="ldexpf"]
139         unsafe fn ldexp(x: c_float, n: c_int) -> c_float;
140
141         #[cfg(unix)]
142         #[link_name="lgammaf_r"] unsafe fn lgamma(n: c_float,
143                                                 sign: &mut c_int) -> c_float;
144
145         #[cfg(windows)]
146         #[link_name="__lgammaf_r"]
147         unsafe fn lgamma(n: c_float, sign: &mut c_int) -> c_float;
148
149         #[link_name="logf"] unsafe fn ln(n: c_float) -> c_float;
150         #[link_name="logbf"] unsafe fn log_radix(n: c_float) -> c_float;
151         #[link_name="log1pf"] unsafe fn ln1p(n: c_float) -> c_float;
152         #[link_name="log2f"] unsafe fn log2(n: c_float) -> c_float;
153         #[link_name="log10f"] unsafe fn log10(n: c_float) -> c_float;
154         #[link_name="ilogbf"] unsafe fn ilog_radix(n: c_float) -> c_int;
155         #[link_name="modff"] unsafe fn modf(n: c_float,
156                                           iptr: &mut c_float) -> c_float;
157         #[link_name="powf"] unsafe fn pow(n: c_float, e: c_float) -> c_float;
158     // FIXME (#1379): enable when rounding modes become available
159     //    #[link_name="rintf"] unsafe fn rint(n: c_float) -> c_float;
160         #[link_name="roundf"] unsafe fn round(n: c_float) -> c_float;
161         #[link_name="scalbnf"] unsafe fn ldexp_radix(n: c_float, i: c_int)
162             -> c_float;
163         #[link_name="sinf"] unsafe fn sin(n: c_float) -> c_float;
164         #[link_name="sinhf"] unsafe fn sinh(n: c_float) -> c_float;
165         #[link_name="sqrtf"] unsafe fn sqrt(n: c_float) -> c_float;
166         #[link_name="tanf"] unsafe fn tan(n: c_float) -> c_float;
167         #[link_name="tanhf"] unsafe fn tanh(n: c_float) -> c_float;
168         #[link_name="tgammaf"] unsafe fn tgamma(n: c_float) -> c_float;
169         #[link_name="truncf"] unsafe fn trunc(n: c_float) -> c_float;
170     }
171 }
172
173 // PORT check these by running src/etc/machconsts.c for your architecture
174
175 // FIXME obtain machine float/math constants automatically (Issue #1986)
176
177 pub mod c_float_targ_consts {
178     pub static radix: uint = 2u;
179     pub static mantissa_digits: uint = 24u;
180     pub static digits: uint = 6u;
181     pub static min_exp: uint = -125u;
182     pub static max_exp: uint = 128u;
183     pub static min_10_exp: int = -37;
184     pub static max_10_exp: int = 38;
185     // FIXME (#1433): this is wrong, replace with hexadecimal (%a) staticants
186     // below.
187     pub static min_value: f32 = 1.175494e-38_f32;
188     pub static max_value: f32 = 3.402823e+38_f32;
189     pub static epsilon: f32 = 0.000000_f32;
190 }
191
192 pub mod c_double_targ_consts {
193     pub static radix: uint = 2u;
194     pub static mantissa_digits: uint = 53u;
195     pub static digits: uint = 15u;
196     pub static min_exp: uint = -1021u;
197     pub static max_exp: uint = 1024u;
198     pub static min_10_exp: int = -307;
199     pub static max_10_exp: int = 308;
200     // FIXME (#1433): this is wrong, replace with hexadecimal (%a) staticants
201     // below.
202     pub static min_value: f64 = 2.225074e-308_f64;
203     pub static max_value: f64 = 1.797693e+308_f64;
204     pub static epsilon: f64 = 2.220446e-16_f64;
205 }
206
207 /*
208
209 FIXME use these once they can be parsed (see Issue #1433)
210
211 pub mod c_float_math_consts {
212     pub static pi: c_float = 0x1.921fb6p+1_f32;
213     pub static div_1_pi: c_float = 0x1.45f306p-2_f32;
214     pub static div_2_pi: c_float = 0x1.45f306p-1_f32;
215     pub static div_pi_2: c_float = 0x1.921fb6p+0_f32;
216     pub static div_pi_4: c_float = 0x1.921fb6p-1_f32;
217     pub static div_2_sqrtpi: c_float = 0x1.20dd76p+0_f32;
218     pub static e: c_float = 0x1.5bf0a8p+1_f32;
219     pub static log2_e: c_float = 0x1.715476p+0_f32;
220     pub static log10_e: c_float = 0x1.bcb7b2p-2_f32;
221     pub static ln_2: c_float = 0x1.62e43p-1_f32;
222     pub static ln_10: c_float = 0x1.26bb1cp+1_f32;
223     pub static sqrt2: c_float = 0x1.6a09e6p+0_f32;
224     pub static div_1_sqrt2: c_float = 0x1.6a09e6p-1_f32;
225 }
226
227 pub mod c_double_math_consts {
228     pub static pi: c_double = 0x1.921fb54442d18p+1_f64;
229     pub static div_1_pi: c_double = 0x1.45f306dc9c883p-2_f64;
230     pub static div_2_pi: c_double = 0x1.45f306dc9c883p-1_f64;
231     pub static div_pi_2: c_double = 0x1.921fb54442d18p+0_f64;
232     pub static div_pi_4: c_double = 0x1.921fb54442d18p-1_f64;
233     pub static div_2_sqrtpi: c_double = 0x1.20dd750429b6dp+0_f64;
234     pub static e: c_double = 0x1.5bf0a8b145769p+1_f64;
235     pub static log2_e: c_double = 0x1.71547652b82fep+0_f64;
236     pub static log10_e: c_double = 0x1.bcb7b1526e50ep-2_f64;
237     pub static ln_2: c_double = 0x1.62e42fefa39efp-1_f64;
238     pub static ln_10: c_double = 0x1.26bb1bbb55516p+1_f64;
239     pub static sqrt2: c_double = 0x1.6a09e667f3bcdp+0_f64;
240     pub static div_1_sqrt2: c_double = 0x1.6a09e667f3bcdp-1_f64;
241 }
242
243 pub mod c_float_targ_consts {
244     pub static radix: uint = 2u;
245     pub static mantissa_digits: uint = 24u;
246     pub static digits: uint = 6u;
247     pub static min_exp: int = -125;
248     pub static max_exp: int = 128;
249     pub static min_10_exp: int = -37;
250     pub static max_10_exp: int = 38;
251     pub static min_value: c_float = 0x1p-126_f32;
252     pub static max_value: c_float = 0x1.fffffep+127_f32;
253     pub static epsilon: c_float = 0x1p-23_f32;
254 }
255
256 pub mod c_double_targ_consts {
257     pub static radix: uint = 2u;
258     pub static mantissa_digits: uint = 53u;
259     pub static digits: uint = 15u;
260     pub static min_exp: int = -1021;
261     pub static max_exp: int = 1024;
262     pub static min_10_exp: int = -307;
263     pub static max_10_exp: int = 308;
264     pub static min_value: c_double = 0x1p-1022_f64;
265     pub static max_value: c_double = 0x1.fffffffffffffp+1023_f64;
266     pub static epsilon: c_double = 0x1p-52_f64;
267 }
268
269 */
270
271 //
272 // Local Variables:
273 // mode: rust
274 // fill-column: 78;
275 // indent-tabs-mode: nil
276 // c-basic-offset: 4
277 // buffer-file-coding-system: utf-8-unix
278 // End:
279 //
280