From a20ab833f68f4e21c795b84fc622e4bf65ae47f8 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sat, 14 Apr 2012 17:21:10 -0700 Subject: [PATCH 1/2] core: Factor out int/i8/16/32/64 mods into int-template --- src/libcore/core.rc | 42 +++++++++++++++--- src/libcore/i16.rs | 41 ------------------ src/libcore/i32.rs | 41 ------------------ src/libcore/i64.rs | 41 ------------------ src/libcore/i8.rs | 41 ------------------ src/libcore/int-template.rs | 51 ++++++++++++++++++++++ src/libcore/int-template/i16.rs | 3 ++ src/libcore/int-template/i32.rs | 3 ++ src/libcore/int-template/i64.rs | 3 ++ src/libcore/int-template/i8.rs | 3 ++ src/libcore/{ => int-template}/int.rs | 62 +++------------------------ 11 files changed, 105 insertions(+), 226 deletions(-) delete mode 100644 src/libcore/i16.rs delete mode 100644 src/libcore/i32.rs delete mode 100644 src/libcore/i64.rs delete mode 100644 src/libcore/i8.rs create mode 100644 src/libcore/int-template.rs create mode 100644 src/libcore/int-template/i16.rs create mode 100644 src/libcore/int-template/i32.rs create mode 100644 src/libcore/int-template/i64.rs create mode 100644 src/libcore/int-template/i8.rs rename src/libcore/{ => int-template}/int.rs (68%) diff --git a/src/libcore/core.rc b/src/libcore/core.rc index c0d46a9fad506..9d07d42f2c329 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -44,16 +44,48 @@ export priv; // Built-in-type support modules +#[doc = "Operations and constants for `int`"] +#[path = "int-template"] +mod int { + import inst::{ hash, parse_buf, from_str, to_str, str, pow }; + export hash, parse_buf, from_str, to_str, str, pow; + #[path = "int.rs"] + mod inst; +} + +#[doc = "Operations and constants for `i8`"] +#[path = "int-template"] +mod i8 { + #[path = "i8.rs"] + mod inst; +} + +#[doc = "Operations and constants for `i16`"] +#[path = "int-template"] +mod i16 { + #[path = "i16.rs"] + mod inst; +} + +#[doc = "Operations and constants for `i32`"] +#[path = "int-template"] +mod i32 { + #[path = "i32.rs"] + mod inst; +} + +#[doc = "Operations and constants for `i64`"] +#[path = "int-template"] +mod i64 { + #[path = "i64.rs"] + mod inst; +} + mod box; mod char; mod float; mod f32; mod f64; -mod int; -mod i8; -mod i16; -mod i32; -mod i64; mod str; mod ptr; mod uint; diff --git a/src/libcore/i16.rs b/src/libcore/i16.rs deleted file mode 100644 index 6ae13ae44423c..0000000000000 --- a/src/libcore/i16.rs +++ /dev/null @@ -1,41 +0,0 @@ -#[doc = "Operations and constants for `i16`"]; - -const min_value: i16 = -1i16 << 15i16; -const max_value: i16 = (-1i16 << 15i16) - 1i16; - -pure fn min(x: i16, y: i16) -> i16 { if x < y { x } else { y } } -pure fn max(x: i16, y: i16) -> i16 { if x > y { x } else { y } } - -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 is_positive(x: i16) -> bool { x > 0i16 } -pure fn is_negative(x: i16) -> bool { x < 0i16 } -pure fn is_nonpositive(x: i16) -> bool { x <= 0i16 } -pure fn is_nonnegative(x: i16) -> bool { x >= 0i16 } - -#[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: i16, hi: i16, it: fn(i16)) { - let mut i = lo; - while i < hi { it(i); i += 1i16; } -} - -#[doc = "Computes the bitwise complement"] -pure fn compl(i: i16) -> i16 { - u16::compl(i as u16) as i16 -} - -#[doc = "Computes the absolute value"] -pure fn abs(i: i16) -> i16 { - if is_negative(i) { -i } else { i } -} diff --git a/src/libcore/i32.rs b/src/libcore/i32.rs deleted file mode 100644 index 9a6e4ff8a19ee..0000000000000 --- a/src/libcore/i32.rs +++ /dev/null @@ -1,41 +0,0 @@ -#[doc = "Operations and constants for `i32`"]; - -const min_value: i32 = -1i32 << 31i32; -const max_value: i32 = (-1i32 << 31i32) - 1i32; - -pure fn min(x: i32, y: i32) -> i32 { if x < y { x } else { y } } -pure fn max(x: i32, y: i32) -> i32 { if x > y { x } else { y } } - -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 is_positive(x: i32) -> bool { x > 0i32 } -pure fn is_negative(x: i32) -> bool { x < 0i32 } -pure fn is_nonpositive(x: i32) -> bool { x <= 0i32 } -pure fn is_nonnegative(x: i32) -> bool { x >= 0i32 } - -#[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: i32, hi: i32, it: fn(i32)) { - let mut i = lo; - while i < hi { it(i); i += 1i32; } -} - -#[doc = "Computes the bitwise complement"] -pure fn compl(i: i32) -> i32 { - u32::compl(i as u32) as i32 -} - -#[doc = "Computes the absolute value"] -pure fn abs(i: i32) -> i32 { - if is_negative(i) { -i } else { i } -} diff --git a/src/libcore/i64.rs b/src/libcore/i64.rs deleted file mode 100644 index a0f8bf3e1ebdf..0000000000000 --- a/src/libcore/i64.rs +++ /dev/null @@ -1,41 +0,0 @@ -#[doc = "Operations and constants for `i64`"]; - -const min_value: i64 = -1i64 << 63i64; -const max_value: i64 = (-1i64 << 63i64) - 1i64; - -pure fn min(x: i64, y: i64) -> i64 { if x < y { x } else { y } } -pure fn max(x: i64, y: i64) -> i64 { if x > y { x } else { y } } - -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 is_positive(x: i64) -> bool { x > 0i64 } -pure fn is_negative(x: i64) -> bool { x < 0i64 } -pure fn is_nonpositive(x: i64) -> bool { x <= 0i64 } -pure fn is_nonnegative(x: i64) -> bool { x >= 0i64 } - -#[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: i64, hi: i64, it: fn(i64)) { - let mut i = lo; - while i < hi { it(i); i += 1i64; } -} - -#[doc = "Computes the bitwise complement"] -pure fn compl(i: i64) -> i64 { - u64::compl(i as u64) as i64 -} - -#[doc = "Computes the absolute value"] -pure fn abs(i: i64) -> i64 { - if is_negative(i) { -i } else { i } -} diff --git a/src/libcore/i8.rs b/src/libcore/i8.rs deleted file mode 100644 index 8d4e429ffc9ba..0000000000000 --- a/src/libcore/i8.rs +++ /dev/null @@ -1,41 +0,0 @@ -#[doc = "Operations and constants for `i8`"]; - -const min_value: i8 = -1i8 << 7i8; -const max_value: i8 = (-1i8 << 7i8) - 1i8; - -pure fn min(x: i8, y: i8) -> i8 { if x < y { x } else { y } } -pure fn max(x: i8, y: i8) -> i8 { if x > y { x } else { y } } - -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 is_positive(x: i8) -> bool { x > 0i8 } -pure fn is_negative(x: i8) -> bool { x < 0i8 } -pure fn is_nonpositive(x: i8) -> bool { x <= 0i8 } -pure fn is_nonnegative(x: i8) -> bool { x >= 0i8 } - -#[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: i8, hi: i8, it: fn(i8)) { - let mut i = lo; - while i < hi { it(i); i += 1i8; } -} - -#[doc = "Computes the bitwise complement"] -pure fn compl(i: i8) -> i8 { - u8::compl(i as u8) as i8 -} - -#[doc = "Computes the absolute value"] -pure fn abs(i: i8) -> i8 { - if is_negative(i) { -i } else { i } -} diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs new file mode 100644 index 0000000000000..53413f3cdb67b --- /dev/null +++ b/src/libcore/int-template.rs @@ -0,0 +1,51 @@ +import T = inst::T; + +export min_value, max_value; +export min, max; +export add, sub, mul, div, rem; +export lt, le, eq, ne, ge, gt; +export is_positive, is_negative; +export is_nonpositive, is_nonnegative; +export range; +export compl; +export abs; + +const min_value: T = -1 as T << (inst::bits - 1 as T); +const max_value: T = min_value - 1 as T; + +pure fn min(x: T, y: T) -> T { if x < y { x } else { y } } +pure fn max(x: T, y: T) -> T { if x > y { x } else { y } } + +pure fn add(x: T, y: T) -> T { x + y } +pure fn sub(x: T, y: T) -> T { x - y } +pure fn mul(x: T, y: T) -> T { x * y } +pure fn div(x: T, y: T) -> T { x / y } +pure fn rem(x: T, y: T) -> T { x % y } + +pure fn lt(x: T, y: T) -> bool { x < y } +pure fn le(x: T, y: T) -> bool { x <= y } +pure fn eq(x: T, y: T) -> bool { x == y } +pure fn ne(x: T, y: T) -> bool { x != y } +pure fn ge(x: T, y: T) -> bool { x >= y } +pure fn gt(x: T, y: T) -> bool { x > y } + +pure fn is_positive(x: T) -> bool { x > 0 as T } +pure fn is_negative(x: T) -> bool { x < 0 as T } +pure fn is_nonpositive(x: T) -> bool { x <= 0 as T } +pure fn is_nonnegative(x: T) -> bool { x >= 0 as T } + +#[doc = "Iterate over the range [`lo`..`hi`)"] +fn range(lo: T, hi: T, it: fn(T)) { + let mut i = lo; + while i < hi { it(i); i += 1 as T; } +} + +#[doc = "Computes the bitwise complement"] +pure fn compl(i: T) -> T { + -1 as T ^ i +} + +#[doc = "Computes the absolute value"] +pure fn abs(i: T) -> T { + if is_negative(i) { -i } else { i } +} diff --git a/src/libcore/int-template/i16.rs b/src/libcore/int-template/i16.rs new file mode 100644 index 0000000000000..06eb96ba59c69 --- /dev/null +++ b/src/libcore/int-template/i16.rs @@ -0,0 +1,3 @@ +type T = i16; + +const bits: T = 16 as T; diff --git a/src/libcore/int-template/i32.rs b/src/libcore/int-template/i32.rs new file mode 100644 index 0000000000000..151f358258624 --- /dev/null +++ b/src/libcore/int-template/i32.rs @@ -0,0 +1,3 @@ +type T = i32; + +const bits: T = 32 as T; diff --git a/src/libcore/int-template/i64.rs b/src/libcore/int-template/i64.rs new file mode 100644 index 0000000000000..1c8a181ab8e02 --- /dev/null +++ b/src/libcore/int-template/i64.rs @@ -0,0 +1,3 @@ +type T = i64; + +const bits: T = 64 as T; diff --git a/src/libcore/int-template/i8.rs b/src/libcore/int-template/i8.rs new file mode 100644 index 0000000000000..c103f1cbca48a --- /dev/null +++ b/src/libcore/int-template/i8.rs @@ -0,0 +1,3 @@ +type T = i8; + +const bits: T = 8 as T; diff --git a/src/libcore/int.rs b/src/libcore/int-template/int.rs similarity index 68% rename from src/libcore/int.rs rename to src/libcore/int-template/int.rs index bc4820390195c..7cc47a52679b9 100644 --- a/src/libcore/int.rs +++ b/src/libcore/int-template/int.rs @@ -1,49 +1,14 @@ -#[doc = "Operations and constants for `int`"]; +type T = int; -#[cfg(target_arch="x86")] -const min_value: int = -1 << 31; +#[cfg(target_arch = "x86")] +const bits: T = 32 as T; -#[cfg(target_arch="x86_64")] -const min_value: int = -1 << 63; - -// FIXME: Find another way to access the machine word size in a const expr -// (See Issue #2001) -#[cfg(target_arch="x86")] -const max_value: int = (-1 << 31)-1; - -#[cfg(target_arch="x86_64")] -const max_value: int = (-1 << 63)-1; - -pure fn min(x: int, y: int) -> int { if x < y { x } else { y } } -pure fn max(x: int, y: int) -> int { if x > y { x } else { y } } - -pure fn add(x: int, y: int) -> int { ret x + y; } -pure fn sub(x: int, y: int) -> int { ret x - y; } -pure fn mul(x: int, y: int) -> int { ret x * y; } -pure fn div(x: int, y: int) -> int { ret x / y; } -pure fn rem(x: int, y: int) -> int { ret x % y; } - -pure fn lt(x: int, y: int) -> bool { ret x < y; } -pure fn le(x: int, y: int) -> bool { ret x <= y; } -pure fn eq(x: int, y: int) -> bool { ret x == y; } -pure fn ne(x: int, y: int) -> bool { ret x != y; } -pure fn ge(x: int, y: int) -> bool { ret x >= y; } -pure fn gt(x: int, y: int) -> bool { ret x > y; } - -pure fn is_positive(x: int) -> bool { ret x > 0; } -pure fn is_negative(x: int) -> bool { ret x < 0; } -pure fn is_nonpositive(x: int) -> bool { ret x <= 0; } -pure fn is_nonnegative(x: int) -> bool { ret x >= 0; } +#[cfg(target_arch = "x86_64")] +const bits: T = 64 as T; #[doc = "Produce a uint suitable for use in a hash table"] pure fn hash(x: int) -> uint { ret x as uint; } -#[doc = "Iterate over the range `[lo..hi)`"] -fn range(lo: int, hi: int, it: fn(int)) { - let mut i = lo; - while i < hi { it(i); i += 1; } -} - #[doc = " Parse a buffer of bytes @@ -105,15 +70,6 @@ fn pow(base: int, exponent: uint) -> int { ret acc; } -#[doc = "Computes the bitwise complement"] -pure fn compl(i: int) -> int { - uint::compl(i as uint) as int -} - -#[doc = "Computes the absolute value"] -fn abs(i: int) -> int { - if is_negative(i) { -i } else { i } -} #[test] fn test_from_str() { @@ -186,11 +142,3 @@ fn test_overflows() { assert (min_value <= 0); assert (min_value + max_value + 1 == 0); } - -// Local Variables: -// mode: rust; -// fill-column: 78; -// indent-tabs-mode: nil -// c-basic-offset: 4 -// buffer-file-coding-system: utf-8-unix -// End: From 56ae99f1f89d1468acd0bb9a710c5a93f01dc0fc Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sat, 14 Apr 2012 22:07:45 -0700 Subject: [PATCH 2/2] core: Factor out uint/u8/16/32/64 mods into uint-template --- src/libcore/core.rc | 53 ++++- src/libcore/u16.rs | 36 --- src/libcore/u32.rs | 41 ---- src/libcore/u8.rs | 41 ---- src/libcore/uint-template.rs | 45 ++++ src/libcore/uint-template/u16.rs | 1 + src/libcore/uint-template/u32.rs | 1 + src/libcore/{ => uint-template}/u64.rs | 33 +-- src/libcore/uint-template/u8.rs | 6 + src/libcore/uint-template/uint.rs | 246 +++++++++++++++++++++ src/libcore/uint.rs | 290 ------------------------- 11 files changed, 351 insertions(+), 442 deletions(-) delete mode 100644 src/libcore/u16.rs delete mode 100644 src/libcore/u32.rs delete mode 100644 src/libcore/u8.rs create mode 100644 src/libcore/uint-template.rs create mode 100644 src/libcore/uint-template/u16.rs create mode 100644 src/libcore/uint-template/u32.rs rename src/libcore/{ => uint-template}/u64.rs (56%) create mode 100644 src/libcore/uint-template/u8.rs create mode 100644 src/libcore/uint-template/uint.rs delete mode 100644 src/libcore/uint.rs diff --git a/src/libcore/core.rc b/src/libcore/core.rc index 9d07d42f2c329..3659e53e99307 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -81,6 +81,54 @@ mod i64 { mod inst; } +#[doc = "Operations and constants for `uint`"] +#[path = "uint-template"] +mod uint { + import inst::{ + div_ceil, div_round, div_floor, hash, iterate, + next_power_of_two, parse_buf, from_str, to_str, str + }; + export div_ceil, div_round, div_floor, hash, iterate, + next_power_of_two, parse_buf, from_str, to_str, str; + + #[path = "uint.rs"] + mod inst; +} + +#[doc = "Operations and constants for `u8`"] +#[path = "uint-template"] +mod u8 { + import inst::is_ascii; + export is_ascii; + + #[path = "u8.rs"] + mod inst; +} + +#[doc = "Operations and constants for `u16`"] +#[path = "uint-template"] +mod u16 { + #[path = "u16.rs"] + mod inst; +} + +#[doc = "Operations and constants for `u32`"] +#[path = "uint-template"] +mod u32 { + #[path = "u32.rs"] + mod inst; +} + +#[doc = "Operations and constants for `u64`"] +#[path = "uint-template"] +mod u64 { + import inst::{ to_str, str, from_str }; + export to_str, str, from_str; + + #[path = "u64.rs"] + mod inst; +} + mod box; mod char; mod float; @@ -88,11 +136,6 @@ mod f32; mod f64; mod str; mod ptr; -mod uint; -mod u8; -mod u16; -mod u32; -mod u64; mod vec; mod bool; diff --git a/src/libcore/u16.rs b/src/libcore/u16.rs deleted file mode 100644 index 16ca4800f81ce..0000000000000 --- a/src/libcore/u16.rs +++ /dev/null @@ -1,36 +0,0 @@ -#[doc = "Operations and constants for `u16`"]; - -const min_value: u16 = 0u16; -const max_value: u16 = 0u16 - 1u16; - -pure fn min(x: u16, y: u16) -> u16 { if x < y { x } else { y } } -pure fn max(x: u16, y: u16) -> u16 { if x > y { x } else { y } } - -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 is_positive(x: u16) -> bool { x > 0u16 } -pure fn is_negative(x: u16) -> bool { x < 0u16 } -pure fn is_nonpositive(x: u16) -> bool { x <= 0u16 } -pure fn is_nonnegative(x: u16) -> bool { x >= 0u16 } - -#[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: u16, hi: u16, it: fn(u16)) { - let mut i = lo; - while i < hi { it(i); i += 1u16; } -} - -#[doc = "Computes the bitwise complement"] -pure fn compl(i: u16) -> u16 { - max_value ^ i -} diff --git a/src/libcore/u32.rs b/src/libcore/u32.rs deleted file mode 100644 index dfe88b19e6a6e..0000000000000 --- a/src/libcore/u32.rs +++ /dev/null @@ -1,41 +0,0 @@ -#[doc = "Operations and constants for `u32`"]; - -const min_value: u32 = 0u32; -const max_value: u32 = 0u32 - 1u32; - -pure fn min(x: u32, y: u32) -> u32 { if x < y { x } else { y } } -pure fn max(x: u32, y: u32) -> u32 { if x > y { x } else { y } } - -pure fn add(x: u32, y: u32) -> u32 { ret x + y; } -pure fn sub(x: u32, y: u32) -> u32 { ret x - y; } -pure fn mul(x: u32, y: u32) -> u32 { ret x * y; } -pure fn div(x: u32, y: u32) -> u32 { ret x / y; } -pure fn rem(x: u32, y: u32) -> u32 { ret x % y; } - -pure fn lt(x: u32, y: u32) -> bool { ret x < y; } -pure fn le(x: u32, y: u32) -> bool { ret x <= y; } -pure fn eq(x: u32, y: u32) -> bool { ret x == y; } -pure fn ne(x: u32, y: u32) -> bool { ret x != y; } -pure fn ge(x: u32, y: u32) -> bool { ret x >= y; } -pure fn gt(x: u32, y: u32) -> bool { ret x > y; } - -#[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: u32, hi: u32, it: fn(u32)) { - let mut i = lo; - while i < hi { it(i); i += 1u32; } -} - -#[doc = "Computes the bitwise complement"] -pure fn compl(i: u32) -> u32 { - max_value ^ i -} - -// -// Local Variables: -// mode: rust -// fill-column: 78; -// indent-tabs-mode: nil -// c-basic-offset: 4 -// buffer-file-coding-system: utf-8-unix -// End: -// diff --git a/src/libcore/u8.rs b/src/libcore/u8.rs deleted file mode 100644 index 613b1d6b15b96..0000000000000 --- a/src/libcore/u8.rs +++ /dev/null @@ -1,41 +0,0 @@ -#[doc = "Operations and constants for `u8`"]; - -const min_value: u8 = 0u8; -const max_value: u8 = 0u8 - 1u8; - -pure fn min(x: u8, y: u8) -> u8 { if x < y { x } else { y } } -pure fn max(x: u8, y: u8) -> u8 { if x > y { x } else { y } } - -pure fn add(x: u8, y: u8) -> u8 { ret x + y; } -pure fn sub(x: u8, y: u8) -> u8 { ret x - y; } -pure fn mul(x: u8, y: u8) -> u8 { ret x * y; } -pure fn div(x: u8, y: u8) -> u8 { ret x / y; } -pure fn rem(x: u8, y: u8) -> u8 { ret x % y; } - -pure fn lt(x: u8, y: u8) -> bool { ret x < y; } -pure fn le(x: u8, y: u8) -> bool { ret x <= y; } -pure fn eq(x: u8, y: u8) -> bool { ret x == y; } -pure fn ne(x: u8, y: u8) -> bool { ret x != y; } -pure fn ge(x: u8, y: u8) -> bool { ret x >= y; } -pure fn gt(x: u8, y: u8) -> bool { ret x > y; } - -pure fn is_ascii(x: u8) -> bool { ret 0u8 == x & 128u8; } - -#[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: u8, hi: u8, it: fn(u8)) { - let mut i = lo; - while i < hi { it(i); i += 1u8; } -} - -#[doc = "Computes the bitwise complement"] -pure fn compl(i: u8) -> u8 { - max_value ^ i -} - -// Local Variables: -// mode: rust; -// fill-column: 78; -// indent-tabs-mode: nil -// c-basic-offset: 4 -// buffer-file-coding-system: utf-8-unix -// End: diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs new file mode 100644 index 0000000000000..0b95aadcb6afe --- /dev/null +++ b/src/libcore/uint-template.rs @@ -0,0 +1,45 @@ +import T = inst::T; + +export min_value, max_value; +export min, max; +export add, sub, mul, div, rem; +export lt, le, eq, ne, ge, gt; +export is_positive, is_negative; +export is_nonpositive, is_nonnegative; +export range; +export compl; + +const min_value: T = 0 as T; +const max_value: T = 0 as T - 1 as T; + +pure fn min(x: T, y: T) -> T { if x < y { x } else { y } } +pure fn max(x: T, y: T) -> T { if x > y { x } else { y } } + +pure fn add(x: T, y: T) -> T { x + y } +pure fn sub(x: T, y: T) -> T { x - y } +pure fn mul(x: T, y: T) -> T { x * y } +pure fn div(x: T, y: T) -> T { x / y } +pure fn rem(x: T, y: T) -> T { x % y } + +pure fn lt(x: T, y: T) -> bool { x < y } +pure fn le(x: T, y: T) -> bool { x <= y } +pure fn eq(x: T, y: T) -> bool { x == y } +pure fn ne(x: T, y: T) -> bool { x != y } +pure fn ge(x: T, y: T) -> bool { x >= y } +pure fn gt(x: T, y: T) -> bool { x > y } + +pure fn is_positive(x: T) -> bool { x > 0 as T } +pure fn is_negative(x: T) -> bool { x < 0 as T } +pure fn is_nonpositive(x: T) -> bool { x <= 0 as T } +pure fn is_nonnegative(x: T) -> bool { x >= 0 as T } + +#[doc = "Iterate over the range [`lo`..`hi`)"] +fn range(lo: T, hi: T, it: fn(T)) { + let mut i = lo; + while i < hi { it(i); i += 1 as T; } +} + +#[doc = "Computes the bitwise complement"] +pure fn compl(i: T) -> T { + max_value ^ i +} diff --git a/src/libcore/uint-template/u16.rs b/src/libcore/uint-template/u16.rs new file mode 100644 index 0000000000000..8d03656d6fe1d --- /dev/null +++ b/src/libcore/uint-template/u16.rs @@ -0,0 +1 @@ +type T = u16; diff --git a/src/libcore/uint-template/u32.rs b/src/libcore/uint-template/u32.rs new file mode 100644 index 0000000000000..df9cf28a16c0e --- /dev/null +++ b/src/libcore/uint-template/u32.rs @@ -0,0 +1 @@ +type T = u32; diff --git a/src/libcore/u64.rs b/src/libcore/uint-template/u64.rs similarity index 56% rename from src/libcore/u64.rs rename to src/libcore/uint-template/u64.rs index a7542894452a8..5de83d41da358 100644 --- a/src/libcore/u64.rs +++ b/src/libcore/uint-template/u64.rs @@ -1,30 +1,10 @@ -#[doc = "Operations and constants for `u64`"]; +type T = u64; -const min_value: u64 = 0u64; -const max_value: u64 = 0u64 - 1u64; +// Type-specific functions here. These must be reexported by the +// parent module so that they appear in core::u8 and not core::u8::u8; -pure fn min(x: u64, y: u64) -> u64 { if x < y { x } else { y } } -pure fn max(x: u64, y: u64) -> u64 { if x > y { x } else { y } } - -pure fn add(x: u64, y: u64) -> u64 { ret x + y; } -pure fn sub(x: u64, y: u64) -> u64 { ret x - y; } -pure fn mul(x: u64, y: u64) -> u64 { ret x * y; } -pure fn div(x: u64, y: u64) -> u64 { ret x / y; } -pure fn rem(x: u64, y: u64) -> u64 { ret x % y; } - -pure fn lt(x: u64, y: u64) -> bool { ret x < y; } -pure fn le(x: u64, y: u64) -> bool { ret x <= y; } -pure fn eq(x: u64, y: u64) -> bool { ret x == y; } -pure fn ne(x: u64, y: u64) -> bool { ret x != y; } -pure fn ge(x: u64, y: u64) -> bool { ret x >= y; } -pure fn gt(x: u64, y: u64) -> bool { ret x > y; } - -#[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: u64, hi: u64, it: fn(u64)) { - let mut i = lo; - while i < hi { it(i); i += 1u64; } -} +// FIXME: Surely we can generalize this to apply to all uint types #[doc = "Convert to a string in a given base"] fn to_str(n: u64, radix: uint) -> str { assert (0u < radix && radix <= 16u); @@ -80,8 +60,3 @@ fn from_str(buf: str, radix: u64) -> option { i -= 1u; }; } - -#[doc = "Computes the bitwise complement"] -pure fn compl(i: u64) -> u64 { - max_value ^ i -} diff --git a/src/libcore/uint-template/u8.rs b/src/libcore/uint-template/u8.rs new file mode 100644 index 0000000000000..bc73536c4a407 --- /dev/null +++ b/src/libcore/uint-template/u8.rs @@ -0,0 +1,6 @@ +type T = u8; + +// Type-specific functions here. These must be reexported by the +// parent module so that they appear in core::u8 and not core::u8::u8; + +pure fn is_ascii(x: T) -> bool { ret 0 as T == x & 128 as T; } diff --git a/src/libcore/uint-template/uint.rs b/src/libcore/uint-template/uint.rs new file mode 100644 index 0000000000000..27a8c26a2483d --- /dev/null +++ b/src/libcore/uint-template/uint.rs @@ -0,0 +1,246 @@ +type T = uint; + +#[doc = " +Divide two numbers, return the result, rounded up. + +# Arguments + +* x - an integer +* y - an integer distinct from 0u + +# Return value + +The smallest integer `q` such that `x/y <= q`. +"] +pure fn div_ceil(x: uint, y: uint) -> uint { + let div = div(x, y); + if x % y == 0u { ret div;} + else { ret div + 1u; } +} + +#[doc = " +Divide two numbers, return the result, rounded to the closest integer. + +# Arguments + +* x - an integer +* y - an integer distinct from 0u + +# Return value + +The integer `q` closest to `x/y`. +"] +pure fn div_round(x: uint, y: uint) -> uint { + let div = div(x, y); + if x % y * 2u < y { ret div;} + else { ret div + 1u; } +} + +#[doc = " +Divide two numbers, return the result, rounded down. + +Note: This is the same function as `div`. + +# Arguments + +* x - an integer +* y - an integer distinct from 0u + +# Return value + +The smallest integer `q` such that `x/y <= q`. This +is either `x/y` or `x/y + 1`. +"] +pure fn div_floor(x: uint, y: uint) -> uint { ret x / y; } + +#[doc = "Produce a uint suitable for use in a hash table"] +pure fn hash(x: uint) -> uint { ret x; } + +#[doc = " +Iterate over the range [`lo`..`hi`), or stop when requested + +# Arguments + +* lo - The integer at which to start the loop (included) +* hi - The integer at which to stop the loop (excluded) +* it - A block to execute with each consecutive integer of the range. + Return `true` to continue, `false` to stop. + +# Return value + +`true` If execution proceeded correctly, `false` if it was interrupted, +that is if `it` returned `false` at any point. +"] +fn iterate(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool { + let mut i = lo; + while i < hi { + if (!it(i)) { ret false; } + i += 1u; + } + ret true; +} + +#[doc = "Returns the smallest power of 2 greater than or equal to `n`"] +fn next_power_of_two(n: uint) -> uint { + let halfbits: uint = sys::size_of::() * 4u; + let mut tmp: uint = n - 1u; + let mut shift: uint = 1u; + while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; } + ret tmp + 1u; +} + +#[doc = " +Parse a buffer of bytes + +# Arguments + +* buf - A byte buffer +* radix - The base of the number + +# Failure + +`buf` must not be empty +"] +fn parse_buf(buf: [u8], radix: uint) -> option { + if vec::len(buf) == 0u { ret none; } + let mut i = vec::len(buf) - 1u; + let mut power = 1u; + let mut n = 0u; + loop { + alt char::to_digit(buf[i] as char, radix) { + some(d) { n += d * power; } + none { ret none; } + } + power *= radix; + if i == 0u { ret some(n); } + i -= 1u; + }; +} + +#[doc = "Parse a string to an int"] +fn from_str(s: str) -> option { parse_buf(str::bytes(s), 10u) } + +#[doc = "Convert to a string in a given base"] +fn to_str(num: uint, radix: uint) -> str { + let mut n = num; + assert (0u < radix && radix <= 16u); + fn digit(n: uint) -> char { + ret alt n { + 0u { '0' } + 1u { '1' } + 2u { '2' } + 3u { '3' } + 4u { '4' } + 5u { '5' } + 6u { '6' } + 7u { '7' } + 8u { '8' } + 9u { '9' } + 10u { 'a' } + 11u { 'b' } + 12u { 'c' } + 13u { 'd' } + 14u { 'e' } + 15u { 'f' } + _ { fail } + }; + } + if n == 0u { ret "0"; } + let mut s: str = ""; + while n != 0u { + s += str::from_byte(digit(n % radix) as u8); + n /= radix; + } + let mut s1: str = ""; + let mut len: uint = str::len(s); + while len != 0u { len -= 1u; s1 += str::from_byte(s[len]); } + ret s1; +} + +#[doc = "Convert to a string"] +fn str(i: uint) -> str { ret to_str(i, 10u); } + +#[test] +fn test_from_str() { + assert uint::from_str("0") == some(0u); + assert uint::from_str("3") == some(3u); + assert uint::from_str("10") == some(10u); + assert uint::from_str("123456789") == some(123456789u); + assert uint::from_str("00100") == some(100u); + + assert uint::from_str("") == none; + assert uint::from_str(" ") == none; + assert uint::from_str("x") == none; +} + +#[Test] +fn test_parse_buf() { + import str::bytes; + assert uint::parse_buf(bytes("123"), 10u) == some(123u); + assert uint::parse_buf(bytes("1001"), 2u) == some(9u); + assert uint::parse_buf(bytes("123"), 8u) == some(83u); + assert uint::parse_buf(bytes("123"), 16u) == some(291u); + assert uint::parse_buf(bytes("ffff"), 16u) == some(65535u); + assert uint::parse_buf(bytes("z"), 36u) == some(35u); + + assert uint::parse_buf(str::bytes("Z"), 10u) == none; + assert uint::parse_buf(str::bytes("_"), 2u) == none; +} + +#[test] +fn test_next_power_of_two() { + assert (uint::next_power_of_two(0u) == 0u); + assert (uint::next_power_of_two(1u) == 1u); + assert (uint::next_power_of_two(2u) == 2u); + assert (uint::next_power_of_two(3u) == 4u); + assert (uint::next_power_of_two(4u) == 4u); + assert (uint::next_power_of_two(5u) == 8u); + assert (uint::next_power_of_two(6u) == 8u); + assert (uint::next_power_of_two(7u) == 8u); + assert (uint::next_power_of_two(8u) == 8u); + assert (uint::next_power_of_two(9u) == 16u); + assert (uint::next_power_of_two(10u) == 16u); + assert (uint::next_power_of_two(11u) == 16u); + assert (uint::next_power_of_two(12u) == 16u); + assert (uint::next_power_of_two(13u) == 16u); + assert (uint::next_power_of_two(14u) == 16u); + assert (uint::next_power_of_two(15u) == 16u); + assert (uint::next_power_of_two(16u) == 16u); + assert (uint::next_power_of_two(17u) == 32u); + assert (uint::next_power_of_two(18u) == 32u); + assert (uint::next_power_of_two(19u) == 32u); + assert (uint::next_power_of_two(20u) == 32u); + assert (uint::next_power_of_two(21u) == 32u); + assert (uint::next_power_of_two(22u) == 32u); + assert (uint::next_power_of_two(23u) == 32u); + assert (uint::next_power_of_two(24u) == 32u); + assert (uint::next_power_of_two(25u) == 32u); + assert (uint::next_power_of_two(26u) == 32u); + assert (uint::next_power_of_two(27u) == 32u); + assert (uint::next_power_of_two(28u) == 32u); + assert (uint::next_power_of_two(29u) == 32u); + assert (uint::next_power_of_two(30u) == 32u); + assert (uint::next_power_of_two(31u) == 32u); + assert (uint::next_power_of_two(32u) == 32u); + assert (uint::next_power_of_two(33u) == 64u); + assert (uint::next_power_of_two(34u) == 64u); + assert (uint::next_power_of_two(35u) == 64u); + assert (uint::next_power_of_two(36u) == 64u); + assert (uint::next_power_of_two(37u) == 64u); + assert (uint::next_power_of_two(38u) == 64u); + assert (uint::next_power_of_two(39u) == 64u); +} + +#[test] +fn test_overflows() { + assert (uint::max_value > 0u); + assert (uint::min_value <= 0u); + assert (uint::min_value + uint::max_value + 1u == 0u); +} + +#[test] +fn test_div() { + assert(uint::div_floor(3u, 4u) == 0u); + assert(uint::div_ceil(3u, 4u) == 1u); + assert(uint::div_round(3u, 4u) == 1u); +} diff --git a/src/libcore/uint.rs b/src/libcore/uint.rs deleted file mode 100644 index d2b04d78dca27..0000000000000 --- a/src/libcore/uint.rs +++ /dev/null @@ -1,290 +0,0 @@ -#[doc = "Operations and constants for `uint`"]; - -const min_value: uint = 0u; -const max_value: uint = 0u - 1u; - -pure fn min(x: uint, y: uint) -> uint { if x < y { x } else { y } } -pure fn max(x: uint, y: uint) -> uint { if x > y { x } else { y } } - -pure fn add(x: uint, y: uint) -> uint { ret x + y; } -pure fn sub(x: uint, y: uint) -> uint { ret x - y; } -pure fn mul(x: uint, y: uint) -> uint { ret x * y; } -pure fn div(x: uint, y: uint) -> uint { ret x / y; } -pure fn rem(x: uint, y: uint) -> uint { ret x % y; } - -pure fn lt(x: uint, y: uint) -> bool { ret x < y; } -pure fn le(x: uint, y: uint) -> bool { ret x <= y; } -pure fn eq(x: uint, y: uint) -> bool { ret x == y; } -pure fn ne(x: uint, y: uint) -> bool { ret x != y; } -pure fn ge(x: uint, y: uint) -> bool { ret x >= y; } -pure fn gt(x: uint, y: uint) -> bool { ret x > y; } - - -#[doc = " -Divide two numbers, return the result, rounded up. - -# Arguments - -* x - an integer -* y - an integer distinct from 0u - -# Return value - -The smallest integer `q` such that `x/y <= q`. -"] -pure fn div_ceil(x: uint, y: uint) -> uint { - let div = div(x, y); - if x % y == 0u { ret div;} - else { ret div + 1u; } -} - -#[doc = " -Divide two numbers, return the result, rounded to the closest integer. - -# Arguments - -* x - an integer -* y - an integer distinct from 0u - -# Return value - -The integer `q` closest to `x/y`. -"] -pure fn div_round(x: uint, y: uint) -> uint { - let div = div(x, y); - if x % y * 2u < y { ret div;} - else { ret div + 1u; } -} - -#[doc = " -Divide two numbers, return the result, rounded down. - -Note: This is the same function as `div`. - -# Arguments - -* x - an integer -* y - an integer distinct from 0u - -# Return value - -The smallest integer `q` such that `x/y <= q`. This -is either `x/y` or `x/y + 1`. -"] -pure fn div_floor(x: uint, y: uint) -> uint { ret x / y; } - -#[doc = "Produce a uint suitable for use in a hash table"] -pure fn hash(x: uint) -> uint { ret x; } - -#[doc = "Iterate over the range [`lo`..`hi`)"] -#[inline(always)] -fn range(lo: uint, hi: uint, it: fn(uint)) { - let mut i = lo; - while i < hi { it(i); i += 1u; } -} - -#[doc = " -Iterate over the range [`lo`..`hi`), or stop when requested - -# Arguments - -* lo - The integer at which to start the loop (included) -* hi - The integer at which to stop the loop (excluded) -* it - A block to execute with each consecutive integer of the range. - Return `true` to continue, `false` to stop. - -# Return value - -`true` If execution proceeded correctly, `false` if it was interrupted, -that is if `it` returned `false` at any point. -"] -fn iterate(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool { - let mut i = lo; - while i < hi { - if (!it(i)) { ret false; } - i += 1u; - } - ret true; -} - -#[doc = "Returns the smallest power of 2 greater than or equal to `n`"] -fn next_power_of_two(n: uint) -> uint { - let halfbits: uint = sys::size_of::() * 4u; - let mut tmp: uint = n - 1u; - let mut shift: uint = 1u; - while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; } - ret tmp + 1u; -} - -#[doc = " -Parse a buffer of bytes - -# Arguments - -* buf - A byte buffer -* radix - The base of the number - -# Failure - -`buf` must not be empty -"] -fn parse_buf(buf: [u8], radix: uint) -> option { - if vec::len(buf) == 0u { ret none; } - let mut i = vec::len(buf) - 1u; - let mut power = 1u; - let mut n = 0u; - loop { - alt char::to_digit(buf[i] as char, radix) { - some(d) { n += d * power; } - none { ret none; } - } - power *= radix; - if i == 0u { ret some(n); } - i -= 1u; - }; -} - -#[doc = "Parse a string to an int"] -fn from_str(s: str) -> option { parse_buf(str::bytes(s), 10u) } - -#[doc = "Convert to a string in a given base"] -fn to_str(num: uint, radix: uint) -> str { - let mut n = num; - assert (0u < radix && radix <= 16u); - fn digit(n: uint) -> char { - ret alt n { - 0u { '0' } - 1u { '1' } - 2u { '2' } - 3u { '3' } - 4u { '4' } - 5u { '5' } - 6u { '6' } - 7u { '7' } - 8u { '8' } - 9u { '9' } - 10u { 'a' } - 11u { 'b' } - 12u { 'c' } - 13u { 'd' } - 14u { 'e' } - 15u { 'f' } - _ { fail } - }; - } - if n == 0u { ret "0"; } - let mut s: str = ""; - while n != 0u { - s += str::from_byte(digit(n % radix) as u8); - n /= radix; - } - let mut s1: str = ""; - let mut len: uint = str::len(s); - while len != 0u { len -= 1u; s1 += str::from_byte(s[len]); } - ret s1; -} - -#[doc = "Convert to a string"] -fn str(i: uint) -> str { ret to_str(i, 10u); } - -#[doc = "Computes the bitwise complement"] -pure fn compl(i: uint) -> uint { - max_value ^ i -} - -#[cfg(test)] -mod tests { - - #[test] - fn test_from_str() { - assert uint::from_str("0") == some(0u); - assert uint::from_str("3") == some(3u); - assert uint::from_str("10") == some(10u); - assert uint::from_str("123456789") == some(123456789u); - assert uint::from_str("00100") == some(100u); - - assert uint::from_str("") == none; - assert uint::from_str(" ") == none; - assert uint::from_str("x") == none; - } - - #[Test] - fn test_parse_buf() { - import str::bytes; - assert uint::parse_buf(bytes("123"), 10u) == some(123u); - assert uint::parse_buf(bytes("1001"), 2u) == some(9u); - assert uint::parse_buf(bytes("123"), 8u) == some(83u); - assert uint::parse_buf(bytes("123"), 16u) == some(291u); - assert uint::parse_buf(bytes("ffff"), 16u) == some(65535u); - assert uint::parse_buf(bytes("z"), 36u) == some(35u); - - assert uint::parse_buf(str::bytes("Z"), 10u) == none; - assert uint::parse_buf(str::bytes("_"), 2u) == none; - } - - #[test] - fn test_next_power_of_two() { - assert (uint::next_power_of_two(0u) == 0u); - assert (uint::next_power_of_two(1u) == 1u); - assert (uint::next_power_of_two(2u) == 2u); - assert (uint::next_power_of_two(3u) == 4u); - assert (uint::next_power_of_two(4u) == 4u); - assert (uint::next_power_of_two(5u) == 8u); - assert (uint::next_power_of_two(6u) == 8u); - assert (uint::next_power_of_two(7u) == 8u); - assert (uint::next_power_of_two(8u) == 8u); - assert (uint::next_power_of_two(9u) == 16u); - assert (uint::next_power_of_two(10u) == 16u); - assert (uint::next_power_of_two(11u) == 16u); - assert (uint::next_power_of_two(12u) == 16u); - assert (uint::next_power_of_two(13u) == 16u); - assert (uint::next_power_of_two(14u) == 16u); - assert (uint::next_power_of_two(15u) == 16u); - assert (uint::next_power_of_two(16u) == 16u); - assert (uint::next_power_of_two(17u) == 32u); - assert (uint::next_power_of_two(18u) == 32u); - assert (uint::next_power_of_two(19u) == 32u); - assert (uint::next_power_of_two(20u) == 32u); - assert (uint::next_power_of_two(21u) == 32u); - assert (uint::next_power_of_two(22u) == 32u); - assert (uint::next_power_of_two(23u) == 32u); - assert (uint::next_power_of_two(24u) == 32u); - assert (uint::next_power_of_two(25u) == 32u); - assert (uint::next_power_of_two(26u) == 32u); - assert (uint::next_power_of_two(27u) == 32u); - assert (uint::next_power_of_two(28u) == 32u); - assert (uint::next_power_of_two(29u) == 32u); - assert (uint::next_power_of_two(30u) == 32u); - assert (uint::next_power_of_two(31u) == 32u); - assert (uint::next_power_of_two(32u) == 32u); - assert (uint::next_power_of_two(33u) == 64u); - assert (uint::next_power_of_two(34u) == 64u); - assert (uint::next_power_of_two(35u) == 64u); - assert (uint::next_power_of_two(36u) == 64u); - assert (uint::next_power_of_two(37u) == 64u); - assert (uint::next_power_of_two(38u) == 64u); - assert (uint::next_power_of_two(39u) == 64u); - } - - #[test] - fn test_overflows() { - assert (uint::max_value > 0u); - assert (uint::min_value <= 0u); - assert (uint::min_value + uint::max_value + 1u == 0u); - } - - #[test] - fn test_div() { - assert(uint::div_floor(3u, 4u) == 0u); - assert(uint::div_ceil(3u, 4u) == 1u); - assert(uint::div_round(3u, 4u) == 1u); - } -} - -// Local Variables: -// mode: rust; -// fill-column: 78; -// indent-tabs-mode: nil -// c-basic-offset: 4 -// buffer-file-coding-system: utf-8-unix -// End: