core: Factor out int/i8/16/32/64 mods into int-template
This commit is contained in:
@@ -44,16 +44,48 @@ export priv;
|
|||||||
|
|
||||||
// Built-in-type support modules
|
// 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 box;
|
||||||
mod char;
|
mod char;
|
||||||
mod float;
|
mod float;
|
||||||
mod f32;
|
mod f32;
|
||||||
mod f64;
|
mod f64;
|
||||||
mod int;
|
|
||||||
mod i8;
|
|
||||||
mod i16;
|
|
||||||
mod i32;
|
|
||||||
mod i64;
|
|
||||||
mod str;
|
mod str;
|
||||||
mod ptr;
|
mod ptr;
|
||||||
mod uint;
|
mod uint;
|
||||||
|
|||||||
@@ -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 }
|
|
||||||
}
|
|
||||||
@@ -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 }
|
|
||||||
}
|
|
||||||
@@ -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 }
|
|
||||||
}
|
|
||||||
@@ -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 }
|
|
||||||
}
|
|
||||||
51
src/libcore/int-template.rs
Normal file
51
src/libcore/int-template.rs
Normal file
@@ -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 }
|
||||||
|
}
|
||||||
3
src/libcore/int-template/i16.rs
Normal file
3
src/libcore/int-template/i16.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
type T = i16;
|
||||||
|
|
||||||
|
const bits: T = 16 as T;
|
||||||
3
src/libcore/int-template/i32.rs
Normal file
3
src/libcore/int-template/i32.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
type T = i32;
|
||||||
|
|
||||||
|
const bits: T = 32 as T;
|
||||||
3
src/libcore/int-template/i64.rs
Normal file
3
src/libcore/int-template/i64.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
type T = i64;
|
||||||
|
|
||||||
|
const bits: T = 64 as T;
|
||||||
3
src/libcore/int-template/i8.rs
Normal file
3
src/libcore/int-template/i8.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
type T = i8;
|
||||||
|
|
||||||
|
const bits: T = 8 as T;
|
||||||
@@ -1,49 +1,14 @@
|
|||||||
#[doc = "Operations and constants for `int`"];
|
type T = int;
|
||||||
|
|
||||||
#[cfg(target_arch = "x86")]
|
#[cfg(target_arch = "x86")]
|
||||||
const min_value: int = -1 << 31;
|
const bits: T = 32 as T;
|
||||||
|
|
||||||
#[cfg(target_arch = "x86_64")]
|
#[cfg(target_arch = "x86_64")]
|
||||||
const min_value: int = -1 << 63;
|
const bits: T = 64 as T;
|
||||||
|
|
||||||
// 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; }
|
|
||||||
|
|
||||||
#[doc = "Produce a uint suitable for use in a hash table"]
|
#[doc = "Produce a uint suitable for use in a hash table"]
|
||||||
pure fn hash(x: int) -> uint { ret x as uint; }
|
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 = "
|
#[doc = "
|
||||||
Parse a buffer of bytes
|
Parse a buffer of bytes
|
||||||
|
|
||||||
@@ -105,15 +70,6 @@ fn pow(base: int, exponent: uint) -> int {
|
|||||||
ret acc;
|
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]
|
#[test]
|
||||||
fn test_from_str() {
|
fn test_from_str() {
|
||||||
@@ -186,11 +142,3 @@ fn test_overflows() {
|
|||||||
assert (min_value <= 0);
|
assert (min_value <= 0);
|
||||||
assert (min_value + max_value + 1 == 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:
|
|
||||||
Reference in New Issue
Block a user