Skip to content

floating point cleanups #12700

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions src/libstd/num/cmath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,15 @@ pub mod c_double {
pub fn atan(n: c_double) -> c_double;
pub fn atan2(a: c_double, b: c_double) -> c_double;
pub fn cbrt(n: c_double) -> c_double;
pub fn ceil(n: c_double) -> c_double;
pub fn copysign(x: c_double, y: c_double) -> c_double;
pub fn cos(n: c_double) -> c_double;
pub fn cosh(n: c_double) -> c_double;
pub fn erf(n: c_double) -> c_double;
pub fn erfc(n: c_double) -> c_double;
pub fn exp(n: c_double) -> c_double;
// rename: for consistency with underscore usage elsewhere
#[link_name="expm1"]
pub fn exp_m1(n: c_double) -> c_double;
pub fn exp2(n: c_double) -> c_double;
#[link_name="fabs"]
pub fn abs(n: c_double) -> c_double;
// rename: for clarity and consistency with add/sub/mul/div
#[link_name="fdim"]
pub fn abs_sub(a: c_double, b: c_double) -> c_double;
pub fn floor(n: c_double) -> c_double;
// rename: for clarity and consistency with add/sub/mul/div
#[link_name="fma"]
pub fn mul_add(a: c_double, b: c_double, c: c_double) -> c_double;
#[link_name="fmax"]
pub fn fmax(a: c_double, b: c_double) -> c_double;
#[link_name="fmin"]
Expand All @@ -63,34 +52,22 @@ pub mod c_double {
#[cfg(windows)]
#[link_name="__lgamma_r"]
pub fn lgamma(n: c_double, sign: &mut c_int) -> c_double;
// renamed: ln seems more natural
#[link_name="log"]
pub fn ln(n: c_double) -> c_double;
// renamed: "logb" /often/ is confused for log2 by beginners
#[link_name="logb"]
pub fn log_radix(n: c_double) -> c_double;
// renamed: to be consitent with log as ln
#[link_name="log1p"]
pub fn ln_1p(n: c_double) -> c_double;
pub fn log10(n: c_double) -> c_double;
pub fn log2(n: c_double) -> c_double;
#[link_name="ilogb"]
pub fn ilog_radix(n: c_double) -> c_int;
pub fn modf(n: c_double, iptr: &mut c_double) -> c_double;
pub fn pow(n: c_double, e: c_double) -> c_double;
// FIXME (#1379): enable when rounding modes become available
// fn rint(n: c_double) -> c_double;
pub fn round(n: c_double) -> c_double;
// rename: for consistency with logradix
#[link_name="scalbn"]
pub fn ldexp_radix(n: c_double, i: c_int) -> c_double;
pub fn sin(n: c_double) -> c_double;
pub fn sinh(n: c_double) -> c_double;
pub fn sqrt(n: c_double) -> c_double;
pub fn tan(n: c_double) -> c_double;
pub fn tanh(n: c_double) -> c_double;
pub fn tgamma(n: c_double) -> c_double;
pub fn trunc(n: c_double) -> c_double;

// These are commonly only available for doubles

Expand Down Expand Up @@ -121,34 +98,18 @@ pub mod c_float {
pub fn atan2(a: c_float, b: c_float) -> c_float;
#[link_name="cbrtf"]
pub fn cbrt(n: c_float) -> c_float;
#[link_name="ceilf"]
pub fn ceil(n: c_float) -> c_float;
#[link_name="copysignf"]
pub fn copysign(x: c_float, y: c_float) -> c_float;
#[link_name="cosf"]
pub fn cos(n: c_float) -> c_float;
#[link_name="coshf"]
pub fn cosh(n: c_float) -> c_float;
#[link_name="erff"]
pub fn erf(n: c_float) -> c_float;
#[link_name="erfcf"]
pub fn erfc(n: c_float) -> c_float;
#[link_name="expf"]
pub fn exp(n: c_float) -> c_float;
#[link_name="expm1f"]
pub fn exp_m1(n: c_float) -> c_float;
#[link_name="exp2f"]
pub fn exp2(n: c_float) -> c_float;
#[link_name="fabsf"]
pub fn abs(n: c_float) -> c_float;
#[link_name="fdimf"]
pub fn abs_sub(a: c_float, b: c_float) -> c_float;
#[link_name="floorf"]
pub fn floor(n: c_float) -> c_float;
#[link_name="frexpf"]
pub fn frexp(n: c_float, value: &mut c_int) -> c_float;
#[link_name="fmaf"]
pub fn mul_add(a: c_float, b: c_float, c: c_float) -> c_float;
#[link_name="fmaxf"]
pub fn fmax(a: c_float, b: c_float) -> c_float;
#[link_name="fminf"]
Expand All @@ -168,41 +129,23 @@ pub mod c_float {
#[link_name="__lgammaf_r"]
pub fn lgamma(n: c_float, sign: &mut c_int) -> c_float;

#[link_name="logf"]
pub fn ln(n: c_float) -> c_float;
#[link_name="logbf"]
pub fn log_radix(n: c_float) -> c_float;
#[link_name="log1pf"]
pub fn ln_1p(n: c_float) -> c_float;
#[link_name="log2f"]
pub fn log2(n: c_float) -> c_float;
#[link_name="log10f"]
pub fn log10(n: c_float) -> c_float;
#[link_name="ilogbf"]
pub fn ilog_radix(n: c_float) -> c_int;
#[link_name="modff"]
pub fn modf(n: c_float, iptr: &mut c_float) -> c_float;
#[link_name="powf"]
pub fn pow(n: c_float, e: c_float) -> c_float;
// FIXME (#1379): enable when rounding modes become available
// #[link_name="rintf"] fn rint(n: c_float) -> c_float;
#[link_name="roundf"]
pub fn round(n: c_float) -> c_float;
#[link_name="scalbnf"]
pub fn ldexp_radix(n: c_float, i: c_int) -> c_float;
#[link_name="sinf"]
pub fn sin(n: c_float) -> c_float;
#[link_name="sinhf"]
pub fn sinh(n: c_float) -> c_float;
#[link_name="sqrtf"]
pub fn sqrt(n: c_float) -> c_float;
#[link_name="tanf"]
pub fn tan(n: c_float) -> c_float;
#[link_name="tanhf"]
pub fn tanh(n: c_float) -> c_float;
#[link_name="tgammaf"]
pub fn tgamma(n: c_float) -> c_float;
#[link_name="truncf"]
pub fn trunc(n: c_float) -> c_float;
}
}
33 changes: 19 additions & 14 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

//! Operations and constants for 32-bits floats (`f32` type)

#[allow(missing_doc)];

use prelude::*;
Expand Down Expand Up @@ -45,37 +46,32 @@ macro_rules! delegate(

delegate!(
// intrinsics
fn abs(n: f32) -> f32 = intrinsics::fabsf32,
fn sqrt(n: f32) -> f32 = intrinsics::sqrtf32,
fn powi(n: f32, e: i32) -> f32 = intrinsics::powif32,
fn sin(n: f32) -> f32 = intrinsics::sinf32,
fn cos(n: f32) -> f32 = intrinsics::cosf32,
fn pow(n: f32, e: f32) -> f32 = intrinsics::powf32,
fn exp(n: f32) -> f32 = intrinsics::expf32,
fn exp2(n: f32) -> f32 = intrinsics::exp2f32,
fn floor(x: f32) -> f32 = intrinsics::floorf32,
fn ln(n: f32) -> f32 = intrinsics::logf32,
fn log10(n: f32) -> f32 = intrinsics::log10f32,
fn log2(n: f32) -> f32 = intrinsics::log2f32,
fn mul_add(a: f32, b: f32, c: f32) -> f32 = intrinsics::fmaf32,
fn pow(n: f32, e: f32) -> f32 = intrinsics::powf32,
// fn powi(n: f32, e: c_int) -> f32 = intrinsics::powif32,
fn sin(n: f32) -> f32 = intrinsics::sinf32,
fn sqrt(n: f32) -> f32 = intrinsics::sqrtf32,

// LLVM 3.3 required to use intrinsics for these four
fn ceil(n: c_float) -> c_float = cmath::c_float::ceil,
fn trunc(n: c_float) -> c_float = cmath::c_float::trunc,
/*
fn abs(n: f32) -> f32 = intrinsics::fabsf32,
fn copysign(x: f32, y: f32) -> f32 = intrinsics::copysignf32,
fn floor(x: f32) -> f32 = intrinsics::floorf32,
fn ceil(n: f32) -> f32 = intrinsics::ceilf32,
fn trunc(n: f32) -> f32 = intrinsics::truncf32,
fn rint(n: f32) -> f32 = intrinsics::rintf32,
fn nearbyint(n: f32) -> f32 = intrinsics::nearbyintf32,
*/
fn round(n: f32) -> f32 = intrinsics::roundf32,

// cmath
fn acos(n: c_float) -> c_float = cmath::c_float::acos,
fn asin(n: c_float) -> c_float = cmath::c_float::asin,
fn atan(n: c_float) -> c_float = cmath::c_float::atan,
fn atan2(a: c_float, b: c_float) -> c_float = cmath::c_float::atan2,
fn cbrt(n: c_float) -> c_float = cmath::c_float::cbrt,
fn copysign(x: c_float, y: c_float) -> c_float = cmath::c_float::copysign,
fn cosh(n: c_float) -> c_float = cmath::c_float::cosh,
// fn erf(n: c_float) -> c_float = cmath::c_float::erf,
// fn erfc(n: c_float) -> c_float = cmath::c_float::erfc,
Expand All @@ -89,7 +85,6 @@ delegate!(
fn ln_1p(n: c_float) -> c_float = cmath::c_float::ln_1p,
// fn ilog_radix(n: c_float) -> c_int = cmath::c_float::ilog_radix,
// fn modf(n: c_float, iptr: &mut c_float) -> c_float = cmath::c_float::modf,
fn round(n: c_float) -> c_float = cmath::c_float::round,
// fn ldexp_radix(n: c_float, i: c_int) -> c_float = cmath::c_float::ldexp_radix,
fn sinh(n: c_float) -> c_float = cmath::c_float::sinh,
fn tan(n: c_float) -> c_float = cmath::c_float::tan,
Expand Down Expand Up @@ -311,6 +306,16 @@ impl Bounded for f32 {
impl Primitive for f32 {}

impl Float for f32 {
#[inline]
fn max(self, other: f32) -> f32 {
unsafe { cmath::c_float::fmax(self, other) }
}

#[inline]
fn min(self, other: f32) -> f32 {
unsafe { cmath::c_float::fmin(self, other) }
}

#[inline]
fn nan() -> f32 { 0.0 / 0.0 }

Expand Down
38 changes: 20 additions & 18 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use num::{Zero, One, Bounded, strconv};
use num;
use intrinsics;

pub use cmp::{min, max};

macro_rules! delegate(
(
$(
Expand All @@ -48,37 +46,32 @@ macro_rules! delegate(

delegate!(
// intrinsics
fn abs(n: f64) -> f64 = intrinsics::fabsf64,
fn sqrt(n: f64) -> f64 = intrinsics::sqrtf64,
fn powi(n: f64, e: i32) -> f64 = intrinsics::powif64,
fn sin(n: f64) -> f64 = intrinsics::sinf64,
fn cos(n: f64) -> f64 = intrinsics::cosf64,
fn pow(n: f64, e: f64) -> f64 = intrinsics::powf64,
fn exp(n: f64) -> f64 = intrinsics::expf64,
fn exp2(n: f64) -> f64 = intrinsics::exp2f64,
fn floor(x: f64) -> f64 = intrinsics::floorf64,
fn ln(n: f64) -> f64 = intrinsics::logf64,
fn log10(n: f64) -> f64 = intrinsics::log10f64,
fn log2(n: f64) -> f64 = intrinsics::log2f64,
fn mul_add(a: f64, b: f64, c: f64) -> f64 = intrinsics::fmaf64,
fn pow(n: f64, e: f64) -> f64 = intrinsics::powf64,
// fn powi(n: f64, e: c_int) -> f64 = intrinsics::powif64,
fn sin(n: f64) -> f64 = intrinsics::sinf64,
fn sqrt(n: f64) -> f64 = intrinsics::sqrtf64,

// LLVM 3.3 required to use intrinsics for these four
fn ceil(n: c_double) -> c_double = cmath::c_double::ceil,
fn trunc(n: c_double) -> c_double = cmath::c_double::trunc,
/*
fn abs(n: f64) -> f64 = intrinsics::fabsf64,
fn copysign(x: f64, y: f64) -> f64 = intrinsics::copysignf64,
fn floor(x: f64) -> f64 = intrinsics::floorf64,
fn ceil(n: f64) -> f64 = intrinsics::ceilf64,
fn trunc(n: f64) -> f64 = intrinsics::truncf64,
fn rint(n: c_double) -> c_double = intrinsics::rintf64,
fn nearbyint(n: c_double) -> c_double = intrinsics::nearbyintf64,
*/
fn rint(n: f64) -> f64 = intrinsics::rintf64,
fn nearbyint(n: f64) -> f64 = intrinsics::nearbyintf64,
fn round(n: f64) -> f64 = intrinsics::roundf64,

// cmath
fn acos(n: c_double) -> c_double = cmath::c_double::acos,
fn asin(n: c_double) -> c_double = cmath::c_double::asin,
fn atan(n: c_double) -> c_double = cmath::c_double::atan,
fn atan2(a: c_double, b: c_double) -> c_double = cmath::c_double::atan2,
fn cbrt(n: c_double) -> c_double = cmath::c_double::cbrt,
fn copysign(x: c_double, y: c_double) -> c_double = cmath::c_double::copysign,
fn cosh(n: c_double) -> c_double = cmath::c_double::cosh,
// fn erf(n: c_double) -> c_double = cmath::c_double::erf,
// fn erfc(n: c_double) -> c_double = cmath::c_double::erfc,
Expand All @@ -92,7 +85,6 @@ delegate!(
fn ln_1p(n: c_double) -> c_double = cmath::c_double::ln_1p,
// fn ilog_radix(n: c_double) -> c_int = cmath::c_double::ilog_radix,
// fn modf(n: c_double, iptr: &mut c_double) -> c_double = cmath::c_double::modf,
fn round(n: c_double) -> c_double = cmath::c_double::round,
// fn ldexp_radix(n: c_double, i: c_int) -> c_double = cmath::c_double::ldexp_radix,
fn sinh(n: c_double) -> c_double = cmath::c_double::sinh,
fn tan(n: c_double) -> c_double = cmath::c_double::tan,
Expand Down Expand Up @@ -313,6 +305,16 @@ impl Bounded for f64 {
impl Primitive for f64 {}

impl Float for f64 {
#[inline]
fn max(self, other: f64) -> f64 {
unsafe { cmath::c_double::fmax(self, other) }
}

#[inline]
fn min(self, other: f64) -> f64 {
unsafe { cmath::c_double::fmin(self, other) }
}

#[inline]
fn nan() -> f64 { 0.0 / 0.0 }

Expand Down
3 changes: 3 additions & 0 deletions src/libstd/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ pub enum FPCategory {
pub trait Float: Signed
+ Round
+ Primitive {
fn max(self, other: Self) -> Self;
fn min(self, other: Self) -> Self;

// FIXME (#5527): These should be associated constants
fn nan() -> Self;
fn infinity() -> Self;
Expand Down
2 changes: 1 addition & 1 deletion src/libtest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ impl MetricMap {
Some(v) => {
let delta = v.value - vold.value;
let noise = match noise_pct {
None => f64::max(vold.noise.abs(), v.noise.abs()),
None => vold.noise.abs().max(v.noise.abs()),
Some(pct) => vold.value * pct / 100.0
};
if delta.abs() <= noise {
Expand Down