Fix formatting in float implementations

This commit is contained in:
Brendan Zabarauskas 2014-04-19 01:27:51 +10:00
parent bed70a42ec
commit 42450ef022
2 changed files with 198 additions and 72 deletions

View File

@ -14,6 +14,7 @@
use prelude::*;
use cast;
use default::Default;
use from_str::FromStr;
use libc::{c_int};
@ -213,12 +214,16 @@ fn neg(&self) -> f32 { -*self }
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
///
@ -227,7 +232,9 @@ fn abs_sub(&self, other: &f32) -> f32 { unsafe{cmath::fdimf(*self, *other)} }
/// - `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`
@ -250,7 +257,9 @@ fn max_value() -> f32 { 3.40282347e+38 }
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 {
@ -302,7 +311,7 @@ fn classify(self) -> FPCategory {
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,
@ -335,7 +344,9 @@ fn max_10_exp(_: Option<f32>) -> int { 38 }
/// 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:
///
@ -353,28 +364,34 @@ fn frexp(self) -> (f32, int) {
/// 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 {
@ -389,19 +406,27 @@ fn integer_decode(self) -> (u64, i16, i8) {
/// 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:
///
@ -485,40 +510,62 @@ fn ln_10() -> f32 { 2.30258509299404568401799145468436421 }
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]
@ -528,15 +575,21 @@ fn sin_cos(self) -> (f32, 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]
@ -544,20 +597,30 @@ 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
///

View File

@ -14,6 +14,7 @@
use prelude::*;
use cast;
use default::Default;
use from_str::FromStr;
use libc::{c_int};
@ -221,12 +222,16 @@ fn neg(&self) -> f64 { -*self }
impl Signed for f64 {
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
#[inline]
fn abs(&self) -> f64 { unsafe{intrinsics::fabsf64(*self)} }
fn abs(&self) -> f64 {
unsafe { intrinsics::fabsf64(*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: &f64) -> f64 { unsafe{cmath::fdim(*self, *other)} }
fn abs_sub(&self, other: &f64) -> f64 {
unsafe { cmath::fdim(*self, *other) }
}
/// # Returns
///
@ -235,7 +240,9 @@ fn abs_sub(&self, other: &f64) -> f64 { unsafe{cmath::fdim(*self, *other)} }
/// - `NAN` if the number is NaN
#[inline]
fn signum(&self) -> f64 {
if self.is_nan() { NAN } else { unsafe{intrinsics::copysignf64(1.0, *self)} }
if self.is_nan() { NAN } else {
unsafe { intrinsics::copysignf64(1.0, *self) }
}
}
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
@ -308,7 +315,7 @@ fn classify(self) -> FPCategory {
static EXP_MASK: u64 = 0x7ff0000000000000;
static MAN_MASK: u64 = 0x000fffffffffffff;
let bits: u64 = unsafe {::cast::transmute(self)};
let bits: u64 = unsafe { cast::transmute(self) };
match (bits & MAN_MASK, bits & EXP_MASK) {
(0, 0) => FPZero,
(_, 0) => FPSubnormal,
@ -341,7 +348,9 @@ fn max_10_exp(_: Option<f64>) -> int { 308 }
/// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp`
#[inline]
fn ldexp(x: f64, exp: int) -> f64 { unsafe{cmath::ldexp(x, exp as c_int)} }
fn ldexp(x: f64, exp: int) -> f64 {
unsafe { cmath::ldexp(x, exp as c_int) }
}
/// Breaks the number into a normalized fraction and a base-2 exponent, satisfying:
///
@ -359,28 +368,34 @@ fn frexp(self) -> (f64, int) {
/// 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) -> f64 { unsafe{cmath::expm1(self)} }
fn exp_m1(self) -> f64 {
unsafe { cmath::expm1(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) -> f64 { unsafe{cmath::log1p(self)} }
fn ln_1p(self) -> f64 {
unsafe { cmath::log1p(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: f64, b: f64) -> f64 { unsafe{intrinsics::fmaf64(self, a, b)} }
fn mul_add(self, a: f64, b: f64) -> f64 {
unsafe { intrinsics::fmaf64(self, a, b) }
}
/// Returns the next representable floating-point value in the direction of `other`
#[inline]
fn next_after(self, other: f64) -> f64 { unsafe{cmath::nextafter(self, other)} }
fn next_after(self, other: f64) -> f64 {
unsafe { cmath::nextafter(self, other) }
}
/// Returns the mantissa, exponent and sign as integers.
fn integer_decode(self) -> (u64, i16, i8) {
let bits: u64 = unsafe {
::cast::transmute(self)
};
let bits: u64 = unsafe { cast::transmute(self) };
let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
let mantissa = if exponent == 0 {
@ -395,19 +410,27 @@ fn integer_decode(self) -> (u64, i16, i8) {
/// Round half-way cases toward `NEG_INFINITY`
#[inline]
fn floor(self) -> f64 { unsafe{intrinsics::floorf64(self)} }
fn floor(self) -> f64 {
unsafe { intrinsics::floorf64(self) }
}
/// Round half-way cases toward `INFINITY`
#[inline]
fn ceil(self) -> f64 { unsafe{intrinsics::ceilf64(self)} }
fn ceil(self) -> f64 {
unsafe { intrinsics::ceilf64(self) }
}
/// Round half-way cases away from `0.0`
#[inline]
fn round(self) -> f64 { unsafe{intrinsics::roundf64(self)} }
fn round(self) -> f64 {
unsafe { intrinsics::roundf64(self) }
}
/// The integer part of the number (rounds towards `0.0`)
#[inline]
fn trunc(self) -> f64 { unsafe{intrinsics::truncf64(self)} }
fn trunc(self) -> f64 {
unsafe { intrinsics::truncf64(self) }
}
/// The fractional part of the number, satisfying:
///
@ -491,43 +514,67 @@ fn ln_10() -> f64 { 2.30258509299404568401799145468436421 }
fn recip(self) -> f64 { 1.0 / self }
#[inline]
fn powf(self, n: f64) -> f64 { unsafe{intrinsics::powf64(self, n)} }
fn powf(self, n: f64) -> f64 {
unsafe { intrinsics::powf64(self, n) }
}
#[inline]
fn powi(self, n: i32) -> f64 { unsafe{intrinsics::powif64(self, n)} }
fn powi(self, n: i32) -> f64 {
unsafe { intrinsics::powif64(self, n) }
}
#[inline]
fn sqrt(self) -> f64 { unsafe{intrinsics::sqrtf64(self)} }
fn sqrt(self) -> f64 {
unsafe { intrinsics::sqrtf64(self) }
}
#[inline]
fn rsqrt(self) -> f64 { self.sqrt().recip() }
#[inline]
fn cbrt(self) -> f64 { unsafe{cmath::cbrt(self)} }
fn cbrt(self) -> f64 {
unsafe { cmath::cbrt(self) }
}
#[inline]
fn hypot(self, other: f64) -> f64 { unsafe{cmath::hypot(self, other)} }
fn hypot(self, other: f64) -> f64 {
unsafe { cmath::hypot(self, other) }
}
#[inline]
fn sin(self) -> f64 { unsafe{intrinsics::sinf64(self)} }
fn sin(self) -> f64 {
unsafe { intrinsics::sinf64(self) }
}
#[inline]
fn cos(self) -> f64 { unsafe{intrinsics::cosf64(self)} }
fn cos(self) -> f64 {
unsafe { intrinsics::cosf64(self) }
}
#[inline]
fn tan(self) -> f64 { unsafe{cmath::tan(self)} }
fn tan(self) -> f64 {
unsafe { cmath::tan(self) }
}
#[inline]
fn asin(self) -> f64 { unsafe{cmath::asin(self)} }
fn asin(self) -> f64 {
unsafe { cmath::asin(self) }
}
#[inline]
fn acos(self) -> f64 { unsafe{cmath::acos(self)} }
fn acos(self) -> f64 {
unsafe { cmath::acos(self) }
}
#[inline]
fn atan(self) -> f64 { unsafe{cmath::atan(self)} }
fn atan(self) -> f64 {
unsafe { cmath::atan(self) }
}
#[inline]
fn atan2(self, other: f64) -> f64 { unsafe{cmath::atan2(self, other)} }
fn atan2(self, other: f64) -> f64 {
unsafe { cmath::atan2(self, other) }
}
/// Simultaneously computes the sine and cosine of the number
#[inline]
@ -537,15 +584,21 @@ fn sin_cos(self) -> (f64, f64) {
/// Returns the exponential of the number
#[inline]
fn exp(self) -> f64 { unsafe{intrinsics::expf64(self)} }
fn exp(self) -> f64 {
unsafe { intrinsics::expf64(self) }
}
/// Returns 2 raised to the power of the number
#[inline]
fn exp2(self) -> f64 { unsafe{intrinsics::exp2f64(self)} }
fn exp2(self) -> f64 {
unsafe { intrinsics::exp2f64(self) }
}
/// Returns the natural logarithm of the number
#[inline]
fn ln(self) -> f64 { unsafe{intrinsics::logf64(self)} }
fn ln(self) -> f64 {
unsafe { intrinsics::logf64(self) }
}
/// Returns the logarithm of the number with respect to an arbitrary base
#[inline]
@ -553,20 +606,30 @@ fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
/// Returns the base 2 logarithm of the number
#[inline]
fn log2(self) -> f64 { unsafe{intrinsics::log2f64(self)} }
fn log2(self) -> f64 {
unsafe { intrinsics::log2f64(self) }
}
/// Returns the base 10 logarithm of the number
#[inline]
fn log10(self) -> f64 { unsafe{intrinsics::log10f64(self)} }
fn log10(self) -> f64 {
unsafe { intrinsics::log10f64(self) }
}
#[inline]
fn sinh(self) -> f64 { unsafe{cmath::sinh(self)} }
fn sinh(self) -> f64 {
unsafe { cmath::sinh(self) }
}
#[inline]
fn cosh(self) -> f64 { unsafe{cmath::cosh(self)} }
fn cosh(self) -> f64 {
unsafe { cmath::cosh(self) }
}
#[inline]
fn tanh(self) -> f64 { unsafe{cmath::tanh(self)} }
fn tanh(self) -> f64 {
unsafe { cmath::tanh(self) }
}
/// Inverse hyperbolic sine
///