Skip to content

Commit 1640538

Browse files
committed
core: added support for bessel functions
1 parent a72b141 commit 1640538

File tree

6 files changed

+104
-92
lines changed

6 files changed

+104
-92
lines changed

src/libcore/bessel.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// PORT import module that is based on cmath::c_double here
2+
// (cant do better via libm; bessel functions only exist for c_double)
3+
4+
// code that wants to use bessel functions should use
5+
// values of type bessel::t and cast from/to float/f32/f64
6+
// when working with them at the peril of precision loss
7+
// for platform neutrality
8+
9+
import f64::*;
10+

src/libcore/cmath.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export c_double;
22
export c_float;
3-
export bessel;
43

54
import ctypes::c_int;
65
import ctypes::c_float;
@@ -57,6 +56,16 @@ native mod c_double {
5756
pure fn tanh(n: c_double) -> c_double;
5857
pure fn tgamma(n: c_double) -> c_double;
5958
pure fn trunc(n: c_double) -> c_double;
59+
60+
// These are commonly only available for doubles
61+
62+
pure fn j0(n: c_double) -> c_double;
63+
pure fn j1(n: c_double) -> c_double;
64+
pure fn jn(i: c_int, n: c_double) -> c_double;
65+
66+
pure fn y0(n: c_double) -> c_double;
67+
pure fn y1(n: c_double) -> c_double;
68+
pure fn yn(i: c_int, n: c_double) -> c_double;
6069
}
6170

6271
#[link_name = "m"]
@@ -83,7 +92,7 @@ native mod c_float {
8392
#[link_name="fabsf"] pure fn abs(n: c_float) -> c_float;
8493
#[link_name="fdimf"] pure fn sub_pos(a: c_float, b: c_float) -> c_float;
8594
#[link_name="floorf"] pure fn floor(n: c_float) -> c_float;
86-
#[link_name="frexpf"] pure fn frexp(n: c_double,
95+
#[link_name="frexpf"] pure fn frexp(n: c_float,
8796
&value: c_int) -> c_float;
8897
#[link_name="fmaf"] pure fn mul_add(a: c_float,
8998
b: c_float, c: c_float) -> c_float;
@@ -97,7 +106,7 @@ native mod c_float {
97106
&sign: c_int) -> c_float;
98107
#[link_name="logf"] pure fn ln(n: c_float) -> c_float;
99108
#[link_name="logbf"] pure fn logb(n: c_float) -> c_float;
100-
#[link_name="log1p"] pure fn ln1p(n: c_double) -> c_double;
109+
#[link_name="log1pf"] pure fn ln1p(n: c_float) -> c_float;
101110
#[link_name="log2f"] pure fn log2(n: c_float) -> c_float;
102111
#[link_name="log10f"] pure fn log10(n: c_float) -> c_float;
103112
#[link_name="ilogbf"] pure fn ilogb(n: c_float) -> c_int;
@@ -116,18 +125,6 @@ native mod c_float {
116125
#[link_name="truncf"] pure fn trunc(n: c_float) -> c_float;
117126
}
118127

119-
#[link_name = "m"]
120-
#[abi = "cdecl"]
121-
native mod bessel {
122-
pure fn j0(n: c_double) -> c_double;
123-
pure fn j1(n: c_double) -> c_double;
124-
pure fn jn(i: c_int, n: c_double) -> c_double;
125-
126-
pure fn y0(n: c_double) -> c_double;
127-
pure fn y1(n: c_double) -> c_double;
128-
pure fn yn(i: c_int, n: c_double) -> c_double;
129-
}
130-
131128
//
132129
// Local Variables:
133130
// mode: rust

src/libcore/core.rc

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#[license = "BSD"];
88
#[crate_type = "lib"];
99

10-
export box, char, float, f32, f64, int, str, ptr;
10+
export box, char, float, bessel, f32, f64, int, str, ptr;
1111
export uint, u8, u32, u64, vec, bool;
1212
export either, option, result;
1313
export ctypes, sys, unsafe, comm, task;
@@ -18,6 +18,7 @@ export extfmt;
1818
mod box;
1919
mod char;
2020
mod float;
21+
mod bessel;
2122
mod f32;
2223
mod f64;
2324
mod int;

src/libcore/f32.rs

+39-37
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,43 @@ Module: f32
33
44
Floating point operations and constants for `f32`
55
*/
6+
67
// PORT
78

89
import cmath::c_float::*;
910

1011
type t = f32;
1112

13+
14+
// These are not defined inside consts:: for consistency with
15+
// the integer types
16+
17+
// PORT check per architecture
18+
19+
const radix: uint = 2u;
20+
21+
const mantissa_digits: uint = 24u;
22+
const digits: uint = 6u;
23+
24+
const epsilon: f32 = 1.19209290e-07_f32;
25+
26+
const min_value: f32 = 1.17549435e-38_f32;
27+
const max_value: f32 = 3.40282347e+38_f32;
28+
29+
const min_exp: int = -125;
30+
const max_exp: int = 128;
31+
32+
const min_10_exp: int = -37;
33+
const max_10_exp: int = 38;
34+
1235
/* Const: NaN */
13-
const NaN: f32 = 0.0f32/0.0f32;
36+
const NaN: f32 = 0.0_f32/0.0_f32;
1437

