Skip to content

Commit

Permalink
added preliminary support for per-architecture generation of math con…
Browse files Browse the repository at this point in the history
…stants (for porting)
  • Loading branch information
boggle committed Jan 5, 2012
1 parent 6284190 commit a16acc0
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 23 deletions.
92 changes: 92 additions & 0 deletions src/etc/cmathconsts.c
@@ -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 = %u;\n", FLT_RADIX);
printf(" const mantissa_digits: uint = %u;\n", FLT_MANT_DIG);
printf(" const digits: uint = %u;\n", FLT_DIG);
printf(" const min_exp: uint = %i;\n", FLT_MIN_EXP);
printf(" const max_exp: uint = %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 = %u;\n", FLT_RADIX);
printf(" const mantissa_digits: uint = %u;\n", DBL_MANT_DIG);
printf(" const digits: uint = %u;\n", DBL_DIG);
printf(" const min_exp: uint = %i;\n", DBL_MIN_EXP);
printf(" const max_exp: uint = %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;
}
103 changes: 102 additions & 1 deletion src/libcore/cmath.rs
@@ -1,5 +1,11 @@
export c_double;
export c_float;
export c_double;

// FIXME export c_float_math_consts;
// FIXME export c_double_math_consts;

export c_float_targ_consts;
export c_double_targ_consts;

import ctypes::c_int;
import ctypes::c_float;
Expand Down Expand Up @@ -143,6 +149,101 @@ native mod c_float {
#[link_name="truncf"] pure fn trunc(n: c_float) -> c_float;
}

// PORT check these by running src/etc/machconsts.c for your architecture

// FIXME obtain machine float/math constants automatically

mod c_float_targ_consts {
const radix: uint = 2;
const mantissa_digits: uint = 24;
const digits: uint = 6;
const min_exp: uint = -125;
const max_exp: uint = 128;
const min_10_exp: int = -37;
const max_10_exp: int = 38;
// FIXME this is wrong! replace with hexadecimal (%a) constants below
const min_value: f32 = 1.175494e-38_f32;
const max_value: f32 = 3.402823e+38_f32;
const epsilon: f32 = 0.000000_f32;
}

mod c_double_targ_consts {
const radix: uint = 2;
const mantissa_digits: uint = 53;
const digits: uint = 15;
const min_exp: uint = -1021;
const max_exp: uint = 1024;
const min_10_exp: int = -307;
const max_10_exp: int = 308;
// FIXME this is wrong! replace with hexadecimal (%a) constants below
const min_value: f64 = 2.225074e-308_f64;
const max_value: f64 = 1.797693e+308_f64;
const epsilon: f64 = 2.220446e-16_f64;
}

/*
FIXME use these once they can be parsed
mod c_float_math_consts {
const pi: c_float = 0x1.921fb6p+1_f32;
const div_1_pi: c_float = 0x1.45f306p-2_f32;
const div_2_pi: c_float = 0x1.45f306p-1_f32;
const div_pi_2: c_float = 0x1.921fb6p+0_f32;
const div_pi_4: c_float = 0x1.921fb6p-1_f32;
const div_2_sqrtpi: c_float = 0x1.20dd76p+0_f32;
const e: c_float = 0x1.5bf0a8p+1_f32;
const log2_e: c_float = 0x1.715476p+0_f32;
const log10_e: c_float = 0x1.bcb7b2p-2_f32;
const ln_2: c_float = 0x1.62e43p-1_f32;
const ln_10: c_float = 0x1.26bb1cp+1_f32;
const sqrt2: c_float = 0x1.6a09e6p+0_f32;
const div_1_sqrt2: c_float = 0x1.6a09e6p-1_f32;
}
mod c_double_math_consts {
const pi: c_double = 0x1.921fb54442d18p+1_f64;
const div_1_pi: c_double = 0x1.45f306dc9c883p-2_f64;
const div_2_pi: c_double = 0x1.45f306dc9c883p-1_f64;
const div_pi_2: c_double = 0x1.921fb54442d18p+0_f64;
const div_pi_4: c_double = 0x1.921fb54442d18p-1_f64;
const div_2_sqrtpi: c_double = 0x1.20dd750429b6dp+0_f64;
const e: c_double = 0x1.5bf0a8b145769p+1_f64;
const log2_e: c_double = 0x1.71547652b82fep+0_f64;
const log10_e: c_double = 0x1.bcb7b1526e50ep-2_f64;
const ln_2: c_double = 0x1.62e42fefa39efp-1_f64;
const ln_10: c_double = 0x1.26bb1bbb55516p+1_f64;
const sqrt2: c_double = 0x1.6a09e667f3bcdp+0_f64;
const div_1_sqrt2: c_double = 0x1.6a09e667f3bcdp-1_f64;
}
mod c_float_targ_consts {
const radix: uint = 2;
const mantissa_digits: uint = 24;
const digits: uint = 6;
const min_exp: uint = -125;
const max_exp: uint = 128;
const min_10_exp: int = -37;
const max_10_exp: int = 38;
const min_value: c_float = 0x1p-126_f32;
const max_value: c_float = 0x1.fffffep+127_f32;
const epsilon: c_float = 0x1p-23_f32;
}
mod c_double_targ_consts {
const radix: uint = 2;
const mantissa_digits: uint = 53;
const digits: uint = 15;
const min_exp: uint = -1021;
const max_exp: uint = 1024;
const min_10_exp: int = -307;
const max_10_exp: int = 308;
const min_value: c_double = 0x1p-1022_f64;
const max_value: c_double = 0x1.fffffffffffffp+1023_f64;
const epsilon: c_double = 0x1p-52_f64;
}
*/

