Skip to content

Commit

Permalink
libcore: Move the numeric operations out of Num. r=brson
Browse files Browse the repository at this point in the history
Sadly I could not use trait inheritance due to a type parameter substitution
bug.
  • Loading branch information
pcwalton committed Feb 14, 2013
1 parent 6efa354 commit 216e85f
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 118 deletions.
2 changes: 1 addition & 1 deletion src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub use vec::{OwnedVector, OwnedCopyableVector};
pub use iter::{BaseIter, ExtendedIter, EqIter, CopyableIter};
pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times};

pub use num::{Num, NumCast};
pub use num::NumCast;
pub use ptr::Ptr;
pub use to_str::ToStr;
pub use clone::Clone;
Expand Down
43 changes: 27 additions & 16 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
use cmath;
use cmp;
use libc::{c_float, c_int};
use num;
use num::NumCast;
use num;
use ops;
use option::Option;
use from_str;
use to_str;
Expand Down Expand Up @@ -271,21 +272,6 @@ impl f32 : cmp::Ord {
pure fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
}

impl f32: num::Num {
#[inline(always)]
pure fn add(&self, other: &f32) -> f32 { return *self + *other; }
#[inline(always)]
pure fn sub(&self, other: &f32) -> f32 { return *self - *other; }
#[inline(always)]
pure fn mul(&self, other: &f32) -> f32 { return *self * *other; }
#[inline(always)]
pure fn div(&self, other: &f32) -> f32 { return *self / *other; }
#[inline(always)]
pure fn modulo(&self, other: &f32) -> f32 { return *self % *other; }
#[inline(always)]
pure fn neg(&self) -> f32 { return -*self; }
}

