46 lines
1.3 KiB
Rust
46 lines
1.3 KiB
Rust
|
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
|
||
|
}
|