Skip to content

Commit 97fc39b

Browse files
bogglebrson
authored andcommitted
std: factored f32 and f64 out from math
1 parent a611496 commit 97fc39b

File tree

6 files changed

+289
-77
lines changed

6 files changed

+289
-77
lines changed

src/lib/cmath.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import ctypes::c_int;
2+
3+
#[link_name = "m"]
4+
#[abi = "cdecl"]
5+
native mod f64 {
6+
7+
// Alpabetically sorted by link_name
8+
9+
pure fn acos(n: f64) -> f64;
10+
pure fn asin(n: f64) -> f64;
11+
pure fn atan(n: f64) -> f64;
12+
pure fn atan2(a: f64, b: f64) -> f64;
13+
pure fn ceil(n: f64) -> f64;
14+
pure fn cos(n: f64) -> f64;
15+
pure fn cosh(n: f64) -> f64;
16+
pure fn exp(n: f64) -> f64;
17+
#[link_name="fabs"] pure fn abs(n: f64) -> f64;
18+
pure fn floor(n: f64) -> f64;
19+
pure fn fmod(x: f64, y: f64) -> f64;
20+
pure fn frexp(n: f64, &value: c_int) -> f64;
21+
pure fn ldexp(x: f64, n: c_int) -> f64;
22+
#[link_name="log"] pure fn ln(n: f64) -> f64;
23+
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
24+
pure fn log10(n: f64) -> f64;
25+
pure fn log2(n: f64) -> f64;
26+
pure fn modf(n: f64, &iptr: f64) -> f64;
27+
pure fn pow(n: f64, e: f64) -> f64;
28+
pure fn rint(n: f64) -> f64;
29+
pure fn round(n: f64) -> f64;
30+
pure fn sin(n: f64) -> f64;
31+
pure fn sinh(n: f64) -> f64;
32+
pure fn sqrt(n: f64) -> f64;
33+
pure fn tan(n: f64) -> f64;
34+
pure fn tanh(n: f64) -> f64;
35+
pure fn trunc(n: f64) -> f64;
36+
}
37+
38+
#[link_name = "m"]
39+
#[abi = "cdecl"]
40+
native mod f32 {
41+
42+
// Alpabetically sorted by link_name
43+
44+
#[link_name="acosf"] pure fn acos(n: f32) -> f32;
45+
#[link_name="asinf"] pure fn asin(n: f32) -> f32;
46+
#[link_name="atanf"] pure fn atan(n: f32) -> f32;
47+
#[link_name="atan2f"] pure fn atan2(a: f32, b: f32) -> f32;
48+
#[link_name="ceilf"] pure fn ceil(n: f32) -> f32;
49+
#[link_name="cosf"] pure fn cos(n: f32) -> f32;
50+
#[link_name="coshf"] pure fn cosh(n: f32) -> f32;
51+
#[link_name="expf"] pure fn exp(n: f32) -> f32;
52+
#[link_name="fabsf"] pure fn abs(n: f32) -> f32;
53+
#[link_name="floorf"] pure fn floor(n: f32) -> f32;
54+
#[link_name="frexpf"] pure fn frexp(n: f64, &value: c_int) -> f32;
55+
#[link_name="fmodf"] pure fn fmod(x: f32, y: f32) -> f32;
56+
#[link_name="ldexpf"] pure fn ldexp(x: f32, n: c_int) -> f32;
57+
#[link_name="logf"] pure fn ln(n: f32) -> f32;
58+
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
59+
#[link_name="log2f"] pure fn log2(n: f32) -> f32;
60+
#[link_name="log10f"] pure fn log10(n: f32) -> f32;
61+
#[link_name="modff"] pure fn modf(n: f32, &iptr: f32) -> f32;
62+
#[link_name="powf"] pure fn pow(n: f32, e: f32) -> f32;
63+
#[link_name="rintf"] pure fn rint(n: f32) -> f32;
64+
#[link_name="roundf"] pure fn round(n: f32) -> f32;
65+
#[link_name="sinf"] pure fn sin(n: f32) -> f32;
66+
#[link_name="sinhf"] pure fn sinh(n: f32) -> f32;
67+
#[link_name="sqrtf"] pure fn sqrt(n: f32) -> f32;
68+
#[link_name="tanf"] pure fn tan(n: f32) -> f32;
69+
#[link_name="tanhf"] pure fn tanh(n: f32) -> f32;
70+
#[link_name="truncf"] pure fn trunc(n: f32) -> f32;
71+
}

