From 910c6a5df8623e3fb18404dfa292dcbc8d930672 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sat, 11 Feb 2012 23:49:13 -0800 Subject: [PATCH] core: Fill out missing functions for basic types --- src/libcore/f32.rs | 2 +- src/libcore/f64.rs | 2 +- src/libcore/float.rs | 2 +- src/libcore/i16.rs | 29 ++++++++++++++++++++++++++++- src/libcore/i32.rs | 29 ++++++++++++++++++++++++++++- src/libcore/i64.rs | 27 +++++++++++++++++++++++++++ src/libcore/i8.rs | 29 ++++++++++++++++++++++++++++- src/libcore/int.rs | 24 ++++++++++++------------ src/libcore/u16.rs | 29 ++++++++++++++++++++++++++++- src/libcore/u32.rs | 2 +- src/libcore/u64.rs | 2 +- src/libcore/u8.rs | 16 ++++++++-------- src/libcore/uint.rs | 2 +- 13 files changed, 165 insertions(+), 30 deletions(-) diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index 72f26958dee..d46a3bda312 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -1,4 +1,4 @@ -#[doc = "Operations and constants constants for `f32`"]; +#[doc = "Operations and constants for `f32`"]; // PORT diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index 1b17d7aaaa0..6d0f838d557 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -1,4 +1,4 @@ -#[doc = "Operations and constants constants for `f64`"]; +#[doc = "Operations and constants for `f64`"]; // PORT diff --git a/src/libcore/float.rs b/src/libcore/float.rs index 1bcb084ccf5..f8a0e5ea7cf 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -1,4 +1,4 @@ -#[doc = "Operations and constants constants for `float`"]; +#[doc = "Operations and constants for `float`"]; /* Module: float diff --git a/src/libcore/i16.rs b/src/libcore/i16.rs index 4a4f4f3e35b..7fc0edebe6f 100644 --- a/src/libcore/i16.rs +++ b/src/libcore/i16.rs @@ -1 +1,28 @@ -#[doc = "Operations and constants constants for `i16`"]; +#[doc = "Operations and constants for `i16`"]; + +const min_value: i16 = -1i16 << 15i16; +const max_value: i16 = (-1i16 << 15i16) - 1i16; + +pure fn add(x: i16, y: i16) -> i16 { x + y } +pure fn sub(x: i16, y: i16) -> i16 { x - y } +pure fn mul(x: i16, y: i16) -> i16 { x * y } +pure fn div(x: i16, y: i16) -> i16 { x / y } +pure fn rem(x: i16, y: i16) -> i16 { x % y } + +pure fn lt(x: i16, y: i16) -> bool { x < y } +pure fn le(x: i16, y: i16) -> bool { x <= y } +pure fn eq(x: i16, y: i16) -> bool { x == y } +pure fn ne(x: i16, y: i16) -> bool { x != y } +pure fn ge(x: i16, y: i16) -> bool { x >= y } +pure fn gt(x: i16, y: i16) -> bool { x > y } + +pure fn positive(x: i16) -> bool { x > 0i16 } +pure fn negative(x: i16) -> bool { x < 0i16 } +pure fn nonpositive(x: i16) -> bool { x <= 0i16 } +pure fn nonnegative(x: i16) -> bool { x >= 0i16 } + +#[doc = "Iterate over the range [`lo`..`hi`)"] +fn range(lo: i16, hi: i16, it: fn(i16)) { + let i = lo; + while i < hi { it(i); i += 1i16; } +} diff --git a/src/libcore/i32.rs b/src/libcore/i32.rs index 03465f5e453..9d5e4e986ef 100644 --- a/src/libcore/i32.rs +++ b/src/libcore/i32.rs @@ -1 +1,28 @@ -#[doc = "Operations and constants constants for `i32`"]; +#[doc = "Operations and constants for `i32`"]; + +const min_value: i32 = -1i32 << 31i32; +const max_value: i32 = (-1i32 << 31i32) - 1i32; + +pure fn add(x: i32, y: i32) -> i32 { x + y } +pure fn sub(x: i32, y: i32) -> i32 { x - y } +pure fn mul(x: i32, y: i32) -> i32 { x * y } +pure fn div(x: i32, y: i32) -> i32 { x / y } +pure fn rem(x: i32, y: i32) -> i32 { x % y } + +pure fn lt(x: i32, y: i32) -> bool { x < y } +pure fn le(x: i32, y: i32) -> bool { x <= y } +pure fn eq(x: i32, y: i32) -> bool { x == y } +pure fn ne(x: i32, y: i32) -> bool { x != y } +pure fn ge(x: i32, y: i32) -> bool { x >= y } +pure fn gt(x: i32, y: i32) -> bool { x > y } + +pure fn positive(x: i32) -> bool { x > 0i32 } +pure fn negative(x: i32) -> bool { x < 0i32 } +pure fn nonpositive(x: i32) -> bool { x <= 0i32 } +pure fn nonnegative(x: i32) -> bool { x >= 0i32 } + +#[doc = "Iterate over the range [`lo`..`hi`)"] +fn range(lo: i32, hi: i32, it: fn(i32)) { + let i = lo; + while i < hi { it(i); i += 1i32; } +} diff --git a/src/libcore/i64.rs b/src/libcore/i64.rs index 67371137041..1cdefcb68a6 100644 --- a/src/libcore/i64.rs +++ b/src/libcore/i64.rs @@ -1 +1,28 @@ #[doc = "Operations and constants constants for `i64`"]; + +const min_value: i64 = -1i64 << 63i64; +const max_value: i64 = (-1i64 << 63i64) - 1i64; + +pure fn add(x: i64, y: i64) -> i64 { x + y } +pure fn sub(x: i64, y: i64) -> i64 { x - y } +pure fn mul(x: i64, y: i64) -> i64 { x * y } +pure fn div(x: i64, y: i64) -> i64 { x / y } +pure fn rem(x: i64, y: i64) -> i64 { x % y } + +pure fn lt(x: i64, y: i64) -> bool { x < y } +pure fn le(x: i64, y: i64) -> bool { x <= y } +pure fn eq(x: i64, y: i64) -> bool { x == y } +pure fn ne(x: i64, y: i64) -> bool { x != y } +pure fn ge(x: i64, y: i64) -> bool { x >= y } +pure fn gt(x: i64, y: i64) -> bool { x > y } + +pure fn positive(x: i64) -> bool { x > 0i64 } +pure fn negative(x: i64) -> bool { x < 0i64 } +pure fn nonpositive(x: i64) -> bool { x <= 0i64 } +pure fn nonnegative(x: i64) -> bool { x >= 0i64 } + +#[doc = "Iterate over the range [`lo`..`hi`)"] +fn range(lo: i64, hi: i64, it: fn(i64)) { + let i = lo; + while i < hi { it(i); i += 1i64; } +} diff --git a/src/libcore/i8.rs b/src/libcore/i8.rs index 6d3331776b2..8b3650cd983 100644 --- a/src/libcore/i8.rs +++ b/src/libcore/i8.rs @@ -1 +1,28 @@ -#[doc = "Operations and constants constants for `i8`"]; +#[doc = "Operations and constants for `i8`"]; + +const min_value: i8 = -1i8 << 7i8; +const max_value: i8 = (-1i8 << 7i8) - 1i8; + +pure fn add(x: i8, y: i8) -> i8 { x + y } +pure fn sub(x: i8, y: i8) -> i8 { x - y } +pure fn mul(x: i8, y: i8) -> i8 { x * y } +pure fn div(x: i8, y: i8) -> i8 { x / y } +pure fn rem(x: i8, y: i8) -> i8 { x % y } + +pure fn lt(x: i8, y: i8) -> bool { x < y } +pure fn le(x: i8, y: i8) -> bool { x <= y } +pure fn eq(x: i8, y: i8) -> bool { x == y } +pure fn ne(x: i8, y: i8) -> bool { x != y } +pure fn ge(x: i8, y: i8) -> bool { x >= y } +pure fn gt(x: i8, y: i8) -> bool { x > y } + +pure fn positive(x: i8) -> bool { x > 0i8 } +pure fn negative(x: i8) -> bool { x < 0i8 } +pure fn nonpositive(x: i8) -> bool { x <= 0i8 } +pure fn nonnegative(x: i8) -> bool { x >= 0i8 } + +#[doc = "Iterate over the range [`lo`..`hi`)"] +fn range(lo: i8, hi: i8, it: fn(i8)) { + let i = lo; + while i < hi { it(i); i += 1i8; } +} diff --git a/src/libcore/int.rs b/src/libcore/int.rs index 62c9be56dc9..e7583b23553 100644 --- a/src/libcore/int.rs +++ b/src/libcore/int.rs @@ -1,9 +1,20 @@ -#[doc = "Operations and constants constants for `int`"]; +#[doc = "Operations and constants for `int`"]; /* Module: int */ +/* +Const: min_value + +The minumum value of an integer +*/ +#[cfg(target_arch="x86")] +const min_value: int = -1 << 31; + +#[cfg(target_arch="x86_64")] +const min_value: int = -1 << 63; + /* Const: max_value @@ -16,17 +27,6 @@ #[cfg(target_arch="x86_64")] const max_value: int = (-1 << 63)-1; -/* -Const: min_value - -The minumum value of an integer -*/ -#[cfg(target_arch="x86")] -const min_value: int = -1 << 31; - -#[cfg(target_arch="x86_64")] -const min_value: int = -1 << 63; - /* Function: add */ pure fn add(x: int, y: int) -> int { ret x + y; } diff --git a/src/libcore/u16.rs b/src/libcore/u16.rs index 39812d8bd7e..36e68ecfdd2 100644 --- a/src/libcore/u16.rs +++ b/src/libcore/u16.rs @@ -1 +1,28 @@ -#[doc = "Operations and constants constants for `u16`"]; +#[doc = "Operations and constants for `u16`"]; + +const min_value: u16 = 0u16; +const max_value: u16 = 0u16 - 1u16; + +pure fn add(x: u16, y: u16) -> u16 { x + y } +pure fn sub(x: u16, y: u16) -> u16 { x - y } +pure fn mul(x: u16, y: u16) -> u16 { x * y } +pure fn div(x: u16, y: u16) -> u16 { x / y } +pure fn rem(x: u16, y: u16) -> u16 { x % y } + +pure fn lt(x: u16, y: u16) -> bool { x < y } +pure fn le(x: u16, y: u16) -> bool { x <= y } +pure fn eq(x: u16, y: u16) -> bool { x == y } +pure fn ne(x: u16, y: u16) -> bool { x != y } +pure fn ge(x: u16, y: u16) -> bool { x >= y } +pure fn gt(x: u16, y: u16) -> bool { x > y } + +pure fn positive(x: u16) -> bool { x > 0u16 } +pure fn negative(x: u16) -> bool { x < 0u16 } +pure fn nonpositive(x: u16) -> bool { x <= 0u16 } +pure fn nonnegative(x: u16) -> bool { x >= 0u16 } + +#[doc = "Iterate over the range [`lo`..`hi`)"] +fn range(lo: u16, hi: u16, it: fn(u16)) { + let i = lo; + while i < hi { it(i); i += 1u16; } +} diff --git a/src/libcore/u32.rs b/src/libcore/u32.rs index 013c15eae7c..ed002a8c1e5 100644 --- a/src/libcore/u32.rs +++ b/src/libcore/u32.rs @@ -1,4 +1,4 @@ -#[doc = "Operations and constants constants for `u32`"]; +#[doc = "Operations and constants for `u32`"]; /* Module: u32 diff --git a/src/libcore/u64.rs b/src/libcore/u64.rs index ebcba1f2d51..a8cbeab9c9c 100644 --- a/src/libcore/u64.rs +++ b/src/libcore/u64.rs @@ -1,4 +1,4 @@ -#[doc = "Operations and constants constants for `u64`"]; +#[doc = "Operations and constants for `u64`"]; /* Module: u64 diff --git a/src/libcore/u8.rs b/src/libcore/u8.rs index c1f09b3bd32..34553c27eb3 100644 --- a/src/libcore/u8.rs +++ b/src/libcore/u8.rs @@ -1,16 +1,9 @@ -#[doc = "Operations and constants constants for `u8`"]; +#[doc = "Operations and constants for `u8`"]; /* Module: u8 */ -/* -Const: max_value - -The maximum value of a u8. -*/ -const max_value: u8 = 255u8; - /* Const: min_value @@ -18,6 +11,13 @@ */ const min_value: u8 = 0u8; +/* +Const: max_value + +The maximum value of a u8. +*/ +const max_value: u8 = 255u8; + /* Function: add */ pure fn add(x: u8, y: u8) -> u8 { ret x + y; } diff --git a/src/libcore/uint.rs b/src/libcore/uint.rs index f9ca9d92bf4..cd1bf7ea555 100644 --- a/src/libcore/uint.rs +++ b/src/libcore/uint.rs @@ -1,4 +1,4 @@ -#[doc = "Operations and constants constants for `uint`"]; +#[doc = "Operations and constants for `uint`"]; /* Module: uint