From 1f10ee365c840cb5d54fe4eb32659f09d29ace38 Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Thu, 5 Jan 2012 17:11:34 +0100 Subject: [PATCH] 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 fccacdcba80..cb00fd08002 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 76a3591cd18..1466d4cf030 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 03a6136656a..ba2d39ba2ec 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 91cb090b83c..515ddba7d35 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 e1e10789e4b..d1732352d96 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 3948dc9c9d4..c59dd252fff 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 eb8714dcb70..e6813f1429d 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 28ffc72f82b..17d5fb1f7f6 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 00000000000..cfd1ec4ca4c --- /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 6b821dc6223..8071ee72847 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 e221beae772..c8335718da9 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;