src/lib/f32.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import cmath::f32::*;
2+
3+
export
4+
acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor, fmod,
5+
frexp, ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin,
6+
sinh, sqrt, tan, tanh, trunc;
7+
8+
export consts;
9+
10+
mod consts {
11+
12+
/*
13+
Const: pi
14+
15+
Archimedes' constant
16+
*/
17+
const pi: f32 = 3.14159265358979323846264338327950288f32;
18+
19+
/*
20+
Const: frac_pi_2
21+
22+
pi/2.0
23+
*/
24+
const frac_pi_2: f32 = 1.57079632679489661923132169163975144f32;
25+
26+
/*
27+
Const: frac_pi_4
28+
29+
pi/4.0
30+
*/
31+
const frac_pi_4: f32 = 0.785398163397448309615660845819875721f32;
32+
33+
/*
34+
Const: frac_1_pi
35+
36+
1.0/pi
37+
*/
38+
const frac_1_pi: f32 = 0.318309886183790671537767526745028724f32;
39+
40+
/*
41+
Const: frac_2_pi
42+
43+
2.0/pi
44+
*/
45+
const frac_2_pi: f32 = 0.636619772367581343075535053490057448f32;
46+
47+
/*
48+
Const: frac_2_sqrtpi
49+
50+
2.0/sqrt(pi)
51+
*/
52+
const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517f32;
53+
54+
/*
55+
Const: sqrt2
56+
57+
sqrt(2.0)
58+
*/
59+
const sqrt2: f32 = 1.41421356237309504880168872420969808f32;
60+
61+
/*
62+
Const: frac_1_sqrt2
63+
64+
1.0/sqrt(2.0)
65+
*/
66+
const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039f32;
67+
68+
/*
69+
Const: e
70+
71+
Euler's number
72+
*/
73+
const e: f32 = 2.71828182845904523536028747135266250f32;
74+
75+
/*
76+
Const: log2_e
77+
78+
log2(e)
79+
*/
80+
const log2_e: f32 = 1.44269504088896340735992468100189214f32;
81+
82+
/*
83+
Const: log10_e
84+
85+
log10(e)
86+
*/
87+
const log10_e: f32 = 0.434294481903251827651128918916605082f32;
88+
89+
/*
90+
Const: ln_2
91+
92+
ln(2.0)
93+
*/
94+
const ln_2: f32 = 0.693147180559945309417232121458176568f32;
95+
96+
/*
97+
Const: ln_10
98+
99+
ln(10.0)
100+
*/
101+
const ln_10: f32 = 2.30258509299404568401799145468436421f32;
102+
}

src/lib/f64.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import cmath::f64::*;
2+
3+
export
4+
acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor, fmod,
5+
frexp, ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin,
6+
sinh, sqrt, tan, tanh, trunc;
7+
8+
export consts;
9+
10+
mod consts {
11+
12+
/*
13+
Const: pi
14+
15+
Archimedes' constant
16+
*/
17+
const pi: f64 = 3.14159265358979323846264338327950288f64;
18+
19+
/*
20+
Const: frac_pi_2
21+
22+
pi/2.0
23+
*/
24+
const frac_pi_2: f64 = 1.57079632679489661923132169163975144f64;
25+
26+
/*
27+
Const: frac_pi_4
28+
29+
pi/4.0
30+
*/
31+
const frac_pi_4: f64 = 0.785398163397448309615660845819875721f64;
32+
33+
/*
34+
Const: frac_1_pi
35+
36+
1.0/pi
37+
*/
38+
const frac_1_pi: f64 = 0.318309886183790671537767526745028724f64;
39+
40+
/*
41+
Const: frac_2_pi
42+
43+
2.0/pi
44+
*/
45+
const frac_2_pi: f64 = 0.636619772367581343075535053490057448f64;
46+
47+
/*
48+
Const: frac_2_sqrtpi
49+
50+
2.0/sqrt(pi)
51+
*/
52+
const frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517f64;
53+
54+
/*
55+
Const: sqrt2
56+
57+
sqrt(2.0)
58+
*/
59+
const sqrt2: f64 = 1.41421356237309504880168872420969808f64;
60+
61+
/*
62+
Const: frac_1_sqrt2
63+
64+
1.0/sqrt(2.0)
65+
*/
66+
const frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039f64;
67+
68+
/*
69+
Const: e
70+
71+
Euler's number
72+
*/
73+
const e: f64 = 2.71828182845904523536028747135266250f64;
74+
75+
/*
76+
Const: log2_e
77+
78+
log2(e)
79+
*/
80+
const log2_e: f64 = 1.44269504088896340735992468100189214f64;
81+
82+
/*
83+
Const: log10_e
84+
85+
log10(e)
86+
*/
87+
const log10_e: f64 = 0.434294481903251827651128918916605082f64;
88+
89+
/*
90+
Const: ln_2
91+
92+
ln(2.0)
93+
*/
94+
const ln_2: f64 = 0.693147180559945309417232121458176568f64;
95+
96+
/*
97+
Const: ln_10
98+
99+
ln(10.0)
100+
*/
101+
const ln_10: f64 = 2.30258509299404568401799145468436421f64;
102+
}

src/lib/math.rs

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
export consts;
44
export min, max;
55

6-
export f32, f64;
7-
86
// Currently this module supports from -lmath:
97
// C95 + log2 + log1p + trunc + round + rint
108