1538
/* Const: infinity */
16-
const infinity: f32 = 1.0f32/0.0f32;
39+
const infinity: f32 = 1.0_f32/0.0_f32;
1740

1841
/* Const: neg_infinity */
19-
const neg_infinity: f32 = -1.0f32/0.0f32;
42+
const neg_infinity: f32 = -1.0_f32/0.0_f32;
2043

2144
/* Predicate: isNaN */
2245
pure fn isNaN(f: f32) -> bool { f != f }
@@ -98,114 +121,93 @@ mod consts {
98121
99122
Archimedes' constant
100123
*/
101-
const pi: f32 = 3.14159265358979323846264338327950288f32;
124+
const pi: f32 = 3.14159265358979323846264338327950288_f32;
102125

103126
/*
104127
Const: frac_pi_2
105128
106129
pi/2.0
107130
*/
108-
const frac_pi_2: f32 = 1.57079632679489661923132169163975144f32;
131+
const frac_pi_2: f32 = 1.57079632679489661923132169163975144_f32;
109132

110133
/*
111134
Const: frac_pi_4
112135
113136
pi/4.0
114137
*/
115-
const frac_pi_4: f32 = 0.785398163397448309615660845819875721f32;
138+
const frac_pi_4: f32 = 0.785398163397448309615660845819875721_f32;
116139

117140
/*
118141
Const: frac_1_pi
119142
120143
1.0/pi
121144
*/
122-
const frac_1_pi: f32 = 0.318309886183790671537767526745028724f32;
145+
const frac_1_pi: f32 = 0.318309886183790671537767526745028724_f32;
123146

124147
/*
125148
Const: frac_2_pi
126149
127150
2.0/pi
128151
*/
129-
const frac_2_pi: f32 = 0.636619772367581343075535053490057448f32;
152+
const frac_2_pi: f32 = 0.636619772367581343075535053490057448_f32;
130153

131154
/*
132155
Const: frac_2_sqrtpi
133156
134157
2.0/sqrt(pi)
135158
*/
136-
const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517f32;
159+
const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517_f32;
137160

138161
/*
139162
Const: sqrt2
140163
141164
sqrt(2.0)
142165
*/
143-
const sqrt2: f32 = 1.41421356237309504880168872420969808f32;
166+
const sqrt2: f32 = 1.41421356237309504880168872420969808_f32;
144167

145168
/*
146169
Const: frac_1_sqrt2
147170
148171
1.0/sqrt(2.0)
149172
*/
150-
const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039f32;
173+
const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039_f32;
151174

152175
/*
153176
Const: e
154177
155178
Euler's number
156179
*/
157-
const e: f32 = 2.71828182845904523536028747135266250f32;
180+
const e: f32 = 2.71828182845904523536028747135266250_f32;
158181

159182
/*
160183
Const: log2_e
161184
162185
log2(e)
163186
*/
164-
const log2_e: f32 = 1.44269504088896340735992468100189214f32;
187+
const log2_e: f32 = 1.44269504088896340735992468100189214_f32;
165188

166189
/*
167190
Const: log10_e
168191
169192
log10(e)
170193
*/
171-
const log10_e: f32 = 0.434294481903251827651128918916605082f32;
194+
const log10_e: f32 = 0.434294481903251827651128918916605082_f32;
172195

173196
/*
174197
Const: ln_2
175198
176199
ln(2.0)
177200
*/
178-
const ln_2: f32 = 0.693147180559945309417232121458176568f32;
201+
const ln_2: f32 = 0.693147180559945309417232121458176568_f32;
179202

180203
/*
181204
Const: ln_10
182205
183206
ln(10.0)
184207
*/
185-
const ln_10: f32 = 2.30258509299404568401799145468436421f32;
208+
const ln_10: f32 = 2.30258509299404568401799145468436421_f32;
186209
}
187210

188-
// These are not defined inside consts:: for consistency with
189-
// the integer types
190-
191-
// PORT check per architecture
192-
193-
const radix: uint = 2u;
194-
195-
const mantissa_digits: uint = 24u;
196-
const digits: uint = 6u;
197-
198-
const epsilon: f32 = 1.19209290e-07f32;
199-
200-
const min_value: f32 = 1.17549435e-38f32;
201-
const max_value: f32 = 3.40282347e+38f32;
202-
203-
const min_exp: int = -125;
204-
const max_exp: int = 128;
205-
206-
const min_10_exp: int = -37;
207-
const max_10_exp: int = 38;
208-
209211
//
210212
// Local Variables:
211213
// mode: rust

0 commit comments

Comments
 (0)