impl f32: num::Zero {
#[inline(always)]
static pure fn zero() -> f32 { 0.0 }
Expand Down Expand Up @@ -320,6 +306,31 @@ pub impl f32: NumCast {
#[inline(always)] pure fn to_float(&self) -> float { *self as float }
}

#[cfg(notest)]
impl ops::Add<f32,f32> for f32 {
pure fn add(&self, other: &f32) -> f32 { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<f32,f32> for f32 {
pure fn sub(&self, other: &f32) -> f32 { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<f32,f32> for f32 {
pure fn mul(&self, other: &f32) -> f32 { *self * *other }
}
#[cfg(notest)]
impl ops::Div<f32,f32> for f32 {
pure fn div(&self, other: &f32) -> f32 { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<f32,f32> for f32 {
pure fn modulo(&self, other: &f32) -> f32 { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<f32> for f32 {
pure fn neg(&self) -> f32 { -*self }
}

#[abi="rust-intrinsic"]
pub extern {
fn floorf32(val: f32) -> f32;
Expand Down
43 changes: 27 additions & 16 deletions src/libcore/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use cmath;
use cmp;
use libc::{c_double, c_int};
use libc;
use num;
use num::NumCast;
use num;
use ops;
use option::Option;
use to_str;
use from_str;
Expand Down Expand Up @@ -296,21 +297,6 @@ impl f64 : cmp::Ord {
pure fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
}

impl f64: num::Num {
#[inline(always)]
pure fn add(&self, other: &f64) -> f64 { return *self + *other; }
#[inline(always)]
pure fn sub(&self, other: &f64) -> f64 { return *self - *other; }
#[inline(always)]
pure fn mul(&self, other: &f64) -> f64 { return *self * *other; }
#[inline(always)]
pure fn div(&self, other: &f64) -> f64 { return *self / *other; }
#[inline(always)]
pure fn modulo(&self, other: &f64) -> f64 { return *self % *other; }
#[inline(always)]
pure fn neg(&self) -> f64 { return -*self; }
}

pub impl f64: NumCast {
/**
* Cast `n` to an `f64`
Expand Down Expand Up @@ -345,6 +331,31 @@ impl f64: num::One {
static pure fn one() -> f64 { 1.0 }
}

#[cfg(notest)]
impl ops::Add<f64,f64> for f64 {
pure fn add(&self, other: &f64) -> f64 { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<f64,f64> for f64 {
pure fn sub(&self, other: &f64) -> f64 { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<f64,f64> for f64 {
pure fn mul(&self, other: &f64) -> f64 { *self * *other }
}
#[cfg(notest)]
impl ops::Div<f64,f64> for f64 {
pure fn div(&self, other: &f64) -> f64 { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<f64,f64> for f64 {
pure fn modulo(&self, other: &f64) -> f64 { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<f64> for f64 {
pure fn neg(&self) -> f64 { -*self }
}

#[abi="rust-intrinsic"]
pub extern {
fn floorf64(val: f64) -> f64;
Expand Down
43 changes: 27 additions & 16 deletions src/libcore/num/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ use m_float = f64;
use cmp::{Eq, Ord};
use cmp;
use f64;
use num;
use num::NumCast;
use num;
use ops;
use option::{None, Option, Some};
use str;
use uint;
Expand Down Expand Up @@ -404,21 +405,6 @@ impl float : Ord {
pure fn gt(&self, other: &float) -> bool { (*self) > (*other) }
}
impl float: num::Num {
#[inline(always)]
pub pure fn add(&self, other: &float) -> float { return *self + *other; }
#[inline(always)]
pub pure fn sub(&self, other: &float) -> float { return *self - *other; }
#[inline(always)]
pub pure fn mul(&self, other: &float) -> float { return *self * *other; }
#[inline(always)]
pub pure fn div(&self, other: &float) -> float { return *self / *other; }
#[inline(always)]
pure fn modulo(&self, other: &float) -> float { return *self % *other; }
#[inline(always)]
pure fn neg(&self) -> float { return -*self; }
}
impl float: num::Zero {
#[inline(always)]
static pure fn zero() -> float { 0.0 }
Expand Down Expand Up @@ -486,6 +472,31 @@ impl float: num::Round {
}
}
#[cfg(notest)]
impl ops::Add<float,float> for float {
pure fn add(&self, other: &float) -> float { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<float,float> for float {
pure fn sub(&self, other: &float) -> float { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<float,float> for float {
pure fn mul(&self, other: &float) -> float { *self * *other }
}
#[cfg(notest)]
impl ops::Div<float,float> for float {
pure fn div(&self, other: &float) -> float { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<float,float> for float {
pure fn modulo(&self, other: &float) -> float { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<float> for float {
pure fn neg(&self) -> float { -*self }
}
#[test]
pub fn test_from_str() {
assert from_str(~"3") == Some(3.);
Expand Down
40 changes: 25 additions & 15 deletions src/libcore/num/int-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,6 @@ impl T : Eq {
pure fn ne(&self, other: &T) -> bool { return (*self) != (*other); }
}
impl T: num::Num {
#[inline(always)]
pure fn add(&self, other: &T) -> T { return *self + *other; }
#[inline(always)]
pure fn sub(&self, other: &T) -> T { return *self - *other; }
#[inline(always)]
pure fn mul(&self, other: &T) -> T { return *self * *other; }
#[inline(always)]
pure fn div(&self, other: &T) -> T { return *self / *other; }
#[inline(always)]
pure fn modulo(&self, other: &T) -> T { return *self % *other; }
#[inline(always)]
pure fn neg(&self) -> T { return -*self; }
}
impl T: num::Zero {
#[inline(always)]
static pure fn zero() -> T { 0 }
Expand All @@ -203,6 +188,31 @@ impl T: num::Round {
pure fn fract(&self) -> T { 0 }
}
#[cfg(notest)]
impl ops::Add<T,T> for T {
pure fn add(&self, other: &T) -> T { *self + *other }
}
#[cfg(notest)]
impl ops::Sub<T,T> for T {
pure fn sub(&self, other: &T) -> T { *self - *other }
}
#[cfg(notest)]
impl ops::Mul<T,T> for T {
pure fn mul(&self, other: &T) -> T { *self * *other }
}
#[cfg(notest)]
impl ops::Div<T,T> for T {
pure fn div(&self, other: &T) -> T { *self / *other }
}
#[cfg(notest)]
impl ops::Modulo<T,T> for T {
pure fn modulo(&self, other: &T) -> T { *self % *other }
}
#[cfg(notest)]
impl ops::Neg<T> for T {
pure fn neg(&self) -> T { -*self }
}
// String conversion functions and impl str -> num
/// Parse a string as a number in base 10.
Expand Down
Loading

5 comments on commit 216e85f

@bors
Copy link
Contributor

@bors bors commented on 216e85f Feb 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at pcwalton@216e85f

@bors
Copy link
Contributor

@bors bors commented on 216e85f Feb 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging pcwalton/rust/num-simplification = 216e85f into auto

@bors
Copy link
Contributor

@bors bors commented on 216e85f Feb 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pcwalton/rust/num-simplification = 216e85f merged ok, testing candidate = 6366e74

@bors
Copy link
Contributor

@bors bors commented on 216e85f Feb 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 216e85f Feb 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 6366e74

Please sign in to comment.