From e0cd06053a404b7e9c4efa8fc0ca77cc7154913c Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Thu, 22 Dec 2011 12:54:38 +0100 Subject: [PATCH 1/9] added useful constants to math --- src/libcore/f32.rs | 24 ++++++++++++++++++++++++ src/libcore/f64.rs | 24 ++++++++++++++++++++++++ src/libcore/float.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index 41110fbd7f50e..293601e3b1872 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -15,6 +15,9 @@ export frexp, ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin, sinh, sqrt, tan, tanh, trunc, t; +export radix, mantissa_digits, digits, epsilon, min_value, max_value, + min_exp, max_exp, min_10_exp, max_10_exp; + export consts; type t = f32; @@ -114,6 +117,27 @@ mod consts { const ln_10: f32 = 2.30258509299404568401799145468436421f32; } +// These are not defined inside consts:: for consistency with +// the integer types + +// PORT check per architecture + +const radix: uint = 2u; + +const mantissa_digits: uint = 24u; +const digits: uint = 6u; + +const epsilon: f32 = 1.19209290e-07f32; + +const min_value: f32 = 1.17549435e-38f32; +const max_value: f32 = 3.40282347e+38f32; + +const min_exp: int = -125; +const max_exp: int = 128; + +const min_10_exp: int = -37; +const max_10_exp: int = 38; + // // Local Variables: // mode: rust diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index 7784933f45281..7eb4ce96789e7 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -15,6 +15,9 @@ export frexp, ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin, sinh, sqrt, tan, tanh, trunc, t; +export radix, mantissa_digits, digits, epsilon, min_value, max_value, + min_exp, max_exp, min_10_exp, max_10_exp; + export consts; type t = f64; @@ -114,6 +117,27 @@ mod consts { const ln_10: f64 = 2.30258509299404568401799145468436421f64; } +// These are not defined inside consts:: for consistency with +// the integer types + +// PORT check per architecture + +const radix: uint = 2u; + +const mantissa_digits: uint = 53u; +const digits: uint = 15u; + +const epsilon: f64 = 2.2204460492503131e-16f64; + +const min_value: f64 = 2.2250738585072014e-308f64; +const max_value: f64 = 1.7976931348623157e+308f64; + +const min_exp: int = -1021; +const max_exp: int = 1024; + +const min_10_exp: int = -307; +const max_10_exp: int = 308; + // // Local Variables: // mode: rust diff --git a/src/libcore/float.rs b/src/libcore/float.rs index fdc5dda996d13..6ff08df4b26f3 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -719,6 +719,31 @@ Returns the integral value nearest to but no larger in magnitude than `x` pure fn trunc(x: float) -> float { ret m_float::trunc(x as m_float) as float } +/* + +FIXME implement this as soon as const expressions may refer to each other + +export radix, mantissa_digits, digits, epsilon, min_value, max_value, + min_exp, max_exp, min_10_exp, max_10_exp; + +const radix: m_float = m_float::radix as m_float; + +const mantissa_digits: m_float = m_float::mantissa_digits as m_float; +const digits: m_float = m_float::digits as m_float; + +const epsilon: m_float = m_float::epsilon as m_float; + +const min_value: m_float = m_float::min_value as m_float; +const max_value: m_float = m_float::max_value as m_float; + +const min_exp: m_float = m_float::min_exp as m_float; +const max_exp: m_float = m_float::max_exp as m_float; + +const min_10_exp: m_float = m_float::min_10_exp as m_float; +const max_10_exp: m_float = m_float::max_10_exp as m_float; + +*/ + // // Local Variables: // mode: rust @@ -728,3 +753,8 @@ pure fn trunc(x: float) -> float // buffer-file-coding-system: utf-8-unix // End: // + + + + + From d124c6eda29c4ba2f2a88b5e336e03ddbc12a2aa Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Thu, 22 Dec 2011 12:54:55 +0100 Subject: [PATCH 2/9] removes std::mtypes --- src/libstd/mtypes.rs | 64 -------------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 src/libstd/mtypes.rs diff --git a/src/libstd/mtypes.rs b/src/libstd/mtypes.rs deleted file mode 100644 index cddea420cc836..0000000000000 --- a/src/libstd/mtypes.rs +++ /dev/null @@ -1,64 +0,0 @@ -/* - -Module: mtypes - -Machine type equivalents of rust int, uint, float, and complex. - -Types useful for interop with C when writing bindings that exist -for different types (float, f32, f64, ...; cf float.rs for an example) -*/ - -export m_int, m_uint, m_float; - -// PORT Change this when porting to a new architecture - -/* -Type: m_int - -Machine type equivalent of an int -*/ -#[cfg(target_arch="x86")] -type m_int = i32; -#[cfg(target_arch="x86_64")] -type m_int = i64; - -// PORT Change this when porting to a new architecture - -/* -Type: m_uint - -Machine type equivalent of a uint -*/ -#[cfg(target_arch="x86")] -type m_uint = u32; -#[cfg(target_arch="x86_64")] -type m_uint = u64; - -// PORT *must* match with "import m_float = fXX" in std::math per arch - -/* -Type: m_float - -Machine type equivalent of a float -*/ -type m_float = f64; - -// PORT *must* match "import m_complex = ..." in std::complex per arch - -/* -FIXME Type m_complex - -Machine type representing a complex value that uses floats for -both the real and the imaginary part. -*/ -// type m_complex = complex_c64::t; - -// -// Local Variables: -// mode: rust -// fill-column: 78; -// indent-tabs-mode: nil -// c-basic-offset: 4 -// buffer-file-coding-system: utf-8-unix -// End: -// From c314a9cad157f3a9821ea08f4f88e791dfff06d8 Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Thu, 22 Dec 2011 12:55:28 +0100 Subject: [PATCH 3/9] added more ops to cmath --- src/libcore/cmath.rs | 44 +++++++++++++++++++++++++++++++++++++++++++ src/libcore/ctypes.rs | 2 ++ src/libcore/mtypes.rs | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/libcore/cmath.rs b/src/libcore/cmath.rs index f0fc6f87dc5f4..8cea074a7c61e 100644 --- a/src/libcore/cmath.rs +++ b/src/libcore/cmath.rs @@ -10,21 +10,36 @@ native mod f64 { pure fn asin(n: f64) -> f64; pure fn atan(n: f64) -> f64; pure fn atan2(a: f64, b: f64) -> f64; + pure fn cbrt(n: f64) -> f64; pure fn ceil(n: f64) -> f64; pure fn cos(n: f64) -> f64; pure fn cosh(n: f64) -> f64; + pure fn erf(n: f64) -> f64; + pure fn erfc(n: f64) -> f64; pure fn exp(n: f64) -> f64; + pure fn expm1(n: f64) -> f64; + pure fn exp2(n: f64) -> f64; #[link_name="fabs"] pure fn abs(n: f64) -> f64; + #[link_name="fdim"] pure fn sub_pos(a: f64, b: f64) -> f64; pure fn floor(n: f64) -> f64; + #[link_name="fma"] pure fn mul_add(a: f64, b: f64, c: f64) -> f64; + #[link_name="fmax"] pure fn max(a: f64, b: f64) -> f64; + #[link_name="fmin"] pure fn min(a: f64, b: f64) -> f64; + pure fn nextafter(x: f64, y: f64) -> f64 pure fn fmod(x: f64, y: f64) -> f64; pure fn frexp(n: f64, &value: c_int) -> f64; + pure fn hypot(x: f64, y: f64) -> f64; pure fn ldexp(x: f64, n: c_int) -> f64; + #[link_name="lgamma_r"] pure fn lgamma(n: f64, sign: *c_int) -> f64; #[link_name="log"] pure fn ln(n: f64) -> f64; + pure fn logb(n: f64) -> f64; #[link_name="log1p"] pure fn ln1p(n: f64) -> f64; pure fn log10(n: f64) -> f64; pure fn log2(n: f64) -> f64; + pure fn ilogb(n: f64) -> c_int; pure fn modf(n: f64, iptr: *f64) -> f64; pure fn pow(n: f64, e: f64) -> f64; + #[link_name="remainder"] pure fn rem(a: f64, b: f64) -> f64; pure fn rint(n: f64) -> f64; pure fn round(n: f64) -> f64; pure fn sin(n: f64) -> f64; @@ -32,6 +47,7 @@ native mod f64 { pure fn sqrt(n: f64) -> f64; pure fn tan(n: f64) -> f64; pure fn tanh(n: f64) -> f64; + pure fn tgamma(n: f64) -> f64; pure fn trunc(n: f64) -> f64; } @@ -45,21 +61,36 @@ native mod f32 { #[link_name="asinf"] pure fn asin(n: f32) -> f32; #[link_name="atanf"] pure fn atan(n: f32) -> f32; #[link_name="atan2f"] pure fn atan2(a: f32, b: f32) -> f32; + #[link_name="cbrtf"] pure fn cbrt(n: f32) -> f32; #[link_name="ceilf"] pure fn ceil(n: f32) -> f32; #[link_name="cosf"] pure fn cos(n: f32) -> f32; #[link_name="coshf"] pure fn cosh(n: f32) -> f32; + #[link_name="erff"] pure fn erf(n: f32) -> f32; + #[link_name="erfcf"] pure fn erfc(n: f32) -> f32; #[link_name="expf"] pure fn exp(n: f32) -> f32; + #[link_name="expm1f"]pure fn expm1(n: f32) -> f32; + #[link_name="exp2f"] pure fn exp2(n: f32) -> f32; #[link_name="fabsf"] pure fn abs(n: f32) -> f32; + #[link_name="fdimf"] pure fn sub_pos(a: f32, b: f32) -> f32; #[link_name="floorf"] pure fn floor(n: f32) -> f32; #[link_name="frexpf"] pure fn frexp(n: f64, &value: c_int) -> f32; + #[link_name="fmaf"] pure fn mul_add(a: f32, b: f32, c: f32) -> f32; + #[link_name="fmaxf"] pure fn max(a: f32, b: f32) -> f32; + #[link_name="fminf"] pure fn min(a: f32, b: f32) -> f32; + #[link_name="nextafterf"] pure fn nextafter(x: f32, y: f32) -> f32 #[link_name="fmodf"] pure fn fmod(x: f32, y: f32) -> f32; + #[link_name="hypotf"] pure fn hypot(x: f32, y: f32) -> f32; #[link_name="ldexpf"] pure fn ldexp(x: f32, n: c_int) -> f32; + #[link_name="lgammaf_r"] pure fn lgamma(n: f32, sign: *c_int) -> f32; #[link_name="logf"] pure fn ln(n: f32) -> f32; + #[link_name="logbf"] pure fn logb(n: f32) -> f32; #[link_name="log1p"] pure fn ln1p(n: f64) -> f64; #[link_name="log2f"] pure fn log2(n: f32) -> f32; #[link_name="log10f"] pure fn log10(n: f32) -> f32; + #[link_name="ilogbf"] pure fn ilogb(n: f32) -> c_int; #[link_name="modff"] pure fn modf(n: f32, iptr: *f32) -> f32; #[link_name="powf"] pure fn pow(n: f32, e: f32) -> f32; + #[link_name="remainderf"] pure fn rem(a: f32, b: f32) -> f32; #[link_name="rintf"] pure fn rint(n: f32) -> f32; #[link_name="roundf"] pure fn round(n: f32) -> f32; #[link_name="sinf"] pure fn sin(n: f32) -> f32; @@ -67,9 +98,22 @@ native mod f32 { #[link_name="sqrtf"] pure fn sqrt(n: f32) -> f32; #[link_name="tanf"] pure fn tan(n: f32) -> f32; #[link_name="tanhf"] pure fn tanh(n: f32) -> f32; + #[link_name="tgammaf"] pure fn tgamma(n: f32) -> f32; #[link_name="truncf"] pure fn trunc(n: f32) -> f32; } +#[link_name = "m"] +#[abi = "cdecl"] +native mod bessel { + pure fn j0(n: m_float) -> m_float; + pure fn j1(n: m_float) -> m_float; + pure fn jn(i: c_int, n: m_float) -> m_float; + + pure fn y0(n: m_float) -> m_float; + pure fn y1(n: m_float) -> m_float; + pure fn yn(i: c_int, n: m_float) -> m_float; +} + // // Local Variables: // mode: rust diff --git a/src/libcore/ctypes.rs b/src/libcore/ctypes.rs index def6c5f11569e..8bc39cede0a88 100644 --- a/src/libcore/ctypes.rs +++ b/src/libcore/ctypes.rs @@ -114,3 +114,5 @@ Type: enum An unsigned integer with the same size as a C enum */ type enum = u32; + + diff --git a/src/libcore/mtypes.rs b/src/libcore/mtypes.rs index d4339dc091c86..a491aca01115b 100644 --- a/src/libcore/mtypes.rs +++ b/src/libcore/mtypes.rs @@ -32,7 +32,7 @@ type m_uint = u32; #[cfg(target_arch="x86_64")] type m_uint = u64; -// PORT *must* match with "import m_float = fXX" in std::math per arch +// PORT *must* match with "import m_float = fXX" in core::float per arch /* Type: m_float From 49d36c7f8521e7059ee991cc361e02089b00baa4 Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Thu, 22 Dec 2011 15:19:43 +0100 Subject: [PATCH 4/9] Trying to remove wrappers from float --- src/libcore/cmath.rs | 190 ++++++++++++----------- src/libcore/ctypes.rs | 17 +- src/libcore/f32.rs | 101 +++++++++++- src/libcore/f64.rs | 98 +++++++++++- src/libcore/float.rs | 50 +++++- src/libcore/mtypes.rs | 4 +- src/test/run-pass/invoke-external-native | Bin 0 -> 10460 bytes 7 files changed, 349 insertions(+), 111 deletions(-) create mode 100755 src/test/run-pass/invoke-external-native diff --git a/src/libcore/cmath.rs b/src/libcore/cmath.rs index 8cea074a7c61e..adef88b791512 100644 --- a/src/libcore/cmath.rs +++ b/src/libcore/cmath.rs @@ -1,117 +1,119 @@ import ctypes::c_int; +import ctypes::c_float; +import ctypes::c_double; + +// FIXME scalbn copysign #[link_name = "m"] #[abi = "cdecl"] -native mod f64 { +native mod c_double { // Alpabetically sorted by link_name - pure fn acos(n: f64) -> f64; - pure fn asin(n: f64) -> f64; - pure fn atan(n: f64) -> f64; - pure fn atan2(a: f64, b: f64) -> f64; - pure fn cbrt(n: f64) -> f64; - pure fn ceil(n: f64) -> f64; - pure fn cos(n: f64) -> f64; - pure fn cosh(n: f64) -> f64; - pure fn erf(n: f64) -> f64; - pure fn erfc(n: f64) -> f64; - pure fn exp(n: f64) -> f64; - pure fn expm1(n: f64) -> f64; - pure fn exp2(n: f64) -> f64; - #[link_name="fabs"] pure fn abs(n: f64) -> f64; - #[link_name="fdim"] pure fn sub_pos(a: f64, b: f64) -> f64; - pure fn floor(n: f64) -> f64; - #[link_name="fma"] pure fn mul_add(a: f64, b: f64, c: f64) -> f64; - #[link_name="fmax"] pure fn max(a: f64, b: f64) -> f64; - #[link_name="fmin"] pure fn min(a: f64, b: f64) -> f64; - pure fn nextafter(x: f64, y: f64) -> f64 - pure fn fmod(x: f64, y: f64) -> f64; - pure fn frexp(n: f64, &value: c_int) -> f64; - pure fn hypot(x: f64, y: f64) -> f64; - pure fn ldexp(x: f64, n: c_int) -> f64; - #[link_name="lgamma_r"] pure fn lgamma(n: f64, sign: *c_int) -> f64; - #[link_name="log"] pure fn ln(n: f64) -> f64; - pure fn logb(n: f64) -> f64; - #[link_name="log1p"] pure fn ln1p(n: f64) -> f64; - pure fn log10(n: f64) -> f64; - pure fn log2(n: f64) -> f64; - pure fn ilogb(n: f64) -> c_int; - pure fn modf(n: f64, iptr: *f64) -> f64; - pure fn pow(n: f64, e: f64) -> f64; - #[link_name="remainder"] pure fn rem(a: f64, b: f64) -> f64; - pure fn rint(n: f64) -> f64; - pure fn round(n: f64) -> f64; - pure fn sin(n: f64) -> f64; - pure fn sinh(n: f64) -> f64; - pure fn sqrt(n: f64) -> f64; - pure fn tan(n: f64) -> f64; - pure fn tanh(n: f64) -> f64; - pure fn tgamma(n: f64) -> f64; - pure fn trunc(n: f64) -> f64; + pure fn acos(n: c_double) -> c_double; + pure fn asin(n: c_double) -> c_double; + pure fn atan(n: c_double) -> c_double; + pure fn atan2(a: c_double, b: c_double) -> c_double; + pure fn cbrt(n: c_double) -> c_double; + pure fn ceil(n: c_double) -> c_double; + pure fn cos(n: c_double) -> c_double; + pure fn cosh(n: c_double) -> c_double; + pure fn erf(n: c_double) -> c_double; + pure fn erfc(n: c_double) -> c_double; + pure fn exp(n: c_double) -> c_double; + pure fn expm1(n: c_double) -> c_double; + pure fn exp2(n: c_double) -> c_double; + #[link_name="fabs"] pure fn abs(n: c_double) -> c_double; + #[link_name="fdim"] pure fn sub_pos(a: c_double, b: c_double) -> c_double; + pure fn floor(n: c_double) -> c_double; + #[link_name="fma"] pure fn mul_add(a: c_double, b: c_double, c: c_double) -> c_double; + #[link_name="fmax"] pure fn fmax(a: c_double, b: c_double) -> c_double; + #[link_name="fmin"] pure fn fmin(a: c_double, b: c_double) -> c_double; + pure fn nextafter(x: c_double, y: c_double) -> c_double; + #[link_name="fmod"] pure fn rem(x: c_double, y: c_double) -> c_double; + pure fn frexp(n: c_double, &value: c_int) -> c_double; + pure fn hypot(x: c_double, y: c_double) -> c_double; + pure fn ldexp(x: c_double, n: c_int) -> c_double; + #[link_name="lgamma_r"] pure fn lgamma(n: c_double, &sign: c_int) -> c_double; + #[link_name="log"] pure fn ln(n: c_double) -> c_double; + pure fn logb(n: c_double) -> c_double; + #[link_name="log1p"] pure fn ln1p(n: c_double) -> c_double; + pure fn log10(n: c_double) -> c_double; + pure fn log2(n: c_double) -> c_double; + pure fn ilogb(n: c_double) -> c_int; + pure fn modf(n: c_double, &iptr: c_double) -> c_double; + pure fn pow(n: c_double, e: c_double) -> c_double; + pure fn rint(n: c_double) -> c_double; + pure fn round(n: c_double) -> c_double; + pure fn sin(n: c_double) -> c_double; + pure fn sinh(n: c_double) -> c_double; + pure fn sqrt(n: c_double) -> c_double; + pure fn tan(n: c_double) -> c_double; + pure fn tanh(n: c_double) -> c_double; + pure fn tgamma(n: c_double) -> c_double; + pure fn trunc(n: c_double) -> c_double; } #[link_name = "m"] #[abi = "cdecl"] -native mod f32 { +native mod c_float { // Alpabetically sorted by link_name - #[link_name="acosf"] pure fn acos(n: f32) -> f32; - #[link_name="asinf"] pure fn asin(n: f32) -> f32; - #[link_name="atanf"] pure fn atan(n: f32) -> f32; - #[link_name="atan2f"] pure fn atan2(a: f32, b: f32) -> f32; - #[link_name="cbrtf"] pure fn cbrt(n: f32) -> f32; - #[link_name="ceilf"] pure fn ceil(n: f32) -> f32; - #[link_name="cosf"] pure fn cos(n: f32) -> f32; - #[link_name="coshf"] pure fn cosh(n: f32) -> f32; - #[link_name="erff"] pure fn erf(n: f32) -> f32; - #[link_name="erfcf"] pure fn erfc(n: f32) -> f32; - #[link_name="expf"] pure fn exp(n: f32) -> f32; - #[link_name="expm1f"]pure fn expm1(n: f32) -> f32; - #[link_name="exp2f"] pure fn exp2(n: f32) -> f32; - #[link_name="fabsf"] pure fn abs(n: f32) -> f32; - #[link_name="fdimf"] pure fn sub_pos(a: f32, b: f32) -> f32; - #[link_name="floorf"] pure fn floor(n: f32) -> f32; - #[link_name="frexpf"] pure fn frexp(n: f64, &value: c_int) -> f32; - #[link_name="fmaf"] pure fn mul_add(a: f32, b: f32, c: f32) -> f32; - #[link_name="fmaxf"] pure fn max(a: f32, b: f32) -> f32; - #[link_name="fminf"] pure fn min(a: f32, b: f32) -> f32; - #[link_name="nextafterf"] pure fn nextafter(x: f32, y: f32) -> f32 - #[link_name="fmodf"] pure fn fmod(x: f32, y: f32) -> f32; - #[link_name="hypotf"] pure fn hypot(x: f32, y: f32) -> f32; - #[link_name="ldexpf"] pure fn ldexp(x: f32, n: c_int) -> f32; - #[link_name="lgammaf_r"] pure fn lgamma(n: f32, sign: *c_int) -> f32; - #[link_name="logf"] pure fn ln(n: f32) -> f32; - #[link_name="logbf"] pure fn logb(n: f32) -> f32; - #[link_name="log1p"] pure fn ln1p(n: f64) -> f64; - #[link_name="log2f"] pure fn log2(n: f32) -> f32; - #[link_name="log10f"] pure fn log10(n: f32) -> f32; - #[link_name="ilogbf"] pure fn ilogb(n: f32) -> c_int; - #[link_name="modff"] pure fn modf(n: f32, iptr: *f32) -> f32; - #[link_name="powf"] pure fn pow(n: f32, e: f32) -> f32; - #[link_name="remainderf"] pure fn rem(a: f32, b: f32) -> f32; - #[link_name="rintf"] pure fn rint(n: f32) -> f32; - #[link_name="roundf"] pure fn round(n: f32) -> f32; - #[link_name="sinf"] pure fn sin(n: f32) -> f32; - #[link_name="sinhf"] pure fn sinh(n: f32) -> f32; - #[link_name="sqrtf"] pure fn sqrt(n: f32) -> f32; - #[link_name="tanf"] pure fn tan(n: f32) -> f32; - #[link_name="tanhf"] pure fn tanh(n: f32) -> f32; - #[link_name="tgammaf"] pure fn tgamma(n: f32) -> f32; - #[link_name="truncf"] pure fn trunc(n: f32) -> f32; + #[link_name="acosf"] pure fn acos(n: c_float) -> c_float; + #[link_name="asinf"] pure fn asin(n: c_float) -> c_float; + #[link_name="atanf"] pure fn atan(n: c_float) -> c_float; + #[link_name="atan2f"] pure fn atan2(a: c_float, b: c_float) -> c_float; + #[link_name="cbrtf"] pure fn cbrt(n: c_float) -> c_float; + #[link_name="ceilf"] pure fn ceil(n: c_float) -> c_float; + #[link_name="cosf"] pure fn cos(n: c_float) -> c_float; + #[link_name="coshf"] pure fn cosh(n: c_float) -> c_float; + #[link_name="erff"] pure fn erf(n: c_float) -> c_float; + #[link_name="erfcf"] pure fn erfc(n: c_float) -> c_float; + #[link_name="expf"] pure fn exp(n: c_float) -> c_float; + #[link_name="expm1f"]pure fn expm1(n: c_float) -> c_float; + #[link_name="exp2f"] pure fn exp2(n: c_float) -> c_float; + #[link_name="fabsf"] pure fn abs(n: c_float) -> c_float; + #[link_name="fdimf"] pure fn sub_pos(a: c_float, b: c_float) -> c_float; + #[link_name="floorf"] pure fn floor(n: c_float) -> c_float; + #[link_name="frexpf"] pure fn frexp(n: c_double, &value: c_int) -> c_float; + #[link_name="fmaf"] pure fn mul_add(a: c_float, b: c_float, c: c_float) -> c_float; + #[link_name="fmaxf"] pure fn fmax(a: c_float, b: c_float) -> c_float; + #[link_name="fminf"] pure fn fmin(a: c_float, b: c_float) -> c_float; + #[link_name="nextafterf"] pure fn nextafter(x: c_float, y: c_float) -> c_float; + #[link_name="fmodf"] pure fn rem(x: c_float, y: c_float) -> c_float; + #[link_name="hypotf"] pure fn hypot(x: c_float, y: c_float) -> c_float; + #[link_name="ldexpf"] pure fn ldexp(x: c_float, n: c_int) -> c_float; + #[link_name="lgammaf_r"] pure fn lgamma(n: c_float, &sign: c_int) -> c_float; + #[link_name="logf"] pure fn ln(n: c_float) -> c_float; + #[link_name="logbf"] pure fn logb(n: c_float) -> c_float; + #[link_name="log1p"] pure fn ln1p(n: c_double) -> c_double; + #[link_name="log2f"] pure fn log2(n: c_float) -> c_float; + #[link_name="log10f"] pure fn log10(n: c_float) -> c_float; + #[link_name="ilogbf"] pure fn ilogb(n: c_float) -> c_int; + #[link_name="modff"] pure fn modf(n: c_float, &iptr: c_float) -> c_float; + #[link_name="powf"] pure fn pow(n: c_float, e: c_float) -> c_float; + #[link_name="rintf"] pure fn rint(n: c_float) -> c_float; + #[link_name="roundf"] pure fn round(n: c_float) -> c_float; + #[link_name="sinf"] pure fn sin(n: c_float) -> c_float; + #[link_name="sinhf"] pure fn sinh(n: c_float) -> c_float; + #[link_name="sqrtf"] pure fn sqrt(n: c_float) -> c_float; + #[link_name="tanf"] pure fn tan(n: c_float) -> c_float; + #[link_name="tanhf"] pure fn tanh(n: c_float) -> c_float; + #[link_name="tgammaf"] pure fn tgamma(n: c_float) -> c_float; + #[link_name="truncf"] pure fn trunc(n: c_float) -> c_float; } #[link_name = "m"] #[abi = "cdecl"] native mod bessel { - pure fn j0(n: m_float) -> m_float; - pure fn j1(n: m_float) -> m_float; - pure fn jn(i: c_int, n: m_float) -> m_float; + pure fn j0(n: c_double) -> c_double; + pure fn j1(n: c_double) -> c_double; + pure fn jn(i: c_int, n: c_double) -> c_double; - pure fn y0(n: m_float) -> m_float; - pure fn y1(n: m_float) -> m_float; - pure fn yn(i: c_int, n: m_float) -> m_float; + pure fn y0(n: c_double) -> c_double; + pure fn y1(n: c_double) -> c_double; + pure fn yn(i: c_int, n: c_double) -> c_double; } // diff --git a/src/libcore/ctypes.rs b/src/libcore/ctypes.rs index 8bc39cede0a88..d26b183a2f38d 100644 --- a/src/libcore/ctypes.rs +++ b/src/libcore/ctypes.rs @@ -9,6 +9,8 @@ FIXME: Add a test that uses some native code to verify these sizes, which are not obviously correct for all potential platforms. */ +// PORT adapt to architecture + /* Type: c_int @@ -72,6 +74,20 @@ when interoperating with C void pointers can help in documentation. */ type void = int; +/* +Type: c_float + +A float value with the same size as a C `float` +*/ +type c_float = f32; + +/* +Type: c_float + +A float value with the same size as a C `double` +*/ +type c_double = f64; + /* Type: size_t @@ -115,4 +131,3 @@ An unsigned integer with the same size as a C enum */ type enum = u32; - diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index 293601e3b1872..7ff91aa9e6aa5 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -8,17 +8,108 @@ This exposes the same operations as `math`, just for `f32` even though they do not show up in the docs right now! */ -import cmath::f32::*; +export t; export - acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor, fmod, - frexp, ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin, - sinh, sqrt, tan, tanh, trunc, t; + acos, + asin, + atan, + atan2, + cbrt, + ceil, + cos, + cosh, + erf, + erfc, + exp, + expm1, + exp2, + abs, + sub_pos, + floor, + mul_add, + fmax, + fmin, + nextafter, + frexp, + hypot, + ldexp, + lgamma, + ln, + logb, + ln1p, + log10, + log2, + ilogb, + modf, + pow, + rem, + rint, + round, + sin, + sinh, + sqrt, + tan, + tanh, + tgamma, + trunc; + +export consts; export radix, mantissa_digits, digits, epsilon, min_value, max_value, min_exp, max_exp, min_10_exp, max_10_exp; -export consts; +// PORT + +import cops = cmath::c_float; + +type t = f64; + +import + cops::acos, + cops::asin, + cops::atan, + cops::atan2, + cops::cbrt, + cops::ceil, + cops::cos, + cops::cosh, + cops::erf, + cops::erfc, + cops::exp, + cops::expm1, + cops::exp2, + cops::abs, + cops::sub_pos, + cops::floor, + cops::mul_add, + cops::max, + cops::min, + cops::nextafter, + cops::fmod, + cops::frexp, + cops::hypot, + cops::ldexp, + cops::lgamma, + cops::ln, + cops::logb, + cops::ln1p, + cops::log10, + cops::log2, + cops::ilogb, + cops::modf, + cops::pow, + cops::rem, + cops::rint, + cops::round, + cops::sin, + cops::sinh, + cops::sqrt, + cops::tan, + cops::tanh, + cops::tgamma, + cops::trunc; + type t = f32; diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index 7eb4ce96789e7..6f27a2c42d10a 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -8,20 +8,108 @@ This exposes the same operations as `math`, just for `f64` even though they do not show up in the docs right now! */ -import cmath::f64::*; +export t; export - acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor, fmod, - frexp, ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin, - sinh, sqrt, tan, tanh, trunc, t; + acos, + asin, + atan, + atan2, + cbrt, + ceil, + cos, + cosh, + erf, + erfc, + exp, + expm1, + exp2, + abs, + sub_pos, + floor, + mul_add, + fmax, + fmin, + nextafter, + frexp, + hypot, + ldexp, + lgamma, + ln, + logb, + ln1p, + log10, + log2, + ilogb, + modf, + pow, + rem, + rint, + round, + sin, + sinh, + sqrt, + tan, + tanh, + tgamma, + trunc; + +export consts; export radix, mantissa_digits, digits, epsilon, min_value, max_value, min_exp, max_exp, min_10_exp, max_10_exp; -export consts; +// PORT + +import cops = cmath::c_double; type t = f64; +import + cops::acos, + cops::asin, + cops::atan, + cops::atan2, + cops::cbrt, + cops::ceil, + cops::cos, + cops::cosh, + cops::erf, + cops::erfc, + cops::exp, + cops::expm1, + cops::exp2, + cops::abs, + cops::sub_pos, + cops::floor, + cops::mul_add, + cops::max, + cops::min, + cops::nextafter, + cops::fmod, + cops::frexp, + cops::hypot, + cops::ldexp, + cops::lgamma, + cops::ln, + cops::logb, + cops::ln1p, + cops::log10, + cops::log2, + cops::ilogb, + cops::modf, + cops::pow, + cops::rem, + cops::rint, + cops::round, + cops::sin, + cops::sinh, + cops::sqrt, + cops::tan, + cops::tanh, + cops::tgamma, + cops::trunc; + /* Module: consts */ mod consts { diff --git a/src/libcore/float.rs b/src/libcore/float.rs index 6ff08df4b26f3..83ba43ad132da 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -10,9 +10,51 @@ export t; export consts; export - acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor, fmod, frexp, - ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin, sinh, sqrt, - tan, tanh, trunc; + acos, + asin, + atan, + atan2, + cbrt, + ceil, + cos, + cosh, + erf, + erfc, + exp, + expm1, + exp2, + abs, + sub_pos, + floor, + mul_add, + max, + min, + nextafter, + rem, + frexp, + hypot, + ldexp, + lgamma, + ln, + logb, + ln1p, + log10, + log2, + ilogb, + modf, + pow, + rint, + round, + sin, + sinh, + sqrt, + tan, + tanh, + tgamma, + trunc; + +export radix, mantissa_digits, digits, epsilon, min_value, max_value, + min_exp, max_exp, min_10_exp, max_10_exp; export to_str_common, to_str_exact, to_str, from_str; export lt, le, eq, ne, gt, eq; @@ -27,9 +69,9 @@ import ctypes::c_int; import ptr; // PORT This must match in width according to architecture + import f64; import m_float = f64; - type t = m_float; /** diff --git a/src/libcore/mtypes.rs b/src/libcore/mtypes.rs index a491aca01115b..4bda1eaae22c4 100644 --- a/src/libcore/mtypes.rs +++ b/src/libcore/mtypes.rs @@ -41,11 +41,11 @@ Machine type equivalent of a float */ type m_float = f64; -// PORT *must* match "import m_complex = ..." in std::complex per arch - /* FIXME Type m_complex +// PORT *must* match "import m_complex = ..." in core::complex per arch + Machine type representing a complex value that uses floats for both the real and the imaginary part. */ diff --git a/src/test/run-pass/invoke-external-native b/src/test/run-pass/invoke-external-native new file mode 100755 index 0000000000000000000000000000000000000000..cae50987190dbdafecccd9d10ad52edd240d84f0 GIT binary patch literal 10460 zcmeHNeQZ@>E zpdv*JYq26EPiv=5Nc;Gs5?eJDsal1$b_!56{86=lw1Kw%(M?ox8>VH`J`k(s{my;& z+Rt_xqW!b|;UnLVbMHC7d)~R{-D}_L4{rbI-&KscDj1t~Gse~-o$)aCZB~aGWA`Iz zNSqJ!JlT7wckmn3T1c)!c1ezKwqXFy5A+TnC=?O;-oiE}dDzBQA>AdrIk${SD=%A& zw+Pz_?+>CRNxG@( z)FWQHvIt4x86iGu>f?su3+MNe#Osnq5x*QB#mOZRC%bzN^f-n@JQT!*ZKq>;DAdbNkeJBHm=D}%OJ!8|&Bv8l1lITS_4OUUQ4mBvG zA7X40CB0_!?$$oc*mjhak1%!}jT9>bNJMwbL~H$Kq%}yNM`s!s+q6U{>C_=%d1Ot= zgxM4c`?!X-O4ZyBh(w&C%HANX%?pP#&Eax2=zoK)Fw1Fh}nkZ7(SMfzWp z+CT1O2R*ljM9)Y*SL8F}x)maC1Y)MKqovIs>}qdq>u7Ij?HDyWcQiM)G{Q|`zf`z0 zPQnWA=O}eHFp~+(w3N;l5PlR#q~|jxjX)BcQpDimsT&)coF57a(hf9Dc6IQMwjFvr9x-+V zb@OOAN-hEh;gQ{h*A$CeLO}%6@|p;15l0@xxrit5E1@GyOMR@(+*s6Rk9)LJNpI@* z%mvr^KUQe2ziO!~3zgc;d5<>J&65A1TIMD|Q}dZO@UH(Y4KlRHw>C6`JNiK1FUfGC zzF*lTW0`hzT^tf)+-zRBOPhJ8`S3;U)bU%HBj{OR+HCJFE!DfYP@B4fGLzbSBXbNr zXwQJZzEH27Q()TcUYNP@eyj;vYFezhez^*t_>u-c~s7EA({aKC2qTV2Jt5F2FB*IJ$Pr3F<4fUv0(? zf4q{zpS@d~b!oGEJS|tXfBr>FRgr;r)E~<04|@96zJ6JAVIJ3#?(|B=-{{ou$CPq3 z9KN_>`+tK$Z2!=pqlNLf)cohJ_IcElrQWw^Z3r&CKw+1?VozBi{u2J| z&EcmumDsa{{KmZeH7)gif9gUejHbilnU1ih4-W3{8R*%sr83&gTy1~qcL<~Y)U^z) zIX%zx3_o+|n;fQj`e%ncec20I>YbkKRV{UMAhQh;!-p@Xvkf7<7ug1S-?9x6kw--y z7r7~NOXL$GpAn_peV1wv zQtdUWZJ^pks?lB-x}YZzF1Zp2{}-9A!FtdV9r}sH2@SBC60Xy@!~uA`Mc7 z=w27%#X*Y0Ix5I@Y@mOTxhB0;r}qNe>#eTxq#^VedTHG+{Za30dDfb-75G^4WL32jRJuQTmmU|h^ zb)VVxij{z5m%w`q&0SKin`DTVkh_8A0V&rhgqD`8hW|oRu5lT;Hps=L9A8H6Imn$^ z0p1D7ot1K*_Z)Ycr=LoFP1)14ruM}O%(HvfyVX_p&WT}|`hGhgzvm9iazvI&y|`4r zXCIKi^R~*eQI=h@d_tD}vOFlu5m_FQrTX4pJdWbulyc`}soFiVqDs}S$~ay@dd`)S z`W>u(m#kE;ELZkG*#l({ls!=PK-mLj50pJn_CVPKWe=1+Q1-z8qX%m0+0d?McMbN& zw}~^R8XxC$^3DUMZW(-BkKyo5(oWn?u=IK#-Ta7~bnXvFgSh#gJ}J+Y zvaa+7A5X>udL&{a<6=A!i|UcEHN~bE^J2n1rf)q48H(bDAjk)eL=qRvX=Z9<*P(qo z6H|!}`$D2+XGDjYu>?0vGiJ7Q^46}-w$_f8z|NK~!yjyK?9u(jkxt>826H6}mx2;WCM{YTKaWY!zp%<9ayCe&!N}ve#YvhI`C0_FK0R zod}z_uKd)^D#t^3M7oJF@V^6{ygzklUmGdHEA8Da!EW8(Vsr+ZceZy0d)c=n*h?;P zM;@iS`#-yJScnVF#TsO1aWUu?@LO~~db`*AI@%aqM+ zMn+eo?g0D9J;6SP@TGd{rR+LoAJvPvTdeV=T>~Q{KA*37q`6z%>8-(mkvcb``_2c! zr>gC9B)Z>RgUd^GPV}(k)%jGDC;Nwq#~GJ8 z4;ur&7DlLZx|rnEdEHUs0o;sZW5Rz$_N()|pMbA$<1DvOf(>8h-;afhYNP$qz_=kL2s5KSPrD%KjN8FZ+Ke z`O9+rHzlv`Yu^Hok2u4}@Q6jhBeyAA#BW5rqluzk(4#h=({C=~yKTNw$@doV2aEVn z5r3?Rf4PXKivZY@!#{8HIeRGH=-hn^QXSGmNcBh*cUzGfkmwG9?nOa9N-Nc=V|*-< zG{hRz$~foaSn=&uj$?U>8#@{PdNgL&6AW)&B9=4*My??){$rD4ttonuTYw~1t?^ip z_7TR#+7*!H1iF`7rCysLm7EZ3#NmxLvFvz(|*)91+Osz`A z?PB|Kt~;dySe=R+3s<({UdNhP+~rsTi@VgCYHt$7d*y$zOO|+2Z1^RuwA0&LX>Jp> tO_PQQ*E;Q%dwzb)RNn&fo26 Date: Fri, 23 Dec 2011 02:31:24 +0100 Subject: [PATCH 5/9] core: killed all math wrappers --- src/libcore/cmath.rs | 34 ++- src/libcore/core.rc | 2 +- src/libcore/f32.rs | 185 ++++++-------- src/libcore/f64.rs | 183 +++++++------- src/libcore/float.rs | 518 +-------------------------------------- src/libcore/mtypes.rs | 62 ----- src/test/stdtest/math.rs | 5 +- 7 files changed, 194 insertions(+), 795 deletions(-) delete mode 100644 src/libcore/mtypes.rs diff --git a/src/libcore/cmath.rs b/src/libcore/cmath.rs index adef88b791512..73cb9af8bdbd7 100644 --- a/src/libcore/cmath.rs +++ b/src/libcore/cmath.rs @@ -1,9 +1,11 @@ +export c_double; +export c_float; +export bessel; + import ctypes::c_int; import ctypes::c_float; import ctypes::c_double; -// FIXME scalbn copysign - #[link_name = "m"] #[abi = "cdecl"] native mod c_double { @@ -16,6 +18,7 @@ native mod c_double { pure fn atan2(a: c_double, b: c_double) -> c_double; pure fn cbrt(n: c_double) -> c_double; pure fn ceil(n: c_double) -> c_double; + pure fn copysign(x: c_double, y: c_double) -> c_double; pure fn cos(n: c_double) -> c_double; pure fn cosh(n: c_double) -> c_double; pure fn erf(n: c_double) -> c_double; @@ -26,15 +29,16 @@ native mod c_double { #[link_name="fabs"] pure fn abs(n: c_double) -> c_double; #[link_name="fdim"] pure fn sub_pos(a: c_double, b: c_double) -> c_double; pure fn floor(n: c_double) -> c_double; - #[link_name="fma"] pure fn mul_add(a: c_double, b: c_double, c: c_double) -> c_double; + #[link_name="fma"] pure fn mul_add(a: c_double, b: c_double, + c: c_double) -> c_double; #[link_name="fmax"] pure fn fmax(a: c_double, b: c_double) -> c_double; #[link_name="fmin"] pure fn fmin(a: c_double, b: c_double) -> c_double; pure fn nextafter(x: c_double, y: c_double) -> c_double; - #[link_name="fmod"] pure fn rem(x: c_double, y: c_double) -> c_double; pure fn frexp(n: c_double, &value: c_int) -> c_double; pure fn hypot(x: c_double, y: c_double) -> c_double; pure fn ldexp(x: c_double, n: c_int) -> c_double; - #[link_name="lgamma_r"] pure fn lgamma(n: c_double, &sign: c_int) -> c_double; + #[link_name="lgamma_r"] pure fn lgamma(n: c_double, + &sign: c_int) -> c_double; #[link_name="log"] pure fn ln(n: c_double) -> c_double; pure fn logb(n: c_double) -> c_double; #[link_name="log1p"] pure fn ln1p(n: c_double) -> c_double; @@ -45,6 +49,7 @@ native mod c_double { pure fn pow(n: c_double, e: c_double) -> c_double; pure fn rint(n: c_double) -> c_double; pure fn round(n: c_double) -> c_double; + pure fn scalbn(n: c_double, i: c_int) -> c_double; pure fn sin(n: c_double) -> c_double; pure fn sinh(n: c_double) -> c_double; pure fn sqrt(n: c_double) -> c_double; @@ -66,6 +71,8 @@ native mod c_float { #[link_name="atan2f"] pure fn atan2(a: c_float, b: c_float) -> c_float; #[link_name="cbrtf"] pure fn cbrt(n: c_float) -> c_float; #[link_name="ceilf"] pure fn ceil(n: c_float) -> c_float; + #[link_name="copysignf"] pure fn copysign(x: c_float, + y: c_float) -> c_float; #[link_name="cosf"] pure fn cos(n: c_float) -> c_float; #[link_name="coshf"] pure fn cosh(n: c_float) -> c_float; #[link_name="erff"] pure fn erf(n: c_float) -> c_float; @@ -76,25 +83,30 @@ native mod c_float { #[link_name="fabsf"] pure fn abs(n: c_float) -> c_float; #[link_name="fdimf"] pure fn sub_pos(a: c_float, b: c_float) -> c_float; #[link_name="floorf"] pure fn floor(n: c_float) -> c_float; - #[link_name="frexpf"] pure fn frexp(n: c_double, &value: c_int) -> c_float; - #[link_name="fmaf"] pure fn mul_add(a: c_float, b: c_float, c: c_float) -> c_float; + #[link_name="frexpf"] pure fn frexp(n: c_double, + &value: c_int) -> c_float; + #[link_name="fmaf"] pure fn mul_add(a: c_float, + b: c_float, c: c_float) -> c_float; #[link_name="fmaxf"] pure fn fmax(a: c_float, b: c_float) -> c_float; #[link_name="fminf"] pure fn fmin(a: c_float, b: c_float) -> c_float; - #[link_name="nextafterf"] pure fn nextafter(x: c_float, y: c_float) -> c_float; - #[link_name="fmodf"] pure fn rem(x: c_float, y: c_float) -> c_float; + #[link_name="nextafterf"] pure fn nextafter(x: c_float, + y: c_float) -> c_float; #[link_name="hypotf"] pure fn hypot(x: c_float, y: c_float) -> c_float; #[link_name="ldexpf"] pure fn ldexp(x: c_float, n: c_int) -> c_float; - #[link_name="lgammaf_r"] pure fn lgamma(n: c_float, &sign: c_int) -> c_float; + #[link_name="lgammaf_r"] pure fn lgamma(n: c_float, + &sign: c_int) -> c_float; #[link_name="logf"] pure fn ln(n: c_float) -> c_float; #[link_name="logbf"] pure fn logb(n: c_float) -> c_float; #[link_name="log1p"] pure fn ln1p(n: c_double) -> c_double; #[link_name="log2f"] pure fn log2(n: c_float) -> c_float; #[link_name="log10f"] pure fn log10(n: c_float) -> c_float; #[link_name="ilogbf"] pure fn ilogb(n: c_float) -> c_int; - #[link_name="modff"] pure fn modf(n: c_float, &iptr: c_float) -> c_float; + #[link_name="modff"] pure fn modf(n: c_float, + &iptr: c_float) -> c_float; #[link_name="powf"] pure fn pow(n: c_float, e: c_float) -> c_float; #[link_name="rintf"] pure fn rint(n: c_float) -> c_float; #[link_name="roundf"] pure fn round(n: c_float) -> c_float; + #[link_name="scalbnf"] pure fn scalbn(n: c_float, i: c_int) -> c_float; #[link_name="sinf"] pure fn sin(n: c_float) -> c_float; #[link_name="sinhf"] pure fn sinh(n: c_float) -> c_float; #[link_name="sqrtf"] pure fn sqrt(n: c_float) -> c_float; diff --git a/src/libcore/core.rc b/src/libcore/core.rc index f0dc1cc4e0fae..e91c2a34ea0fa 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -10,7 +10,7 @@ export box, char, float, f32, f64, int, str, ptr; export uint, u8, u32, u64, vec, bool; export either, option, result; -export ctypes, mtypes, sys, unsafe, comm, task; +export ctypes, sys, unsafe, comm, task; export extfmt; // Built-in-type support modules diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index 7ff91aa9e6aa5..192d3f14ea418 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -1,117 +1,94 @@ - /* Module: f32 Floating point operations and constants for `f32` - -This exposes the same operations as `math`, just for `f32` even though -they do not show up in the docs right now! */ +// PORT -export t; - -export - acos, - asin, - atan, - atan2, - cbrt, - ceil, - cos, - cosh, - erf, - erfc, - exp, - expm1, - exp2, - abs, - sub_pos, - floor, - mul_add, - fmax, - fmin, - nextafter, - frexp, - hypot, - ldexp, - lgamma, - ln, - logb, - ln1p, - log10, - log2, - ilogb, - modf, - pow, - rem, - rint, - round, - sin, - sinh, - sqrt, - tan, - tanh, - tgamma, - trunc; - -export consts; - -export radix, mantissa_digits, digits, epsilon, min_value, max_value, - min_exp, max_exp, min_10_exp, max_10_exp; +import cmath::c_float::*; -// PORT +type t = f32; -import cops = cmath::c_float; - -type t = f64; - -import - cops::acos, - cops::asin, - cops::atan, - cops::atan2, - cops::cbrt, - cops::ceil, - cops::cos, - cops::cosh, - cops::erf, - cops::erfc, - cops::exp, - cops::expm1, - cops::exp2, - cops::abs, - cops::sub_pos, - cops::floor, - cops::mul_add, - cops::max, - cops::min, - cops::nextafter, - cops::fmod, - cops::frexp, - cops::hypot, - cops::ldexp, - cops::lgamma, - cops::ln, - cops::logb, - cops::ln1p, - cops::log10, - cops::log2, - cops::ilogb, - cops::modf, - cops::pow, - cops::rem, - cops::rint, - cops::round, - cops::sin, - cops::sinh, - cops::sqrt, - cops::tan, - cops::tanh, - cops::tgamma, - cops::trunc; +/* Const: NaN */ +const NaN: f32 = 0.0f32/0.0f32; +/* Const: infinity */ +const infinity: f32 = 1.0f32/0.0f32; -type t = f32; +/* Const: neg_infinity */ +const neg_infinity: f32 = -1.0f32/0.0f32; + +/* Predicate: isNaN */ +pure fn isNaN(f: f32) -> bool { f != f } + +/* Function: add */ +pure fn add(x: f32, y: f32) -> f32 { ret x + y; } + +/* Function: sub */ +pure fn sub(x: f32, y: f32) -> f32 { ret x - y; } + +/* Function: mul */ +pure fn mul(x: f32, y: f32) -> f32 { ret x * y; } + +/* Function: div */ +pure fn div(x: f32, y: f32) -> f32 { ret x / y; } + +/* Function: rem */ +pure fn rem(x: f32, y: f32) -> f32 { ret x % y; } + +/* Predicate: lt */ +pure fn lt(x: f32, y: f32) -> bool { ret x < y; } + +/* Predicate: le */ +pure fn le(x: f32, y: f32) -> bool { ret x <= y; } + +/* Predicate: eq */ +pure fn eq(x: f32, y: f32) -> bool { ret x == y; } + +/* Predicate: ne */ +pure fn ne(x: f32, y: f32) -> bool { ret x != y; } + +/* Predicate: ge */ +pure fn ge(x: f32, y: f32) -> bool { ret x >= y; } + +/* Predicate: gt */ +pure fn gt(x: f32, y: f32) -> bool { ret x > y; } + +/* +Predicate: positive + +Returns true if `x` is a positive number, including +0.0f320 and +Infinity. + */ +pure fn positive(x: f32) -> bool + { ret x > 0.0f32 || (1.0f32/x) == infinity; } + +/* +Predicate: negative + +Returns true if `x` is a negative number, including -0.0f320 and -Infinity. + */ +pure fn negative(x: f32) -> bool + { ret x < 0.0f32 || (1.0f32/x) == neg_infinity; } + +/* +Predicate: nonpositive + +Returns true if `x` is a negative number, including -0.0f320 and -Infinity. +(This is the same as `f32::negative`.) +*/ +pure fn nonpositive(x: f32) -> bool { + ret x < 0.0f32 || (1.0f32/x) == neg_infinity; +} + +/* +Predicate: nonnegative + +Returns true if `x` is a positive number, including +0.0f320 and +Infinity. +(This is the same as `f32::positive`.) +*/ +pure fn nonnegative(x: f32) -> bool { + ret x > 0.0f32 || (1.0f32/x) == infinity; +} /* Module: consts */ mod consts { diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index 6f27a2c42d10a..6413b5c5c1600 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -1,114 +1,95 @@ - /* Module: f64 -Floating point operations and constants for `f64`s - -This exposes the same operations as `math`, just for `f64` even though -they do not show up in the docs right now! +Floating point operations and constants for `f64` */ -export t; - -export - acos, - asin, - atan, - atan2, - cbrt, - ceil, - cos, - cosh, - erf, - erfc, - exp, - expm1, - exp2, - abs, - sub_pos, - floor, - mul_add, - fmax, - fmin, - nextafter, - frexp, - hypot, - ldexp, - lgamma, - ln, - logb, - ln1p, - log10, - log2, - ilogb, - modf, - pow, - rem, - rint, - round, - sin, - sinh, - sqrt, - tan, - tanh, - tgamma, - trunc; - -export consts; - -export radix, mantissa_digits, digits, epsilon, min_value, max_value, - min_exp, max_exp, min_10_exp, max_10_exp; - // PORT -import cops = cmath::c_double; +import cmath::c_double::*; type t = f64; -import - cops::acos, - cops::asin, - cops::atan, - cops::atan2, - cops::cbrt, - cops::ceil, - cops::cos, - cops::cosh, - cops::erf, - cops::erfc, - cops::exp, - cops::expm1, - cops::exp2, - cops::abs, - cops::sub_pos, - cops::floor, - cops::mul_add, - cops::max, - cops::min, - cops::nextafter, - cops::fmod, - cops::frexp, - cops::hypot, - cops::ldexp, - cops::lgamma, - cops::ln, - cops::logb, - cops::ln1p, - cops::log10, - cops::log2, - cops::ilogb, - cops::modf, - cops::pow, - cops::rem, - cops::rint, - cops::round, - cops::sin, - cops::sinh, - cops::sqrt, - cops::tan, - cops::tanh, - cops::tgamma, - cops::trunc; +/* Const: NaN */ +const NaN: f64 = 0.0f64/0.0f64; + +/* Const: infinity */ +const infinity: f64 = 1.0f64/0.0f64; + +/* Const: neg_infinity */ +const neg_infinity: f64 = -1.0f64/0.0f64; + +/* Predicate: isNaN */ +pure fn isNaN(f: f64) -> bool { f != f } + +/* Function: add */ +pure fn add(x: f64, y: f64) -> f64 { ret x + y; } + +/* Function: sub */ +pure fn sub(x: f64, y: f64) -> f64 { ret x - y; } + +/* Function: mul */ +pure fn mul(x: f64, y: f64) -> f64 { ret x * y; } + +/* Function: div */ +pure fn div(x: f64, y: f64) -> f64 { ret x / y; } + +/* Function: rem */ +pure fn rem(x: f64, y: f64) -> f64 { ret x % y; } + +/* Predicate: lt */ +pure fn lt(x: f64, y: f64) -> bool { ret x < y; } + +/* Predicate: le */ +pure fn le(x: f64, y: f64) -> bool { ret x <= y; } + +/* Predicate: eq */ +pure fn eq(x: f64, y: f64) -> bool { ret x == y; } + +/* Predicate: ne */ +pure fn ne(x: f64, y: f64) -> bool { ret x != y; } + +/* Predicate: ge */ +pure fn ge(x: f64, y: f64) -> bool { ret x >= y; } + +/* Predicate: gt */ +pure fn gt(x: f64, y: f64) -> bool { ret x > y; } + +/* +Predicate: positive + +Returns true if `x` is a positive number, including +0.0f640 and +Infinity. + */ +pure fn positive(x: f64) -> bool + { ret x > 0.0f64 || (1.0f64/x) == infinity; } + +/* +Predicate: negative + +Returns true if `x` is a negative number, including -0.0f640 and -Infinity. + */ +pure fn negative(x: f64) -> bool + { ret x < 0.0f64 || (1.0f64/x) == neg_infinity; } + +/* +Predicate: nonpositive + +Returns true if `x` is a negative number, including -0.0f640 and -Infinity. +(This is the same as `f64::negative`.) +*/ +pure fn nonpositive(x: f64) -> bool { + ret x < 0.0f64 || (1.0f64/x) == neg_infinity; +} + +/* +Predicate: nonnegative + +Returns true if `x` is a positive number, including +0.0f640 and +Infinity. +(This is the same as `f64::positive`.) +*/ +pure fn nonnegative(x: f64) -> bool { + ret x > 0.0f64 || (1.0f64/x) == infinity; +} /* Module: consts */ mod consts { diff --git a/src/libcore/float.rs b/src/libcore/float.rs index 83ba43ad132da..b482f169fc200 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -2,77 +2,12 @@ Module: float */ -// Currently this module supports from -lm -// C95 + log2 + log1p + trunc + round + rint - -export t; - -export consts; - -export - acos, - asin, - atan, - atan2, - cbrt, - ceil, - cos, - cosh, - erf, - erfc, - exp, - expm1, - exp2, - abs, - sub_pos, - floor, - mul_add, - max, - min, - nextafter, - rem, - frexp, - hypot, - ldexp, - lgamma, - ln, - logb, - ln1p, - log10, - log2, - ilogb, - modf, - pow, - rint, - round, - sin, - sinh, - sqrt, - tan, - tanh, - tgamma, - trunc; - -export radix, mantissa_digits, digits, epsilon, min_value, max_value, - min_exp, max_exp, min_10_exp, max_10_exp; - -export to_str_common, to_str_exact, to_str, from_str; -export lt, le, eq, ne, gt, eq; -export NaN, isNaN, infinity, neg_infinity; -export pow_uint_to_uint_as_float; -export min, max; -export add, sub, mul, div; -export positive, negative, nonpositive, nonnegative; - -import mtypes::m_float; -import ctypes::c_int; -import ptr; - // PORT This must match in width according to architecture -import f64; import m_float = f64; -type t = m_float; +import m_float::*; + +type t = float; /** * Section: String Conversions @@ -325,185 +260,6 @@ fn pow_uint_to_uint_as_float(x: uint, pow: uint) -> float { } -/* Const: NaN */ -const NaN: float = 0./0.; - -/* Const: infinity */ -const infinity: float = 1./0.; - -/* Const: neg_infinity */ -const neg_infinity: float = -1./0.; - -/* Predicate: isNaN */ -pure fn isNaN(f: float) -> bool { f != f } - -/* Function: add */ -pure fn add(x: float, y: float) -> float { ret x + y; } - -/* Function: sub */ -pure fn sub(x: float, y: float) -> float { ret x - y; } - -/* Function: mul */ -pure fn mul(x: float, y: float) -> float { ret x * y; } - -/* Function: div */ -pure fn div(x: float, y: float) -> float { ret x / y; } - -/* Function: rem */ -pure fn rem(x: float, y: float) -> float { ret x % y; } - -/* Predicate: lt */ -pure fn lt(x: float, y: float) -> bool { ret x < y; } - -/* Predicate: le */ -pure fn le(x: float, y: float) -> bool { ret x <= y; } - -/* Predicate: eq */ -pure fn eq(x: float, y: float) -> bool { ret x == y; } - -/* Predicate: ne */ -pure fn ne(x: float, y: float) -> bool { ret x != y; } - -/* Predicate: ge */ -pure fn ge(x: float, y: float) -> bool { ret x >= y; } - -/* Predicate: gt */ -pure fn gt(x: float, y: float) -> bool { ret x > y; } - -/* -Predicate: positive - -Returns true if `x` is a positive number, including +0.0 and +Infinity. - */ -pure fn positive(x: float) -> bool { ret x > 0. || (1./x) == infinity; } - -/* -Predicate: negative - -Returns true if `x` is a negative number, including -0.0 and -Infinity. - */ -pure fn negative(x: float) -> bool { ret x < 0. || (1./x) == neg_infinity; } - -/* -Predicate: nonpositive - -Returns true if `x` is a negative number, including -0.0 and -Infinity. -(This is the same as `float::negative`.) -*/ -pure fn nonpositive(x: float) -> bool { - ret x < 0. || (1./x) == neg_infinity; -} - -/* -Predicate: nonnegative - -Returns true if `x` is a positive number, including +0.0 and +Infinity. -(This is the same as `float::positive`.) -*/ -pure fn nonnegative(x: float) -> bool { - ret x > 0. || (1./x) == infinity; -} - -/* -Module: consts -*/ -mod consts { - /* - Const: pi - - Archimedes' constant - */ - const pi: float = 3.14159265358979323846264338327950288; - - /* - Const: frac_pi_2 - - pi/2.0 - */ - const frac_pi_2: float = 1.57079632679489661923132169163975144; - - /* - Const: frac_pi_4 - - pi/4.0 - */ - const frac_pi_4: float = 0.785398163397448309615660845819875721; - - /* - Const: frac_1_pi - - 1.0/pi - */ - const frac_1_pi: float = 0.318309886183790671537767526745028724; - - /* - Const: frac_2_pi - - 2.0/pi - */ - const frac_2_pi: float = 0.636619772367581343075535053490057448; - - /* - Const: frac_2_sqrtpi - - 2.0/sqrt(pi) - */ - const frac_2_sqrtpi: float = 1.12837916709551257389615890312154517; - - /* - Const: sqrt2 - - sqrt(2.0) - */ - const sqrt2: float = 1.41421356237309504880168872420969808; - - /* - Const: frac_1_sqrt2 - - 1.0/sqrt(2.0) - */ - const frac_1_sqrt2: float = 0.707106781186547524400844362104849039; - - /* - Const: e - - Euler's number - */ - const e: float = 2.71828182845904523536028747135266250; - - /* - Const: log2_e - - log2(e) - */ - const log2_e: float = 1.44269504088896340735992468100189214; - - /* - Const: log10_e - - log10(e) - */ - const log10_e: float = 0.434294481903251827651128918916605082; - - /* - Const: ln_2 - - ln(2.0) - */ - const ln_2: float = 0.693147180559945309417232121458176568; - - /* - Const: ln_10 - - ln(10.0) - */ - const ln_10: float = 2.30258509299404568401799145468436421; -} - - -// FIXME min/max type specialize via libm when overloading works -// (in theory fmax/fmin, fmaxf, fminf /should/ be faster) - /* Function: min @@ -518,274 +274,6 @@ Returns the maximum of two values */ pure fn max(x: T, y: T) -> T { x < y ? y : x } -/* -Function: acos - -Returns the arccosine of an angle (measured in rad) -*/ -pure fn acos(x: float) -> float - { ret m_float::acos(x as m_float) as float } - -/* -Function: asin - -Returns the arcsine of an angle (measured in rad) -*/ -pure fn asin(x: float) -> float - { ret m_float::asin(x as m_float) as float } - -/* -Function: atan - -Returns the arctangents of an angle (measured in rad) -*/ -pure fn atan(x: float) -> float - { ret m_float::atan(x as m_float) as float } - - -/* -Function: atan2 - -Returns the arctangent of an angle (measured in rad) -*/ -pure fn atan2(y: float, x: float) -> float - { ret m_float::atan2(y as m_float, x as m_float) as float } - -/* -Function: ceil - -Returns the smallest integral value less than or equal to `n` -*/ -pure fn ceil(n: float) -> float - { ret m_float::ceil(n as m_float) as float } - -/* -Function: cos - -Returns the cosine of an angle `x` (measured in rad) -*/ -pure fn cos(x: float) -> float - { ret m_float::cos(x as m_float) as float } - -/* -Function: cosh - -Returns the hyperbolic cosine of `x` - -*/ -pure fn cosh(x: float) -> float - { ret m_float::cosh(x as m_float) as float } - - -/* -Function: exp - -Returns `consts::e` to the power of `n* -*/ -pure fn exp(n: float) -> float - { ret m_float::exp(n as m_float) as float } - -/* -Function: abs - -Returns the absolute value of `n` -*/ -pure fn abs(n: float) -> float - { ret m_float::abs(n as m_float) as float } - -/* -Function: floor - -Returns the largest integral value less than or equal to `n` -*/ -pure fn floor(n: float) -> float - { ret m_float::floor(n as m_float) as float } - -/* -Function: fmod - -Returns the floating-point remainder of `x/y` -*/ -pure fn fmod(x: float, y: float) -> float - { ret m_float::fmod(x as m_float, y as m_float) as float } - -/* -Function: ln - -Returns the natural logaritm of `n` -*/ -pure fn ln(n: float) -> float - { ret m_float::ln(n as m_float) as float } - -/* -Function: ldexp - -Returns `x` multiplied by 2 to the power of `n` -*/ -pure fn ldexp(n: float, i: int) -> float - { ret m_float::ldexp(n as m_float, i as c_int) as float } - -/* -Function: ln1p - -Returns the natural logarithm of `1+n` accurately, -even for very small values of `n` -*/ -pure fn ln1p(n: float) -> float - { ret m_float::ln1p(n as m_float) as float } - -/* -Function: log10 - -Returns the logarithm to base 10 of `n` -*/ -pure fn log10(n: float) -> float - { ret m_float::log10(n as m_float) as float } - -/* -Function: log2 - -Returns the logarithm to base 2 of `n` -*/ -pure fn log2(n: float) -> float - { ret m_float::log2(n as m_float) as float } - -/* -Function: modf - -Breaks `n` into integral and fractional parts such that both -have the same sign as `n` - -The integral part is stored in `iptr`. - -Returns: - -The fractional part of `n` -*/ -#[no(warn_trivial_casts)] // FIXME Implement -pure fn modf(n: float, &iptr: float) -> float { unsafe { - ret m_float::modf(n as m_float, ptr::addr_of(iptr) as *m_float) as float -} } - -/* -Function: frexp - -Breaks `n` into a normalized fraction and an integral power of 2 - -The inegral part is stored in iptr. - -The functions return a number x such that x has a magnitude in the interval -[1/2, 1) or 0, and `n == x*(2 to the power of exp)`. - -Returns: - -The fractional part of `n` -*/ -pure fn frexp(n: float, &exp: c_int) -> float - { ret m_float::frexp(n as m_float, exp) as float } - -/* -Function: pow -*/ -pure fn pow(v: float, e: float) -> float - { ret m_float::pow(v as m_float, e as m_float) as float } - - -/* -Function: rint - -Returns the integral value nearest to `x` (according to the -prevailing rounding mode) in floating-point format -*/ -pure fn rint(x: float) -> float - { ret m_float::rint(x as m_float) as float } - -/* -Function: round - - -Return the integral value nearest to `x` rounding half-way -cases away from zero, regardless of the current rounding direction. -*/ -pure fn round(x: float) -> float - { ret m_float::round(x as m_float) as float } - -/* -Function: sin - -Returns the sine of an angle `x` (measured in rad) -*/ -pure fn sin(x: float) -> float - { ret m_float::sin(x as m_float) as float } - -/* -Function: sinh - -Returns the hyperbolic sine of an angle `x` (measured in rad) -*/ -pure fn sinh(x: float) -> float - { ret m_float::sinh(x as m_float) as float } - -/* -Function: sqrt - -Returns the square root of `x` -*/ -pure fn sqrt(x: float) -> float - { ret m_float::sqrt(x as m_float) as float } - -/* -Function: tan - -Returns the tangent of an angle `x` (measured in rad) - -*/ -pure fn tan(x: float) -> float - { ret m_float::tan(x as m_float) as float } - -/* -Function: tanh - -Returns the hyperbolic tangent of an angle `x` (measured in rad) - -*/ -pure fn tanh(x: float) -> float - { ret m_float::tanh(x as m_float) as float } - -/* -Function: trunc - -Returns the integral value nearest to but no larger in magnitude than `x` - -*/ -pure fn trunc(x: float) -> float - { ret m_float::trunc(x as m_float) as float } - -/* - -FIXME implement this as soon as const expressions may refer to each other - -export radix, mantissa_digits, digits, epsilon, min_value, max_value, - min_exp, max_exp, min_10_exp, max_10_exp; - -const radix: m_float = m_float::radix as m_float; - -const mantissa_digits: m_float = m_float::mantissa_digits as m_float; -const digits: m_float = m_float::digits as m_float; - -const epsilon: m_float = m_float::epsilon as m_float; - -const min_value: m_float = m_float::min_value as m_float; -const max_value: m_float = m_float::max_value as m_float; - -const min_exp: m_float = m_float::min_exp as m_float; -const max_exp: m_float = m_float::max_exp as m_float; - -const min_10_exp: m_float = m_float::min_10_exp as m_float; -const max_10_exp: m_float = m_float::max_10_exp as m_float; - -*/ - // // Local Variables: // mode: rust diff --git a/src/libcore/mtypes.rs b/src/libcore/mtypes.rs deleted file mode 100644 index 4bda1eaae22c4..0000000000000 --- a/src/libcore/mtypes.rs +++ /dev/null @@ -1,62 +0,0 @@ -/* - -Module: mtypes - -Machine type equivalents of rust int, uint, float, and complex. - -Types useful for interop with C when writing bindings that exist -for different types (float, f32, f64, ...; cf float.rs for an example) -*/ - -// PORT Change this when porting to a new architecture - -/* -Type: m_int - -Machine type equivalent of an int -*/ -#[cfg(target_arch="x86")] -type m_int = i32; -#[cfg(target_arch="x86_64")] -type m_int = i64; - -// PORT Change this when porting to a new architecture - -/* -Type: m_uint - -Machine type equivalent of a uint -*/ -#[cfg(target_arch="x86")] -type m_uint = u32; -#[cfg(target_arch="x86_64")] -type m_uint = u64; - -// PORT *must* match with "import m_float = fXX" in core::float per arch - -/* -Type: m_float - -Machine type equivalent of a float -*/ -type m_float = f64; - -/* -FIXME Type m_complex - -// PORT *must* match "import m_complex = ..." in core::complex per arch - -Machine type representing a complex value that uses floats for -both the real and the imaginary part. -*/ -// type m_complex = complex_c64::t; - -// -// Local Variables: -// mode: rust -// fill-column: 78; -// indent-tabs-mode: nil -// c-basic-offset: 4 -// buffer-file-coding-system: utf-8-unix -// End: -// diff --git a/src/test/stdtest/math.rs b/src/test/stdtest/math.rs index 51531f0863d6e..e221beae77292 100644 --- a/src/test/stdtest/math.rs +++ b/src/test/stdtest/math.rs @@ -18,6 +18,7 @@ fn test_max_min() { // FIXME use macros to execute the tests below for all float types +/* #[test] fn test_trig() { assert sin(0.0) == 0.0; @@ -297,4 +298,6 @@ fn test_log_functions() { assert ln1p(-1.0) == float::neg_infinity; assert float::isNaN(ln1p(-2.0f)); assert ln1p(float::infinity) == float::infinity; -} \ No newline at end of file +} + +*/ \ No newline at end of file From a72b1413edec32c3e9cd5b2ef5f12636a0b75a92 Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Fri, 23 Dec 2011 02:39:43 +0100 Subject: [PATCH 6/9] Removed now obsolete mtypes module --- src/libcore/core.rc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libcore/core.rc b/src/libcore/core.rc index e91c2a34ea0fa..6315d32b6d462 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -44,7 +44,6 @@ mod result; // Runtime and language-primitive support mod ctypes; -mod mtypes; mod cmath; mod sys; mod unsafe; From 16405386f0a843167e234d8d54855a537b0f261d Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Wed, 28 Dec 2011 02:20:14 +0100 Subject: [PATCH 7/9] core: added support for bessel functions --- src/libcore/bessel.rs | 10 ++++++ src/libcore/cmath.rs | 27 +++++++-------- src/libcore/core.rc | 3 +- src/libcore/f32.rs | 76 ++++++++++++++++++++++--------------------- src/libcore/f64.rs | 75 +++++++++++++++++++++--------------------- src/libcore/float.rs | 5 +-- 6 files changed, 104 insertions(+), 92 deletions(-) create mode 100644 src/libcore/bessel.rs diff --git a/src/libcore/bessel.rs b/src/libcore/bessel.rs new file mode 100644 index 0000000000000..85f8a8ed49cfc --- /dev/null +++ b/src/libcore/bessel.rs @@ -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::*; + diff --git a/src/libcore/cmath.rs b/src/libcore/cmath.rs index 73cb9af8bdbd7..dd922ddf1bcb6 100644 --- a/src/libcore/cmath.rs +++ b/src/libcore/cmath.rs @@ -1,6 +1,5 @@ export c_double; export c_float; -export bessel; import ctypes::c_int; import ctypes::c_float; @@ -57,6 +56,16 @@ native mod c_double { pure fn tanh(n: c_double) -> c_double; pure fn tgamma(n: c_double) -> c_double; pure fn trunc(n: c_double) -> c_double; + + // These are commonly only available for doubles + + pure fn j0(n: c_double) -> c_double; + pure fn j1(n: c_double) -> c_double; + pure fn jn(i: c_int, n: c_double) -> c_double; + + pure fn y0(n: c_double) -> c_double; + pure fn y1(n: c_double) -> c_double; + pure fn yn(i: c_int, n: c_double) -> c_double; } #[link_name = "m"] @@ -83,7 +92,7 @@ native mod c_float { #[link_name="fabsf"] pure fn abs(n: c_float) -> c_float; #[link_name="fdimf"] pure fn sub_pos(a: c_float, b: c_float) -> c_float; #[link_name="floorf"] pure fn floor(n: c_float) -> c_float; - #[link_name="frexpf"] pure fn frexp(n: c_double, + #[link_name="frexpf"] pure fn frexp(n: c_float, &value: c_int) -> c_float; #[link_name="fmaf"] pure fn mul_add(a: c_float, b: c_float, c: c_float) -> c_float; @@ -97,7 +106,7 @@ native mod c_float { &sign: c_int) -> c_float; #[link_name="logf"] pure fn ln(n: c_float) -> c_float; #[link_name="logbf"] pure fn logb(n: c_float) -> c_float; - #[link_name="log1p"] pure fn ln1p(n: c_double) -> c_double; + #[link_name="log1pf"] pure fn ln1p(n: c_float) -> c_float; #[link_name="log2f"] pure fn log2(n: c_float) -> c_float; #[link_name="log10f"] pure fn log10(n: c_float) -> c_float; #[link_name="ilogbf"] pure fn ilogb(n: c_float) -> c_int; @@ -116,18 +125,6 @@ native mod c_float { #[link_name="truncf"] pure fn trunc(n: c_float) -> c_float; } -#[link_name = "m"] -#[abi = "cdecl"] -native mod bessel { - pure fn j0(n: c_double) -> c_double; - pure fn j1(n: c_double) -> c_double; - pure fn jn(i: c_int, n: c_double) -> c_double; - - pure fn y0(n: c_double) -> c_double; - pure fn y1(n: c_double) -> c_double; - pure fn yn(i: c_int, n: c_double) -> c_double; -} - // // Local Variables: // mode: rust diff --git a/src/libcore/core.rc b/src/libcore/core.rc index 6315d32b6d462..e1e10789e4b04 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -7,7 +7,7 @@ #[license = "BSD"]; #[crate_type = "lib"]; -export box, char, float, f32, f64, int, str, ptr; +export box, char, float, bessel, f32, f64, int, str, ptr; export uint, u8, u32, u64, vec, bool; export either, option, result; export ctypes, sys, unsafe, comm, task; @@ -18,6 +18,7 @@ export extfmt; mod box; mod char; mod float; +mod bessel; mod f32; mod f64; mod int; diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index 192d3f14ea418..26ec20a2ed9fa 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -3,20 +3,43 @@ Module: f32 Floating point operations and constants for `f32` */ + // PORT import cmath::c_float::*; type t = f32; + +// These are not defined inside consts:: for consistency with +// the integer types + +// PORT check per architecture + +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.0f32/0.0f32; +const NaN: f32 = 0.0_f32/0.0_f32; /* Const: infinity */ -const infinity: f32 = 1.0f32/0.0f32; +const infinity: f32 = 1.0_f32/0.0_f32; /* Const: neg_infinity */ -const neg_infinity: f32 = -1.0f32/0.0f32; +const neg_infinity: f32 = -1.0_f32/0.0_f32; /* Predicate: isNaN */ pure fn isNaN(f: f32) -> bool { f != f } @@ -98,114 +121,93 @@ mod consts { Archimedes' constant */ - const pi: f32 = 3.14159265358979323846264338327950288f32; + const pi: f32 = 3.14159265358979323846264338327950288_f32; /* Const: frac_pi_2 pi/2.0 */ - const frac_pi_2: f32 = 1.57079632679489661923132169163975144f32; + const frac_pi_2: f32 = 1.57079632679489661923132169163975144_f32; /* Const: frac_pi_4 pi/4.0 */ - const frac_pi_4: f32 = 0.785398163397448309615660845819875721f32; + const frac_pi_4: f32 = 0.785398163397448309615660845819875721_f32; /* Const: frac_1_pi 1.0/pi */ - const frac_1_pi: f32 = 0.318309886183790671537767526745028724f32; + const frac_1_pi: f32 = 0.318309886183790671537767526745028724_f32; /* Const: frac_2_pi 2.0/pi */ - const frac_2_pi: f32 = 0.636619772367581343075535053490057448f32; + const frac_2_pi: f32 = 0.636619772367581343075535053490057448_f32; /* Const: frac_2_sqrtpi 2.0/sqrt(pi) */ - const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517f32; + const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517_f32; /* Const: sqrt2 sqrt(2.0) */ - const sqrt2: f32 = 1.41421356237309504880168872420969808f32; + const sqrt2: f32 = 1.41421356237309504880168872420969808_f32; /* Const: frac_1_sqrt2 1.0/sqrt(2.0) */ - const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039f32; + const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039_f32; /* Const: e Euler's number */ - const e: f32 = 2.71828182845904523536028747135266250f32; + const e: f32 = 2.71828182845904523536028747135266250_f32; /* Const: log2_e log2(e) */ - const log2_e: f32 = 1.44269504088896340735992468100189214f32; + const log2_e: f32 = 1.44269504088896340735992468100189214_f32; /* Const: log10_e log10(e) */ - const log10_e: f32 = 0.434294481903251827651128918916605082f32; + const log10_e: f32 = 0.434294481903251827651128918916605082_f32; /* Const: ln_2 ln(2.0) */ - const ln_2: f32 = 0.693147180559945309417232121458176568f32; + const ln_2: f32 = 0.693147180559945309417232121458176568_f32; /* Const: ln_10 ln(10.0) */ - const ln_10: f32 = 2.30258509299404568401799145468436421f32; + const ln_10: f32 = 2.30258509299404568401799145468436421_f32; } -// These are not defined inside consts:: for consistency with -// the integer types - -// PORT check per architecture - -const radix: uint = 2u; - -const mantissa_digits: uint = 24u; -const digits: uint = 6u; - -const epsilon: f32 = 1.19209290e-07f32; - -const min_value: f32 = 1.17549435e-38f32; -const max_value: f32 = 3.40282347e+38f32; - -const min_exp: int = -125; -const max_exp: int = 128; - -const min_10_exp: int = -37; -const max_10_exp: int = 38; - // // Local Variables: // mode: rust diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index 6413b5c5c1600..c7ee7e1a5dbfa 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -10,14 +10,36 @@ import cmath::c_double::*; type t = f64; + +// These are not defined inside consts:: for consistency with +// the integer types + +// PORT check per architecture + +const radix: uint = 2u; + +const mantissa_digits: uint = 53u; +const digits: uint = 15u; + +const epsilon: f64 = 2.2204460492503131e-16_f64; + +const min_value: f64 = 2.2250738585072014e-308_f64; +const max_value: f64 = 1.7976931348623157e+308_f64; + +const min_exp: int = -1021; +const max_exp: int = 1024; + +const min_10_exp: int = -307; +const max_10_exp: int = 308; + /* Const: NaN */ -const NaN: f64 = 0.0f64/0.0f64; +const NaN: f64 = 0.0_f64/0.0_f64; /* Const: infinity */ -const infinity: f64 = 1.0f64/0.0f64; +const infinity: f64 = 1.0_f64/0.0_f64; /* Const: neg_infinity */ -const neg_infinity: f64 = -1.0f64/0.0f64; +const neg_infinity: f64 = -1.0_f64/0.0_f64; /* Predicate: isNaN */ pure fn isNaN(f: f64) -> bool { f != f } @@ -99,114 +121,93 @@ mod consts { Archimedes' constant */ - const pi: f64 = 3.14159265358979323846264338327950288f64; + const pi: f64 = 3.14159265358979323846264338327950288_f64; /* Const: frac_pi_2 pi/2.0 */ - const frac_pi_2: f64 = 1.57079632679489661923132169163975144f64; + const frac_pi_2: f64 = 1.57079632679489661923132169163975144_f64; /* Const: frac_pi_4 pi/4.0 */ - const frac_pi_4: f64 = 0.785398163397448309615660845819875721f64; + const frac_pi_4: f64 = 0.785398163397448309615660845819875721_f64; /* Const: frac_1_pi 1.0/pi */ - const frac_1_pi: f64 = 0.318309886183790671537767526745028724f64; + const frac_1_pi: f64 = 0.318309886183790671537767526745028724_f64; /* Const: frac_2_pi 2.0/pi */ - const frac_2_pi: f64 = 0.636619772367581343075535053490057448f64; + const frac_2_pi: f64 = 0.636619772367581343075535053490057448_f64; /* Const: frac_2_sqrtpi 2.0/sqrt(pi) */ - const frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517f64; + const frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517_f64; /* Const: sqrt2 sqrt(2.0) */ - const sqrt2: f64 = 1.41421356237309504880168872420969808f64; + const sqrt2: f64 = 1.41421356237309504880168872420969808_f64; /* Const: frac_1_sqrt2 1.0/sqrt(2.0) */ - const frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039f64; + const frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039_f64; /* Const: e Euler's number */ - const e: f64 = 2.71828182845904523536028747135266250f64; + const e: f64 = 2.71828182845904523536028747135266250_f64; /* Const: log2_e log2(e) */ - const log2_e: f64 = 1.44269504088896340735992468100189214f64; + const log2_e: f64 = 1.44269504088896340735992468100189214_f64; /* Const: log10_e log10(e) */ - const log10_e: f64 = 0.434294481903251827651128918916605082f64; + const log10_e: f64 = 0.434294481903251827651128918916605082_f64; /* Const: ln_2 ln(2.0) */ - const ln_2: f64 = 0.693147180559945309417232121458176568f64; + const ln_2: f64 = 0.693147180559945309417232121458176568_f64; /* Const: ln_10 ln(10.0) */ - const ln_10: f64 = 2.30258509299404568401799145468436421f64; + const ln_10: f64 = 2.30258509299404568401799145468436421_f64; } -// These are not defined inside consts:: for consistency with -// the integer types - -// PORT check per architecture - -const radix: uint = 2u; - -const mantissa_digits: uint = 53u; -const digits: uint = 15u; - -const epsilon: f64 = 2.2204460492503131e-16f64; - -const min_value: f64 = 2.2250738585072014e-308f64; -const max_value: f64 = 1.7976931348623157e+308f64; - -const min_exp: int = -1021; -const max_exp: int = 1024; - -const min_10_exp: int = -307; -const max_10_exp: int = 308; - // // Local Variables: // mode: rust diff --git a/src/libcore/float.rs b/src/libcore/float.rs index b482f169fc200..cf8ea28a00b66 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -2,7 +2,7 @@ Module: float */ -// PORT This must match in width according to architecture +// PORT this must match in width according to architecture import m_float = f64; import m_float::*; @@ -25,11 +25,12 @@ digits - The number of significant digits exact - Whether to enforce the exact number of significant digits */ fn to_str_common(num: float, digits: uint, exact: bool) -> str { + if isNaN(num) { ret "NaN"; } let (num, accum) = num < 0.0 ? (-num, "-") : (num, ""); let trunc = num as uint; let frac = num - (trunc as float); accum += uint::str(trunc); - if frac == 0.0 || digits == 0u { ret accum; } + if frac < epsilon || digits == 0u { ret accum; } accum += "."; let i = digits; let epsilon = 1. / pow_uint_to_uint_as_float(10u, i); From a16acc0c105c438a5cf1500eb4c4a5c4388c2dca Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Thu, 5 Jan 2012 03:14:47 +0100 Subject: [PATCH 8/9] added preliminary support for per-architecture generation of math constants (for porting) --- src/etc/cmathconsts.c | 92 +++++++++++++++++++++++++++++++++++++ src/libcore/cmath.rs | 103 +++++++++++++++++++++++++++++++++++++++++- src/libcore/f32.rs | 32 +++++-------- src/libcore/f64.rs | 12 ++++- 4 files changed, 216 insertions(+), 23 deletions(-) create mode 100644 src/etc/cmathconsts.c diff --git a/src/etc/cmathconsts.c b/src/etc/cmathconsts.c new file mode 100644 index 0000000000000..5c32594912a71 --- /dev/null +++ b/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 +// and ensure that libcore/cmath.rs complies to the output +// +// Requires a printf that supports "%a" specifiers +// + +#include +#include +#include + +// 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 \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; +} diff --git a/src/libcore/cmath.rs b/src/libcore/cmath.rs index 4f3c5a8d68b88..3eea7147ea922 100644 --- a/src/libcore/cmath.rs +++ b/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; @@ -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 diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index 7c7d45694e50b..421398d20fd74 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -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; @@ -150,6 +130,8 @@ pure fn is_finite(x: f32) -> bool { /* Module: consts */ mod consts { + // FIXME replace with mathematical constants from cmath + /* Const: pi @@ -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); } diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index 058d662010604..245d7c26478e6 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -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 @@ -147,6 +147,8 @@ pure fn is_finite(x: f64) -> bool { /* Module: consts */ mod consts { + // FIXME replace with mathematical constants from cmath + /* Const: pi @@ -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); } From 1f10ee365c840cb5d54fe4eb32659f09d29ace38 Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Thu, 5 Jan 2012 17:11:34 +0100 Subject: [PATCH 9/9] Moved generic float::min, max to core::math and cleaned up some imports --- src/comp/back/rpath.rs | 2 +- src/comp/middle/ty.rs | 2 +- src/comp/util/common.rs | 3 +-- src/fuzzer/fuzzer.rs | 5 ++--- src/libcore/core.rc | 2 ++ src/libcore/f32.rs | 6 ++++++ src/libcore/f64.rs | 6 ++++++ src/libcore/float.rs | 17 ++--------------- src/libcore/math.rs | 18 ++++++++++++++++++ src/libstd/rope.rs | 6 +----- src/test/stdtest/math.rs | 1 + 11 files changed, 41 insertions(+), 27 deletions(-) create mode 100644 src/libcore/math.rs diff --git a/src/comp/back/rpath.rs b/src/comp/back/rpath.rs index fccacdcba801b..cb00fd080027a 100644 --- a/src/comp/back/rpath.rs +++ b/src/comp/back/rpath.rs @@ -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] { diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs index 76a3591cd1838..1466d4cf0302d 100644 --- a/src/comp/middle/ty.rs +++ b/src/comp/middle/ty.rs @@ -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); diff --git a/src/comp/util/common.rs b/src/comp/util/common.rs index 03a6136656a09..ba2d39ba2ec61 100644 --- a/src/comp/util/common.rs +++ b/src/comp/util/common.rs @@ -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; diff --git a/src/fuzzer/fuzzer.rs b/src/fuzzer/fuzzer.rs index 91cb090b83cd8..515ddba7d35fc 100644 --- a/src/fuzzer/fuzzer.rs +++ b/src/fuzzer/fuzzer.rs @@ -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}; @@ -241,9 +240,9 @@ fn check_variants_T( 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 diff --git a/src/libcore/core.rc b/src/libcore/core.rc index e1e10789e4b04..d1732352d9674 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -12,6 +12,7 @@ export uint, u8, u32, u64, vec, bool; export either, option, result; export ctypes, sys, unsafe, comm, task; export extfmt; +export math; // Built-in-type support modules @@ -45,6 +46,7 @@ mod result; // Runtime and language-primitive support mod ctypes; +mod math; mod cmath; mod sys; mod unsafe; diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index 3948dc9c9d42e..c59dd252fff2b 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -224,10 +224,16 @@ mod consts { const ln_10: f32 = 2.30258509299404568401799145468436421_f32; } +pure fn signbit(x: f32) -> int { + if is_negative(x) { ret 1; } else { ret 0; } +} + #[cfg(target_os="linux")] #[cfg(target_os="macos")] #[cfg(target_os="win32")] pure fn logarithm(n: f32, b: f32) -> f32 { + // FIXME check if it is good to use log2 instead of ln here; + // in theory should be faster since the radix is 2 ret log2(n) / log2(b); } diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index eb8714dcb70db..e6813f1429de1 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -241,10 +241,16 @@ mod consts { const ln_10: f64 = 2.30258509299404568401799145468436421_f64; } +pure fn signbit(x: f64) -> int { + if is_negative(x) { ret 1; } else { ret 0; } +} + #[cfg(target_os="linux")] #[cfg(target_os="macos")] #[cfg(target_os="win32")] pure fn logarithm(n: f64, b: f64) -> f64 { + // FIXME check if it is good to use log2 instead of ln here; + // in theory should be faster since the radix is 2 ret log2(n) / log2(b); } diff --git a/src/libcore/float.rs b/src/libcore/float.rs index 28ffc72f82b14..17d5fb1f7f63f 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -4,7 +4,7 @@ Module: float // FIXME find out why these have to be exported explicitly -export to_str_common, to_str_exact, to_str, from_str, min, max; +export to_str_common, to_str_exact, to_str, from_str; export add, sub, mul, div, rem, lt, le, gt, eq, eq, ne; export is_positive, is_negative, is_nonpositive, is_nonnegative; export is_zero, is_infinite, is_finite; @@ -16,6 +16,7 @@ export erf, erfc, exp, expm1, exp2, abs, abs_sub; export mul_add, fmax, fmin, nextafter, frexp, hypot, ldexp; export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix; export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc; +export signbit; // export when m_float == c_double @@ -280,20 +281,6 @@ fn pow_uint_to_uint_as_float(x: uint, pow: uint) -> float { } -/* -Function: min - -Returns the minimum of two values -*/ -pure fn min(x: T, y: T) -> T { x < y ? x : y } - -/* -Function: max - -Returns the maximum of two values -*/ -pure fn max(x: T, y: T) -> T { x < y ? y : x } - // // Local Variables: // mode: rust diff --git a/src/libcore/math.rs b/src/libcore/math.rs new file mode 100644 index 0000000000000..cfd1ec4ca4ca2 --- /dev/null +++ b/src/libcore/math.rs @@ -0,0 +1,18 @@ +// Generic functions that have been defined for all numeric types +// +// (may very well go away again soon) + +/* +Function: min + +Returns the minimum of two values +*/ +pure fn min(x: T, y: T) -> T { x < y ? x : y } + +/* +Function: max + +Returns the maximum of two values +*/ +pure fn max(x: T, y: T) -> T { x < y ? y : x } + diff --git a/src/libstd/rope.rs b/src/libstd/rope.rs index 6b821dc622318..8071ee7284760 100644 --- a/src/libstd/rope.rs +++ b/src/libstd/rope.rs @@ -26,10 +26,6 @@ The following operations are algorithmically faster in ropes: */ -import core::option; - - - /* Type: rope @@ -1103,7 +1099,7 @@ mod node { right : right, char_len: char_len(left) + char_len(right), byte_len: byte_len(left) + byte_len(right), - height: float::max(height(left), height(right)) + 1u + height: math::max(height(left), height(right)) + 1u }) } diff --git a/src/test/stdtest/math.rs b/src/test/stdtest/math.rs index e221beae77292..c8335718da9af 100644 --- a/src/test/stdtest/math.rs +++ b/src/test/stdtest/math.rs @@ -2,6 +2,7 @@ import core::*; use std; +import math::{min, max}; import float::*; import float; import c_int = ctypes::c_int;