Skip to content

Use two module templates to implement mods for all 10 integer types #2210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 85 additions & 10 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,98 @@ 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;
}

#[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;
mod f32;
mod f64;
mod int;
mod i8;
mod i16;
mod i32;
mod i64;
mod str;
mod ptr;
mod uint;
mod u8;
mod u16;
mod u32;
mod u64;
mod vec;
mod bool;

Expand Down
41 changes: 0 additions & 41 deletions src/libcore/i16.rs

This file was deleted.

41 changes: 0 additions & 41 deletions src/libcore/i32.rs

This file was deleted.

41 changes: 0 additions & 41 deletions src/libcore/i64.rs

This file was deleted.

41 changes: 0 additions & 41 deletions src/libcore/i8.rs

This file was deleted.

51 changes: 51 additions & 0 deletions src/libcore/int-template.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions src/libcore/int-template/i16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type T = i16;

const bits: T = 16 as T;
3 changes: 3 additions & 0 deletions src/libcore/int-template/i32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type T = i32;

const bits: T = 32 as T;
3 changes: 3 additions & 0 deletions src/libcore/int-template/i64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type T = i64;

const bits: T = 64 as T;
3 changes: 3 additions & 0 deletions src/libcore/int-template/i8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type T = i8;

const bits: T = 8 as T;
Loading