@@ -19,77 +17,7 @@ import ctypes::c_float;
1917
import ctypes::c_int;
2018
import c_float = f64;
2119

22-
23-
#[link_name = "m"]
24-
#[abi = "cdecl"]
25-
native mod f64 {
26-
27-
// Alpabetically sorted by link_name
28-
29-
pure fn acos(n: f64) -> f64;
30-
pure fn asin(n: f64) -> f64;
31-
pure fn atan(n: f64) -> f64;
32-
pure fn atan2(a: f64, b: f64) -> f64;
33-
pure fn ceil(n: f64) -> f64;
34-
pure fn cos(n: f64) -> f64;
35-
pure fn cosh(n: f64) -> f64;
36-
pure fn exp(n: f64) -> f64;
37-
#[link_name="fabs"] pure fn abs(n: f64) -> f64;
38-
pure fn floor(n: f64) -> f64;
39-
pure fn fmod(x: f64, y: f64) -> f64;
40-
pure fn frexp(n: f64, &value: c_int) -> f64;
41-
pure fn ldexp(x: f64, n: c_int) -> f64;
42-
#[link_name="log"] pure fn ln(n: f64) -> f64;
43-
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
44-
pure fn log10(n: f64) -> f64;
45-
pure fn log2(n: f64) -> f64;
46-
pure fn modf(n: f64, &iptr: f64) -> f64;
47-
pure fn pow(n: f64, e: f64) -> f64;
48-
pure fn rint(n: f64) -> f64;
49-
pure fn round(n: f64) -> f64;
50-
pure fn sin(n: f64) -> f64;
51-
pure fn sinh(n: f64) -> f64;
52-
pure fn sqrt(n: f64) -> f64;
53-
pure fn tan(n: f64) -> f64;
54-
pure fn tanh(n: f64) -> f64;
55-
pure fn trunc(n: f64) -> f64;
56-
}
57-
58-
#[link_name = "m"]
59-
#[abi = "cdecl"]
60-
native mod f32 {
61-
62-
// Alpabetically sorted by link_name
63-
64-
#[link_name="acosf"] pure fn acos(n: f32) -> f32;
65-
#[link_name="asinf"] pure fn asin(n: f32) -> f32;
66-
#[link_name="atanf"] pure fn atan(n: f32) -> f32;
67-
#[link_name="atan2f"] pure fn atan2(a: f32, b: f32) -> f32;
68-
#[link_name="ceilf"] pure fn ceil(n: f32) -> f32;
69-
#[link_name="cosf"] pure fn cos(n: f32) -> f32;
70-
#[link_name="coshf"] pure fn cosh(n: f32) -> f32;
71-
#[link_name="expf"] pure fn exp(n: f32) -> f32;
72-
#[link_name="fabsf"] pure fn abs(n: f32) -> f32;
73-
#[link_name="floorf"] pure fn floor(n: f32) -> f32;
74-
#[link_name="frexpf"] pure fn frexp(n: f64, &value: c_int) -> f32;
75-
#[link_name="fmodf"] pure fn fmod(x: f32, y: f32) -> f32;
76-
#[link_name="ldexpf"] pure fn ldexp(x: f32, n: c_int) -> f32;
77-
#[link_name="logf"] pure fn ln(n: f32) -> f32;
78-
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
79-
#[link_name="log2f"] pure fn log2(n: f32) -> f32;
80-
#[link_name="log10f"] pure fn log10(n: f32) -> f32;
81-
#[link_name="modff"] pure fn modf(n: f32, &iptr: f32) -> f32;
82-
#[link_name="powf"] pure fn pow(n: f32, e: f32) -> f32;
83-
#[link_name="rintf"] pure fn rint(n: f32) -> f32;
84-
#[link_name="roundf"] pure fn round(n: f32) -> f32;
85-
#[link_name="sinf"] pure fn sin(n: f32) -> f32;
86-
#[link_name="sinhf"] pure fn sinh(n: f32) -> f32;
87-
#[link_name="sqrtf"] pure fn sqrt(n: f32) -> f32;
88-
#[link_name="tanf"] pure fn tan(n: f32) -> f32;
89-
#[link_name="tanhf"] pure fn tanh(n: f32) -> f32;
90-
#[link_name="truncf"] pure fn trunc(n: f32) -> f32;
91-
}
92-
20+
// FIXME replace with redirect to c_float::consts::FOO as soon as it works
9321
mod consts {
9422
/*
9523
Const: pi
@@ -181,9 +109,12 @@ mod consts {
181109
ln(10.0)
182110
*/
183111
const ln_10: float = 2.30258509299404568401799145468436421;
184-
185112
}
186113

114+
115+
// FIXME min/max type specialize via libm when overloading works
116+
// (in theory fmax/fmin, fmaxf, fminf /should/ be faster)
117+
187118
/*
188119
Function: min
189120

0 commit comments

Comments
 (0)