Skip to content

Commit e02ab2d

Browse files
committed
Merge pull request #1440 from boggle/kmath
Upgraded math to C99 + bessel functions and replaced wrappers with imports
2 parents cc929fc + c846797 commit e02ab2d

22 files changed

+790
-769
lines changed

src/comp/back/rpath.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn get_relative_to(abs1: fs::path, abs2: fs::path) -> fs::path {
129129
assert len1 > 0u;
130130
assert len2 > 0u;
131131

132-
let max_common_path = float::min(len1, len2) - 1u;
132+
let max_common_path = math::min(len1, len2) - 1u;
133133
let start_idx = 0u;
134134
while start_idx < max_common_path
135135
&& split1[start_idx] == split2[start_idx] {

src/comp/middle/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ mod unify {
17651765
let vb = alt cx.st {
17661766
in_bindings(vb) { vb }
17671767
};
1768-
ufind::grow(vb.sets, float::max(set_a, set_b) + 1u);
1768+
ufind::grow(vb.sets, math::max(set_a, set_b) + 1u);
17691769
let root_a = ufind::find(vb.sets, set_a);
17701770
let root_b = ufind::find(vb.sets, set_b);
17711771

src/comp/util/common.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import core::{str, option};
2-
import core::float::{max, min};
1+
import math::{max, min};
32
import std::map::hashmap;
43
import option::{some};
54
import syntax::ast;

src/etc/cmathconsts.c

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// This is a helper C program for generating required math constants
2+
//
3+
// Should only be required when porting to a different target architecture
4+
// (or c compiler/libmath)
5+
//
6+
// Call with <rust machine type of c_float> <rust machine type of c_double>
7+
// and ensure that libcore/cmath.rs complies to the output
8+
//
9+
// Requires a printf that supports "%a" specifiers
10+
//
11+
12+
#include <float.h>
13+
#include <math.h>
14+
#include <stdio.h>
15+
16+
// must match core::ctypes
17+
18+
#define C_FLT(x) (float)x
19+
#define C_DBL(x) (double)x
20+
21+
int main(int argc, char** argv) {
22+
if (argc != 3) {
23+
fprintf(stderr, "%s <ctypes::c_float> <ctypes::c_double>\n", argv[0]);
24+
return 1;
25+
}
26+
char* c_flt = argv[1];
27+
char* c_dbl = argv[2];
28+
29+
printf("mod c_float_math_consts {\n");
30+
printf(" const pi: c_float = %a_%s;\n", C_FLT(M_PI), c_flt);
31+
printf(" const div_1_pi: c_float = %a_%s;\n", C_FLT(M_1_PI), c_flt);
32+
printf(" const div_2_pi: c_float = %a_%s;\n", C_FLT(M_2_PI), c_flt);
33+
printf(" const div_pi_2: c_float = %a_%s;\n", C_FLT(M_PI_2), c_flt);
34+
printf(" const div_pi_4: c_float = %a_%s;\n", C_FLT(M_PI_4), c_flt);
35+
printf(" const div_2_sqrtpi: c_float = %a_%s;\n",
36+
C_FLT(M_2_SQRTPI), c_flt);
37+
printf(" const e: c_float = %a_%s;\n", C_FLT(M_E), c_flt);
38+
printf(" const log2_e: c_float = %a_%s;\n", C_FLT(M_LOG2E), c_flt);
39+
printf(" const log10_e: c_float = %a_%s;\n", C_FLT(M_LOG10E), c_flt);
40+
printf(" const ln_2: c_float = %a_%s;\n", C_FLT(M_LN2), c_flt);
41+
printf(" const ln_10: c_float = %a_%s;\n", C_FLT(M_LN10), c_flt);
42+
printf(" const sqrt2: c_float = %a_%s;\n", C_FLT(M_SQRT2), c_flt);
43+
printf(" const div_1_sqrt2: c_float = %a_%s;\n",
44+
C_FLT(M_SQRT1_2), c_flt);
45+
printf("}\n\n");
46+
47+
printf("mod c_double_math_consts {\n");
48+
printf(" const pi: c_double = %a_%s;\n", C_DBL(M_PI), c_dbl);
49+
printf(" const div_1_pi: c_double = %a_%s;\n", C_DBL(M_1_PI), c_dbl);
50+
printf(" const div_2_pi: c_double = %a_%s;\n", C_DBL(M_2_PI), c_dbl);
51+
printf(" const div_pi_2: c_double = %a_%s;\n", C_DBL(M_PI_2), c_dbl);
52+
printf(" const div_pi_4: c_double = %a_%s;\n", C_DBL(M_PI_4), c_dbl);
53+
printf(" const div_2_sqrtpi: c_double = %a_%s;\n",
54+
C_DBL(M_2_SQRTPI), c_dbl);
55+
printf(" const e: c_double = %a_%s;\n", C_DBL(M_E), c_dbl);
56+
printf(" const log2_e: c_double = %a_%s;\n", C_DBL(M_LOG2E), c_dbl);
57+
printf(" const log10_e: c_double = %a_%s;\n", C_DBL(M_LOG10E), c_dbl);
58+
printf(" const ln_2: c_double = %a_%s;\n", C_DBL(M_LN2), c_dbl);
59+
printf(" const ln_10: c_double = %a_%s;\n", C_DBL(M_LN10), c_dbl);
60+
printf(" const sqrt2: c_double = %a_%s;\n", C_DBL(M_SQRT2), c_dbl);
61+
printf(" const div_1_sqrt2: c_double = %a_%s;\n",
62+
C_DBL(M_SQRT1_2), c_dbl);
63+
printf("}\n\n");
64+
65+
printf("mod c_float_targ_consts {\n");
66+
printf(" const radix: uint = %uu;\n", FLT_RADIX);
67+
printf(" const mantissa_digits: uint = %uu;\n", FLT_MANT_DIG);
68+
printf(" const digits: uint = %uu;\n", FLT_DIG);
69+
printf(" const min_exp: int = %i;\n", FLT_MIN_EXP);
70+
printf(" const max_exp: int = %i;\n", FLT_MAX_EXP);
71+
printf(" const min_10_exp: int = %i;\n", FLT_MIN_10_EXP);
72+
printf(" const max_10_exp: int = %i;\n", FLT_MAX_10_EXP);
73+
printf(" const min_value: c_float = %a_%s;\n", C_FLT(FLT_MIN), c_flt);
74+
printf(" const max_value: c_float = %a_%s;\n", C_FLT(FLT_MAX), c_flt);
75+
printf(" const epsilon: c_float = %a_%s;\n", C_FLT(FLT_EPSILON), c_flt);
76+
printf("}\n\n");
77+
78+
printf("mod c_double_targ_consts {\n");
79+
printf(" const radix: uint = %uu;\n", FLT_RADIX);
80+
printf(" const mantissa_digits: uint = %uu;\n", DBL_MANT_DIG);
81+
printf(" const digits: uint = %uu;\n", DBL_DIG);
82+
printf(" const min_exp: int = %i;\n", DBL_MIN_EXP);
83+
printf(" const max_exp: int = %i;\n", DBL_MAX_EXP);
84+
printf(" const min_10_exp: int = %i;\n", DBL_MIN_10_EXP);
85+
printf(" const max_10_exp: int = %i;\n", DBL_MAX_10_EXP);
86+
printf(" const min_value: c_double = %a_%s;\n", C_DBL(DBL_MIN), c_dbl);
87+
printf(" const max_value: c_double = %a_%s;\n", C_DBL(DBL_MAX), c_dbl);
88+
printf(" const epsilon: c_double = %a_%s;\n", C_DBL(DBL_EPSILON), c_dbl);
89+
printf("}\n");
90+
91+
return 0;
92+
}

src/fuzzer/fuzzer.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import core::{vec, str, int, uint, option, result};
21
import std::{fs, io};
32

43
import rustc::syntax::{ast, ast_util, fold, visit, codemap};
@@ -241,9 +240,9 @@ fn check_variants_T<T: copy>(
241240
let L = vec::len(things);
242241

243242
if L < 100u {
244-
under(float::min(L, 20u)) {|i|
243+
under(math::min(L, 20u)) {|i|
245244
log(error, "Replacing... #" + uint::str(i));
246-
under(float::min(L, 30u)) {|j|
245+
under(math::min(L, 30u)) {|j|
247246
log(error, "With... " + stringifier(@things[j]));
248247
let crate2 = @replacer(crate, i, things[j], cx.mode);
249248
// It would be best to test the *crate* for stability, but testing the

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+

0 commit comments

Comments
 (0)