Skip to content

Commit

Permalink
Fix formatting in float implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Apr 19, 2014
1 parent bed70a4 commit 42450ef
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 72 deletions.
135 changes: 99 additions & 36 deletions src/libstd/num/f32.rs
Expand Up @@ -14,6 +14,7 @@

use prelude::*;

use cast;
use default::Default;
use from_str::FromStr;
use libc::{c_int};
Expand Down Expand Up @@ -213,12 +214,16 @@ impl Neg<f32> for f32 {
impl Signed for f32 {
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
#[inline]
fn abs(&self) -> f32 { unsafe{intrinsics::fabsf32(*self)} }
fn abs(&self) -> f32 {
unsafe { intrinsics::fabsf32(*self) }
}

/// The positive difference of two numbers. Returns `0.0` if the number is less than or
/// equal to `other`, otherwise the difference between`self` and `other` is returned.
#[inline]
fn abs_sub(&self, other: &f32) -> f32 { unsafe{cmath::fdimf(*self, *other)} }
fn abs_sub(&self, other: &f32) -> f32 {
unsafe { cmath::fdimf(*self, *other) }
}

/// # Returns
///
Expand All @@ -227,7 +232,9 @@ impl Signed for f32 {
/// - `NAN` if the number is NaN
#[inline]
fn signum(&self) -> f32 {
if self.is_nan() { NAN } else { unsafe{intrinsics::copysignf32(1.0, *self)} }
if self.is_nan() { NAN } else {
unsafe { intrinsics::copysignf32(1.0, *self) }
}
}

/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
Expand All @@ -250,7 +257,9 @@ impl Bounded for f32 {
impl Primitive for f32 {}

impl Float for f32 {
fn powi(self, n: i32) -> f32 { unsafe{intrinsics::powif32(self, n)} }
fn powi(self, n: i32) -> f32 {
unsafe { intrinsics::powif32(self, n) }
}

#[inline]
fn max(self, other: f32) -> f32 {
Expand Down Expand Up @@ -302,7 +311,7 @@ impl Float for f32 {
static EXP_MASK: u32 = 0x7f800000;
static MAN_MASK: u32 = 0x007fffff;

let bits: u32 = unsafe {::cast::transmute(self)};
let bits: u32 = unsafe { cast::transmute(self) };
match (bits & MAN_MASK, bits & EXP_MASK) {
(0, 0) => FPZero,
(_, 0) => FPSubnormal,
Expand Down Expand Up @@ -335,7 +344,9 @@ impl Float for f32 {

/// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp`
#[inline]
fn ldexp(x: f32, exp: int) -> f32 { unsafe{cmath::ldexpf(x, exp as c_int)} }
fn ldexp(x: f32, exp: int) -> f32 {
unsafe { cmath::ldexpf(x, exp as c_int) }
}

/// Breaks the number into a normalized fraction and a base-2 exponent, satisfying:
///
Expand All @@ -353,28 +364,34 @@ impl Float for f32 {
/// Returns the exponential of the number, minus `1`, in a way that is accurate
/// even if the number is close to zero
#[inline]
fn exp_m1(self) -> f32 { unsafe{cmath::expm1f(self)} }
fn exp_m1(self) -> f32 {
unsafe { cmath::expm1f(self) }
}

/// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more accurately
/// than if the operations were performed separately
#[inline]
fn ln_1p(self) -> f32 { unsafe{cmath::log1pf(self)} }
fn ln_1p(self) -> f32 {
unsafe { cmath::log1pf(self) }
}

/// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
/// produces a more accurate result with better performance than a separate multiplication
/// operation followed by an add.
#[inline]
fn mul_add(self, a: f32, b: f32) -> f32 { unsafe{intrinsics::fmaf32(self, a, b)} }
fn mul_add(self, a: f32, b: f32) -> f32 {
unsafe { intrinsics::fmaf32(self, a, b) }
}

/// Returns the next representable floating-point value in the direction of `other`
#[inline]
fn next_after(self, other: f32) -> f32 { unsafe{cmath::nextafterf(self, other)} }
fn next_after(self, other: f32) -> f32 {
unsafe { cmath::nextafterf(self, other) }
}

/// Returns the mantissa, exponent and sign as integers.
fn integer_decode(self) -> (u64, i16, i8) {
let bits: u32 = unsafe {
::cast::transmute(self)
};
let bits: u32 = unsafe { cast::transmute(self) };
let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
let mantissa = if exponent == 0 {
Expand All @@ -389,19 +406,27 @@ impl Float for f32 {

/// Round half-way cases toward `NEG_INFINITY`
#[inline]
fn floor(self) -> f32 { unsafe{intrinsics::floorf32(self)} }
fn floor(self) -> f32 {
unsafe { intrinsics::floorf32(self) }
}

/// Round half-way cases toward `INFINITY`
#[inline]
fn ceil(self) -> f32 { unsafe{intrinsics::ceilf32(self)} }
fn ceil(self) -> f32 {
unsafe { intrinsics::ceilf32(self) }
}

/// Round half-way cases away from `0.0`
#[inline]
fn round(self) -> f32 { unsafe{intrinsics::roundf32(self)} }
fn round(self) -> f32 {
unsafe { intrinsics::roundf32(self) }
}

/// The integer part of the number (rounds towards `0.0`)
#[inline]
fn trunc(self) -> f32 { unsafe{intrinsics::truncf32(self)} }
fn trunc(self) -> f32 {
unsafe { intrinsics::truncf32(self) }
}

/// The fractional part of the number, satisfying:
///
Expand Down Expand Up @@ -485,40 +510,62 @@ impl Float for f32 {
fn recip(self) -> f32 { 1.0 / self }

#[inline]
fn powf(self, n: f32) -> f32 { unsafe{intrinsics::powf32(self, n)} }
fn powf(self, n: f32) -> f32 {
unsafe { intrinsics::powf32(self, n) }
}

#[inline]
fn sqrt(self) -> f32 { unsafe{intrinsics::sqrtf32(self)} }
fn sqrt(self) -> f32 {
unsafe { intrinsics::sqrtf32(self) }
}

#[inline]
fn rsqrt(self) -> f32 { self.sqrt().recip() }

#[inline]
fn cbrt(self) -> f32 { unsafe{cmath::cbrtf(self)} }
fn cbrt(self) -> f32 {
unsafe { cmath::cbrtf(self) }
}

#[inline]
fn hypot(self, other: f32) -> f32 { unsafe{cmath::hypotf(self, other)} }
fn hypot(self, other: f32) -> f32 {
unsafe { cmath::hypotf(self, other) }
}

#[inline]
fn sin(self) -> f32 { unsafe{intrinsics::sinf32(self)} }
fn sin(self) -> f32 {
unsafe { intrinsics::sinf32(self) }
}

#[inline]
fn cos(self) -> f32 { unsafe{intrinsics::cosf32(self)} }
fn cos(self) -> f32 {
unsafe { intrinsics::cosf32(self) }
}

#[inline]
fn tan(self) -> f32 { unsafe{cmath::tanf(self)} }
fn tan(self) -> f32 {
unsafe { cmath::tanf(self) }
}

#[inline]
fn asin(self) -> f32 { unsafe{cmath::asinf(self)} }
fn asin(self) -> f32 {
unsafe { cmath::asinf(self) }
}

#[inline]
fn acos(self) -> f32 { unsafe{cmath::acosf(self)} }
fn acos(self) -> f32 {
unsafe { cmath::acosf(self) }
}

#[inline]
fn atan(self) -> f32 { unsafe{cmath::atanf(self)} }
fn atan(self) -> f32 {
unsafe { cmath::atanf(self) }
}

#[inline]
fn atan2(self, other: f32) -> f32 { unsafe{cmath::atan2f(self, other)} }
fn atan2(self, other: f32) -> f32 {
unsafe { cmath::atan2f(self, other) }
}

/// Simultaneously computes the sine and cosine of the number
#[inline]
Expand All @@ -528,36 +575,52 @@ impl Float for f32 {

/// Returns the exponential of the number
#[inline]
fn exp(self) -> f32 { unsafe{intrinsics::expf32(self)} }
fn exp(self) -> f32 {
unsafe { intrinsics::expf32(self) }
}

/// Returns 2 raised to the power of the number
#[inline]
fn exp2(self) -> f32 { unsafe{intrinsics::exp2f32(self)} }
fn exp2(self) -> f32 {
unsafe { intrinsics::exp2f32(self) }
}

/// Returns the natural logarithm of the number
#[inline]
fn ln(self) -> f32 { unsafe{intrinsics::logf32(self)} }
fn ln(self) -> f32 {
unsafe { intrinsics::logf32(self) }
}

/// Returns the logarithm of the number with respect to an arbitrary base
#[inline]
fn log(self, base: f32) -> f32 { self.ln() / base.ln() }

/// Returns the base 2 logarithm of the number
#[inline]
fn log2(self) -> f32 { unsafe{intrinsics::log2f32(self)} }
fn log2(self) -> f32 {
unsafe { intrinsics::log2f32(self) }
}

/// Returns the base 10 logarithm of the number
#[inline]
fn log10(self) -> f32 { unsafe{intrinsics::log10f32(self)} }
fn log10(self) -> f32 {
unsafe { intrinsics::log10f32(self) }
}

#[inline]
fn sinh(self) -> f32 { unsafe{cmath::sinhf(self)} }
fn sinh(self) -> f32 {
unsafe { cmath::sinhf(self) }
}

#[inline]
fn cosh(self) -> f32 { unsafe{cmath::coshf(self)} }
fn cosh(self) -> f32 {
unsafe { cmath::coshf(self) }
}

#[inline]
fn tanh(self) -> f32 { unsafe{cmath::tanhf(self)} }
fn tanh(self) -> f32 {
unsafe { cmath::tanhf(self) }
}

/// Inverse hyperbolic sine
///
Expand Down

0 comments on commit 42450ef

Please sign in to comment.