Skip to content

Commit

Permalink
Merge pull request #4467 from gifnksm/inlining-core
Browse files Browse the repository at this point in the history
Inlining methods/functions in core.
  • Loading branch information
catamorphism committed Jan 13, 2013
2 parents 38315fb + 62f2749 commit 50cd3c1
Show file tree
Hide file tree
Showing 22 changed files with 338 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub use is_XID_continue = unicode::derived_property::XID_Continue;
* Indicates whether a character is in lower case, defined
* in terms of the Unicode General Category 'Ll'
*/
#[inline(always)]
pub pure fn is_lowercase(c: char) -> bool {
return unicode::general_category::Ll(c);
}
Expand All @@ -72,6 +73,7 @@ pub pure fn is_lowercase(c: char) -> bool {
* Indicates whether a character is in upper case, defined
* in terms of the Unicode General Category 'Lu'.
*/
#[inline(always)]
pub pure fn is_uppercase(c: char) -> bool {
return unicode::general_category::Lu(c);
}
Expand All @@ -81,6 +83,7 @@ pub pure fn is_uppercase(c: char) -> bool {
* terms of the Unicode General Categories 'Zs', 'Zl', 'Zp'
* additional 'Cc'-category control codes in the range [0x09, 0x0d]
*/
#[inline(always)]
pub pure fn is_whitespace(c: char) -> bool {
return ('\x09' <= c && c <= '\x0d')
|| unicode::general_category::Zs(c)
Expand All @@ -93,6 +96,7 @@ pub pure fn is_whitespace(c: char) -> bool {
* defined in terms of the Unicode General Categories 'Nd', 'Nl', 'No'
* and the Derived Core Property 'Alphabetic'.
*/
#[inline(always)]
pub pure fn is_alphanumeric(c: char) -> bool {
return unicode::derived_property::Alphabetic(c) ||
unicode::general_category::Nd(c) ||
Expand All @@ -101,11 +105,13 @@ pub pure fn is_alphanumeric(c: char) -> bool {
}

/// Indicates whether the character is an ASCII character
#[inline(always)]
pub pure fn is_ascii(c: char) -> bool {
c - ('\x7F' & c) == '\x00'
}

/// Indicates whether the character is numeric (Nd, Nl, or No)
#[inline(always)]
pub pure fn is_digit(c: char) -> bool {
return unicode::general_category::Nd(c) ||
unicode::general_category::Nl(c) ||
Expand All @@ -122,6 +128,7 @@ pub pure fn is_digit(c: char) -> bool {
* 'b' or 'B', 11, etc. Returns none if the char does not
* refer to a digit in the given radix.
*/
#[inline]
pub pure fn to_digit(c: char, radix: uint) -> Option<uint> {
let val = match c {
'0' .. '9' => c as uint - ('0' as uint),
Expand Down Expand Up @@ -190,6 +197,7 @@ pub pure fn escape_default(c: char) -> ~str {
*
* -1 if a < b, 0 if a == b, +1 if a > b
*/
#[inline(always)]
pub pure fn cmp(a: char, b: char) -> int {
return if b > a { -1 }
else if b < a { 1 }
Expand Down
1 change: 1 addition & 0 deletions src/libcore/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ pub trait Clone {
}

impl (): Clone {
#[inline(always)]
fn clone(&self) -> () { () }
}
6 changes: 6 additions & 0 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,32 @@ pub trait Ord {
pure fn gt(&self, other: &self) -> bool;
}

#[inline(always)]
pub pure fn lt<T: Ord>(v1: &T, v2: &T) -> bool {
(*v1).lt(v2)
}

#[inline(always)]
pub pure fn le<T: Ord Eq>(v1: &T, v2: &T) -> bool {
(*v1).lt(v2) || (*v1).eq(v2)
}

#[inline(always)]
pub pure fn eq<T: Eq>(v1: &T, v2: &T) -> bool {
(*v1).eq(v2)
}

#[inline(always)]
pub pure fn ne<T: Eq>(v1: &T, v2: &T) -> bool {
(*v1).ne(v2)
}

#[inline(always)]
pub pure fn ge<T: Ord>(v1: &T, v2: &T) -> bool {
(*v1).ge(v2)
}

#[inline(always)]
pub pure fn gt<T: Ord>(v1: &T, v2: &T) -> bool {
(*v1).gt(v2)
}
Expand Down
7 changes: 7 additions & 0 deletions src/libcore/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum Either<T, U> {
Right(U)
}

#[inline(always)]
pub fn either<T, U, V>(f_left: fn(&T) -> V,
f_right: fn(&U) -> V, value: &Either<T, U>) -> V {
/*!
Expand Down Expand Up @@ -90,6 +91,7 @@ pub fn partition<T, U>(eithers: ~[Either<T, U>])
return (move lefts, move rights);
}

#[inline(always)]
pub pure fn flip<T, U>(eith: Either<T, U>) -> Either<U, T> {
//! Flips between left and right of a given either
Expand All @@ -99,6 +101,7 @@ pub pure fn flip<T, U>(eith: Either<T, U>) -> Either<U, T> {
}
}

#[inline(always)]
pub pure fn to_result<T, U>(eith: Either<T, U>)
-> Result<U, T> {
/*!
Expand All @@ -114,18 +117,21 @@ pub pure fn to_result<T, U>(eith: Either<T, U>)
}
}

#[inline(always)]
pub pure fn is_left<T, U>(eith: &Either<T, U>) -> bool {
//! Checks whether the given value is a left
match *eith { Left(_) => true, _ => false }
}

#[inline(always)]
pub pure fn is_right<T, U>(eith: &Either<T, U>) -> bool {
//! Checks whether the given value is a right
match *eith { Right(_) => true, _ => false }
}

#[inline(always)]
pub pure fn unwrap_left<T,U>(eith: Either<T,U>) -> T {
//! Retrieves the value in the left branch. Fails if the either is Right.
Expand All @@ -134,6 +140,7 @@ pub pure fn unwrap_left<T,U>(eith: Either<T,U>) -> T {
}
}

#[inline(always)]
pub pure fn unwrap_right<T,U>(eith: Either<T,U>) -> U {
//! Retrieves the value in the right branch. Fails if the either is Left.
Expand Down
37 changes: 37 additions & 0 deletions src/libcore/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,38 +105,52 @@ pub const infinity: f32 = 1.0_f32/0.0_f32;

pub const neg_infinity: f32 = -1.0_f32/0.0_f32;

#[inline(always)]
pub pure fn is_NaN(f: f32) -> bool { f != f }

#[inline(always)]
pub pure fn add(x: f32, y: f32) -> f32 { return x + y; }

#[inline(always)]
pub pure fn sub(x: f32, y: f32) -> f32 { return x - y; }

#[inline(always)]
pub pure fn mul(x: f32, y: f32) -> f32 { return x * y; }

#[inline(always)]
pub pure fn div(x: f32, y: f32) -> f32 { return x / y; }

#[inline(always)]
pub pure fn rem(x: f32, y: f32) -> f32 { return x % y; }

#[inline(always)]
pub pure fn lt(x: f32, y: f32) -> bool { return x < y; }

#[inline(always)]
pub pure fn le(x: f32, y: f32) -> bool { return x <= y; }

#[inline(always)]
pub pure fn eq(x: f32, y: f32) -> bool { return x == y; }

#[inline(always)]
pub pure fn ne(x: f32, y: f32) -> bool { return x != y; }

#[inline(always)]
pub pure fn ge(x: f32, y: f32) -> bool { return x >= y; }

#[inline(always)]
pub pure fn gt(x: f32, y: f32) -> bool { return x > y; }

// FIXME (#1999): replace the predicates below with llvm intrinsics or
// calls to the libmath macros in the rust runtime for performance.

/// Returns true if `x` is a positive number, including +0.0f320 and +Infinity
#[inline(always)]
pub pure fn is_positive(x: f32) -> bool
{ return x > 0.0f32 || (1.0f32/x) == infinity; }

/// Returns true if `x` is a negative number, including -0.0f320 and -Infinity
#[inline(always)]
pub pure fn is_negative(x: f32) -> bool
{ return x < 0.0f32 || (1.0f32/x) == neg_infinity; }

Expand All @@ -145,6 +159,7 @@ pub pure fn is_negative(x: f32) -> bool
*
* This is the same as `f32::is_negative`.
*/
#[inline(always)]
pub pure fn is_nonpositive(x: f32) -> bool {
return x < 0.0f32 || (1.0f32/x) == neg_infinity;
}
Expand All @@ -154,21 +169,25 @@ pub pure fn is_nonpositive(x: f32) -> bool {
*
* This is the same as `f32::is_positive`.)
*/
#[inline(always)]
pub pure fn is_nonnegative(x: f32) -> bool {
return x > 0.0f32 || (1.0f32/x) == infinity;
}

/// Returns true if `x` is a zero number (positive or negative zero)
#[inline(always)]
pub pure fn is_zero(x: f32) -> bool {
return x == 0.0f32 || x == -0.0f32;
}

/// Returns true if `x`is an infinite number
#[inline(always)]
pub pure fn is_infinite(x: f32) -> bool {
return x == infinity || x == neg_infinity;
}

/// Returns true if `x`is a finite number
#[inline(always)]
pub pure fn is_finite(x: f32) -> bool {
return !(is_NaN(x) || is_infinite(x));
}
Expand Down Expand Up @@ -219,45 +238,63 @@ pub mod consts {
pub const ln_10: f32 = 2.30258509299404568401799145468436421_f32;
}

#[inline(always)]
pub pure fn signbit(x: f32) -> int {
if is_negative(x) { return 1; } else { return 0; }
}

#[inline(always)]
pub pure fn logarithm(n: f32, b: f32) -> f32 {
return log2(n) / log2(b);
}

#[cfg(notest)]
impl f32 : cmp::Eq {
#[inline(always)]
pure fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
#[inline(always)]
pure fn ne(&self, other: &f32) -> bool { (*self) != (*other) }
}

#[cfg(notest)]
impl f32 : cmp::Ord {
#[inline(always)]
pure fn lt(&self, other: &f32) -> bool { (*self) < (*other) }
#[inline(always)]
pure fn le(&self, other: &f32) -> bool { (*self) <= (*other) }
#[inline(always)]
pure fn ge(&self, other: &f32) -> bool { (*self) >= (*other) }
#[inline(always)]
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; }

#[inline(always)]
pure fn to_int(&self) -> int { return *self as int; }
#[inline(always)]
static pure fn from_int(n: int) -> f32 { return n as f32; }
}

impl f32: num::Zero {
#[inline(always)]
static pure fn zero() -> f32 { 0.0 }
}

impl f32: num::One {
#[inline(always)]
static pure fn one() -> f32 { 1.0 }
}

Expand Down
Loading

0 comments on commit 50cd3c1

Please sign in to comment.