Skip to content

Upgraded math to C99 + bessel functions and replaced wrappers with imports #1440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 5, 2012
2 changes: 1 addition & 1 deletion src/comp/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn get_relative_to(abs1: fs::path, abs2: fs::path) -> fs::path {
assert len1 > 0u;
assert len2 > 0u;

let max_common_path = float::min(len1, len2) - 1u;
let max_common_path = math::min(len1, len2) - 1u;
let start_idx = 0u;
while start_idx < max_common_path
&& split1[start_idx] == split2[start_idx] {
Expand Down
2 changes: 1 addition & 1 deletion src/comp/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ mod unify {
let vb = alt cx.st {
in_bindings(vb) { vb }
};
ufind::grow(vb.sets, float::max(set_a, set_b) + 1u);
ufind::grow(vb.sets, math::max(set_a, set_b) + 1u);
let root_a = ufind::find(vb.sets, set_a);
let root_b = ufind::find(vb.sets, set_b);

Expand Down
3 changes: 1 addition & 2 deletions src/comp/util/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import core::{str, option};
import core::float::{max, min};
import math::{max, min};
import std::map::hashmap;
import option::{some};
import syntax::ast;
Expand Down
92 changes: 92 additions & 0 deletions src/etc/cmathconsts.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// This is a helper C program for generating required math constants
//
// Should only be required when porting to a different target architecture
// (or c compiler/libmath)
//
// Call with <rust machine type of c_float> <rust machine type of c_double>
// and ensure that libcore/cmath.rs complies to the output
//
// Requires a printf that supports "%a" specifiers
//

#include <float.h>
#include <math.h>
#include <stdio.h>

// must match core::ctypes

#define C_FLT(x) (float)x
#define C_DBL(x) (double)x

int main(int argc, char** argv) {
if (argc != 3) {
fprintf(stderr, "%s <ctypes::c_float> <ctypes::c_double>\n", argv[0]);
return 1;
}
char* c_flt = argv[1];
char* c_dbl = argv[2];

printf("mod c_float_math_consts {\n");
printf(" const pi: c_float = %a_%s;\n", C_FLT(M_PI), c_flt);
printf(" const div_1_pi: c_float = %a_%s;\n", C_FLT(M_1_PI), c_flt);
printf(" const div_2_pi: c_float = %a_%s;\n", C_FLT(M_2_PI), c_flt);
printf(" const div_pi_2: c_float = %a_%s;\n", C_FLT(M_PI_2), c_flt);
printf(" const div_pi_4: c_float = %a_%s;\n", C_FLT(M_PI_4), c_flt);
printf(" const div_2_sqrtpi: c_float = %a_%s;\n",
C_FLT(M_2_SQRTPI), c_flt);
printf(" const e: c_float = %a_%s;\n", C_FLT(M_E), c_flt);
printf(" const log2_e: c_float = %a_%s;\n", C_FLT(M_LOG2E), c_flt);
printf(" const log10_e: c_float = %a_%s;\n", C_FLT(M_LOG10E), c_flt);
printf(" const ln_2: c_float = %a_%s;\n", C_FLT(M_LN2), c_flt);
printf(" const ln_10: c_float = %a_%s;\n", C_FLT(M_LN10), c_flt);
printf(" const sqrt2: c_float = %a_%s;\n", C_FLT(M_SQRT2), c_flt);
printf(" const div_1_sqrt2: c_float = %a_%s;\n",
C_FLT(M_SQRT1_2), c_flt);
printf("}\n\n");

printf("mod c_double_math_consts {\n");
printf(" const pi: c_double = %a_%s;\n", C_DBL(M_PI), c_dbl);
printf(" const div_1_pi: c_double = %a_%s;\n", C_DBL(M_1_PI), c_dbl);
printf(" const div_2_pi: c_double = %a_%s;\n", C_DBL(M_2_PI), c_dbl);
printf(" const div_pi_2: c_double = %a_%s;\n", C_DBL(M_PI_2), c_dbl);
printf(" const div_pi_4: c_double = %a_%s;\n", C_DBL(M_PI_4), c_dbl);
printf(" const div_2_sqrtpi: c_double = %a_%s;\n",
C_DBL(M_2_SQRTPI), c_dbl);
printf(" const e: c_double = %a_%s;\n", C_DBL(M_E), c_dbl);
printf(" const log2_e: c_double = %a_%s;\n", C_DBL(M_LOG2E), c_dbl);
printf(" const log10_e: c_double = %a_%s;\n", C_DBL(M_LOG10E), c_dbl);
printf(" const ln_2: c_double = %a_%s;\n", C_DBL(M_LN2), c_dbl);
printf(" const ln_10: c_double = %a_%s;\n", C_DBL(M_LN10), c_dbl);
printf(" const sqrt2: c_double = %a_%s;\n", C_DBL(M_SQRT2), c_dbl);
printf(" const div_1_sqrt2: c_double = %a_%s;\n",
C_DBL(M_SQRT1_2), c_dbl);
printf("}\n\n");

printf("mod c_float_targ_consts {\n");
printf(" const radix: uint = %uu;\n", FLT_RADIX);
printf(" const mantissa_digits: uint = %uu;\n", FLT_MANT_DIG);
printf(" const digits: uint = %uu;\n", FLT_DIG);
printf(" const min_exp: int = %i;\n", FLT_MIN_EXP);
printf(" const max_exp: int = %i;\n", FLT_MAX_EXP);
printf(" const min_10_exp: int = %i;\n", FLT_MIN_10_EXP);
printf(" const max_10_exp: int = %i;\n", FLT_MAX_10_EXP);
printf(" const min_value: c_float = %a_%s;\n", C_FLT(FLT_MIN), c_flt);
printf(" const max_value: c_float = %a_%s;\n", C_FLT(FLT_MAX), c_flt);
printf(" const epsilon: c_float = %a_%s;\n", C_FLT(FLT_EPSILON), c_flt);
printf("}\n\n");

printf("mod c_double_targ_consts {\n");
printf(" const radix: uint = %uu;\n", FLT_RADIX);
printf(" const mantissa_digits: uint = %uu;\n", DBL_MANT_DIG);
printf(" const digits: uint = %uu;\n", DBL_DIG);
printf(" const min_exp: int = %i;\n", DBL_MIN_EXP);
printf(" const max_exp: int = %i;\n", DBL_MAX_EXP);
printf(" const min_10_exp: int = %i;\n", DBL_MIN_10_EXP);
printf(" const max_10_exp: int = %i;\n", DBL_MAX_10_EXP);
printf(" const min_value: c_double = %a_%s;\n", C_DBL(DBL_MIN), c_dbl);
printf(" const max_value: c_double = %a_%s;\n", C_DBL(DBL_MAX), c_dbl);
printf(" const epsilon: c_double = %a_%s;\n", C_DBL(DBL_EPSILON), c_dbl);
printf("}\n");

return 0;
}
5 changes: 2 additions & 3 deletions src/fuzzer/fuzzer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import core::{vec, str, int, uint, option, result};
import std::{fs, io};

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

if L < 100u {
under(float::min(L, 20u)) {|i|
under(math::min(L, 20u)) {|i|
log(error, "Replacing... #" + uint::str(i));
under(float::min(L, 30u)) {|j|
under(math::min(L, 30u)) {|j|
log(error, "With... " + stringifier(@things[j]));
let crate2 = @replacer(crate, i, things[j], cx.mode);
// It would be best to test the *crate* for stability, but testing the
Expand Down
10 changes: 10 additions & 0 deletions src/libcore/bessel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// PORT import module that is based on cmath::c_double here
// (cant do better via libm; bessel functions only exist for c_double)

// code that wants to use bessel functions should use
// values of type bessel::t and cast from/to float/f32/f64
// when working with them at the peril of precision loss
// for platform neutrality

import f64::*;

Loading