//
// Local Variables:
// mode: rust
Expand Down
32 changes: 11 additions & 21 deletions src/libcore/f32.rs
Expand Up @@ -7,33 +7,13 @@ Floating point operations and constants for `f32`
// PORT

import cmath::c_float::*;
import cmath::c_float_targ_consts::*;

type t = f32;


// These are not defined inside consts:: for consistency with
// the integer types

// PORT check per architecture

// FIXME obtain these in a different way

const radix: uint = 2u;

const mantissa_digits: uint = 24u;
const digits: uint = 6u;

const epsilon: f32 = 1.19209290e-07_f32;

const min_value: f32 = 1.17549435e-38_f32;
const max_value: f32 = 3.40282347e+38_f32;

const min_exp: int = -125;
const max_exp: int = 128;

const min_10_exp: int = -37;
const max_10_exp: int = 38;

/* Const: NaN */
const NaN: f32 = 0.0_f32/0.0_f32;

Expand Down Expand Up @@ -150,6 +130,8 @@ pure fn is_finite(x: f32) -> bool {
/* Module: consts */
mod consts {

// FIXME replace with mathematical constants from cmath

/*
Const: pi
Expand Down Expand Up @@ -242,6 +224,14 @@ mod consts {
const ln_10: f32 = 2.30258509299404568401799145468436421_f32;
}

#[cfg(target_os="linux")]
#[cfg(target_os="macos")]
#[cfg(target_os="win32")]
pure fn logarithm(n: f32, b: f32) -> f32 {
ret log2(n) / log2(b);
}

#[cfg(target_os="freebsd")]
pure fn logarithm(n: f32, b: f32) -> f32 {
ret ln(n) / ln(b);
}
Expand Down
12 changes: 11 additions & 1 deletion src/libcore/f64.rs
Expand Up @@ -7,10 +7,10 @@ Floating point operations and constants for `f64`
// PORT

import cmath::c_double::*;
import cmath::c_double_targ_consts::*;

type t = f64;


// These are not defined inside consts:: for consistency with
// the integer types

Expand Down Expand Up @@ -147,6 +147,8 @@ pure fn is_finite(x: f64) -> bool {
/* Module: consts */
mod consts {

// FIXME replace with mathematical constants from cmath

/*
Const: pi
Expand Down Expand Up @@ -239,6 +241,14 @@ mod consts {
const ln_10: f64 = 2.30258509299404568401799145468436421_f64;
}

#[cfg(target_os="linux")]
#[cfg(target_os="macos")]
#[cfg(target_os="win32")]
pure fn logarithm(n: f64, b: f64) -> f64 {
ret log2(n) / log2(b);
}

#[cfg(target_os="freebsd")]
pure fn logarithm(n: f64, b: f64) -> f64 {
ret ln(n) / ln(b);
}
Expand Down

0 comments on commit a16acc0

Please sign in to comment.